#!/bin/sh

ECH=0
DEBUG=0
TDM=0
TDMINCL=""
TDMLIBS=""
MESAPATH=""
MESADEFAULT=1
XLIBPATH="/usr/X11R6/lib"
TR=0
TRINCL=""
TRLIBS=""
DLLIBS=""
VRML97COMP=0
IRISGL=0

if [ $ECH = "1" ] ; then echo; echo Processing options; echo Options are `echo $* | sed 's/--/:/g'` ; fi

IFS=:
for i in `echo $* --fin | sed 's/--/:/g'`
do 
    if [ "$i" != "" ] ; then 
    if [ $ECH = "1" ] ; then echo; echo Processing option $i ; fi

    case $i in
    "fin")
	;;

    "debug ")
	if [ $ECH = "1" ] ; then echo Debug option detected ; fi
	DEBUG=1;
	;;

    TDMLIBS*)
	if [ $ECH = "1" ] ; then echo TDM library path detected ; fi
	TDM=1
	TDMLIBS=`echo $i | cut -f2 -d"=" | awk '{print substr($0, 1, length-1)}'`
	if [ $ECH = "1" ] ; then echo TDM library path set to "["$TDMLIBS"]" ; fi
	;;

    TDMINCL*)
	if [ $ECH = "1" ] ; then echo TDM include path detected ; fi
	TDM=1
	TDMINCL=`echo $i | cut -f2 -d"=" | awk '{print substr($0, 1, length-1)}'`
	if [ $ECH = "1" ] ; then echo TDM include path set to "["$TDMINCL"]" ; fi
	;;

    TRLIBS*)
	if [ $ECH = "1" ] ; then echo TR library path detected ; fi
	TR=1
	TRLIBS=`echo $i | cut -f2 -d"=" | awk '{print substr($0, 1, length-1)}'`
	if [ $ECH = "1" ] ; then echo TR library path set to "["$TRLIBS"]" ; fi
	;;

    TRINCL*)
	if [ $ECH = "1" ] ; then echo TR include path detected ; fi
	TR=1
	TRINCL=`echo $i | cut -f2 -d"=" | awk '{print substr($0, 1, length-1)}'`
	if [ $ECH = "1" ] ; then echo TR include path set to "["$TRINCL"]" ; fi
	;;

    MESAPATH*)
	if [ $ECH = "1" ] ; then echo Mesa path detected ; fi
	MESADEFAULT=0
	MESAPATH=`echo $i | cut -f2 -d"=" | awk '{print substr($0, 1, length-1)}'`
	if [ $ECH = "1" ] ; then echo Mesa path set to "["$MESAPATH"]" ; fi
	echo Ensure location of Mesa library is present in your LD_LIBRARY_PATH
	;;

    XLIBPATH*)
	if [ $ECH = "1" ] ; then echo X library path detected ; fi
	XLIBPATH=`echo $i | cut -f2 -d"=" | awk '{print substr($0, 1, length-1)}'`
	if [ $ECH = "1" ] ; then echo X library path set to "["$XLIBPATH"]" ; fi
	;;

    "VRML97 ")
	if [ $ECH = "1" ] ; then echo VRML97 option detected ; fi
	VRML97COMP=1
	;;

    "IrisGL ")
	if [ $ECH = "1" ] ; then echo IrisGL option detected ; fi
	IRISGL=1
	;;

    "help ")
	echo "Usage $0 [options]"
	echo "Where options are:"
	echo "  --debug"
	echo "  --TDMINCL=<include command> (e.g. -I/usr/local/TDM-1.3/incl)"
	echo "  --TDMLIBS=<link command> (e.g. -L/usr/local/TDM-1.3/lib -ltdm)"
	echo "  --MESAPATH=<path> (e.g. /usr/local/Mesa)"
	echo "  --XLIBPATH=<path> (e.g. /usr/X11R6/lib)"
	echo "  --TRINCL=<include command> (e.g. -I/home/people/cookj/tr-1.1)"
	echo "  --TRLIBS=<link command> (e.g. -L/home/people/cookj/tr-1.1/lib/Irix6 -ltr)"
	echo "  --VRML97 (requires a C++ compiler, flex and bison)"
	echo "  --IrisGL"
	exit 1
	;;

    *)
	echo Unknown option $i
	echo Try $0 --help
	exit 1
	;;
    esac
    fi
done


if [ $ECH = "1" ] ; then echo ; echo Determining machine, ABI etc... ; fi


case `uname -s` in
IRIX|IRIX64)
    if [ $ECH = "1" ] ; then echo An SGI box ... ; fi
    PRO=`hinv | grep CPU | awk '{print $3}'`
    if [ $ECH = "1" ] ; then echo With a $PRO processor ... ; fi

    case `uname -r` in
    6*)
	if [ $ECH = "1" ] ; then echo Running Irix 6.x ... ; fi
	OS="-DMAV_IRIX6"
	if [ "`uname -s`" = "IRIX" ] ; then	
	    if [ $ECH = "1" ] ; then echo Using n32 ABI ... ; fi
	    ABI="-n32"
	else
	    if [ $ECH = "1" ] ; then echo Using n32 ABI ... ; fi
	    ABI="-n32" 
	fi
	if [ $PRO = "R10000" ] ; then MIPS="-mips4 -r10000" ; fi
	CXXFLAG=`echo -woff 1681,1682,1110`
	;;

    5*)
	if [ $ECH = "1" ] ; then echo Running Irix 5.x ... ; fi
	OS="-DMAV_IRIX5"
	CXXFLAG=`echo -woff 3262,3203`
       	;;
    *)
       	echo Dont know how to configure Irix `uname -r`, exiting
	exit 1
       	;;
    esac

    CC="cc"
    if [ "$DEBUG" = "1" ] ; then 
	OT="-g"
	CXXFLAG=`echo -g ${CXXFLAG}`
    else
	OT="-O2"
    fi
    CFLAG=`echo $OS $ABI $MIPS $OT -woff 1048`
    LD=`echo ld $ABI -shared`
    LEX="flex"
    YACC="bison -y"
    CXX="CC"
    CXXFLAG=`echo $OS $ABI $MIPS ${CXXFLAG}`
    OPENGLLIBS="-lGL -lGLU"
    ;;

Linux|linux-gnu)
    if [ $ECH = "1" ] ; then echo A Linux box...; fi

    CC="gcc"
    DLLIBS="-ldl"
    if [ "$DEBUG" = "1" ] ; then 
	OT="-Wall -ansi -g"
    else
	OT="-Wall -ansi -O2 -finline-functions -fomit-frame-pointer -funroll-loops -ffast-math"
    fi
    OS="-DMAV_LINUX"
    CFLAG=`echo $OS $OT`
    LD="ld -shared"
    LEX="flex"
    YACC="bison -y"
    CXX="g++"
    CXXFLAG=`echo $OS`
    if [ "$MESADEFAULT" = "1" ] ; then
	MESAPATH="/usr"
    fi
    OPENGLINCL="-I${MESAPATH}/include"
    OPENGLLIBS="-L${MESAPATH}/lib -lMesaGL -lMesaGLU -L${XLIBPATH} -lX11 -lXext"

    if [ $ECH = "1" ] ; then echo Looking for OpenGL include file in ${MESAPATH}/include/GL/gl.h ; fi

    if [ ! -e ${MESAPATH}/include/GL/gl.h ] ; then
	echo Cant find OpenGL include file [${MESAPATH}/include/GL/gl.h]
	exit 1
    fi
    ;;

FreeBSD)
    if [ $ECH = "1" ] ; then echo A FreeBSD box...; fi

    CC="gcc"
    DLLIBS=""
    if [ "$DEBUG" = "1" ] ; then 
        OT="-Wall -ansi -g"
    else
        OT="-Wall -ansi -O2 -finline-functions -fomit-frame-pointer -funroll-loops -ffast-math"
    fi
    OS="-DMAV_LINUX -DMAV_FREEBSD" # The Linux defs also work for FreeBSD with a few exceptions
    CFLAG=`echo $OS $OT`
    LD="ld -shared"
    LEX="flex"
    YACC="bison -y"
    CXX="g++"
    CXXFLAG=`echo $OS`
    if [ "$MESADEFAULT" = "1" ] ; then
	MESAPATH="/usr/X11R6"
    fi
    OPENGLINCL="-I${MESAPATH}/include"
    OPENGLLIBS="-L${MESAPATH}/lib -lMesaGL -lMesaGLU -L${XLIBPATH} -lX11 -lXext"

    if [ $ECH = "1" ] ; then echo Looking for OpenGL include file in ${MESAPATH}/include/GL/gl.h ; fi

    if [ ! -e ${MESAPATH}/include/GL/gl.h ] ; then
        echo Cant find OpenGL include file [${MESAPATH}/include/GL/gl.h]
        exit 1
    fi
    ;;

SunOS)
    if [ $ECH = "1" ] ; then echo A SunOS box...; fi

    case `uname -r` in
    5*)
	if [ $ECH = "1" ] ; then echo Running SunOS 5.x ... ; fi

	CC="cc"
	if [ "$DEBUG" = "1" ] ; then
	    OT="-g"
	else
	    OT="-O"
	fi
	OS="-DMAV_SUNOS5"
	CFLAG=`echo $OS $OT`
	LD="ld -G"
	LEX="flex"
	YACC="bison -y"
	CXX="CC"
	CXXFLAG=`echo $OS $OT`
	OPENGLINCL="-I/usr/openwin/include"
	OPENGLLIBS="-L/usr/openwin/lib -lGL -lGLU -lX11 -lXext"
	;;

    *)
       	echo Dont know how to configure SunOS `uname -r`, exiting
	exit 1
	;;
    esac
    ;;

*)
    echo Dont know about `uname -s`, exiting
    exit 1
    ;;
esac


cat << EOF >Makefile
all:    plat

libs:
	cd src; make

egs:
	cd examples; make

dems:
	cd demos; make

clean:
	cd src; make clean
	cd examples; make clean
	cd demos; make clean

plat:
	make libs \\
	"CC= ${CC}" \\
	"CFLAG= ${CFLAG}" \\
	"LD= ${LD}" \\
	"DLLIBS= ${DLLIBS}" \\
	"LEX= ${LEX}" \\
	"YACC= ${YACC}" \\
	"CXX= ${CXX}" \\
	"CXXFLAG= ${CXXFLAG}" \\
	"VRML97COMP= ${VRML97COMP}" \\
EOF

if [ $TDM = "1" ] ; then
cat <<EOF >>Makefile
	"TDMINCL= -DMAV_TDM ${TDMINCL}" \\
	"TDMLIBS= ${TDMLIBS}" \\
EOF
fi

if [ $TR = "1" ] ; then
cat <<EOF >>Makefile
	"TRINCL= -DMAV_TR ${TRINCL}" \\
	"TRLIBS= ${TRLIBS}" \\
EOF
fi

if [ $IRISGL = "1" ] ; then
cat <<EOF >>Makefile
	"IRISGL= ../../lib/libmav_gfxIrisGL.so" \\
	"IRISGLLIBS= -lgl" \\
EOF
fi

cat <<EOF >>Makefile
	"OPENGLINCL= ${OPENGLINCL}" \\
	"OPENGLLIBS= ${OPENGLLIBS}"
EOF

cat <<EOF >>Makefile
	make egs \\
	MAV_HOME=\`pwd\` \\
	"CC= ${CC} ${ABI} ${MIPS} ${OT}"
	make dems \\
	MAV_HOME=\`pwd\` \\
	"CC= ${CC} ${ABI} ${MIPS} ${OT}" \\
	"OPENGLINCL= ${OPENGLINCL}" \\
	"OPENGLLIBS= ${OPENGLLIBS}"
EOF
