#!/bin/sh

# Do not use optimization for the IRIX platforms, since that
# seems to fail at the build step.
# Our Makefile's always build with $(SDSS_CFLAGS), so we can use that
# variable to set optimization.

UNAME=`uname`
echo "OS type detected: "$UNAME
case $UNAME in
	"SunOS") 
	    # Try to use Sun compiler if it exists.
	    # which doesn't set useful exit value...
	    haveF77=`which f77 | grep -q -v 'no f77' && echo yes`
	    if test $haveF77; then
	        echo "Using Sun f77 compiler (better not be g77 named f77)"
		X_FFLAGS="-e"
		X_CFLAGS="-KPIC"
	        LDF77=f77
	    else
	        echo "Using GNU g77 compiler"
		F77=g77
		X_CFLAGS="-fPIC"
	        LDF77=g77
	    fi
	    X_LD_FLAGS=-G
	    SO_EXT=so
	    SDSS_CFLAGS=-O3
	;;
	"HP-UX")
		LDF77=g77
		SO_EXT=sl
		X_CFLAGS="+z -Aa -D_HPUX_SOURCE"
	  X_FFLAGS="-extend_source"
		X_LD_FLAGS=-b
	;;
	"IRIX" )
		LDF77=g77
		SO_EXT=so
		X_CFLAGS="-32 -KPIC"
    X_FFLAGS="-32 -extend_source"
    MAKE_FTNLIB="-lftn"
		X_LD_FLAGS="-shared -32"
	;;
	"IRIX64" )
		SO_EXT=so
		LDF77=g77
		X_CFLAGS="-64 -KPIC -mips4"
		CC=cc
		LD=ld
		LDF77=ld
		X_FFLAGS="-64 -extend_source -KPIC"
		MAKE_FTNLIB="-lftn"
		X_LD_FLAGS="-shared -64 -mips4"
	;;
	"OSF1" )
		LDF77=g77
		SO_EXT=so
    X_FFLAGS="-extend_source"
		X_LD_FLAGS="-S -shared"
		SDSS_CFLAGS=-O3 
	;;
	"Darwin" )
           SO_EXT=dylib
           X_CFLAGS="-dynamic"
           if [ `uname -m` == 'i386' ]; then
                echo "Darwin/i386; requires a native (x86) IDL and gfortran"
                idlver=`echo 'print, !VERSION.OS, " ", !VERSION.arch' | idl`
                if [ "$idlver" != "darwin i386" ]; then
                    echo idlutils now requires a native IDL on x86 machines >&2
                    exit 1
                fi

	        F77=gfortran
                LDF77=gcc

		LD=gcc
                export MACOSX_DEPLOYMENT_TARGET=10.4 # This, of course, will bite us come 10.5, etc.
                                                     # It changes how undefined lookups happen.
		X_LD_FLAGS="-dynamiclib -undefined dynamic_lookup -single_module"
                SDSS_CFLAGS=-O2
           else
               echo "Darwin/ppc"
               LDF77=ld
               F77=g77
               X_LD_FLAGS="-bundle /usr/lib/bundle1.o -L/usr/lib  -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0 -lc -lgcc -flat_namespace -undefined suppress"
               if [ `uname -r | cut -c 1` \< 8 ]; then
                       X_LD_FLAGS="-bundle /usr/lib/bundle1.o -L/usr/lib -lc -lcc_dynamic -flat_namespace -undefined suppress"
               fi
               SDSS_CFLAGS=-O2
           fi
	;;
	"Linux" )
		LDF77=g77
		SO_EXT=so
		X_CFLAGS="-fPIC -g -Wall"
		X_FFLAGS="-ffixed-line-length-none"
    MAKE_FTNLIB="-lg2c"
#    MAKE_FTNLIB="-lg2c  -L/usr/lib/gcc-lib/i386-redhat-linux/`gcc -dumpversion`"
		X_LD_FLAGS=-shared
		SDSS_CFLAGS="-O3 -DSDSS_LITTLE_ENDIAN"
	;;
	*) echo "This system is not supported" >&2
		exit 1;;
esac

export SO_EXT X_CFLAGS X_FFLAGS X_LD_FLAGS SDSS_CFLAGS LD F77 MAKE_FTNLIB UNAME LDF77

make "$@"
