#!/bin/sh -f

# $Revision: 1.6 $
# $Name: OpenRM-1-4-0-FCS $
# $Log: configure,v $
# Revision 1.6  2001/06/04 01:53:07  wes
# Added warning msg concerning deficiency wrt JPEG.
#
# Revision 1.5  2001/06/03 20:16:41  wes
# Better support for IRIX ABIs, default is -64. Added -jpeg=/dir/to/jpeg-stuff
# command line flag.
#
# Revision 1.4  2000/12/03 23:30:55  wes
# Added -lposix4 to default X11 libs on Solaris.
#
# Revision 1.3  2000/11/17 03:20:48  wes
# Added -lpthread to the default list of X11 libraries. Starting with
# OpenRM 1.3.1, this library is needed for semaphores and thread ops.
#
# Revision 1.2  2000/08/31 02:15:59  wes
# Linux platforms: it is now assumed that OpenGL includes are located
# in /usr/include (like SGIs), and libraries in /usr/lib (also like SGIs).
#
# Revision 1.1.1.1  2000/02/28 21:55:30  wes
# OpenRM 1.2 Release
#
# Revision 1.3  2000/02/28 17:21:54  wes
# RM 1.2, pre-OpenRM
#
# Revision 1.2  1999/12/09 16:32:44  wes
# Architecture specific flags added to support the various IRIX species.
#
# Revision 1.1  1999/11/21 17:35:49  wes
# Initial entry.
#

#
# usage: configure [-opengl=/opengl/home/dir] [-rm=/rm/home/dir] [-x11=/x11/home/dir] [-opt=flag (eg: -g, -O2, etc.)] [-irixarch=flag (eg, 32, n32 or 64)] [-jpeg=/dir/to/jpeg-stuff]
#

#
# the purpose of this script is to generate a file included by
# a Makefile in the rmdemo distribution. the included file contains
# definitions that locate several components needed to build
# the demos. these components include:
#
# 1. RM Scene Graph: the location of the RM Scene Graph distribution
#    (includes & libs), compile-time flags
# 2. OpenGL: where does OpenGL live? 
# 3. X11: Where is X11?
# 4. Compiler information: optimization and arch specific flags
# 5. Location of libjpeg and JPEG header files on your system. We assume that
#    if you have to specify a location for the JPEG hdrs and libs, that 
#    you've downloaded the jpeg-6b src distribution, and that the hdrs
#    and libs live in the same directory.
#

#
# names of the files modified by this script:
#
MakeIncFName="makeinclude"
MakeIncFNameBackup="makeinclude.bak"


RMHOME=""
OPENGLHOME=""
X11HOME=""
JPEGHOME=""

#
# internally-used variables
#
OSversion=""

#
# RM stuff
#
# the following are the default values used for RM Scene Graph.
# stuff prefixed with "const" is what the developer should change
# or add to if different libararies are needed.
#
constRMLibs="-lrm -lrmaux -lrmv -lrmi"
constRMCFlags="-DRM_X"

# the default values that will be written to the makeinclude
# file unless overridden by command line args to this script
RMHome=""
RMLibs=""
RMCFlags=""

#
# OpenGL stuff
#
#
# the following are the default values used for OpenGL
# stuff prefixed with "const" is what the developer should change
# or add to if different libararies are needed.
#
constOpenGLLibs="-lGL -lGLU"
constOpenGLCFlags=""

# the default values that will be written to the makeinclude
# file unless overridden by command line args to this script
OpenGLHome=""
OpenGLLibs=""
OpenGLCFlags=""

#
# X Stuff
#
constX11Libs="-lX11 -lpthread -lm"
constX11CFlags=""
X11Home=""
X11Include=""
X11Libs=""

constX11LinuxLibs="-lXmu"

#
# Compile flags
#
# default optimization flag is -g. this default value may be 
# overridden by a command line argument.

constOptFlag="-g"
compileOptFlag=""

constArchFlag=""
archFlags=""

#
# JPEG stuff
#
constJPEGCFlags=""
constJPEGLibs="-ljpeg"
JPEGLibs=""
JPEGInc=""

#
# support subroutines
#

print_error()
{
    echo " $*" 2>&1;
    echo usage: $0 [-opengl=/opengl/home/dir] [-rm=/rm/home/dir] [-x11=/x11/home/dir] [-opt=flag \(eg: -g, -O2, etc.\)] [-irixarch=flag \(one of: 32, n32 or 64, n32 is default if on an IRIX box\)] [-jpeg=/dir/to/jpeg-stuff]
}

doPreamble()
{
    echo "# " >> $MakeIncFName
    echo "# autogenerated by configure" >> $MakeIncFName
    echo "# on `date`" >> $MakeIncFName
    echo "# " >> $MakeIncFName
}

doOpenGL()
{
# 
# input parms:
# $1 - $OPENGLHOME
# $2 - $OSversion
# $3 - $archFlags

# we're assuming we won't have -n32 or -64 unless the OS is IRIX

    case $3 in
    -n32 )
       oglLibDir=`echo $1`/lib32
       ;;

    -64 )
       oglLibDir=`echo $1`/lib64
       ;;

    *)
       oglLibDir=`echo $1`/lib
       ;;

    esac

    echo configuring for OpenGL..
    OpenGLHome=`echo $1`
    OpenGLLibs=`echo -L$oglLibDir $constOpenGLLibs`
    OpenGLCFlags=`echo $constOpenGLCFlags -I$1/include`
}

doRM()
{
    echo configuring for RM..
    RMHome=`echo $1`
    RMLibs=`echo -L$1/lib $constRMLibs`
    RMCFlags=`echo $constRMCFlags -I$1/include`
}

doX11()
{

# 
# input parms:
# $1 - $OPENGLHOME
# $2 - $OSversion
# $3 - $archFlags
#
# we're assuming we won't have -n32 or -64 unless the OS is IRIX
#

    case $3 in
    -n32 )
       x11LibDir=`echo $1`/lib32
       ;;

    -64 )
       x11LibDir=`echo $1`/lib64
       ;;

    *)
       x11LibDir=`echo $1`/lib
       ;;

    esac

#
# do arch-specific processing.
#
    case $2 in

    Linux )
      archX11Libs="-lXmu"
      ;;

    SunOS )
      archX11Libs="-lposix4"
      ;;

    *)
      archX11Libs=""
      ;;

    esac

    echo configuring for X11..
    X11Home=`echo $1`
    X11Libs=`echo -L$x11LibDir $archX11Libs $constX11Libs`
    X11CFlags=`echo $constX11CFlags -I$1/include`
}

doX11Defaults ()
{
    case $1 in

    Linux )
    X11HOME=/usr/X11R6
    ;;

    SunOS )
    X11HOME=/usr/openwin
    ;;

    IRIX* )
    X11HOME=/usr
    ;;

    *)
      ;;

    esac
}

doOpenGLDefaults ()
{
    case $1 in

    Linux )
    OPENGLHOME=/usr
    ;;

    SunOS )
    OPENGLHOME=/usr/openwin
    ;;

    IRIX* )
    OPENGLHOME=/usr
    ;;

    *)
      ;;

    esac
}

doRMDefaults ()
{
    case $1 in

    Linux )
    RMHOME=/usr/local/rm140
    ;;

    SunOS )
    RMHOME=/usr/local/rm140
    ;;

    IRIX* )
    RMHOME=/usr/local/rm140
    ;;

    *)
      ;;

    esac
}

doArchDefaults ()
{
    case $1 in

    Linux )
    archFlags="-DLINUX"
    ;;

    SunOS )
    archFlags="-DSOLARIS"
    ;;

# most IRIXs are 64 bit; make this the default for RMdemo

    IRIX64 )
    archFlags="-64"
    ;;

    *)
      ;;

    esac
}

doArchFlags ()
{
    archFlags=$1
}

doCompileOpt ()
{
    compileOptFlag=$1
}

doJPEGDefaults ()
{
    case $1 in

    Linux )
    JPEGLibs="-ljpeg"
    JPEGInc="" 
    ;;

    IRIX* )
    JPEGLibs="-ljpeg"
    JPEGInc=""
    ;;

    *)
      ;;

    esac
} 

doJPEG ()
{
    JPEGLibs=`echo -L$JPEGHOME $constJPEGLibs`
    JPEGInc=`echo -I$JPEGHOME`
    echo "Warning: you will probably have to edit the makeinclude file by hand in order to resolve deficiencies in this script wrt JPEG hdrs and libs. Sorry."
}


#############################
#
# main
#
#############################

# set the OSversion
OSversion=`uname -s`

#
# assign default values for OPENGLHOME and X11HOME depending
# upon the OSversion. later, we may override these values if
# the user specified something on the command line

doX11Defaults $OSversion
doOpenGLDefaults $OSversion
doRMDefaults $OSversion
doCompileOpt $constOptFlag
doArchDefaults $OSversion
doJPEGDefaults $OSversion

# process the command line args

for arg do
    case $arg in

    -opengl=* )
# strip off any -foo= stuff before the real args
    OPENGLHOME=`echo $arg | sed 's/[-a-z]*=//'` 
    ;;

    -rm=* )
# strip off any -foo= stuff before the real args
    RMHOME=`echo $arg | sed 's/[-a-z]*=//'` 
    ;;

    -x11=* )
# strip off any -foo= stuff before the real args
    X11HOME=`echo $arg | sed 's/[-a-z,0-9]*=//'` 
    ;;

    -opt=* )
# strip off any -foo= stuff before the real args
    USEROPT=`echo $arg | sed 's/[-a-z,0-9]*=//'` 
    ;;

    -irixarch=* )
# strip off any -foo= stuff before the real args
    IRIXARCH=`echo $arg | sed 's/[-a-z,0-9]*=//'` 
    ;;

    -jpeg=* )
# strip off any -foo= stuff before the real args
    JPEGHOME=`echo $arg | sed 's/[-a-z,0-9]*=//'` 
    ;;

    *)
    print_error "unrecognized config option $arg"
    exit 1
    ;;

    esac

done

#
# check to see if the incfile exists, move it to .bak if so
#
if test -r $MakeIncFName ; then
    cp $MakeIncFName $MakeIncFNameBackup
    rm $MakeIncFName
fi

touch  $MakeIncFName


#
# each of the following routines is invoked to set 
# flags and settings stored in a file included into a Makefile.
#
if test -n "`echo $IRIXARCH`" ; then
    doArchFlags $IRIXARCH
fi

if test -n "`echo $OPENGLHOME`" ; then
    doOpenGL $OPENGLHOME $OSversion $archFlags
fi

if test -n "`echo $RMHOME`" ; then
    doRM $RMHOME
fi

if test -n "`echo $X11HOME`" ; then
    doX11 $X11HOME $OSversion $archFlags
fi

if test -n "`echo $USEROPT`" ; then
    doCompileOpt $USEROPT
fi

if test -n "`echo $JPEGHOME`" ; then
    doJPEG $JPEGHOME
fi


#
# now dump the variables into the makeinclude file
#

doPreamble

echo RMHOME=$RMHome >> $MakeIncFName
echo RMLIBS=$RMLibs >> $MakeIncFName
echo RMCFLAGS=$RMCFlags >> $MakeIncFName
echo "#  " >> $MakeIncFName

echo OPENGLHOME=$OpenGLHome >> $MakeIncFName
echo OPENGLLIBS=$OpenGLLibs >> $MakeIncFName
echo OPENGLCFLAGS=$OpenGLCFlags >> $MakeIncFName
echo "#  " >> $MakeIncFName

echo X11HOME=$X11Home >> $MakeIncFName
echo X11LIBS=$X11Libs >> $MakeIncFName
echo X11CFLAGS=$X11CFlags >> $MakeIncFName
echo "#  " >> $MakeIncFName

echo OPTFLAGS=$compileOptFlag >> $MakeIncFName
echo ARCHFLAGS=$archFlags >> $MakeIncFName
echo "#  " >> $MakeIncFName

echo "# Location of JPEG stuff on your machine " >> $MakeIncFName
echo JPEGLIB=$JPEGLibs >> $MakeIncFName
echo JPEGINC=$JPEGInc >> $MakeIncFName

echo "all done, created a new $MakeIncFName"

#############################
#
# end main
#
#############################
