#!/usr/bin/perl -w
use strict;

#thumbnail_webpage.pl
#version 1.7
#Copyright 2003-2012, 2013, 2014 John C. Vernaleo
#Unfortunately, I am not comfortable putting unobscured email addresses
#on the web, but this shouldn't be too hard to figure out.
#
#		(my_first_name)@netpurgatory.com
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    See readme_thumbnails.txt for more information and a copy of the GPL

#Making these two variables global so I can use strict without restructuring.
my $i;
my @pics;

my $main_dir=$ARGV[0];
unless(defined($main_dir)){
    print "Type directory name with pictures: ";
    chomp($main_dir=<STDIN>);
}
chdir $main_dir or die "Cannot change to $main_dir $!";

print "now in $main_dir\n";

my $clean=0;
if(defined($ARGV[1])&&($ARGV[1] eq "-c")){
    $clean=1;
    print "Making new thumbnails and mid-sized images\n";
}

#Need this to take care of main_dir
my @dirs;
my @tmp2=glob "*";
my $haspic=0;
my $top=0;
foreach my $tmp2 (@tmp2){
    if($tmp2=~".JPG"||".jpg"||".jpeg"||".JPEG"||".png"||".PNG"){
	$haspic=1;
    }
}
if($haspic){
    push(@dirs,"./");
}

#this finds sub directories
&get_dir();

#Only make main page if there are subdirectories.
if($#dirs==0){
    if($haspic){
	&mkthumbs;
    }
}
#extra if instead of an else so if there are no pictures an empty
#index.html file is not made.
if($#dirs>0){
    if($haspic){
	$top=1;
	&mkthumbs;
	$top=0;
    }
    open MAINPAGE, ">index.html" or die "cannot open index.html $!";
    select MAINPAGE;
    &page_top;
    print "<title>Listing of Pictures</title>\n";
    print "</head>\n<body>\n";
    print "<h1>Listing of Picture Thumbnails</h1>\n";
    print "<p>\n";
    print "<!-- Generated by thumbnail_page.pl by jcv http://www.netpurgatory.com/ -->\n";
    print '<a href="../">Go back</a><br /><br />'."\n";
    &caption("index2.html");
    for(my $j=0;$j<=$#dirs;$j++){
	$_=$dirs[$j];
	if(($j==0)&&($_=~'./')){
	    print "<a href=".'"'."./index2.html".'">'."$_</a>\n";
	}else{
	    print "<a href=".'"'."./$_/index.html".'">'."$_</a>\n";
	}
	print "<br />\n";
    }

    print "</p>\n</body>\n</html>";
    close MAINPAGE;
}

#Subroutines
sub get_dir{
    my ($base)=(@_);
    my $base2;
    my @tmp=glob "*";
    my @tmp2;
    my $haspic;
    foreach (@tmp){
	$haspic=0;
	if((-d $_)&&($_ ne "pages")&&($_ ne "thumbs")&&($_ ne "mid")){
	    chdir $_ or die "Cannot change to $_ $!";
	    if($base)
	    {
		$base2=$base."/$_";
	    }else{
		$base2=$_;
	    }
	    @tmp2=glob "*";
	    foreach my $tmp2 (@tmp2){
		if($tmp2=~".JPG"||".jpg"||".jpeg"||".JPEG"||".png"||".PNG"){
		    $haspic=1;
		}
	    }
	    if($haspic){
		push(@dirs,$base2);
		&mkthumbs;
	    }
	    &get_dir($base2);
	    chdir "..";
	}
    }
}

sub get_subdirs{
    my ($base)=(@_);
    my $base2;
    my @tmp=glob "*";
    my @tmp2;
    my $haspic;
    my @subdirs;
    foreach (@tmp){
	$haspic=0;
	if((-d $_)&&($_ ne "pages")&&($_ ne "thumbs")&&($_ ne "mid")){
	    chdir $_ or die "Cannot change to $_ $!";
	    if($base){
		$base2=$base."/$_";
	    }else{
		$base2=$_;
	    }
	    @tmp2=glob "*";
	    foreach my $tmp2 (@tmp2){
		if($tmp2=~".JPG"||".jpg"||".jpeg"||".JPEG"||".png"||".PNG"){
		    $haspic=1;
		}
	    }
	    if($haspic){
		push(@subdirs,$base2);
	    }
	    &get_subdirs($base2);
	    chdir "..";
	}
    }
    return @subdirs;
}

sub mkthumbs{
    my @jpg=glob "*.jpg";
    my @jpg2=glob "*.JPG";
    my @gif=glob "*.gif";
    my @gif2=glob "*.GIF";
    my @png=glob "*.png";
    my @png2=glob "*.PNG";

    @pics = ();
    push(@pics,@jpg);
    push(@pics,@jpg2);
    push(@pics,@gif);
    push(@pics,@gif2);
    push(@pics,@png);
    push(@pics,@png2);

    unless (-e "thumbs"){
	mkdir "thumbs" or die "Can't make directory: $!";
    }
    unless (-e "mid"){
	mkdir "mid" or die "Can't make directory: $!";
    }
    unless (-e "pages"){
	mkdir "pages" or die "Can't make directory: $!";
    }

    foreach my $file (@pics){
	my $size="120";
	my $command="convert -size $size"."x$size $file -resize $size"."x$size +profile ".'"*"'." thumbs/thum_".$file;
	if(($clean)||!(-e "thumbs/thum_$file")){
	    system $command;
	}
	$size="900";
	$command="convert -size $size"."x$size $file -resize $size"."x$size +profile ".'"*"'." mid/mid_".$file;
	if(($clean)||!(-e "mid/mid_$file")){
	    system $command;
	}
    }
    #This is just to help clean up files from old version of script
    if(-e "pages/pictures.html"){
	unlink "pages/pictures.html";
    }

    if($top){
	open WEBPAGE, ">index2.html" or die "Cannot create file: $!";
    }else{
	open WEBPAGE, ">index.html" or die "Cannot create file: $!";
    }
    select WEBPAGE;
    &page_top;
    print "<!-- Generated by thumbnail_page.pl by jcv http://www.netpurgatory.com/ -->\n";
    &body(@pics);
    #Add links to subdirs here.
    my @sub_dirs = &get_subdirs;
    if($#sub_dirs>0){
	foreach(@sub_dirs){
	    print "<a href=".'"'."./$_/index.html".'">'."$_</a>\n";
	    print "<br />\n";
	}
    }

    close WEBPAGE;
    select STDOUT;
    print @pics,"\n";
}

sub page_top{
    print "<!doctype html>\n";
    print "<html>\n";
    print "<head>\n";
    print '<meta charset="UTF-8">'."\n";
}

sub body{
    (@pics)=(@_);
    my $file;
    print "<title>Pictures</title>\n";
    print "</head>\n<body>\n";
    print '<p><a href="../">Back to main picture page.</a></p>'."\n";
    &caption("index.html");
    print "<p>\n";
    for($i=0;$i<=$#pics;$i++){
	$file=$pics[$i];
	select WEBPAGE;
	print '<a href="pages/'.($i+1).'.html">';
	print '<img src="thumbs/thum_'.$file.'" alt="'.$file.'" />';
	print "</a>\n";
	#make individual picture page
	open PAGE, ">pages/".($i+1).".html" or die "Cannot create file: $!";
	select PAGE;
	&page_top;
	print "<title>Picture ".($i+1)." of ".($#pics+1)."</title>\n";
	print "</head>\n<body>\n";
	&navigation;
	print '<p><a href="../'.$file.'">';
	print '<img src="../mid/mid_'.$file.'" alt="'.$file.'" style="width:70%;"/>'."\n<br /></a></p>\n";
	&caption($file);
	&navigation;
	print "</body>\n</html>";
	close PAGE;
    }
    select WEBPAGE;
    print "</p>\n</body>\n</html>";

}

sub caption{
    my $filename=$_[0];
    if (-e $filename.".txt"){
	print "<p>\n";
	open (CAPFILE,$filename.".txt");
	while (<CAPFILE>){
	    chomp;
	    print "$_\n";
	}
	close (CAPFILE);
	print "</p>\n";
    }
}

sub navigation{
    print "<p>\n";
    if($#pics==0){
	print '<a href="../index.html">Index</a>'."\n";
    }else{
	if(($i==0)||($i==$#pics)){
	    if($i==0){
		print '<a href="../index.html">Index</a>'."\n";
		print '<a href="./'.($i+2).'.html">Next Picture</a>'."\n";
	    }
	    if($i==($#pics)){
		print '<a href="./'.($#pics).'.html">Previous Picture</a>'."\n";
		print '<a href="../index.html">Index</a>'."\n";
	    }
	}else{
	    print '<a href="./'.($i).'.html">Previous Picture</a>'."\n";
	    print '<a href="../index.html">Index</a>'."\n";
	    print '<a href="./'.($i+2).'.html">Next Picture</a>'."\n";
	}
	print "<br />\nClick on picture for Full-Sized version.\n";
    }
    print "</p>\n";
}
