#!/bin/sh

CC=${CC:-"cc"}


mlbinary=/math/V2.2/Sun/Versions/SPARC/MathLink
mlinclude=/math/V2.2/Common/Source/MathLink
mllibrary=/math/V2.2/Sun/Versions/SPARC/MathLink
mloutfile=""

myname="$0"
outfile="-o a.out"

cnt=$#
while [ $cnt != 0 ]
do
    case ".$1" in
        .-E|.-P)   # preprocess only, filename
            passtocc="$passtocc $1" 
            outfile=""
            ;;

        .-o)       # output filename
            if [ ".$2" = . ] ; then
                echo "$myname: -o option but no filename"
                exit 1
            else
                outfile="-o $2";
                cnt=`expr $cnt - 1`
                shift
            fi
            ;;
        
        .-c)       # save dot o files
            passtocc="$passtocc $1"
            savedotos=1		
            outfile=""
            ;;

        .*.tm)    # mprep it and add dot c file to passtocc
            if [ ".$mloutfile" = "." ] ; then
               mloutfile=$1
            fi
            mlfiles="$mlfiles $1"
            ;;

        .-g)       # want to keep generated files after mpreping
               passtocc="$passtocc $1"
               debug=1
               savedotos=1
               ;;

        .*)        # pass through to cc
            passtocc="$passtocc $1"
            ;;
    esac
 
    cnt=`expr $cnt - 1`
    shift

done


  if [ ".$mloutfile" != "." ] ; then
      $mlbinary/mprep $mlfiles > $mloutfile.c
      retval=$?
      mlteddotofiles="$mlteddotofiles $mloutfile.o"
      passtocc="$passtocc $mloutfile.c"
   fi


echo $CC $outfile -I$mlinclude $passtocc $otherlibs -L$mllibrary -lML -lm -DSTANDALONEMLINK $LIBS
$CC $outfile -I$mlinclude $passtocc $otherlibs  -L$mllibrary -lML -lm -DSTANDALONEMLINK $LIBS
retval=$?

   if [ ".$mloutfile" != "." ] ; then
       if [ ".$debug" = "." ] ; then
         rm -f $mloutfile.c
       fi
       if [ ".$savedotos" != ".1" ] ; then
           rm -f $mlteddotofiles
       fi
   fi

exit $retval
