#!perl -w

use File::Basename;
use Getopt::Long;

$mkfile = "mkfile.txt";

exit(main());
sub main
{
    init();
    my $command = "";

    # cabarc N cabfile [files]

    open(INPUT, $mkfile) || die "Couldn't open $mkfile: $!";
    while (my $line = <INPUT>)
    {
	chomp $line;

	$line =~ s/^#.*//;    # convert commented line to blank line
        if ($line =~ /^\s*$/) {        # Ignore any blank lines
            next;
	} elsif ($line =~ /^\s*([^=\s]+)\s*=\s*$/)    #cabinet name
	{
	    my @arr = split / /, $command;
	    if ($#arr > 2) {print "$command\n"; `$command`;}
	    $command = "cabarc N $1";
	} elsif ($line =~ /^\s*(.+)\s*\\*\s*$/)    #file name
	{
	    my $file = $1;
	    if (-e $file)
	    {
		$command .= " \"$file\"";
	    } else
	    {
		print "$file does not exist!\n";
	    }
	} else
	{
	    print "Corrupt mkfile: $mkfile\n";
	}
    }
    my @arr = split / /, $command;
    if ($#arr > 2) {print "$command\n"; `$command`;}

    close INPUT;
}

sub init
{
    if ($#ARGV == -1) {usage();}
    $mkfile = $ARGV[0];
}

sub usage
{
    print "Usage: " . basename($0) . " mkfile\n";
    print <<USAGE;

Make sure that all files listed in your mkfile are accessible from
the directory this program is run in.
All cabinet files created will be placed in this directory.
USAGE
    exit(0);
}
