#!/bin/csh -f
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Side Effects Software Inc., and is not to be reproduced,
# transmitted, or disclosed in any way without written permission.
#
# Produced by:
#	Side Effects
#	477 Richmond Street West
#	Toronto, Ontario
#	Canada   M5V 3E7
#	416-504-9876
#
# COMMENTS:	This is a shell script which makes a custom DSO for Houdini.
#		There may be a lot of warnings about unresolved functions
#		at the completion of a make.  This is ok.  The DSO will still
#		work.
#

#
#  The following variables should be set to the correct locations.
#

set argv = ( `/bin/getopt gsoti:l:L:I: $*` )

if ($#argv < 2) then
    echo "Usage:  $0 [-g] [-s] [-o]"
    echo "    [-L libdir] [-I incdir] [-l lname] [-i install_dir] source_files"
    echo
    echo "This program will make custom DSO's for Houdini."
    echo 'Options:'
    echo '  -g      Create debug version of the DSO'
    echo '  -i dir  Specify an install directory (default $HOME/houdini6.5/dso)'
    echo '  -s      Create a stand alone application instead of a DSO'
    echo '  -o      Compile and link with the -32 and -mips2 option.'
    echo '  -l      Link with the library named liblname.a.  libraries are passed'
    echo '          to the compiler in the order in which they are given' 
    echo '  -L dir  Add libdir to the library file search path'
    echo '  -I dir  Add incdir to the include file search path'
    echo '  -t      Turn of creation of tagging information.  At least one'
    echo '          object file in an archive must have the tagging'
    echo '          information for Houdini to load the .so successfully'
    echo
    echo 'The DSO created should be somewhere in the HOUDINI_PATH so that'
    echo 'it gets picked up the next time Houdini is run.  The default location'
    echo 'is in the default path.'
    exit 0
endif

set INSTDIR	= $HOME/houdini6.5/dso
set APPINSTDIR	= "."
set OPT_FLAG	= -O2
set STANDALONE	= ""
set STD_LIBS    = ""
set LIBS	= ""
set LIBINC	= ""
set INCINC	= ""

set ABI_OPT	= ""
set IRIX	= "-DIRIX5"
set TEMPLATES	= " -pte.C" 
set OSLFLAGS	= " -U"
set WOFF	= ""
set HC_OPT	= ""
switch ( `/bin/uname -r` )
    case "6.5":
    case "6.4":
    case "6.3":
    case "6.2":
    case "6.1":
	set ABI_OPT	= " -n32 -mips3"
	set IRIX	= "-DIRIX6"
	set STD_LIBS	= "-L/lib32 -L/usr/lib32"
	set TEMPLATES	= ""
	set OSLFLAGS	= ""
	set WOFF	= "1174,1201,1209,1233,1355.1.1155"
	breaksw
endsw

foreach i ( $* )
    switch ( $i )
	case "-g":
	    set OPT_FLAG = -g
	    echo Making debug version
	    shift
	    breaksw
	case "-i"
	    shift
	    set INSTDIR = $1
	    set APPINSTDIR = $1
	    echo Install directory = "'$INSTDIR'"
	    shift
	    breaksw
	case "-s"
	    set STANDALONE = "yes"
	    shift
	    breaksw
	case "-o"
	    set ABI_OPT	 = " -32 -mips2"
	    set STD_LIBS = "-L/lib -L/usr/lib"
	    shift
	    breaksw
	case "-l"
	    shift
	    set LIBS = "$1 $LIBS"
	    echo Linking with "'lib$1'"
	    shift
	    breaksw
	case "-L"
	    shift
	    set LIBINC = "$1 $LIBINC"
	    shift
	    breaksw
	case "-I"
	    shift
	    set INCINC = "$1 $INCINC"
	    shift
	    breaksw
	case "-t"
	    shift
	    set HC_OPT = "-t"
	    breaksw
	case "--"
	    shift
	    break		# Break out of the loop
    endsw
end

# build the option strings for extra user libraries
if ( "$LIBINC" != "" ) then
    set TMP = ""
    foreach l ($LIBINC)
	set TMP = "-L$l $TMP"
    end
    set LIBINC = "$TMP"
endif

if ( "$INCINC" != "" ) then
    set TMP = ""
    foreach l ($INCINC)
	set TMP = "-I$l $TMP"
    end
    set INCINC = "$TMP"
endif

if ( "$LIBS" != "" ) then
    set TMP = ""
    foreach l ($LIBS)
	set TMP = "-l$l $TMP"
    end
    set LIBS = "$TMP"
endif


#
#  The rest of the script should be left alone
#
if ( ! -d $INSTDIR ) then
    echo Making directory $INSTDIR
    mkdir -p $INSTDIR
endif

set CC		= "/bin/CC -DMAKING_DSO"
set PRELINK	= "/usr/lib/DCC/edg_prelink"
set IFLAGS	= "-I. -nostdinc -I/usr/include/CC -I/usr/include"
set WFLAGS	= "-woff $WOFF -woff 3262"
set TFLAGS	= "-c $TEMPLATES -ptused -fullwarn"
set OPTIMIZER	= "-float -xansi $OPT_FLAG"
set CCFLAGS	= '-DVERSION=\"6.5.67\" -DOFSTREAM_PERMISSIONS=\", 0666\"'" -DExport= $ABI_OPT $IRIX -D_MODERN_C -D_SVR4_SOURCE -D_SGI_SOURCE $INCINC"

set HDSOLIB	= "$HFS/dsolib"
set LFLAGS	= "$OSLFLAGS -nostdlib $STD_LIBS $INCINC $LIBINC $LIBS"
set SHARED	= "-shared -ignore_unresolved -quickstart_info"
set HLIBS	= "-lHoudiniUI -lHoudiniOPZ -lHoudiniOP3 -lHoudiniOP2 -lHoudiniOP1 -lHoudiniPRM -lHoudiniGEO -lHoudiniUT"
set SAFLAGS	= "$ABI_OPT -L $HDSOLIB $HLIBS $LFLAGS"

setenv BASE_CFLAGS  "$CCFLAGS $IFLAGS $TFLAGS $WFLAGS $OPTIMIZER"

set STAND_NAMES = ""
set APPNAME = ""

foreach file ( $* )
    set ONAME  = ${file:r}.o
    if ( "$STANDALONE" == "" ) then
	set SONAME = ${INSTDIR}/${file:r}.so
	echo Making $ONAME and $SONAME from $file
	setenv CFLAGS "$BASE_CFLAGS -DMAKING_DSO -o $ONAME"
	hcompile $HC_OPT $file
	if ($status == 1) then
	    echo Compile failed
	    exit 1
	endif
	$PRELINK $ONAME -L $HDSOLIB
	$CC $ABI_OPT $SHARED $ONAME $LFLAGS -o $SONAME
    else
	if ( "$APPNAME" == "" ) then
	    set APPNAME = ${file:r}
	endif
	echo Making $ONAME from $file
	setenv CFLAGS "$BASE_CFLAGS -o $ONAME"
	hcompile $HC_OPT $file
	if ($status == 1) then
	    echo Compile failed
	    exit 1
	endif
	$PRELINK $ONAME
	set STAND_NAMES = "$STAND_NAMES $ONAME"
    endif
end

if ( "$STANDALONE" != "" ) then
    set APPNAME = "$APPINSTDIR/$APPNAME"
    echo Making $APPNAME from $STAND_NAMES
    $CC $STAND_NAMES -o "$APPNAME" $SAFLAGS
endif
