#!/bin/sh
#
#  Script to integrate the XRT/graph, XRT/3d, XRT/table, and/or XRT/field
#  with WorkShop Visual
#
# (c) Copyright 1994, KL GROUP INC.
# ALL RIGHTS RESERVED
#
# This file is provided for demonstration and educational uses only.
# Permission to use, copy, modify and distribute this file for
# any purpose and without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all
# copies, and that the name of KL Group not be used in advertising
# or publicity pertaining to this material without the specific,
# prior written permission of an authorized representative of
# KL Group.
#
# KL GROUP MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES, EX-
# PRESS OR IMPLIED, WITH RESPECT TO THE SOFTWARE, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR ANY PARTICULAR PURPOSE, AND THE WARRANTY AGAINST IN-
# FRINGEMENT OF PATENTS OR OTHER INTELLECTUAL PROPERTY RIGHTS.  THE
# SOFTWARE IS PROVIDED "AS IS", AND IN NO EVENT SHALL KL GROUP OR
# ANY OF ITS AFFILIATES BE LIABLE FOR ANY DAMAGES, INCLUDING ANY
# LOST PROFITS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES
# RELATING TO THE USE OF THIS SOFTWARE.
#
#  SCCSID -- @(#)Configure	1.8    95/11/27 KL Group Inc.
##################################################################

# Hello world
cat ./VERSION > /dev/tty

print_error()
{
    echo                    > /dev/tty
    echo "Error($1): $2"   > /dev/tty
    echo                    > /dev/tty
}

abort_script()
{
    print_error "$1" "$2"
    exit $1
}

ckabort()
{
    #  This function is passed the exit status of a completed
    #  function.  It will abort the program if that exit status
    #  is non-zero.

    if [ $1 -ne 0 ]; then
        abort_script $1 "Aborting!"
    fi
}

print_usage()
{
	echo                                                               > /dev/tty
    echo "usage: ./Configure [option ...] [arch]"                      > /dev/tty
    echo "where:"                                                      > /dev/tty
    echo " option in -nograph | -no3d | -notable | -nofield | -nogear" > /dev/tty
    echo " arch in SUNOS | SOLARIS | HPUX | IBM |"                     > /dev/tty
    echo "         ALPHA | SGI | NCR | NOVELL"                         > /dev/tty
    echo                                                               > /dev/tty
}


#Start script -- check environment variables
if [ -z "$XRTHOME" ]; then
    abort_script 1 "XRTHOME environment variable must be set."
fi

if [ ! -d $XRTHOME/lib ]; then
    abort_script 1 "Non-standard XRTHOME: $XRTHOME/lib does not exist!"
fi

if [ -z "$VISUROOT" ]; then
    abort_script 1 "WorkShop Visual environment variable VISUROOT must be set."
fi

echo "Using 'XRTHOME=$XRTHOME'" > /dev/tty
echo "Using 'VISUROOT=$VISUROOT'"   > /dev/tty


# Figure out which XRT products are wanted

want_graph=1
want_xrt3d=1
want_table=1
want_field=1
want_gear=1
arch=

for arg
do
    case $arg in

        -*) case $arg in
                -nograph) want_graph=0 ;;
                -no3d) want_xrt3d=0 ;;
                -notable) want_table=0 ;;
                -nofield) want_field=0;;
                -nogear) want_gear=0;;
                -\?|-h*) print_usage; exit 0 ;;
                *)  abort_script 1 "Invalid option: $arg" ;;
            esac
            ;;
        sunos|SUNOS|SunOS) arch=SUNOS ;;
        solaris|SOLARIS) arch=SOLARIS ;;
        hpux|HPUX) arch=HPUX ;;
        ibm|IBM|rs6000|RS6000|aix|AIX) arch=AIX ;;
        alpha|ALPHA|osf|OSF) arch=ALPHA ;;
        sgi|SGI|irix|IRIX) arch=SGI ;;
        novell|NOVELL) arch=NOVELL ;;
        ncr|NCR) arch=NCR ;;
        *)  abort_script 1 "Unknown architecture: $arg"
            ;;
    esac
done

if [ $want_graph -eq 0 -a $want_xrt3d -eq 0 -a $want_table -eq 0 -a $want_field -eq 0  -a $want_gear -eq 0 ]; then
    abort_script 1 "No XRT products wanted - aborting"
fi


#
# If architecture has not been specified try to figure it out
#
if [ -z "$arch" ]
then
    case "`uname -sr`" in
        AIX*) arch="AIX" ;;
        SunOS\ 4*)  arch="SUNOS" ;;
        SunOS\ 5*) arch="SOLARIS" ;;
        HP*) arch="HPUX" ;;
        OSF*) arch="ALPHA" ;;
        IRIX*) arch="SGI" ;;
        *) abort_script 1 "Cannot determine architecture.  Must specify." ;;
    esac
fi
echo "Using 'Architecture=$arch'" > /dev/tty

#
echo "Searching for installed XRT libraries ..." > /dev/tty
xrtliblist="`$XRTHOME/bin/xrt_license|sed -n -e 's/Found XRT \([^-]*\)-Motif, version \(.*\)$/\1-\2/p'`"

hasflags=""
libxrtall=""
install_xpm=0
for xrtlib in $xrtliblist
do
    case $xrtlib in
        graph-*)
            if [ $want_graph -eq 1 ]
            then
                echo "    including $xrtlib" > /dev/tty
                libxrtall="$libxrtall -lxrtm"
                hasflags="$hasflags -DHASXRTGRAPH"
            else
                echo "    skipping $xrtlib" > /dev/tty
            fi
            ;;
        3d-*)
            if [ $want_xrt3d -eq 1 ]
            then
                echo "    including $xrtlib" > /dev/tty
                libxrtall="$libxrtall -lxrt3d"
                hasflags="$hasflags -DHASXRT3D"
            else
                echo "    skipping $xrtlib" > /dev/tty
            fi
            ;;
        table-*)
            if [ $want_table -eq 1 ]
            then
                echo "    including $xrtlib" > /dev/tty
                libxrtall="$libxrtall -lxrttable"
                hasflags="$hasflags -DHASXRTTABLE"
				install_xpm=1
            else
                echo "    skipping $xrtlib" > /dev/tty
            fi
            ;;
        field-*)
            if [ $want_field -eq 1 ]
            then
                echo "    including $xrtlib" > /dev/tty
                libxrtall="$libxrtall -lxrtfield"
                hasflags="$hasflags -DHASXRTFIELD"
            else
                echo "    skipping $xrtlib" > /dev/tty
            fi
            ;;
        gear-*)
            if [ $want_gear -eq 1 ]
            then
                echo "    including $xrtlib" > /dev/tty
                libxrtall="$libxrtall -lxrtgear"
                hasflags="$hasflags -DHASXRTGEAR"
				install_xpm=1
            else
                echo "    skipping $xrtlib" > /dev/tty
            fi
            ;;
    esac
done    

# install Xpm only for certain products
if [ $install_xpm -eq 1 ]; then
    libxrtall="$libxrtall -lXpm"
fi

#
# WorkShop Visual 4.0a and later use a different directory to store visu.o
#
xdobjdir="cmd"
if [ -d $VISUROOT/user_widgets/obj ]; then
	xdobjdir="obj"
fi

#
# Later versions of WorkShop Visual use a different name for the WorkShop Visual binary
#
xdbinnam="visu.bin"

makefile_tem="./config/Makefile.${arch}"

#
# Install appropriate Makefile
#
echo ""                             > /dev/tty
echo "Creating customized Makefile..." > /dev/tty
eval "sed \
    -e 's/^\(HASFLAGS[  ]*=\).*$/\1${hasflags}/' \
    -e 's/^\(LIBXRTALL[     ]*=\).*$/\1${libxrtall}/' \
    -e 's/^\(XDOBJDIR[  ]*=\).*$/\1${xdobjdir}/' \
    -e 's/^\(XDBINNAM[  ]*=\).*$/\1${xdbinnam}/' \
${makefile_tem} > ./Makefile" || abort_script $? "Can't create Makefile"

if [ -f xrtcode.c -a -f xrtconfig.c ]
then
    echo ""                          > /dev/tty
    echo "Creating new WorkShop Visual..." > /dev/tty
    echo ""                          > /dev/tty
    make
    ckabort $?
else
    abort_script 1 "Must first run XDconfig"
fi


echo ""                                         > /dev/tty
echo "XRT Integration with WorkShop Visual Complete." > /dev/tty
echo ""                                         > /dev/tty

exit 0


