#!/bin/sh
#  Installation script for Sybase stored procedures and others.

#  Author:  Randy Zack, Acucorp, Inc.
#  Written: 04/11/94

#  First, figure out how to echo without a terminating newline....
VAL=`echo -n "Test" | wc -l`
if test $VAL = 0
then
	n="-n"
	c=
else
	VAL=`echo "Test" \\\\c | wc -l`
	if test $VAL = 0
	then
		n=
		c="\\c"
	else
		n=
		c=
	fi
fi

InvalidDBList="master model sybsystemprocs temp"
SEDSCRIPT=acutmp$$

trap "rm -f *.$$; exit" 0 1 2 3 15

echo During the execution of this script, we will create an SQL script
echo which you will need to execute from isql, using a command like
echo
echo '	"isql -Usa -Ppassword < syb_inst.sql"'
echo
echo This script will add some stored procedures, and create some tables
echo in a database that you specify during the execution of this script.
echo
echo In order to implement locking from ACUCOBOL-GT, we need to create a lock
echo table.  This should be a pretty small table, but we need to decide which
echo database to create this table in.  You should now enter the name of the
echo database you want the lock tables created in.  If the database does not
echo exist, it will be created.

FoundDB=0
while test $FoundDB = 0
do
	echo $n "Which database would you like to create this table in? " $c
	read LockDB
	FoundDB=1
	temp=`expr "$LockDB" : '\([a-zA-Z_][a-zA-Z0-9_]*\)'`
	cnt=`expr $temp : '.*'`
	if test "$cnt" -gt 30
	then
		echo Error: $LockDB is too long a name
		FoundDB=0
		continue
	fi
	if test $temp != "$LockDB"
	then
		echo Warning: $LockDB changed to $temp
		echo $n "Is that ok? " $c
		read ans
		if test "$ans" != "y" && test "$ans" != "Y"
		then
			FoundDB=0
			continue
		fi
		LockDB=$temp
	fi
	for db in $InvalidDBList
	do
		if test "$LockDB" = "$db"
		then
			echo Error: $db is not a valid choice. Please do not
			echo use any of $InvalidDBList
			FoundDB=0
		fi
	done
done

Huns=0
Tens=0
Ones=1
echo Saving any old version of syb_inst.sql....
while test -f syb_inst.sql
do
	if test -f syb_inst$Huns$Tens$Ones.sql
	then
		Ones=`expr $Ones + 1`
		if test $Ones = 10
		then
			Ones=0
			Tens=`expr $Tens + 1`
			if test $Tens = 10
			then
				Tens=0
				Huns=`expr $Huns + 1`
			fi
		fi
	else
		echo Saving syb_inst.sql as syb_inst$Huns$Tens$Ones.sql
		mv syb_inst.sql syb_inst$Huns$Tens$Ones.sql
	fi
done

echo Creating syb_inst.sql....
sed -e "/quit/d" -e "s/@LockDataBase@/$LockDB/" syb_inst.in > syb_inst.sql
echo Creating cblconfig.syb....
sed -e "/quit/d" -e "s/@LockDataBase@/$LockDB/" syb_cfg.in > cblconfig.syb

#  Are we re-linking the runtime on this system?
cat <<EOF
In order to use the Acu4GL for Sybase interface, you need to relink the
runtime system.  We are assuming that the Makefile that came with your
runtime system still has the "FSI_SUBS =" and "FSI_LIBS =" lines in it,
and will base our changes on that Makefile.  However, we will create a new
file, called Makefile.syb, that has these changes in it.

EOF
ansok=0
while test $ansok = 0
do
	echo $n "Do you want to set up the Makefile for this system? " $c
	read ans
	case $ans in
	    [nN]*)
		ansok=1
		ans=N
		;;
	    [yY]*)
		ansok=1
		ans=Y
		;;
	    *)
		echo Please answer \"Y\" or \"N\"
		;;
	esac
done
if test $ans = N
then
	exit 0
fi

#  Now set up the Makefile
echo We next need to determine where you have installed the
echo SYBASE client libraries.
SYBASE_Found=0
while test $SYBASE_Found = 0
do
	echo ""
	echo Enter the directory where the SYBASE client libraries
	echo $n "are installed: " $c
	read dir
	dir=`echo $dir | sed -e "s^~^$HOME^"`
	if test ! -d "$dir"
	then
		echo $dir isn\'t a directory.
	elif test ! -d "$dir"/lib
	then
		echo The SYBASE lib subdirectory doesn\'t seem to exist in $dir
	elif test ! -r "$dir"/lib/libsybdb.a
	then
		echo libsybdb.a doesn\'t exist in $dir/lib
		if test -r "$dir"/lib/libsybdb64.a
		then
		    echo We found a 64-bit Sybase library.
		    ansok=0
		    while test $ansok = 0
		    do
		        echo $n "Is this a 64-bit runtime port? " $c
			read ans
			case $ans in
			    [nN]*)
			        ansok=1
				ans=N
				;;
			    [yY]*)
			        ansok=1
				ans=Y
				SYBLIBDIR=$dir/lib
				SYBLIB=libsybdb64.a
				SYBASE_Found=1
				;;
			    *)
				echo Please answer \"Y\" or \"N\"
				;;
			esac
		    done
		fi
	else
		SYBLIBDIR=$dir/lib
		SYBLIB=libsybdb.a
		SYBASE_Found=1
	fi
done

if test -r ../lib/Makefile
then
	echo We seem to have the ACUCOBOL library files in this directory.
	ACULIBDIR=../lib
	SYBASE_O=sybase.o
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
			SYBASE_O=`pwd`/../lib/sybase.o
		fi
	done
fi

echo Creating Makefile.syb ...
rm -f $SEDSCRIPT
echo "/^FSI_SUBS/s,=,= $SYBASE_O," >>$SEDSCRIPT
echo "/^FSI_LIBS/s,=,= $SYBLIBDIR/$SYBLIB," >>$SEDSCRIPT
echo "/^CFLAGS/s,=,= -DACU4GL -DUSE_SYBASE=1," >>$SEDSCRIPT
sed -f $SEDSCRIPT < $ACULIBDIR/Makefile > $ACULIBDIR/Makefile.syb
rm -f $SEDSCRIPT
rm -f $ACULIBDIR/sub.o $ACULIBDIR/filetbl.o

echo $ACULIBDIR/Makefile.syb created.  You should be able to execute
echo \"make -f Makefile.syb\" from the directory $ACULIBDIR.
