#!/usr/bin/env perl

if (@ARGV != 3) { die "USAGE: mkindex.dvo.pl (catdir) (catdir_name) (ippdata | dvodist)\n" ;}

@nlist = ("n0000", "n0730", "n1500", "n2230", "n3000", "n3730", "n4500", "n5230", "n6000", "n6730", "n7500", "n8230");
@slist = ("s0000", "s0730", "s1500", "s2230", "s3000", "s3730", "s4500", "s5230", "s6000", "s6730", "s7500", "s8230");

$catdir = &basename($ARGV[0]);
$catdir_name = $ARGV[1];
$target_name = $ARGV[2];
# "Synthetic Photometry Catdir";

&mkindex ($ARGV[0], "topdir");
foreach $subdir (@nlist, @slist) { 
    $dirname = "$ARGV[0]/$subdir";
    print "subdir: $dirname\n";
    if (! -e $dirname) { next; }
    &mkindex ($dirname, "subdir"); 
}
exit 0;

sub mkindex {
    # input  : (path) (mode)
    # (mode) = "topdir" or "subdir"
    # output : path/index.mkd

    my($topdir) = $_[0];
    my($mode) = $_[1];

    $mydir = &basename($topdir);
    if ($mode eq "topdir") { $thisdir = $catdir; } else { $thisdir = "$catdir/$mydir"; }

    if (! -e $thisdir) { mkdir $thisdir; }
    open (FILE, ">$thisdir/index.mkd");

    # XXX ippdata.page.php vs dvodist.page.php
    if ($target_name eq "ippdata") {
	print FILE "<?php include \"\@HTDOCS\@/www-util/ippdata.page.php\"\; ?>\n\n";
    } else {
	print FILE "<?php include \"\@HTDOCS\@/www-util/dvodist.page.php\"\; ?>\n\n";
    }

    # CATDIR Title supplied on cmd line
    print FILE "# $catdir_name\n";

    if ($mode eq "subdir") {
	print FILE "## $mydir\n\n";
    }
    print FILE "\n";

    if ($mode eq "topdir") {
	print FILE "(up to [PARENT](\@WWWHOST\@/index.php))\n\n";
    } else {
	print FILE "(up to [$catdir_name](\@WWWHOST\@/$catdir/index.php))\n\n";
    }

    print FILE "Download this entire directory with the following command:\n\n";

    print FILE "<tt> wget -c -r -np -nH \@WWWHOST\@/$thisdir/index.php</tt>\n\n";

    print FILE "@ Data Product : none : plain : title : size (bytes)\n";

    my($N) = 1;
    my(@entries, $entry);

    if ($mode eq "topdir") {
	my(@Nentries) = <$topdir/n????>;
	my(@Sentries) = <$topdir/s????>;
	my(@all) = <$topdir/*>;
	foreach $other (@all) {
	    $base = &basename($other);
	    if ($base =~ m|^n....|) { next; }
	    if ($base =~ m|^s....|) { next; }
	    push @other, $other;
	}
	@entries = (@other, @Nentries, @Sentries);
    } else {
	@entries = <$topdir/*>;
    }

    foreach $entry (@entries) {

	$file = &basename($entry);
	if ($file =~ m|~$|) { next; }

	if ($file eq "index.php") { next; }
	if ($file eq "index.mkd") { next; }
  
	@filestat = stat ($entry);
	$size = $filestat[7];
	print "file: $file, size: $size\n";

	if (-d $entry) {
	    print FILE "@ $file : dir : dir$N : directory\n";
	}

	if (-f $entry) {
	    print FILE "@ $file : file : file$N : $size\n";
	}

	if ($N == 1) { $N = 2; } else { $N = 1; }
    }
    close (FILE);
}

sub vsystem {
    # print STDERR "@_\n";
    my($status) = system ("@_");
    $status;
}

sub goodbye {
    die "@_\n";
}

# behaves just like system basename function (returns last portion of path)
sub basename {

    my ($file) = $_[0];
    my ($base, @words);

    @words = split ("/", $file);
    $base = $words[-1];
    return $base;
}

