#!/bin/sh

SCRIPTDIR="`dirname $0`"
TMPDIR="/tmp/spstmp.$$"
DECK="$TMPDIR/deck"
STARTUP="$TMPDIR/para.tmp"
RUN_STARTUP="$TMPDIR/run.tmp"
I1401="i1401-2.6"
#I1401=i1401

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

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

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

mkdir $TMPDIR

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

cat "$SCRIPTDIR/sps1.obj" $source "$SCRIPTDIR/sps2.obj" $source >$DECK

cat > $STARTUP << EOF
at cdr $DECK
at cdp $source_stem.obj
at lpt $source_stem.lst
b cdr
g
g
EOF

"$I1401" $STARTUP
if [ "$run_prog" ]
then
  cat > $RUN_STARTUP << EOF
at lpt print.txt
at cdr $source_stem.obj
b cdr
EOF
"$I1401" $RUN_STARTUP
fi


rm -f $DECK $STARTUP $RUN_STARTUP
rmdir $TMPDIR
