#!/usr/bin/perl -w

# use strict;

unless (@ARGV == 1) {
    print <<EOD;
Usage:
  make-dylan-lib libname
EOD
    exit 1;
}

$libname = $ARGV[0];

mkdir $libname, 0777 or die "Can't create directory $libname: $!";
chdir $libname or die "Can't chdir to directory $libname: $!";

&write_file("$libname.lid", <<"EOD");
library: $libname
files:
  $libname-exports
  $libname
EOD

&write_file("$libname-exports.dylan", <<"EOD");
module: dylan-user

define library $libname
  use common-dylan;
  use io;

  export $libname;
end library;

define module $libname
  use common-dylan;
  use format-out;

  export foo;
end module;
EOD

&write_file("$libname.dylan", <<"EOD");
module: $libname
synopsis: 
author: 
copyright: 

define method foo(libname)
  format-out("Hello, world from $libname!\\n");
end method foo;
EOD

&write_file("Makefile", <<"EOD");
$libname.lib.du: $libname.lid $libname.dylan $libname-exports.dylan
\td2c $libname.lid

clean:
\t-rm -f *.o *.s *.a *.c *.mak *~ $libname.lib.du
\t-rm -rf .libs

install: $libname.lib.du 
\tlibtool /usr/bin/install -c lib$libname.a $libname.lib.du `d2c --dylan-user-location`
EOD
		 
sub write_file {
    local ($filename, $contents) = @_;
    open(OUTPUT, ">$filename") or die "Can't create $filename: $!";
    print OUTPUT $contents;
    close OUTPUT;
}
