#!/usr/bin/perl

#################
##### Author:	Sebastian Enger 
##### eMail:	sebastian.enger@gmail.com
##### Web:		http://www.sebastian-enger.com
##### Licence:	Its only mine!
#################

system("renice 20 $$");
system("clear");

# https://www.google.com/webmasters/sitemaps/addsitemap?siteUrl=http://www.zoozle.net/emule-bittorrent-download/&amp;hl=en&type=0&sitemapUrl0= PLUS SITEMAP URI

# first do a to install needed modules: perl -MCPAN -e 'force install "use HTTP::Date"'

use HTTP::Date;
use WWW::Google::SiteMap::Ping;
use URI::Escape qw(uri_escape);
use POSIX qw(strftime);
use POSIX;
use IO::Handle;
use strict;
use LWP::Simple;

my $VERSION = "24.10.2006 - V.2";

my $StatementHandle;
my $SiteMapFile;
my $KeyCounts;
my @data = ();
my $SiteMap;
my $data;

my @months = (
	'January',
	'February',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December'
);

my @monate = (
	"Januar",
	"Februar",
	"Maerz",
	"April",
	"Mai",
	"Juni",
	"Juli",
	"August",
	"September",
	"Oktober",
	"November",
	"Dezember"
);

my $CurrentDate			= getLastModTimeStamp();
my $StoreSiteMapPath	= '/server/wwwroot/emule-bittorrent-download';	# no tailing /
my $StoreSiteMapWebPath	= 'http://www.zoozle.org/emule-bittorrent-download';		# no tailing /

#my $StartUrlDE = "http://www.zoozle.net/emule-bittorrent-news-deutsch";
my $StartUrlEN = "http://www.zoozle.org/emule-bittorrent-news-english";

#my $NewsStartUrlDE = "http://www.zoozle.net/emule-bittorrent-newsarchive-deutsch";
my $NewsStartUrlEN = "http://www.zoozle.org/emule-bittorrent-newsarchive-english";


my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my ($wkday,$month,$day,$time,$year)						 = split(/\s+/, localtime);
# $mon = $mon; 
# $year = $year + 1900;


GenerateSiteMaps( );

sub GenerateSiteMaps(){

	my $sitemapUp = 0;
	my $count = 0;
	
	# PingGoogleForSitemap("$StoreSiteMapWebPath/zoozle-news-sitemap.xml");
	$SiteMapFile	= "$StoreSiteMapPath/zoozle-news-sitemap.xml";
	my $lastmod		= getLastModTimeStamp( time() );

	print "Installing Sitemap $SiteMapFile\n";
		
	open(SITEMAP,">$SiteMapFile");
		binmode SITEMAP, ":utf8";
		flock(SITEMAP, 2);
	SITEMAP->autoflush(1);
	print SITEMAP PreXMLHeader();

	
	######## für 2006 hier nur ##########
	for ( my $i=0; $i<=11; $i++ ) {		# monate
		for ( my $j=1; $j<=31; $j++ ) {	# tage
			
			my $dateDE		= @monate[$i];
			my $dateEN		= @months[$i];
		
			my $StringSimpleDE	= lc "$j-$dateDE-2006";
			my $StringSimpleEN	= lc "$j-$dateEN-2006";
			
		#	print SITEMAP "<url><loc>$StartUrlDE/$StringSimpleDE.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
			print SITEMAP "<url><loc>$StartUrlEN/$StringSimpleEN.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";

			foreach ( "0-9", "a-d", "e-k", "l-n", "o-s", "t-z" )  {
				
				# http://www.zoozle.net/emule-bittorrent-news-english/4-january-2007,a-d,emule.html

		#		print SITEMAP "<url><loc>$StartUrlDE/$StringSimpleDE,$_,torrent.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.95</priority></url>\n";
				print SITEMAP "<url><loc>$StartUrlEN/$StringSimpleEN,$_,torrent.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.95</priority></url>\n";

		#		print SITEMAP "<url><loc>$StartUrlDE/$StringSimpleDE,$_,emule.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
				print SITEMAP "<url><loc>$StartUrlEN/$StringSimpleEN,$_,emule.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";

			}; # foreach 

		}; # for ( $j=1; $j<=31; $j++ ) {	# tage
	}; # for ( $i=0; $i<=12; $i++ ) {		# monate	
	######## ENDE: für 2006 hier nur ##########
	
	######## für 2007 hier nur ##########
	my $howmuch = 0;
	for ( my $i=0; $i<=$mon; $i++ ) {		# monate
		
		# zähle immer nur bis zum aktuellen tag des monats und bereite dazu nur die sitemaps vor: sprich wenn jetzt grade januar der 21 ist, dann ist es sinnfrei die sitemap bis zum 31.1 schon zu erstellen
		if ( $i == $mon ) {
			$howmuch = $mday;
		} else {
			$howmuch = 31;
		};

		for ( my $j=1; $j<=$howmuch; $j++ ) {	# tage
			
			my $dateDE		= @monate[$i];
			my $dateEN		= @months[$i];
		
			my $StringSimpleDE	= lc "$j-$dateDE-2007";
			my $StringSimpleEN	= lc "$j-$dateEN-2007";
			
		#	print SITEMAP "<url><loc>$StartUrlDE/$StringSimpleDE.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
			print SITEMAP "<url><loc>$StartUrlEN/$StringSimpleEN.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";

			foreach ( "0-9", "a-d", "e-k", "l-n", "o-s", "t-z" )  {
				
				# http://www.zoozle.net/emule-bittorrent-news-english/4-january-2007,a-d,emule.html

		#		print SITEMAP "<url><loc>$StartUrlDE/$StringSimpleDE,$_,torrent.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.95</priority></url>\n";
				print SITEMAP "<url><loc>$StartUrlEN/$StringSimpleEN,$_,torrent.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.95</priority></url>\n";

		#		print SITEMAP "<url><loc>$StartUrlDE/$StringSimpleDE,$_,emule.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
				print SITEMAP "<url><loc>$StartUrlEN/$StringSimpleEN,$_,emule.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";

			}; # foreach 

		}; # for ( $j=1; $j<=31; $j++ ) {	# tage
	}; # for ( $i=0; $i<=12; $i++ ) {		# monate	
	######## ENDE: für 2007 hier nur ##########

	

	######## für 2006 newsarchive hier nur ##########
	for ( my $i=0; $i<=11; $i++ ) {		# monate
		
		my $dateDE		= @monate[$i];
		my $dateEN		= @months[$i];
	
		my $StringSimpleDE	= lc "$dateDE-2006";
		my $StringSimpleEN	= lc "$dateEN-2006";
		
		#print SITEMAP "<url><loc>$NewsStartUrlDE/$StringSimpleDE.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
		print SITEMAP "<url><loc>$NewsStartUrlEN/$StringSimpleEN.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
		
		#	print "NEWSARCHIVE 2006: $NewsStartUrlDE/$StringSimpleDE.html \n";
		#	print "NEWSARCHIVE 2006: $NewsStartUrlEN/$StringSimpleEN.html \n";

	}; # for ( $i=0; $i<=12; $i++ ) {		# monate	
	######## ENDE: für 2006 newsarchive hier nur ##########



	######## für 2007 newsarchive hier nur ##########
	my $howmuch = 0;
	for ( my $i=0; $i<=$mon; $i++ ) {		# monate
		
		# zähle immer nur bis zum aktuellen tag des monats und bereite dazu nur die sitemaps vor: sprich wenn jetzt grade januar der 21 ist, dann ist es sinnfrei die sitemap bis zum 31.1 schon zu erstellen
		if ( $i == $mon ) {
			$howmuch = $mday;
		} else {
			$howmuch = 31;
		};
		
		my $dateDE		= @monate[$i];
		my $dateEN		= @months[$i];
	
		my $StringSimpleDE	= lc "$dateDE-2007";
		my $StringSimpleEN	= lc "$dateEN-2007";
		
	#	print SITEMAP "<url><loc>$NewsStartUrlDE/$StringSimpleDE.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
		print SITEMAP "<url><loc>$NewsStartUrlEN/$StringSimpleEN.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
			
		#	print "NEWSARCHIVE 2006: $NewsStartUrlDE/$StringSimpleDE.html \n";
		#	print "NEWSARCHIVE 2006: $NewsStartUrlEN/$StringSimpleEN.html \n";

	}; # for ( $i=0; $i<=12; $i++ ) {		# monate	
	######## ENDE: für 2007 newsarchive hier nur ##########

	print SITEMAP SuffixXMLHeader();
	close SITEMAP;

	# compressing file
	print "\tCompressing: $SiteMapFile.gz\n";

	system("/usr/bin/gzip -f -b 9 $SiteMapFile");
	system("/bin/chmod 755 $SiteMapFile.gz");
	PingGoogleForSitemap("$StoreSiteMapWebPath/zoozle-news-sitemap.xml.gz");

	return 1;

}; # sub GenerateSiteMaps(){


sub PingGoogleForSitemap(){

	my $SiteMapUrl = shift;	

	open(WH,">>sitemapurls-news.html");
		print WH "<a href=\"https://www.google.com/webmasters/sitemaps/addsitemap?siteUrl=http://www.zoozle.net/emule-bittorrent-download/&amp;hl=en&type=0&sitemapUrl0=$SiteMapUrl\">$SiteMapUrl</a><br><br>\n";
	close WH;
  
	my $ping = WWW::Google::SiteMap::Ping->new(
		"$SiteMapUrl",
	);

	$ping->submit;
	# print "\tThese pings succeeded:\n";
	foreach($ping->success) {
		print "\tSUCESS: $_ \n" if ( $_ =~ /\w/ );
		print "$_\n";
	}

	# print "\tThese pings failed:\n";
	foreach($ping->failure) {
		print "\t\tFAILURE $_\n" if ( $_ =~ /\w/ );
		print "$_\n";
	}

	my $yahoo		= get("http://www.yahoo.com/ping?sitemap=$SiteMapUrl");
	my $msn			= get("http://search.msn.com/ping?sitemap=$SiteMapUrl");
	my $msn1		= get("http://search.live.com/ping?sitemap=$SiteMapUrl");
	my $yahoo1		= get("https://siteexplorer.search.yahoo.com/submit/ping?sitemap=$SiteMapUrl");
	   
	return 1;

}; # sub PingGoogleForSitemap

sub PreXMLHeader(){

	my $returnString = <<"END";
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.4" -->
<!-- sitemap-generator-url="http://www.zoozle.org/" sitemap-generator-version="3.0b1" -->
<!-- generated-on="19. October 2006 20:21" -->
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

END
	
	return $returnString;

}; # sub PreXMLHeader(){


sub SuffixXMLHeader(){

	return "</urlset>";

}; # sub SuffixXMLHeader(){


sub getLastModTimeStamp(){

	my $now = shift || time();

	# We need to munge the timezone indicator to add a colon between the hour and minute part
	my $tz = strftime("%z", localtime($now));
	$tz =~ s/(\d{2})(\d{2})/$1:$2/;

	# ISO8601
	return  strftime("%Y-%m-%dT%H:%M:%S", localtime($now)) . "+00:00\n";
	
	# simple version, because WWW::Google::SiteMap ads tail automatically
	# return  strftime("%Y-%m-%d", localtime($now));

}; # sub getLastModTimeStamp(){

#	if ( length($i) == 1 ) {
			#		$i = "0". $i;
			#	};
			#	if ( length($j) == 1 ) {
			#		$j = "0". $j;
			#	};


exit;