#!/usr/local/bin/perl

#    
#    Copyright (C) 1996-2001 Kenneth Stokkeland <kstokke@pogostick.net>
#    
#    Hva : Web album
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
#    
use CGI qw(:standard *table start_ul);

$indexer = "album.cgi";              # hva heter dette scriptet
$arg = $ARGV[0] || "album"; 
$html_dir = "album_html";            # sub dir med html kode


print header;
if( -d $arg)    { &show_dir($arg);  }
elsif( -f $arg) { &show_file($arg); }

print "</blockquote>\n";
print "<HR NOSHADE=1>";
print "<I>Server time: ", scalar localtime(), "</I>", "&nbsp;" x 50;
print "Made by <A HREF=\"mailto:kstokke\@pogostick.net\">Kenneth Stokkeland</A>";
print end_html;


sub read_desc {
     my($filename) =  @_;
     open(DESC,"$filename/desc") or warn("$! : $filename\n");
     while(<DESC>) {
	  ($file, @desc) = split(/\s+/,$_);
	  $DESC{$file} = join(" ", @desc);
     }
     close DESC;
}


sub show_dir {     
     my($pic_dir)=@_;
     &read_desc("$pic_dir");
     opendir(DIR, $pic_dir) || warn "can't opendir $pic_dir: $!";
     # plukk ut kun filer som ikke er . , .. 
     @files = grep { !/^\./ } readdir(DIR);
     closedir(DIR);
     print start_html(-bgcolor=>"white", -title=>$pic_dir),"\n";
     print "<H2>Velg en link å kikke på</H2><blockquote><br>\n";
     print start_table;
     print "<TR><TD ALIGN=CENTER></TD>";
     print "<TD><B>Beskrivelse</TD>";
     print "<TD><B>Sist endret</TD></TR>";
     foreach $file (@files) {
	  next if($file eq "desc"); # denne har vi lest tidligere
	  $desc = $DESC{$file} || $file;
	  $file = $pic_dir . "/$file";
	  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	   $atime,$mtime,$ctime,$blksize,$blocks)
	       = stat($file);
	
	  if( -d $file) { 
	       
	       print "<TR><TD ALIGN=CENTER><IMG SRC=\"/~kstokke/images/pil.gif\" BORDER=0> </TD> ";
	       print "<TD><A HREF=\"$indexer?$file\"> $desc</A></TD>";
	       print "<TD>", &date($mtime),"</TD>";
	       print "<TD><I> (Album)</I> </TD></TR>\n"; 
	  }
	  elsif ( $file !~ /thumb/) { 
	       $thumb = $file;
	       $thumb =~ s/(\.jpg)|(\.gif)/-thumb$1/;
	       $file =~ m/.+\/(.+(jpg|gif))$/;
	       $desc = $DESC{$1}|| $1;
	       $ahref = "<A HREF=\"$indexer?$file\">";

	       if( -f $thumb ) { # Med forhåndstitt bilde
		    print "<TR><TD ALIGN=RIGHT>$ahref<IMG SRC=\"${thumb}\" BORDER=0></A></TD>";
		    print "<TD>$desc </TD>";
		    print "<TD>", &date($mtime), "</TD></TR>\n";
	       }
	       else {		# Kun tekst
		    print "<TR><TD><IMG SRC=\"/~kstokke/images/pil.gif\" BORDER=0></TD>";
		    print "<TD>$ahref $desc </A></TD>";
		    print "<TD>", &date($mtime), "</TD></TR>\n";
	       }
	  }
     }
     print end_table;
}

sub show_file {
     my($pic) = @_;
     $pic =~ m/(.+)\/(.+(jpg|gif))$/;
     $pic_file = $2;
     $pic_dir = $1;
     &read_desc("$pic_dir");
     print start_html(-bgcolor=>"white", -title=>$pic),"\n";
     print "<B><I>Dir : $pic_dir <BR> Fil :  $pic_file</I></B> <BR><BR>\n";
     print "<H3 ALIGN=CENTER><IMG SRC=\"$pic\" BORDER=0><BR><BR>\n";
     print "$DESC{$pic_file}</H3>\n";

}

sub date {
     my ($time) = @_;
     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
#     sprintf("%04d-%02d-%02d %2d:%02d:%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
     sprintf("%02d/%02d-%04d", $mday, $mon+1, $year + 1900);
 }
