#!/bin/sh
#
# Occam For All project KROC (Kent Retargetable Occam Compiler) Driver program
#
# (C) Copyright 1995,1996 Dave Beckett <D.J.Beckett@ukc.ac.uk>
# University of Kent at Canterbury, England
#
# This file has been modified to work with the single processor scheduler
# release of the Aberdeen port of KRoC version 0.7beta for the Parytec
# PowerXplorer.

progname=`echo $0 | sed 's/^.*\///'`

KROC=/home2/kroc/distdir/kroc-ppc

package_version=0.7beta
host_canonical=sparc-sun-sunos4.1.3_U1
octranopts=""
ccopts=""
ocllibs="-loccam8 -loccamutl -lm -lf77"

fatal=
occopts=

occ21=${OCC21:-occ21}
octran=${OCTRAN:-octran}
ocrtsdir=${OCRTSDIR:-$KROC/lib}
ocrts=${OCRTS:-$ocrtsdir/libkroc.a}

cc=${CC:-gcc-ppx}
as=${AS:-gnu-as}
asswitches=-m601
ocspath="$ocrtsdir ./"
if [ "X$OCSEARCH" != "X" ]; then
  ocspath="$OCSEARCH $ocspath"
fi

oclibpath=
ocsearch=
for dir in $ocspath; do
  dir=`echo $dir | sed -e 's:/$::'`
  oclibpath="$oclibpath -L$dir"
  ocsearch="$ocsearch $dir/"
done

srcs=
verbose=
link=yes
slink=
outfile=
dorun=yes

# How kroc Uses Temporary Files
# =============================
# 1) Before the compiling starts, all old temporary files are always
#    deleted.
# 
# 2) Compilation is done
# 
# 3) If compiling
#    a) Fails    : the temporary files up to then are preserved
#    b) Succeeds : all the temporary files are deleted as they are used
# 
# You can explicitly ask for the temporary files to be saved with
# 'kroc --keep_temp' or 'kroc -k' but note, stage 1) is always done.
delete_temp=yes
mpplib=""
while :
do
	case $# in
	0)
		break
		;;
	esac

	option=$1
	shift

	orig_option=$option     # Save original for error messages

# Split out the argument for options that take them
	case $option in
	--*=*)
		optarg=`echo $option | sed -e 's/^[^=]*=//'`
		;;
# These options have mandatory values.  Since we didn't find an = sign,
# the value must be in the next argument
	--octran_opts | --oc_opts | --cc_opts)
		optarg=$1
		shift
		;;
	--kroc_opts)
		optarg=$1
		shift
		;;
	esac


	case "$option" in
	-c)     link=;
		octranopts="$octranopts +l"
		islib=1;
		;;
	-n | --dryrun)  dorun=;
		verbose=yes
		;;
	-o)     outfile=$1;
		shift
		;;
	-o*)	outfile=`echo $option | sed -e 's/^..//'`
		;;
	--octran_opts*) octranopts="$octranopts $optarg"
		;;
	--kroc_opts*) octranopts="$octranopts $optarg"
		;;
	--oc_opts*) occopts="$occopts $optarg"
		;;
	--cc_opts*) ccopts="$ccopts $optarg"
		;;
	-l*)	ocllibs="$ocllibs $option"
		;;
	-L*)	oclibpath="$oclibpath $option"
		;;
	-v | --verbose | --v | --verb*)
		verbose=yes
		;;
	-k | --keep_temp)
		delete_temp=
		;;
	--version)
		echo "$package_version" 1>&2
		exit 1
		;;
	-h | --help | --he*)
		fatal=yes
		;;
	--* | -*)
		echo "$progname: Unrecognized option: \"$orig_option\"; use --help for usage." 1>&2
		exit 1
		;;
	*)      srcs="$srcs $option"
		;;

	esac
done

if [ -n "${fatal}" -o "X$srcs" = "X" ] ; then
	exec 1>&2
echo "KROC version $package_version configured for $host_canonical"
echo "Usage: $progname [options] [occam sources] -llibs -Ldir -oname"
echo "Options:"
echo "  -c                                      Separately compile (no link)"
echo "  -lname                                  Link in C library libname.a"
echo "  -Ldirectory                             Search this dir for C libraries"
echo "  -oname, -o name                         Output to this executable / object"
echo "  -n --dryrun                             Just show commands to run"
echo "     --octran_opts=option,"
echo "     --octran_opts options                Options passed to 'octran"
echo "     --oc_opts=option, --oc_opts options  Options passed to 'oc'"
echo "     --cc_opts=option, --cc_opts options  Options passed to C compiler"
echo "  -v --verbose                            Show commands being run"
echo "  -k --keep_temp                          Keep temporary files"
echo "     --version                            Print version"
echo "  -h --help                               Print this message and exit"
	exit 1
fi

objs=
myobjs=
efile=
for src in $srcs; do
  origsrc=$src
  suffix=`echo $src | sed -e 's/^.*\.\(.*\)$/\1/'`
  if [ $suffix = $src ]; then
    if [ -r "$src.occ" ]; then
      echo "$progname: Assuming you meant to compile $src.occ" 1>&2
      suffix=occ
    else
      echo "$progname: Ignoring $src (no $src.occ found)" 1>&2
      continue
    fi
  fi

  dir=`echo $src | sed -e 's%[^/][^/]*$%%'`
  if [ "X$dir" = "X$src" ]; then
    dir=
  else
    src=`echo $src | sed -e 's%^.*/%%'`
    base=`echo $src | sed -e 's/\.[^\.]*$//'`
  fi

  base=`echo $src | sed -e 's/\.[^\.]*$//'`

  # Pick executable name, either from -o exe if given or basename of exe
  #if [ $link ] ; then
    if [ "X$efile" = "X" ]; then
      if [ "X$outfile" = "X" ]; then
	efile=$base
      else
	efile=$outfile
	outfile=
      fi
    fi
  #fi

  if [ $suffix != "occ" ]; then
    objs="$objs $origsrc"
    continue
  fi

  src="$dir$base.occ"

  if [ ! -r $src ]; then
    echo "$progname: Can't find occam source file '$src'" 1>&2
    exit 1
  fi

  if [ $link ]; then
    if [ $efile = $src ]; then
      efile="$efile.out";
      echo "$progname: Compiling '$src' to '$efile'" 1>&2
    fi
  fi

  if [ $delete_temp ]; then
    trap "rm -f $tfile $tcofile $sfile $ofile" 1 2 3 9 15
  fi

  tfile="$base.t"
  tcofile="$base.tco"
  sfile="$base.s"
  if [ $outfile ]; then
    ofile=$outfile
  else
    ofile="$base.o"
  fi

  ISEARCH=$ocsearch; export ISEARCH
  if [ $verbose ]; then
    echo "$progname: Search path for libraries/#USEs and #INCLUDEs:" 1>&2
    echo "  $ocsearch" 1>&2
  fi

  if [ $dorun ]; then
    rm -f $tfile $tcofile
  fi

  cmd="$occ21 -w -t8 -y -nwu -nwp -zasm -znd $occopts $src -o $tfile"
  if [ $verbose ]; then
    echo $cmd 1>&2
  fi
  if [ $dorun ]; then
    $cmd
    if [ $? -ne 0 ]; then
      echo "$progname: $occ21 failed to compile $src to $tfile" 1>&2
      exit 1;
    fi
    if [ $link ]; then
      if [ $delete_temp ]; then
        rm -f $tcofile
      fi
    fi
  fi


  if [ $dorun ]; then
    rm -f $sfile
  fi

  cmd="$octran $octranopts $mpplib $tfile $sfile"
  if [ $verbose ]; then
    echo $cmd 1>&2
  fi
  if [ $dorun ]; then
    $cmd
    result=$?
    if [ $result -ne 0 ]; then
      echo "$progname: $octran failed to translate $tfile to $sfile  error $result" 1>&2
      exit 1;
    fi
  fi


  if [ $dorun ]; then
    rm -f $ofile
  fi

  cmd="$as $asswitches -o $ofile $sfile"
  if [ $verbose ]; then
    echo $cmd 1>&2
  fi
  if [ $dorun ]; then
    $cmd
    if [ $? -ne 0 ]; then
      echo "$progname: $as failed to assemble $sfile to $ofile" 1>&2
      exit 1;
    fi
  fi
  objs="$objs $ofile"
  myobjs="$myobjs $ofile"

  if [ $dorun ]; then
    if [ $delete_temp ]; then
      rm -f $tfile $sfile
    fi
  fi

done
if [ $slink ] ; then link=yes
fi

if [  $link ]; then
  if [ "X$efile" = "X" ]; then
    echo "$progname: No output executable file name found (and no -oname)" 1>&2
    exit 1
  fi


  if [ $dorun ]; then
    rm -f $efile
  fi

    cmd="$cc $objs $ocrts $oclibpath $ocllibs $ccopts -o $efile"
    if [ $verbose ]; then
      echo $cmd  1>&2
    fi
    if [ $dorun ]; then
      $cmd
      if [ $? -ne 0 ]; then
        echo "$progname: $cc failed to link $efile" 1>&2
        exit 1;
      fi
    fi

  if [ $delete_temp ]; then
    if [ "X$myobjs" != "X" ]; then
      rm -f $myobjs
    fi
  fi
fi

exit 0
