
SCCSid="@(#) install 4.8"

umask 000

#default PATH for BIN and COBDIR
DEFBIN=/usr/bin
DEFCOBDIR=/opt/lib/cobol

BASE=`pwd`
COBDIR=$BASE

if test ! -w /etc/passwd
then
	echo "You must be root to run this script"
	exit 1
fi

if test -d cobtmp
then

	valid_cobdir=false
	while test "$valid_cobdir" = "false"
	do
# Check to see if a product already exists in this directory
		cd $COBDIR

		test -f check.lbr* && currcobol=y || currcobol=n
		test -f osxver* -a ! -f toolboxver* && currosx=y || currosx=n
		test -f tbox.lbr* && currtbox=y || currtbox=n

		test -f $BASE/cobtmp/check.lbr* && subcobol=y || subcobol=n
		test -f $BASE/cobtmp/osxver* -a ! -f $BASE/cobtmp/toolboxver* && subosx=y || subosx=n
		test -f $BASE/cobtmp/tbox.lbr* && subtbox=y || subtbox=n

		currprod=$currcobol$currosx$currtbox
		subprod=$subcobol$subosx$subtbox

		case $currprod$subprod in
		ynnnny|nnnyn?|nnnnyn)
				valid_cobdir=true
				;;
		*)		
				COBDIR=
				while test ".$COBDIR" = "."
				do
				echo "\nA copy of the product has been detected in the directory where you are"
				echo "now trying to install."
				echo "Please enter a different directory for this installation."
				echo "\nq(uit) or <PATH> or RETURN for default($DEFCOBDIR): \c"
				read COBDIR

				if test "$COBDIR" = "q"
				then
					remove_product=N
					echo "The product you have extracted will not be installed."
					echo "Do you wish to remove this product (Y/N) ?"
					read remove_product
					case $remove_product in
					Y*|y*)	rm -rf $BASE/cobtmp
							;;
					esac
					echo "\ninstallation abandoned"
					trap "" 0
					exit 1
				fi
				COBDIR=`eval echo $COBDIR`
				if test "$COBDIR." = "."
				then
					COBDIR=$DEFCOBDIR
				fi
				if expr "$COBDIR" : '[^\/]' > /dev/null
				then
					echo "absolute pathname required (ie must start with '/')"
					COBDIR=
					continue
				fi
				if test "$COBDIR" = "$BASE"
				then
					COBDIR=
					continue
				fi
				if test ! -d "$COBDIR"
				then
					echo "$COBDIR does not exist"
					echo "do you wish to create it ? (y/n) \c"
					read y
					case "$y" in
						y*|Y*)	mkdir $COBDIR
							chmod 755 $COBDIR
							;;
						*)	COBDIR=
							;;
					esac
					continue
				fi
				if test ! -w "$COBDIR"
				then
					echo "cannot write to $COBDIR"
					COBDIR=
				fi
				done
				;;
		esac
	done

		cd $BASE
		if test "$BASE" != "$COBDIR"
		then
			echo "Creating directory structure for installation ..."
			find install gzipsrc -print | cpio -pud $COBDIR/
		fi
		test -f cobtmp/cobver* && mv cobtmp/cobver* $COBDIR
fi

export COBDIR
cd $COBDIR

if test -f cobver.z
then
	unpack cobver
	if test $? -ne 0
	then
		echo "Error during unpack - check disk space/permissions and try again"
		exit 1
	fi
fi

if test ! -f cobver
then
	echo "You must have a COBOL system present to install this product"
	exit 1
fi

trap "trap "" 0;echo '\n\ninstallation aborted'; exit 1" 2 3 14 15

trap "echo '\ninstall error, check disk space/permissions and try again'; exit 1" 0


DEFLIBDIR=$BASE/coblib
lib_update=no
if test -d $DEFLIBDIR
then
        cd $DEFLIBDIR
        for shlib in `ls lib*.so.?.? 2> /dev/null`
        do
                if test -f $BASE/cobtmp/coblib/$shlib*
                then
                        lib_update=yes
                fi
        done
fi
if test $lib_update = yes
then
        echo "\nWould you like to save existing shared libraries"
        echo "which will be updated (Y/N) ?"
        read save_shlibs
        case $save_shlibs in
        Y*|y*)  SAVDIR=
                while test ".$SAVDIR" = "."
                do
                        echo "\n\nPlease enter the full pathname where the"
                        echo "libraries are to be saved."
                        echo "\nq(quit) or <PATH> or RETURN for default($DEFLIBDIR):  \c"
			read SAVDIR
                        if test "$SAVDIR" = "q"
                        then
                                echo "\nInstallation abandoned"
                                trap "" 0
                                exit 1
                        fi
                        SAVDIR=`eval echo $SAVDIR`
                        if test "$SAVDIR." = "."
                        then
                                SAVDIR=$DEFLIBDIR
                        fi
                        if expr "$SAVDIR" : '[^\/]' > /dev/null
                        then
                                echo "absolute pathname required (ie must start with '/')"
                                SAVDIR=
                                continue
                        fi
                        if test ! -d "$SAVDIR"
                        then
                                echo "$SAVDIR does not exist"
                                echo "do you wish to create it ? (y/n) \c"
                                read y
                                case "$y" in
                                        Y*|y*)  mkdir $SAVDIR
                                                chmod 755 $SAVDIR
                                                ;;
                                        *)      SAVDIR=
                                                ;;
                                esac
                                continue
                        fi
                        if test ! -w "$SAVDIR"
                        then
                                echo "cannot write to $SAVDIR"
                                SAVDIR=
                        fi
                done

                verno=`echo | grep cobol $COBDIR/cobver | \
                sed -e "s/\(cobol v\)\(.\)\.\(.\)\(\..*\)/\2\3/"`
                revno=`echo | grep PRN $COBDIR/cobver | cut -d. -f4`

                echo "\nSaving shared libraries:"
                cd $COBDIR/coblib
                for i in `ls lib*.so.?.?`
                do
                        if test -f $BASE/cobtmp/coblib/$i*
                        then
                                echo $i
                                cp $COBDIR/coblib/$i $SAVDIR/$i.$verno.$revno
                        fi
                done

                echo "\nOld shared libraries saved under"
                echo " $SAVDIR"
                echo "\nPlease refer to your documentation for instructions"
                echo "on how to continue using your old shared libraries."
                ;;
        esac
fi
cd $COBDIR

if test -d $BASE/cobtmp
then
	echo "Creating directory structure for installation. Arranging files may take"
	echo "a few minutes. Please wait...."
	cd $BASE/cobtmp
	find * -print | cpio -pud $COBDIR
	cd ..
	rm -rf cobtmp
fi

cd $COBDIR

echo "\nUnpacking files ..."
find * -name \*.z -print | xargs unpack > /dev/null 2>&1


LD_LIBRARY_PATH=$COBDIR/coblib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

chmod 755 $COBDIR

#
# USCEG _update support.
# Should be available generically, thus not to be ifdef'd out.
#
cd $COBDIR
if test -d _update
then
    if test -f osxver
    then
        if test -f toolboxver
        then
            cd _update/cbl
            #echo "COBOL files being replaced ..."
            find . -print | cpio -pvdu $COBDIR
            if test -d ../tbox
            then
                cd ../tbox
                #echo "Toolbox files being replaced ..."
                find . -print | cpio -pvdu $COBDIR
            fi
        else
            if test -d _update/osx
            then
                cd _update/osx
                find . -print | cpio -pvdu $COBDIR
            fi
        fi
    else
        cd _update/cbl
        find . -print | cpio -pvdu $COBDIR
    fi
    if test -f dsver
    then
        if test -d _update/dschar
        then
            cd _update/dschar
            find . -print | cpio -pvdu $COBDIR
        fi
    fi
    cd $COBDIR
    rm -rf _update
fi

if test -d $COBDIR/coblib
then
	if test /bin/true != /bin/true
	then
		echo "\nConverting archives to random libraries:"
		for i in $COBDIR/coblib/*.a
		do
			echo "\t$i"
			/bin/true $i
		done
		echo
	fi

	if test $? -ne 0
	then
		exit 1
	fi

	cd $COBDIR/coblib


#Modify liblist to add the path of the SunPRO C compiler.

 pkginfo -q SPROcc*
 if [ $? -eq 0 ]
 then
	echo "Looking for a supported C compiler.  Please wait"
    if test -f liblist.base
    then
	  ccpkg=`pkginfo SPROcc* | fgrep 3.0.1 | awk '{ print $2 } '`
	  basedir=`pkginfo -l $ccpkg | fgrep BASEDIR | awk '{ print $2 }'`
	if [ $? = 0 ] 
  	 then
             if test -f $basedir/SUNWspro/SC3.0.1/lib/crti.o
             then
                input=liblist.base
		output=liblist
                (sed -e "s:BASEDIR:$basedir:g" > $output) < $input
             fi
        else
	  ccpkg=`pkginfo SPROcc* | fgrep 4.0 | awk '{ print $2 } '`
	  basedir=`pkginfo -l $ccpkg | fgrep BASEDIR | awk '{ print $2 }'`
	  if [ $? = 0 ]
	  then
            if test -f $basedir/SUNWspro/SC4.0/lib/crti.o
             then
               input=liblist.40base
	       output=liblist
               (sed -e "s:BASEDIR:$basedir:g" > $output) < $input
             fi
           else
       echo "\nWARNING: SPROcc (which is part of the SunPro 'C' development"
       echo "         package)  SunPro 'C' is not recognized by this version"
       echo "         of Micro Focus COBOL."
       echo "         In order to create statically linked executables using"
       echo "         Micro Focus COBOL the SunPro 'C' development package V3.0.1"
       echo "         or V4.0 must be installed prior to installing Micro Focus"
       echo "         COBOL."
       echo "         If you continue with this installation you will still be"
       echo "         able to create and execute Micro Focus intermediate and"
       echo "         generated code."
       echo "\nDo you wish to continue this installation? (y/n) \c"
       read y
       case "$y" in
       n*|N*)
            echo "\nInstallation abandoned"
	    rm -rf /tmp/VRD	
            trap "" 0
            exit 1;;
       esac

             fi
	fi
    fi
 else
    echo "\nWARNING: SPROcc (which is part of the SunPro 'C' development"
    echo "         package) does not appear to be installed on this machine."
    echo "         In order to create statically linked executables using"
    echo "         Micro Focus COBOL it is necessary for the SunPro package"
    echo "         SPROcc to be installed prior to installing Micro Focus"

    echo "         COBOL."
    echo "         If you continue with this installation you will still be"
    echo "         able to create and execute Micro Focus intermediate and"
    echo "         generated code."
    echo "\nDo you wish to continue this installation? (y/n) \c"
    read y
    case "$y" in
    n*|N*)
            echo "\nInstallation abandoned"
 	    rm -rf /tmp/VRD	
            trap "" 0
            exit 1;;
    esac
 fi


	SEARCHDIR=/usr/lib
	SEARCHDIR2=/lib
	SOURCEFILE=liblist.wrk
	SOURCEFILE2=liblist.wrk2


	remove_socket=YES

	SEARCHFILE=libsocket


	if test ! -f $SEARCHDIR/$SEARCHFILE.a -a ! -f $SEARCHDIR2/$SEARCHFILE.a
	then
	    if test ! -f $SEARCHDIR/$SEARCHFILE.so -a ! -f $SEARCHDIR2/$SEARCHFILE.so
	    then

		echo "WARNING: unable to find sockets library $SEARCHDIR/$SEARCHFILE"

		echo "CCITCP requires support from this library. Executables linked without this"
		echo "library cannot run applications that use CCITCP functionality."
		echo "If you require this support, you will need to install the"
		echo "TCP/IP Development System libraries prior to installation of your COBOL system."
		echo "Do you wish to continue this installation? (y/n) \c"
		read y
		case "$y" in
		n*|N*)
			echo "\ninstallation abandoned"
			trap "" 0
			exit 1;;
		esac
	    else
		remove_socket=NO
	    fi

	else
		remove_socket=NO
	fi


fi



BIN=
while test ".$BIN" = "."
do
	echo "\n\nPlease enter the full pathname where the"
	echo "driver programs are to be installed."
	echo "\nq(uit) or <PATH> or RETURN for default($DEFBIN): \c"
	read BIN
	if test "$BIN" = "q"
	then
		echo "\ninstallation abandoned"
		trap "" 0
		exit 1
	fi
	BIN=`eval echo $BIN`
	if test "$BIN." = "."
	then
		BIN=$DEFBIN
	fi
	if expr "$BIN" : '[^\/]' > /dev/null
	then
		echo "absolute pathname required (ie must start with '/')"
		BIN=
		continue
	fi
	if test "$BIN" = "$COBDIR/bin" -o "$BIN" = "$COBDIR"
	then
		echo "\ncannot install drivers into $BIN"
		BIN=
		continue
	fi
	if test ! -d "$BIN"
	then
		echo "$BIN does not exist"
		echo "do you wish to create it ? (y/n) \c"
		read y
		case "$y" in
			y*|Y*)	mkdir $BIN
				chmod 755 $BIN
				;;
			*)	BIN=
				;;
		esac
		continue
	fi
	if test ! -w "$BIN"
	then
		echo "cannot write to $BIN"
		BIN=
	fi
done

echo "\nSetting up the utility driver programs"
cd $COBDIR/bin
FILES=`echo * | tr ' ' '\012'`
if test "$FILES" != "*"
then
	rm -f $BIN/cobprog
	cp cobprog $BIN/cobprog
# Every now and then permissions seem wrong here, so let's make absolutely
# sure..
        if [ -f mfsupport ]
        then
            cp mfsupport $BIN
            chmod +x mfsupport $BIN/mfsupport
        fi
	cd $BIN
	for i in $FILES
	do
		test "$i" = "cobprog" && continue
		test "$i" = "mfsupport" && continue
		if test -f $i
		then
			if cmp -s $i cobprog
			then
				continue
			fi

			echo "saving current \"$BIN/$i\" in $COBDIR/oldbin"
			if test ! -d $COBDIR/oldbin
			then
		    		mkdir $COBDIR/oldbin
			fi
			mv -f $i $COBDIR/oldbin/
		fi
		ln cobprog $i
	done
	rm -f cobprog
fi

set +e
trap "" 0


# Remove the dialog 'tutorial' demonstration executable.
cd $COBDIR
if test -f $COBDIR/dsver
then
	rm -f $BIN/ds $COBDIR/bin/ds
	ln $BIN/cobrun $BIN/ds
	ln $COBDIR/bin/cobrun $COBDIR/bin/ds
	rm -f ./dialog/DSC
fi

# Now we need to set up the browser files, if they exist

if test -d $COBDIR/browser
then
	cd $COBDIR/browser
	rm -f menfl menfl.idx
	if test -f $COBDIR/toolboxver
	then
		if test -f $COBDIR/dsver
		then
			ln tboxdsfl menfl
			ln tboxdsfl.idx menfl.idx
		else
			ln tboxfl menfl
			ln tboxfl.idx menfl.idx
		fi
	else
		if test -f $COBDIR/dsver
		then
			ln cobdsfl menfl
			ln cobdsfl.idx menfl.idx
		else
			ln cobolfl menfl
			ln cobolfl.idx menfl.idx
		fi
	fi
fi



cd $COBDIR
echo "\nInstallation completed successfully."
echo "\nThe COBOL system is ready to use.\n"

echo "(remember to set COBDIR to $COBDIR\n to include $COBDIR/coblib in LD_LIBRARY_PATH\n and include $BIN on your PATH)\n"

if test -f toolboxver
then
	echo "The Co-Writer system has been installed as part of Micro Focus Toolbox, and"
	echo "it is necessary to configure the UNIX environment before invoking Co-Writer."
	echo "Please refer to the Micro Focus documentation for further information on using"
	echo "Co-Writer.\n"
fi


echo "\nIf you have re-linked the Run-Time System (i.e. used the "cob -x" command or"
echo "mkrts script at any time) then you must re-create these executable programs"
echo "using this new release. If you do not re-create these executables, the results"
echo "of running the older executables with this new release will be undefined."
