#!/usr/local/bin/perl -w-- -*- perl -*-
#
# $Id: pascal,v 1.1 1994/02/22 02:45:21 qjb Exp $
# $Source: /home/qjb/scripts/RCS/pascal,v $
# $Author: qjb $
#

{
    local(@whoami) = split('/', $0);
    $whoami = pop(@whoami);
};

$tmp_out = "/tmp/nmpas.tmp.$$.c";

$SIG{"INT"} = "rmtmp";

if ((scalar(@ARGV) == 3) && ($ARGV[1] eq "-o"))
{
    ($source, $target) = ($ARGV[0], $ARGV[2]);
}
elsif (scalar(@ARGV) == 1)
{
    $target = $source = $ARGV[0];
    $target =~ s,.*/([^/]*),$1,;
    $target =~ s,([^/]*)\.pas$,$1,;
}
else
{
    &usage;
}

&usage if (! ($source =~ m/\.pas$/));
(system("p2c $source -o $tmp_out") == 0) || die "$whoami: p2c failed\n";

$result =
    system("gcc -o $target $tmp_out -lm -lp2c");
&rmtmp;

print "$source compiled into $target\n" if ($result == 0);

exit $result;

sub usage {
    print STDERR "Usage: $whoami file.pas [-o target]\n";
    print STDERR "  Converts pascal to c and compiles.\n";
    exit 1;
}

sub rmtmp {
    unlink($tmp_out);
}
