#!%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 lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::Panel::Group;

use XML::RSS;

my $vhffs = init Vhffs::Main;

my $url;
$url = '' unless defined ( $url = $vhffs->get_config->get_panel->{'url_public'} );


if( $vhffs->get_config->get_panel->{'rss_groups'} eq 'yes' && $vhffs->get_config->get_panel->{'use_public'} eq 'yes' ) {
	my $groups = Vhffs::Panel::Group::get_last_groups( $vhffs );
	
	my $rss = new XML::RSS( version => '1.0' );
	my $title;

	if( defined  $vhffs->get_config->get_host_name ) {
		$title = 'Last groups on '.$vhffs->get_config->get_host_name;
	} else {
		$title = 'VHFFS last groups';
	}

	$rss->channel(
	  title        => $title,
	  link         => "$url/lastgroups.pl",
	  description  => 'Best hosting platform',
	  dc => {
	    date       => '2000-08-23T07:00+00:00',
        subject    => 'Last groups on '.$vhffs->get_config->get_host_name,
	    subject    => 'danstoncul',
	    creator    => 'vhffs@vhffs.org',
	    publisher  => 'vhffs@vhffs.org',
	    rights     => 'Copyright 2004, Vhffs Dream Team',
	    language   => 'en_US',
	  },
	  syn => {
	    updatePeriod     => 'hourly',
	    updateFrequency  => '1',
	    updateBase       => '1901-01-01T00:00+00:00',
	  },
	  taxo => [
	    'http://dmoz.org/Computers/Internet',
	    'http://dmoz.org/Computers/PC'
	  ]
	);
	
	foreach(@{$groups}) {
		$rss->add_item(
			title       => $_->{realname},
			link        => $url.'/group.pl?name='.$_->{groupname},
			description => 'Vhffs Group',
		);	
	}

	$rss->{output} = '2.0';

	binmode STDOUT , ':utf8';
	print "Content-Type: text/xml; charset=utf-8\n\n" . $rss->as_string;

} else {
        use POSIX qw(locale_h);
        use locale;
        use Locale::gettext;
        use HTML::Template;
        my $templatedir = $vhffs->get_config->get_templatedir;
        my $template = new HTML::Template( filename => $templatedir.'/public/simplemsg.tmpl' );
        my $message = gettext( 'RSS infos are not published' );
        $template->param( MESSAGE => $message );

	binmode STDOUT , ':utf8';
        print "Content-Type: application/rss+xml; charset=utf-8\n\n" . $template->output;
}
