#!/bin/sh -e

# Copyright (c) 2004 Robert Nordier.  All rights reserved.

# $Id: bcpl,v 1.10 2004/12/21 10:51:50 rn Exp $

usage()
{
    echo 'usage: bcpl [-O] [-o output] file'
    exit 2
}

d=/usr/local/lib/bcplkit

oflag=0
args=`getopt Oo: $*`
if test $? -ne 0; then
    usage
fi
set -- $args
for i
do
    case "$i"
    in
        -O)
            oflag=1; shift;;
        -o)
            oarg="$2"; shift; shift;;
        --)
            shift; break;;
    esac
done
if test $# -ne 1; then
    usage
fi

rm -f OCODE INTCODE ASM
if test -n "$oarg"; then
    f="$oarg"
else
    f=`basename $1 .b`
fi
rm -f $f.int $f.s $f.o

$d/st < $1
$d/cg < OCODE
cat $d/iclib.i $d/blib.i INTCODE > $f.int
$d/xg < $f.int
if test $oflag -ne 0; then
    copt $d/rules < ASM > ASM.op && mv ASM.op ASM
fi
cat ASM > $f.s
as --32 -o $f.o $f.s
ld -m elf_i386 -o $f $d/su.o $f.o $d/rt.o $d/sys.o
