#!/usr/bin/perl -w
# ialbum by Hans Schou

# In vi: set ts=2
# (tabstop = 2)

# This script parses through all desenvue files and make one big file with all data

use strict;
use Image::Size;  # debian , in: libimage-size-perl

# Output file format
# 'alldes' file format, semicolon sep-file
# 0: event date in ISO format
# 1: full local image path
# 2: web-site full path without 'http://'
# 3: image file name - the big raw picture
# 4: Width, big image
# 5: Height, big image
# 6: Width, small image
# 7: Height, small image
# 8: Width, medium image
# 9: Height, medium image
#10: location, address, country
#11: title of session
#12: Danish description
#13: English description

my $icount = 0;

my $hostname=`hostname`;
chomp $hostname;

#my $locate = 'locate description| grep "/home/www/"| grep "description$"|';
my $locate = 'slocate -r "/home/www/.*/desenvue$" |';
#my $locate = '< deslist';

# Open alldes for sorting in reverse order and writing
open(ALLDES,"| sort -r > alldes_$hostname") || die "Can't spwan sort";
# Open and create a list of sessions
open(SESSION,"| sort -r > session_$hostname") || die "Can't create $!";
# Get a list of all valid 'desenvue'-files
open(DESLIST, $locate ) || die "Can't spawn slocate";
while (<DESLIST>) {
	# skip trailing newline
	chomp;
	# Set desenvue file
	my $desfile = $_;
	# mask out fullpath and webpath from $_
	m|(/home/www/(.*))/desenvue$|;
	my $fullpath = $1;
	# set full web-path without 'http://'
	my $webpath = $2;
	$webpath=~s|/htdocs||;
	# Set and init default title
	my $title = "";
	my $edate = "";
	my $location = "";
	# Open '$fullpath/index.php' and get '$title'
	my $indexfile = "$fullpath/index.php";
	# if index.php does not exist, try index.php3
	if (! -r $indexfile) {
		$indexfile .= "3";
	}
	# if indexfile exist, open it and get $title, $edate and $location
	if (-r $indexfile) {
		open(INDEX, "<$indexfile");
		while (<INDEX>) {
			chomp;
			# if $title defined in this line, grap it
			if (/^\s*\$title\s*=\s*["']+(.*)["'];$/) {
				# $title = "An event";
				$title = $1;
			}
			if (/^\s*\$edate\s*=\s*["']+(.*)["'];$/) {
				# $edate = "2000-03-03";
				$edate = $1;
			}
			if (/^\s*\$location\s*=\s*["']+(.*)["'];$/) {
				# $location = "Symbion, København, Danmark";
				$location = $1;
			}
			# all vars has been picked up, break out of loop
			if ($title and $edate and $location) {
				last;
			}
		}	# while
		close(INDEX);
	}	# if
	if (!($title and $edate and $location)) {
		my $envuedulocal = "$fullpath/envuedulocal.inc";
		if (-r $envuedulocal ) {
			open(LOCAL, "<$envuedulocal");
			while (<LOCAL>) {
				chomp;
				# if $title defined in this line, grap it
				if (/^\s*\$title\s*=\s*["']+(.*)["'];$/) {
					# $title = "An event";
					$title = $1;
				}
				if (/^\s*\$edate\s*=\s*["']+(.*)["'];$/) {
				        # $edate = "2000-03-03";
					$edate = $1;
				}
				if (/^\s*\$location\s*=\s*["']+(.*)["'];$/) {
				        # $location = "Symbion, København, Danmark";
					$location = $1;
				}
				# all vars has been picked up, break out of loop
				if ($title and $edate and $location) {
				        last;
				}
			}
			close(LOCAL);
		}
	if (!($title and $edate and $location)) {
		print "title/edate/location missing: $indexfile\n";
	}
	}
	# Open 'desenvue' for reading
	if (-r $desfile) {
		my $imgcount = 0;
		open(DES, "<$desfile" );
		while (<DES>) {
			chomp;
			my ($imgfile,$imagedate,$datxt,$entxt) = split /;/;
			# imgfile is defined, proseed
			if ($imgfile) {
				$imgcount++;
				# Get image width and height from all 3 images
				my ($w, $h, $w1, $h1, $w2, $h2) = (0, 0, 0, 0, 0, 0);
				if (-r "$fullpath/$imgfile") {
					($w,$h) = imgsize("$fullpath/$imgfile");
				}
				if (-r "$fullpath/1$imgfile") {
					($w1,$h1) = imgsize("$fullpath/1$imgfile");
				}
				if (-r "$fullpath/2$imgfile") {
					($w2,$h2) = imgsize("$fullpath/2$imgfile");
				}
				# Output a full line
				print ALLDES "$imagedate;$fullpath;$webpath;$imgfile;$w;$h;$w1;$h1;$w2;$h2;$location;$title";
				print ALLDES ($datxt ? ";$datxt" : ";--");
				print ALLDES ($entxt ? ";$entxt" : ";**");
				print ALLDES "\n";
			} # if valid file
		} # while (<DES>)
		close(DES);
	  print SESSION "$edate;$imgcount;$webpath;$title\n";
	} # file exist
} # while (<DESLIST>)
close(DESLIST);
close(SESSION);
close(ALLDES);

if ($hostname eq "www.sslug.dk") {
	system("wget -q -O alldes_tyge http://cvs.sslug.dk/billeder/alldes_tyge");
	system("wget -q -O session_tyge http://cvs.sslug.dk/billeder/session_tyge");
}

system("cat alldes_* | sort -r > alldes");
system("cat session_* | sort -r > session");

