#!/bin/sh
PATH=.:$PATH

# Edit these to match your configuration
TAPE=.
DASD=../dasd

#
# Run CTSS Extract Job.
#
usage()
{
   echo "usage: extractctss commandfile [outputfile]"
   echo "       extractctss mode name1 name2 projname progname [outputfile]"
   echo "  mode: "
   echo "    B  - BSS file"
   echo "    L  - Listing file"
   echo "    P  - Plotter file"
   echo "    T  - Text file"
   echo "  example: "
   echo "    extractctss L hello bcd m1416 guest hello.lst"
   exit 1
}

if [ $# -eq 0 ]; then
   usage
fi

mode=""
text="N"
plot="N"
if [ -f $1 ]; then
   cp $1 cmd.txt
   outfile=$2
   option=$3
   assign=$4
else
   if [ $# -le 2 ]; then
      echo "$1: File not found"
      exit 1
   fi
   if [ $# -lt 5 ]; then
      usage
   fi
   case "$1" in
   l | L)
      mode="-p"
      text="Y"
      ;;
   t | T)
      text="Y"
      ;;
   b | B)
      ;;
   p | P)
      plot="Y"
      ;;
   *)
      echo "\"$1\": Unsupported extract mode"
      exit 1
      ;;
   esac
   genext $1 $2 $3 $4 $5 >cmd.txt
   outfile=$6
   option=$7
   assign=$8
fi
txt2bcd cmd.txt cmd.bcd 84 84

#
# Run CTSS extract image
#
rm -f trace.bin sysout.bcd $outfile
s709 $option -C8 -mCTSS -cutilctss.cmd p=print \
	a1r=${TAPE}/extract.tap a9=trace.bin b2r=cmd.bcd b3=sysout.bcd $assign \
	cd00=${DASD}/DISK1.BIN cn01=${DASD}/DRUM1.BIN cd42=${DASD}/DISK2.BIN  \
	gh00=${DASD}/DRUM2.BIN gh01=${DASD}/DRUM3.BIN
#
# Process output tape
#
if [ -s sysout.bcd -a "$outfile" != "" ]; then
   if [ "$text" = "Y" ]; then
      bcd2txt $mode sysout.bcd $outfile
   elif [ "$plot" = "Y" ]; then
      plotcvt sysout.bcd
      cp calcomp_099.ps $outfile
      rm calcomp_099.ps
   else
      cp sysout.bcd $outfile
   fi
fi
rm -f cmd.txt cmd.bcd
