#!/usr/local/bin/perl

# ls2cdf
#
# Convert simple list of files into a CDF file
#
# Author: Andy Powell <a.powell@ukoln.ac.uk>
#
# $Id: ls2cdf,v 1.3 1998/02/02 23:02:54 lisap Exp $

require "getopts.pl";

$debug = 1;
$abstract = '';
$period = 1;
$chanurl = '';
$title = '';
$today = &date8601(time);
@file2url = (
    's/\/opt\/web\/content\//http:\/\//',
    's/intro.html$//',
    's/intro.htm$//',
    's/^$/.\//',
);

&Getopts('a:p:u:t:');

$abstract = $opt_a if $opt_a;
$period = $opt_p if $opt_p;
$chanurl = $opt_u if $opt_u;
$title = $opt_t if $opt_t;

print <<EOF;
<?XML VERSION="1.0" ?>

<CHANNEL HREF="$chanurl"
  LASTMOD="$today">
EOF
print "    <TITLE>$title</TITLE>\n" if $title;
print "    <ABSTRACT>$abstract</ABSTRACT>\n" if $abstract;
print <<EOF;
    <SCHEDULE>
	<INTERVALTIME DAY="$period"/>
    </SCHEDULE>
EOF

while (<STDIN>) {
    chop;
    my ($html, $file, $url, $t);
    $file = $_;
    $url = $file;
    if ($file =~ /\.htm/i && open(HTML, "<$file")) {
	while(<HTML>) {
	    $html = $html . $_;
	}
	$* = 1;
	if ($html =~ /\s*<TITLE>\s*(.*)\s*<\/TITLE>.*/i) {
	    $t = $1;
	}
	$* = 0;
    }
    unless ($t) {
	$t = $file;
	$t =~ s/.*\///;
    }
    foreach $re (@file2url) {
	eval "\$url =~ $re";
    }
#    $date = &date8601(-M $file);
    $date = &date8601((stat($file))[9]);
    print <<EOF;
    <ITEM HREF="$url" LASTMOD="$date">
	<TITLE>$t</TITLE>
    </ITEM>
EOF
}

print "</CHANNEL>\n";

exit;

sub date8601() {
#   1994-11-05T08:15
    my ($time) = @_;
    my ($year);
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
    if ($year < 97) {
	$year += 2000;
    }
    else {
        $year += 1900;
    }
    $mon++;
    $mon = '0' . $mon if ($mon < 10);
    $mday = '0' . $mday if ($mday < 10);
    $hour = '0' . $hour if ($hour < 10);
    $min = '0' . $min if ($min < 10);
    return("$year-$mon-$mday"."T$hour:$min");
}

# UKOLN is funded by the British Library Research and Innovation Centre, the
# Joint Information Systems Committee of the Higher Education Funding
# Councils, as well as by project funding from the JISC's Electronic
# Libraries Programme and the European Union.  UKOLN also receives support
# from the University of Bath where it is based.
