#!/usr/local/bin/perl
###
### converts lsm to INFO.LDV
### Usage: lsm2html <Infile> <Outfile>
### If Oufile is omittet, ouput will go to stdout
### If Infile is omittet, input will be read from stdin
###
### (c) FJL 1998
###

%validFields = (
    "Title"          => 1,
    "Version"        => 1,
    "Entered-date"   => 1,
    "Description"    => 1,
    "Keywords"       => 1,
    "Maintained-by"  => 1,
    "Author"         => 1,
    "Primary-site"   => 1,
    "Alternate-site" => 1,
    "Original-site"  => 1,
    "Platforms"      => 1,
    "Platform"       => 1,
    "Copying-policy" => 1,
        # for compatibility's sake:
    "Home-page"      => 1,
    "Maintainer"     => 1
);

@mandatoryFields = (
    "Title",
    "Version",
    "Entered-date",
    "Description",
    "Primary-site"
);

### open IN and OUT
if( $ARGV[0] ) {
    open( IN, "<$ARGV[0]" ) or die "Couldn't read `$ARGV[0]'";
} else {
    open( IN, "<-" );
}

if( $ARGV[1] ) {
    open( OUT, ">$ARGV[1]" ) or die "Couldn't write `$ARGV[1]'";
} else {
    open( OUT, ">-" );
}

### Scan LSM File

$part=0;
$line=0;
# Search for the "Begin" statement
while( <IN> ) {
    $line++;
    ( /^Begin[:3]?\s*$/ ) and $part=1, last;
}

( $part==1 ) or die "Invalid format! Missing `Begin' or `Begin3'";

# parse "body"
while( <IN> ) {
    $line++;
    # handle MS etc. end-of-line-marker (just remove carriage return)
    $_ =~ s:\r+::;

    ### empty Line?
    ( /^\s*$/ ) and next;

    ### end of LSM block
    ( /^End/ ) and $part=2, last;

    ### in block
    if (/^([\w-]+):\s*(.*)$/) {
        ### new block
	if( $validFields{$1} == 1) {
	    ### valid block
	    $field = $1
	} else {
	    warn "Invalid field `$1' on line $line";
	    $field = "invalid"
	}
	$lsm{$field} .= "$2";
    } elsif( /^\s+(.+)$/ ) {
        ### valid additional line
        $lsm{$field} .= "\n$1";
    } else {
        warn "Invalid line $line";
    }
}

( $part==2 ) or warn "Missing Delimiter `End'";

### found all mandatory fields?
foreach $field (@mandatoryFields) {
    ( length($lsm{$field}) > 0 ) or warn "Missing field `$field'";
}

### process some fileds and gather system info

# avoid problems with html tags in description field
$usage = $lsm{'Description'};
$usage =~ s/&/&amp;/g;
$usage =~ s/</&lt;/g;
$usage =~ s/>/&gt;/g;
$usage = "<PRE>$usage</PRE>";

# who am i
$user = getlogin || (getpwuid($<))[0];

# date
($x,$x,$x, $mday, $mon, $year) = localtime(time);
$mon++;
$year+=1900;
$date = "$mday.$mon.$year";

# url (might be correct LSM 09AUG94 or just plain URL)
($host, $path) = (split(/\s+/, $lsm{'Primary-site'}));
if ($host =~ /^\s*(ftp|http):/) {
    $url = "$host"
} else {
    $url = "ftp://$host$path"
}
$source = "<PRE><A HREF=\"$url\">$lsm{'Primary-site'}</A></PRE>";

# keywords should be separated by ",", but often whitepsace is used
if ($lsm{'Keywords'} =~ /,/) {
    $keys = join( "\n", split( /\s*,\s*/, $lsm{'Keywords'} ) );
} else {
    $keys = join( "\n", split( /\s+/, $lsm{'Keywords'} ) );
}

### build hdocu file

print OUT
"# This file was generated automatically by lsm2hdocu

_NAME
$lsm{'Title'}

_VERSION
$lsm{'Version'}

_KEY
$keys

_USAGE
$usage

_DOCU

_BIN

_LIB

_INC

_MAN

_OWNER
$user

_DATE
$date

_SOURCE
$source

_INSTALL

_END
";

close IN;
close OUT;
