#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

use POSIX qw(locale_h);
use locale;
use Locale::gettext;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Group;
use Vhffs::Panel::Public;
use Vhffs::Tag;

my $panel = new Vhffs::Panel::Public();
my $cgi = $panel->{cgi};
my $vhffs = $panel->{vhffs};
my $config = $vhffs->get_config();

my $groupname = $cgi->param('name');

unless(defined $groupname) {
	$panel->render('common/error.tt', {
		message => gettext('CGI Error')
	});
	exit(0);
}

my $group = Vhffs::Group::get_by_groupname($panel->{vhffs}, $groupname);

unless(defined $group) {
	$panel->render('common/error.tt', {
		message => gettext('Group not found')
	});
	exit(0);
}

my $tag_categories = $group->get_tags(Vhffs::Constants::TAG_VISIBILITY_PUBLIC); 

my $vars = {
	group => $group,
	tag_categories => $tag_categories
};

# Services filling... Really boring
if( $config->get_service_availability('web') == 1 ) {
	use Vhffs::Panel::Web;
	$vars->{websites} = Vhffs::Panel::Web::get_websites_per_group($vhffs, $group->get_gid);
}

if( $vhffs->get_config->get_service_availability('cvs') == 1 ) {
	use Vhffs::Panel::Cvs;
	$vars->{cvs} = {
		cvs_web_url => $config->get_service('cvs')->{'cvsweb_url'},
		repositories => Vhffs::Panel::Cvs::get_repos_per_group($vhffs, $group->get_gid )
	};
}

if( $vhffs->get_config->get_service_availability('svn') == 1 ) {
	use Vhffs::Panel::Svn;
	$vars->{svn} = {
		svn_web_url => $config->get_service('svn')->{'svnweb_url'},
		repositories => Vhffs::Panel::Svn::get_repos_per_group($vhffs, $group->get_gid )
	};
}

if( $vhffs->get_config->get_service_availability('git') == 1 ) {
	use Vhffs::Panel::Git;
	$vars->{git} = {
		git_web_url => $config->get_service('git')->{'gitweb_url'},
		repositories => Vhffs::Panel::Git::get_repos_per_group($vhffs, $group->get_gid )
	};
}

if( $vhffs->get_config->get_service_availability('mercurial') == 1 ) {
	use Vhffs::Panel::Mercurial;
	$vars->{mercurial} = {
		mercurial_web_url => $config->get_service('mercurial')->{'mercurialweb_url'},
		repositories => Vhffs::Panel::Mercurial::get_repos_per_group($vhffs, $group->get_gid )
	};
}


if( $vhffs->get_config->get_service_availability('mailinglist') == 1 ) {
	use Vhffs::Panel::MailingList;;
	$vars->{ml} = {
		archives_url => $config->get_service('mailinglist')->{'url_archives'},
		lists => Vhffs::Panel::MailingList::get_lists_per_group($vhffs, $group->get_gid )
	};
}

$panel->render('content/group-details.tt', $vars);
