#!/bin/sh

#	id: @(#)tokenize.sh 1.1 97/03/27
#	purpose: 
#	copyright: Copyright 1991-1997 Sun Microsystems, Inc.  All Rights Reserved

PATH=/bin:/usr/bin:$PATH ; export PATH
myname=`basename $0`

FPATH=`dirname $0`:${FPATH:=.:$HOME/tokenizr/sparc}:${PATH}
export FPATH

PATH=${FPATH} ; export PATH

if test -f forth.Z
then
  uncompress -f forth.Z
fi

if test -f tokenize.exe.Z
then
  uncompress -f tokenize.exe.Z
fi

f1=
f2=
set_filename () {

if test "$f1" -eq ""
   then
     f1=$1
     echo f1 is $f1
   else
   if test "$f2" -eq ""
      then
        f2=$1
	echo f2 is $f2
      else
        usage
   fi
fi

}
say () {
  if test -n "$VERBOSE"
  then
     echo "$*" 2>&1 > /dev/tty
  fi
}

errorsay () {
  echo $myname: "$*" 2>&1 > /dev/tty
}

usage () {
VERBOSE=t
  say "    usage: $myname  [ -8 ] [ -s ] [ -n ] [ -D sym ] prog[.fth] [ out[.fcode] ] "
  say
  say '         -8 : use 8-bit offsets ( Used only in Fcode-version1 programs )'
  say
  say '         -s : silent mode ( Warnings etc. will not be displayed)'
  say
  say '         -n :  Output file will not have a.out(5) header.'
  say
  say '         -D symbol : Define a symbol named "symbol".  Use with "ifdef"'
  say
  say '         prog[.fth] : name of the FORTH program to be tokenized '
  say '                      .fth extension is optional.'
  say
  say '         out[.fcode]  : name of the output file,'
  say '                      default name is the same as input name and,'
  say '                      default extension is .fcode.'
  say
  say '         example:'
  say '              tokenize the program my-prog.fth and create my-prog.fcode'
  say "                 $myname my-prog"
  say "                 $myname my-prog.fth  my-prog.fcode"
  say
  say '         example:'
  say '              tokenize the program my-prog.fth and create result.fcode'
  say "                 $myname my-prog  result"
  say "                 $myname my-prog.fth  result.fcode"
  say
  exit 1
  }

if test $# -eq 0
 then
	usage
fi

OFFSET8=f
VERBOSE=t
FCODE=f
LABEL=f

while [ x"$1" != x"" ]
do
case $1 in
    	-8)
                OFFSET8=t
                shift
                ;;
    	-s)
                VERBOSE=f
                shift
                ;;
	-l)
		LABEL=t
		shift
		label=$1
		if [ $# -gt 0 ]
 		then
  		  shift
		fi
		;;
	-n)
		FCODE=t
		shift
		;;
	-D)
		shift
		DEFINE="$DEFINE define $1"
		shift
		;;
	-*)
		usage
		exit 1
		;;
	*)
		if [ "$f1" = "" ] ;
                then
                     f1=$1
		     shift
		else
                     if [ "$f2" = "" ] ;
                     then
                       f2=$1
                       shift
                     else
                        usage
                     fi
		fi
		;;
esac
done

if [ "$f1" = "" ]
then
  errorsay No input file.
  usage
fi

if [ "`dirname $f1`" = "" ]
then
    fthfile=`basename $f1 .fth`
else
    fthfile=`dirname $f1`/`basename $f1 .fth`
fi

if [ "$f2" = "" ]
then
    if [ "`dirname $f1`" = "" ]
    then
        fcodefile=`basename $f1 .fth`
    else
        fcodefile=`dirname $f1`/`basename $f1 .fth`
    fi
else
    if [ "`dirname $f1`" = "" ]
    then
        fcodefile=`basename $f2 .fcode`
    else
        fcodefile=`dirname $f2`/`basename $f2 .fcode`
    fi
fi

if [ "$LABEL" = "t" ]
then
  fcodefile=`basename $fcodefile .o`
fi

if [ $LABEL = t -a $FCODE = t ]
then
  errorsay " $myname :  only one of -l and -n is allowed."
  usage
fi


if [ $LABEL = t -a "$label" = "" ]
then
  label=`basename $fthfile | tr '[A-Z- .]' '[a-z___]'`
fi

fthcmd="$DEFINE tokenize "

if [ $VERBOSE = f ]
then
  fthcmd=" silent on "$fthcmd
fi

if [ $OFFSET8 = t ]
then
  fthcmd=" offset-8? on "$fthcmd
else
  fthcmd=" offset-8? off "$fthcmd
fi

if [ $LABEL = t ]
then
  fthcmd="  append-label? on "$fthcmd
  fcodefile=$fcodefile.o
else
  fcodefile=$fcodefile.fcode
fi

fthfile=$fthfile.fth

if [ $FCODE = t ]
then
  fthcmd="  aout-header? off "$fthcmd
else
  fthcmd="  aout-header? on "$fthcmd
fi

forth tokenize.exe  -s " $fthcmd $fthfile $fcodefile $label "

if [ $VERBOSE = t -a -f $fcodefile ]
then
   echo
   echo $myname: Output is in  $fcodefile
   echo
fi