#!/bin/bash

SCRIPTDIR="`dirname $0`"
TMPDIR="/tmp"
STARTUP="$TMPDIR/para.$$.$RANDOM.tmp"
I1401="i1401"

run_prog=""
if [ x"$1" = x-r ]
then
  run_prog="true"
  shift
fi

if [ x"$2" = x ]
then
  echo "usage: "`basename $0`" [-r] <source file> [Autocoder tape]"
  exit 1
fi

source="$1"

if [ -d "$source" ]
then
  echo `basename $0`": $source: is a directory"
  exit 1
fi

if [ ! -s "$source" ]
then
  echo `basename $0`": $source: no such file (or it is empty)"
  exit 1
fi

if [ ! -r "$source" ]
then
  echo `basename $0`": $source is unreadable"
  exit 1
fi

tape="$2"
if [ x"$tape" = x ]
then
  tape="Autocoder+IOCS.tap"
fi

source_stem="`echo $source | sed 's/\..*$//g'`"

rm -f print.out

cat > $STARTUP << EOF
at mt1 $SCRIPTDIR/$tape
at mt4 scratch.mt4
at mt5 scratch.mt5
at mt6 scratch.mt6
at cdr $source
at cdp $source_stem.obj
at lpt $source_stem.lst
b mt1
EOF

if [ "$run_prog" ]
then
  cat >> $STARTUP << EOF
! rm -f print.txt
at lpt print.txt
at cdr $source_stem.obj
b cdr
! cat print.txt
EOF
fi

rm -f scratch.mt*

"$I1401" $STARTUP
rm -f $STARTUP
