#!/bin/sh

#
# UNIX Compiler Driver for Intel PL/M-80 Cross Compiler
#
# December 2006, Udo Munk
# January 2007, improved error handling, Udo Munk
# September 2007, source file now can be located anywhere.
#		  pass 2 might not generate the hex output file,
#		  if too may errors in pass 1, trapped. Udo Munk
# October 2007, clean temp if error in pass 2, Udo Munk
#

TEMP=/tmp/plm80-$$
RET=0

# check for input source file
if [ $# -lt 1 ]; then
	echo "Usage: `basename $0` file"
	exit 1
fi
if [ ! -f $1 ]; then
	echo "no source file"
	exit 1
fi

# figure where source is, assume extension .plm
DDIR=`pwd`
DIR=`dirname $1`
if [ $DIR = "." ]; then
	DIR=$DDIR
fi
FN=`basename $1 plm`

# create temp
mkdir $TEMP || (echo "can't create temp"; exit 1)

# setup I/O for pass 1
# source read from channel 2
cp $1 $TEMP/fort.2
# channel 1 compiler switches: we use defaults, so just one empty line
cat >$TEMP/fort.1 <<!

!

# run pass 1
cd $TEMP
plm81
cd $DDIR

# get listing from channel 12
cp $TEMP/fort.12 $DIR/${FN}prn

# prepare for pass 2
cd $TEMP
if [ -f fort.16 -a -f fort.17 ]; then
    # setup I/O for pass 2
    # get output files from pass 1 into place
    mv fort.16 fort.4
    mv fort.17 fort.7
    # channel 1 compiler switches: we use defaults, so just one empty line
    cat >fort.1 <<!

!

    # run pass 2
    plm82
    cd $DDIR

    # get hex output from channel 17
    if [ -f $TEMP/fort.17 ]; then
	cp $TEMP/fort.17 $DIR/${FN}hex
    else
	echo "too many errors in pass 1"
	RET=2
    fi
fi

# clean up
rm -rf $TEMP
exit $RET
