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

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

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

use strict;

# 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 $locate = 'locate description| grep "/home/www/"| grep "description$"|';

# Open alldes for sorting in reverse order and writing
open(ALLDES,"|sort -r>alldes") || die "Can't spwan sort";
# Get a list of all valid 'description'-files
open(DESLIST, $locate ) || die "Can't spawn locate";
while (<DESLIST>) {
	# skip trailing newline
	chomp;
	# Set description file
	my $desfile = $_;
	# mask out fullpath and webpath from $_
	m|(/home/www/(.*))/description$|;
	my $fullpath = $1;
	# set full web-path without 'http://'
	my $webpath = $2;
	# 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
	}	# if
	close(INDEX);
	if (!($title and $edate and $location)) {
		print "title/edate/location missing: $indexfile\n";
	}

	# Open 'description' for reading
	if (-r $desfile) {
		open(DES, "<$desfile" );
		while (<DES>) {
			chomp;
			my ($imgfile,$datxt,$entxt) = split /;/;
			# imgfile is defined, proseed
			if ($imgfile) {
				my $w; my $h;
				if (-r "$fullpath/$imgfile") {
					open(IMG, "cat $fullpath/$imgfile|djpeg|head -n 2|tail -n 1|");
					while (<IMG>) {
						($w,$h) = split;
					}
					close(IMG);
				}
				my $w1; my $h1;
				if (-r "$fullpath/1$imgfile") {
					open(IMG, "cat $fullpath/1$imgfile|djpeg|head -n 2|tail -n 1|");
					while (<IMG>) {
						($w1,$h1) = split;
					}
					close(IMG);
				}
				my $w2; my $h2;
				if (-r "$fullpath/2$imgfile") {
					open(IMG, "cat $fullpath/2$imgfile|djpeg|head -n 2|tail -n 1|");
					while (<IMG>) {
						($w2,$h2) = split;
					}
					close(IMG);
				}
				print ALLDES "$edate;$fullpath;$webpath;$imgfile;$w;$h;$w1;$h1;$w2;$h2;$location;$title";
				if ($datxt) {
					print ALLDES ";$datxt";
				} else {
					print ALLDES ";--";
				}
				if ($entxt) {
					print ALLDES ";$entxt";
				}
				print ALLDES "\n";
			} # if valid file
		} # while (<DES>)
		close(DES);
	} # file exist
} # while (<DESLIST>)
close(DESLIST);
close(ALLDES);


