#!/usr/bin/env perl

sub help {
 print <<EOF;
lgen.pl -- generate license files for mkcabal
usage:
lgen.pl [license] [outfile]
where license is simply a file containing
the text of the license (such as a .txt)
and outfile is the file to append
the haskellized version of the license
to. ex:

 \$ ./lgen.pl apache ../Licenses.hs

to add the apache license to the list of
licenses (note the last arg above isn't
needed explicitly, since that file is
implied.)

note that this will not add the license
to the system, you will have to do
a few slight manual edits so mkcabal
can use the license you add.
EOF
 exit;
}

help unless defined($ARGV[0]);

open(LICENSE,"<".$ARGV[0]);
open(OUTFILE,">>".(defined($ARGV[1]) ? $ARGV[1] : "../Licenses.hs"));

print OUTFILE #ARGV[0]." :: License\n"
print OUTFILE $ARGV[0]." = unlines\n    [";
$first = <LICENSE>;                # print first line seperately since
$first =~ s/"/\Q\"\E/g;            # it shouldn't be prefixed with a comma
chomp($first); print OUTFILE " \"".$first."\"\n    ";

while(<LICENSE>) { chomp;
 $_ =~ s/"/\Q\"\E/g;
 print OUTFILE ", \"".$_."\"\n    ";
}
print OUTFILE "]\n\n";

close(LICENSE); close(OUTFILE);
