#!/bin/sh
#-------------------------------------------------
# MathLink template compiler
#-------------------------------------------------

if [ -z "$CC" -o "$CC" = "`basename "$0"`" ] ; then
    CC=cc
fi

spath=`expr "$0"'/' : '\(/\)[^/]*//*$' \| "$0"'/' : \
            '\(.*[^/]\)//*[^/][^/]*//*$' \| .`

if [ "." = "$spath" ]; then
   MLPATH=`pwd`
else
   if [ ".`(echo $spath | grep \"^/\")`" = ".$spath" ]; then
      MLPATH="$spath"
   else
      MLPATH="`pwd`/$spath"
   fi
fi

sysid=HPUX-PA64

CheckMacCompileArch() {
if [ -f /tmp/mcctest ] ; then
	rm -rf /tmp/mcctest /tmp/mcctest.c;
fi
var="#include <stdio.h>\n\nint main()\n{\n\tprintf(\"Hello World\");\n\treturn 0;\n}\n"
echo -e ${var} > /tmp/mcctest.c
`${CC} -arch $1 -o /tmp/mcctest /tmp/mcctest.c > /dev/null 2>&1`
rm -rf /tmp/mcctest.c
if [ -f /tmp/mcctest ] ; then
	rm -rf /tmp/mcctest;
	extraarchs="${extraarchs} -arch $1"
fi
}

determine_mac_status() {
arch=`arch`
case ".${arch}" in
	.ppc)
		sysid=MacOSX
		;;
	.i386)
		sctl=`sysctl -A 2> /dev/null | grep x86_64 | sed -e "s/hw\.optional\.x86_64\: //"`
		case ".${sctl}" in
			.1)
				sysid=MacOSX-x86-64
				;;
			.*)
				sysid=MacOSX-x86
				;;
		esac
		;;
esac

ccversionstring=`${CC} --version`
ccversion=`echo ${ccversionstring} | sed -E -e "s/.*([0-9]\.[0-9]).*$/\1/"`

case ".${ccversion}" in
	.3.3)
		mllibrary=${topdir}/AlternativeLibraries
		;;
esac

CheckMacCompileArch ppc
CheckMacCompileArch ppc64
CheckMacCompileArch i386
CheckMacCompileArch x86_64
}

topdir=`\cd "$MLPATH"; pwd`

mlbinary=$topdir
mlinclude=$topdir
mllibrary=$topdir
mloutfile=""


case ".${sysid}" in
	.MacOSX|.MacOSX-x86|.MacOSX-x86-64)
		determine_mac_status
		;;
esac

extracc="+DD64 +DA2.0W"

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

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

        .-c)       # save dot o files
            passtocc="$passtocc $1"
            savedotos=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
            ;;

        .-xo)
            if [ ".$2" = "." ] ; then
                echo "$myname: -xo option but no filename"
                exit 1
            else
		[ ! -d "$2" ] && mkdir $2 > /dev/null 2>&1
		[ ! -d "$2/$sysid" ] && mkdir "$2/$sysid" > /dev/null 2>&1
                outfile="-o $2/$sysid/$2";
                if [ ! -d $2 ] ; then
                	echo "Error creating the directory \"$2\" because the file \"$2\" already exists."
                	exit 1
                fi
                cnt=`expr $cnt - 1`
                shift
            fi
            ;;

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

        .*.tm)      # make a list
            if [ ".$mloutfile" = "." ] ; then
                mloutfile=$1
            fi
            mlfiles="$mlfiles $1"
            ;;

        .*)        # pass through to cc
            passtocc="$passtocc $1"
            ;;
    esac

    cnt=`expr $cnt - 1`
    shift

done

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

   $CC $outfile -I"$mlinclude" $passtocc $extracc -L"$mllibrary" -lML64i3 -L/usr/lib/pa20_64 /usr/lib/pa20_64/libdld.sl -lm /usr/lib/pa20_64/libm.a -lpthread -lrt

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