#!/usr/bin/perl -w
#
# VERSION 1.3
#
# CHANGES:
# 1.3
#	IMG quality in params
# 1.2
#	IMG height/width in HTML tags
# 1.1
#	s_ et b_ prefixes
# 1.0
#	Initial...
#
use Getopt::Long;

############# DEFAULT
$dsrc=".";
$ddst=".";
$dsize1="150x75";
$dsize2="800x600";
$dtitle="TITLE";
$dtheme="default_theme.html";
$quality="90";

############# TELLMAINOPTIONS
sub tellmainoptions()
{
	print "usage:\n";
	print "htmlgallery.pl [--title=\"Page Title\"] [--theme=/path/to/theme]\n";
	print "	[--src=/source/dir] [--dst=/destination/dir] [--noconvert]\n";
	print "	[--smallsize=150x75] [--largesize=800x600]\n"; 
	

	print "\nDefault values:\n";
	print "	src=$dsrc\n	dst=$ddst\n	smallsize=$dsize1\n";
	print "	largesize=$dsize2\n	quality=$quality\n";
	print "	theme=$dtheme\n	title=$dtitle\n";

	print "\nParser example:";
	print "
	<@
		PAGE_TITLE=\"Title of page\";
		LOOP_STEP=3;
		LOOP_ROW_BEFORE=\"	<TR>\";
		LOOP_ROW_AFTER=\"</TR>\\n\";
		LOOP_COL_BEFORE=\"		<TD>\";
		LOOP_COL_AFTER=\"</TD>\\n\";
		LOOP_NOW
	@>\n";
}

############# GETMAINOPTIONS
sub getmainoptions()
{
	$src=$dsrc; $dst=$ddst; $theme=$dtheme; $size1=$dsize1;
	$size2=$dsize2; $title=$dtitle; $help=0; $noconvert=0;
	print "Getting options...\n";
	GetOptions(
		"noconvert+"	=> \$noconvert,
		"help+"		=> \$help,
		"src=s"		=> \$src,
		"dst=s"		=> \$dst,
		"theme=s"	=> \$theme,
		"quality=s"	=> \$quality,
		"smallsize=s" 	=> \$size1,
		"largesize=s" 	=> \$size2,
		"title=s"	=> \$title
	);

	if ($help) { &tellmainoptions(); exit 0; }
	$src =~ s/\/$//g;
	$dst =~ s/\/$//g;
}

############# SHOWMAINOPTIONS
sub showmainoptions()
{
	print "Using:\n";
	print "	src=$src\n	dst=$dst\n	smallsize=$size1\n";
	print "	largesize=$size2\n	quality=$quality\n";
        print "	theme=$theme\n	title=$title\n"
}

############# MINIATURIZING
sub miniaturizing()
{
	print "Miniaturizing:\n";
	opendir PICTURES, $src or die "Beuarh!!! Can't open src directory $src";
	@pictures = readdir PICTURES;
        closedir PICTURES;
	$nbpictures=0;
	while($pic = shift @pictures) 
	{	
		if( ! ($pic =~ /\.(JPG|jpg|jpeg|JPEG)/) ) { next; }
		$pictab[$nbpictures]="$pic"; 
		if (!($noconvert)) { 
			print("	$src/$pic -> $dst/s_$pic\n");
			system("convert -quality $quality -geometry $size1 $src/$pic $dst/s_$pic");
			print("	$src/$pic -> $dst/b_$pic\n");
			system("convert -quality $quality -geometry $size2 $src/$pic $dst/b_$pic");
		}
		$size=""; $ret=`identify $dst/s_$pic`;
		$ret =~ / ([\d]+)x([\d]+) /; $size=" WIDTH=$1 HEIGHT=$2";
		$picsizetab[$nbpictures]=$size;
		$nbpictures++;
	}
}

############# GETHTMLOPTIONS
sub gethtmloptions()
{
	print "Parsing theme file ($theme):\n";
	$inbloc=0; $page="";
	open(THEME,$theme);
	while(<THEME>)
	{
		if (/(.*)\<\@(.*)\@\>(.*)/) 
		{ 	&parseoutside($1); &parseinside($2); 
			&parseoutside($3); $inbloc=0; }
		else {  if (/(.*)\<\@(.*)/) { &parseoutside($1); &parseinside($2); $inbloc=1; }
			else {
				if (/(.*)\@\>(.*)/) { &parseinside($1); &parseoutside($2); $inbloc=0; }
				else {
					if ($inbloc) { &parseinside($_); }
					else	     { &parseoutside($_); }
				     }
			     }
                     }
	}
	close(THEME);
}

############# PARSEINSIDE
sub parseinside()
{
	if (/PRINTTITRE;/) 		{ $page.=$title; print "\tfound PRINTTITRE\n"; }
	if (/PAGE_TITLE="(.*)";/) 	{ if ($dtitle eq $title) {$title=$1; } print "\tfound PAGE_TITLE\n"; }
	if (/LOOP_STEP=(.*);/) 		{ $loopstep=$1; print "\tfound LOOP_STEP\n"; }
	if (/LOOP_ROW_BEFORE="(.*)";/) 	{ $looprowbefore=$1; print "\tfound LOOP_ROW_BEFORE\n"; }
	if (/LOOP_ROW_AFTER="(.*)";/) 	{ $looprowafter=$1; print "\tfound LOOP_ROW_AFTER\n"; }
	if (/LOOP_COL_BEFORE="(.*)";/) 	{ $loopcolbefore=$1; print "\tfound LOOP_COL_BEFORE\n"; }
	if (/LOOP_COL_AFTER="(.*)";/) 	{ $loopcolafter=$1; print "\tfound LOOP_COL_AFTER\n"; }
	if (/LOOP_NOW;/) 		{ print "\tfound LOOP_NOW\n"; 
		if (!($loopstep>1)) { $loopstep=10; }
		for ($i=0; $i<$nbpictures; $i+=$loopstep)
		{
			$page.=$looprowbefore."\n";
			if ($i+$loopstep<$nbpictures) { $min=$i+$loopstep; } 
			else { $min=$nbpictures; }
			for($j=$i; $j<$min; $j++)
			{
				$page.=$loopcolbefore;
				$page.="<A HREF=b_".$pictab[$j].">";
				$page.="<IMG BORDER=0 SRC=s_".$pictab[$j];
				$page.=$picsizetab[$j].">";
				$page.="</A>";
				$page.=$loopcolafter."\n";
			}
			$page.=$looprowafter."\n";
		}
	}	
}

############# PARSEOUTSIDE
sub parseoutside()
{
	$page.="@_";
}

############# WRITEHTMLINDEX
sub writehtmlindex()
{
	print "Writing html index file ($dst/index.html)\n";
	open(INDEX, ">$dst/index.html");
	print INDEX $page;
	close(INDEX);
}

############# MAIN
&getmainoptions();
&showmainoptions();
&miniaturizing(); 
&gethtmloptions();
&writehtmlindex();

