#!/bin/sh
#
#  This script should generate a makefile that is at least close to what is
#  required.  Unfortunately, the makefile may not be compeletely correct for all
#  database version on all platforms.  You should not have any "cc" or
#  "smart_cc" entries in the "FSI_LIBS" line.  If these are present, remove 
#  them.  You should not have any "????main.o" type entries.  If these are 
#  present, remove them.
#
#  As always, if problems continue, please feel free to contact Acucorp
#  technical support.
#
#  This is a bourne-shell script.  If you are running an old version
#  of the c-shell, it may not recognize the first line there, and die
#  a horrible death.  If so, execute this script using the bourne-shell
#  explicitly, as in "sh ora_inst".
#

echo If this script dies with an error like \"VAL=0: command not found\",
echo try executing it with a bourne-shell, as in
echo "	sh $0"
echo

#  First, let's figure out how to echo without a terminating newline....
VAL=`echo -n "This is a test" | wc -l`
if [ $VAL = 0 ]
then
    n="-n"
    c=
else
    VAL=`echo "This is a test" \\\\c | wc -l`
    if [ $VAL = 0 ]
    then
        n=
        c="\\c"
    else
#  We can't figure out how to do that on this system, so just echo....
        n=
        c=
    fi
fi

echo We first need to determine where you have installed ORACLE.
ORACLE_VERSION=unknown
while test $ORACLE_VERSION = unknown
do
    echo ""
    while test -z "$ORACLE_HOME"
    do
        echo $n "Enter the directory where ORACLE is installed: " $c
        read ORACLE_HOME
    done
    if test ! -d "$ORACLE_HOME"
    then
        echo $ORACLE_HOME isn\'t a directory.
        ORACLE_VERSION=unknown
        ORACLE_HOME=
    else
        ORACLE_LIBDIR=$ORACLE_HOME/lib
        ORACLE_VERSION=9
        if test ! -d $ORACLE_HOME/lib 
        then
            echo ORACLE lib subdirectory doesn\'t seem to exist in $ORACLE_HOME
			echo Attempting to use Instant Client
            ORACLE_VERSION=Instant
            ORACLE_LIBDIR=$ORACLE_HOME
        else
            ORACLE_LIBDIR=$ORACLE_HOME/lib
            SYSLIBLIST=$ORACLE_LIBDIR/sysliblist
            ORASYSLIB="`cat  $SYSLIBLIST`"

        fi
        for ext in a so sl
            do
                    if test -r $ORACLE_LIBDIR/libclntsh.$ext
            then
                    ORACLE_MODS="-L$ORACLE_LIBDIR -lclntsh $ORASYSLIB"
            fi
            done

        ORACLE_FSI="oraoci.o "
    fi
done



if test -r ../lib/Makefile
then
	echo We seem to have the Acucobol library files in this directory.
	ACULIBDIR=../lib
else
	echo We need to find the Acucobol library files....
	ACUCOBOL_Found=0
	while test $ACUCOBOL_Found = 0
	do
		echo ""
		echo $n "Enter the directory where ACUCOBOL-GT is installed: " $c
		read acu_dir
		dir=`echo $acu_dir | sed -e "s^~^$HOME^"`
		if test ! -d "$dir"
		then
			echo $dir is not a directory
		elif test ! -d "$dir"/lib
		then
			echo The ACUCOBOL-GT lib subdirectory does not exist in $dir
		elif test ! -r "$dir"/lib/Makefile
		then
			echo Makefile does not exist in $dir/lib
		else
			ACULIBDIR=$dir/lib
			ACUCOBOL_Found=1
		fi
	done
fi

echo Creating Makefile.ora ...
cat > SEDTMP1.$$ << EOF
/^CFLAGS/s,=,= -DUSE_ORACLE -DACU4GL ,
/^FSI_SUBS/s,=,= $ORACLE_FSI ,
/^FSI_LIBS/s,=,= $ORACLE_MODS ,
EOF
sed -f SEDTMP1.$$ $ACULIBDIR/Makefile > $ACULIBDIR/Makefile.ora
# rm -f SEDTMP1.$$
#rm $ACULIBDIR/filetbl.o sub.o
echo You should now be able to execute a make with the command
echo "	make -f Makefile.ora"
echo in the $ACULIBDIR directory.
