#!/usr/bin/perl -w

######################################################################
##
## genlist
##
## Created 7.12.1998 by Lasse Antero Vartiainen
##
##
## $Modified:$
##
######################################################################


BEGIN { $0 =~ /.*\//;($main::MYPATH=$&)||($main::MYPATH="./"); }

use Time::Local;
use strict;

#Version history:

#0.01 - 071298 Created


# --- Some Globals ---
$Main::filealpha="LIST_alpha.html";
$Main::filedate="LIST_date.html";
# --- Initialization ---
$Main::verbose=1;
# --- main program ---

print "Scanning files..\n" if($Main::verbose);
$Main::list= qx{find /files/pics/};

#$Main::list =~ s/^.*?(\/\.).*?\n//gm;
$Main::list =~ s/^(\/files\/pics\/)//gm;
#$Main::list =~ s/^.*?(priv).*?\n//gm;

@Main::files = split('\n',$Main::list);

print "Sorting by name..\n" if($Main::verbose);

#BEGIN SORT_BY_NAME
@Main::files = sort(@Main::files);
$Main::sorttype="alphabetic";

print "Saving to $Main::filealpha..\n" if($Main::verbose);

save_file_safe($Main::filealpha, doPrint());
#End  -SbN-


print "Sorting by date..\n" if($Main::verbose);

#BEGIN SORT_BY_DATE
@Main::tmp=@Main::files;
@Main::nums=();
for (@Main::tmp) {
    my($wgt)=0;
    if(/(\d{2})(\d{4})/) {
	if($1<30) {$wgt=(100+$1)*10000+$2;}
	else {$wgt=($1)*10000+$2;}
    } else {
	$wgt="0000001";
    }
    push @Main::nums, $wgt;    
#    push @Main::nums, /(\d{6})/ if (/\d{6}/);
#    push @Main::nums, 1 unless (/\d{6}/);
}


@Main::files = @Main::tmp[ sort{
    $Main::nums[$::b] <=> $Main::nums[$::a]} 0..$#Main::tmp
			  ];

#foreach $test (@Main::files) {
#    print "D:$test\n";
#}
#print "Debug\n$#Main::tmp\n";

$Main::sorttype="chronological";

print "Saving to $Main::filedate..\n" if($Main::verbose);

save_file_safe($Main::filedate, doPrint());


print "Generating dir.html\n";
qx { mv /files/pics/index.html /files/pics/index.html.bak };
qx { lynx --source http://tirannon.iki.fi/pics/ >/files/pics/dir.html };
qx { mv /files/pics/index.html.bak /files/pics/index.html };

#End -SbD-






#--- End of main ---

sub doPrint {
    my($res)="";
    $res.=genTitle("Picture Archive Listing - $Main::sorttype");
    $res.="<ul>\n";
    foreach $Main::entry (@Main::files) {
	if(($Main::entry =~ /(jpg|gif)/i) && 
	   !($Main::entry =~ /^\.|\/\./) &&
	   !($Main::entry =~ /priv/i) &&
	   !($Main::entry =~ /incoming/i) &&
	   !($Main::entry =~ /\_t\./i)) {
	    $res.="<li><a href=\"";
	    $res.="$Main::entry";
	    $res.="\">$Main::entry"."</a>\n";
	}
    }
    $res.="</ul>";
    
    $res.=genFoot();
    return $res;
}

sub save_file_safe {
    my($filename,$data)=@_;
    my($tmp);
    if(-e "$filename") {
        if(system("cp \"$filename\" \"${filename}~\"")) {
            error("Failed to backup $filename",0);
            return -1;
        }
    }
    open (SFILE,">$filename")|| return -2;
    $tmp = syswrite(SFILE,$data,length($data));
    if($tmp != length($data)) {
        error("Wrote $tmp of ".length($data)." bytes to $filename",0);
        return -3;
    }
    close(SFILE);
    return 0;
}
    
    


# Function: genTitle
# Inputs: $title
# Output: $form
# Globals(IN): $main::version,$main::admin
sub genTitle {
    my($title)=@_;
#Stardard heading part with our title
    
    my($form)="
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">
<HTML><HEAD>
<TITLE>$title</TITLE>
<link rev=\"made\" href=\"mailto:lav\@iki.fi\">
</HEAD>
<BODY BGCOLOR=\"#ffffff\"  text=\"#000000\" link=\"#0000ff\" vlink=\"#600080\" alink=\"#ff0000\">
<H1>$title</H1>
";
    
    return $form;
}

# Function: genFoot
# Generate standard footer
# Inputs: 
# Output: $form
# Globals(IN) :$main::version, $main::admin,$main::maillist
# Globals(OUT):-
sub genFoot {
    my($form)="";
    
    $form.="\
<HR><FONT SIZE=-1>
<!-- hhmts start -->
<!-- hhmts end -->
</FONT></BODY></HTML>
";
}





sub basename {
    my($name)=@_;
    $name =~ /(.*\/)*(.*?)$/;
    return $2;
}
