#!/bin/sh
#
#  This script should generate a makefile that is at least close to what is
#  required.  Unfortunately, the makefile may not be completely correct for all
#  database versions on all platforms.  This is especially true for sites that
#  do not have the pre-compiler software installed and we are forced to "guess" 
#  the required libraries.  If you have a library that is "close" to a listed
#  library (e.g. "libnetstub.a" vs. "netstub.a") and the listed library is not
#  available on your platform, change the listed library to the "close" library.
#  Informix libraries are in the $INFORMIXDIR/lib and $INFORMIXDIR/lib/esql
#  sub-directories.
#
#  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 inf_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

#  The lib*14*.a files are sometimes used for version 5.x
INF5MODS="-lsql -lgen -los"
INF51MODS="-lsql -lgen -los"
INF72MODS="libixsql.so libixos.so libixasf.so libixgen.so libixgls.so netstub.a libixgen.so libixos.so libixgen.so libixasf.so checkapi.o"

#  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 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

INFORMIX_VERSION=unknown
while test $INFORMIX_VERSION = unknown
do
    echo ""
    while test -z "$INFORMIXDIR"
    do
        echo $n "Enter the directory where INFORMIX is installed: " $c
        read INFORMIXDIR
    if test ! -d "$INFORMIXDIR"
    then
        echo $INFORMIXDIR isn\'t a directory.
	INFORMIXDIR=
	INFORMIX_VERSION=unknown
	fi
    done
#INFORMIXDIR=
	echo $n "Which version of INFORMIX do you have installed?" 
	echo $n "(Enter 3 for 7.3, 4 for 7.3 64-bit, 5 for 5.1, 7 for 7.2)" $c
	read INFORMIX_VERSION
	case $INFORMIX_VERSION in
	  7)
	    if test ! -r $INFORMIXDIR/lib/netstub.a 
	    then
		echo INFORMIX version 7.2 doesn\'t seem to exist in $INFORMIXDIR
		INFORMIX_VERSION=unknown
		ACU_DIR=
	    else
		INFORMIX_LIBDIR=$INFORMIXDIR/lib
		INF_VERSION="inf72.o"
		INFEMB_VERSION="infemb72.o"
		if test -r $INFORMIXDIR/bin/esql
		then
			INFORMIX_MODS="-L$INFORMIXDIR/lib -L$INFORMIXDIR/esql/lib `esql -libs | tr '\012' ' '`"
			if test -r $INFORMIXDIR/lib/esql/checkapi.o
			then
				INFORMIX_MODS="$INFORMIX_MODS $INFORMIXDIR/lib/esql/checkapi.o"
			fi
		else
			INFORMIX_MODS="$INF72MODS"
		fi
	    fi
	    ;;
	  3)
	    if test ! -r $INFORMIXDIR/lib/netstub.a 
	    then
		echo INFORMIX version 7.3 doesn\'t seem to exist in $INFORMIXDIR
		INFORMIX_VERSION=unknown
		ACU_DIR=
	    else
		INFORMIX_LIBDIR=$INFORMIXDIR/lib
		INF_VERSION="inf73.o"
		INFEMB_VERSION="infemb73.o"
		if test -r $INFORMIXDIR/bin/esql
		then
			INFORMIX_MODS="-L$INFORMIXDIR/lib -L$INFORMIXDIR/esql/lib `esql -libs | tr '\012' ' '`"
			if test -r $INFORMIXDIR/lib/esql/checkapi.o
			then
				INFORMIX_MODS="$INFORMIX_MODS $INFORMIXDIR/lib/esql/checkapi.o"
			fi
		else
			INFORMIX_MODS="$INF72MODS"
		fi
	    fi
	    ;;
	  4)
	    if test ! -r $INFORMIXDIR/lib/netstub.a 
	    then
		echo INFORMIX version 7.3 64-bit doesn\'t seem to exist in $INFORMIXDIR
		INFORMIX_VERSION=unknown
		ACU_DIR=
	    else
		INFORMIX_LIBDIR=$INFORMIXDIR/lib
		INF_VERSION="inf7364.o"
		INFEMB_VERSION="infemb7364.o"
		if test -r $INFORMIXDIR/bin/esql
		then
			INFORMIX_MODS="-L$INFORMIXDIR/lib -L$INFORMIXDIR/esql/lib `esql -libs | tr '\012' ' '`"
			if test -r $INFORMIXDIR/lib/esql/checkapi.o
			then
				INFORMIX_MODS="$INFORMIX_MODS $INFORMIXDIR/lib/esql/checkapi.o"
			fi
		else
			INFORMIX_MODS="$INF7MODS"
		fi
	    fi
	    ;;
	  5)
	    if test ! -r $INFORMIXDIR/lib/esql/libsql.a
	    then
		echo INFORMIX version 5 doesn\'t seem to exist in $INFORMIXDIR
		INFORMIX_VERSION=unknown
		ACU_DIR=
	    else
		INFORMIX_LIBDIR=$INFORMIXDIR/lib
		INF_VERSION="inf51.o"
		INFEMB_VERSION="infemb51.o"
		INFORMIX_MODS="$INF51MODS"
	    fi
	    ;;
	  *)
	    echo Unsupported version of INFORMIX: $INFORMIX_VERSION
	    INFORMIX_VERSION=unknown
	    ACU_DIR=
	    ;;
	esac
done

#  Try to determine whether we use -lnsl or -lnsl_s for networking
cat > conftest.c <<EOF

main () { exit (0); }
t() { main (); }
EOF
echo $n Checking for -lnsl or -lnsl_s ... $c
if cc conftest.c -o conftest -lnsl >/dev/null 2>&1
then
	NSL="-lnsl"
	echo using -lnsl
elif cc conftest.c -o conftest -lnsl_s >/dev/null 2>&1
then
	NSL="-lnsl_s"
	echo using -lnsl_s
else
	NSL=
	echo neither found
fi
rm -f conftest*
INF_MODS="-L$INFORMIXDIR/lib -L$INFORMIXDIR/lib/esql $INFORMIX_MODS $NSL"

#if test -r ../lib/Makefile
#then
#	echo We seem to have the Acucobol library files in this directory.
#	ACULIBDIR=../lib
#else
#fi

echo Creating Makefile.inf ...
cat > SEDTMP1.$$ << EOF
/^CFLAGS/s,=,= -DUSE_INFORMIX -DACU4GL,
/^FSI_SUBS/s,=,= $INF_VERSION $INFEMB_VERSION cur.o ,
/^FSI_LIBS/s,=,= $INF_MODS,
s,@,@\\\\\\
		,g
EOF
sed -f SEDTMP1.$$ $ACULIBDIR/Makefile > $ACULIBDIR/Makefile.inf
# rm -f SEDTMP1.$$
echo You should now be able to execute a make with the command
echo "	make -f Makefile.inf"
echo in the $ACULIBDIR directory.
