#!/bin/sh
# @(#) 7276.7659

test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"

yorn ()
{
        YN=b
        while [ "$YN" != "n" -a "$YN" != "no" -a \
                "$YN" != "N" -a "$YN" != "NO" -a \
                "$YN" != "y" -a "$YN" != "yes" -a \
                "$YN" != "Y" -a "$YN" != "YES" -a \
                "$YN" != "Yes" -a "$YN" != "No" ]; do
                $echocmd $1
                read YN
                if [ "$YN" = "y" -o "$YN" = "yes" -o "$YN" = "Yes" -o \
                     "$YN" = "Y" -o "$YN" = "YES" ]; then
                        return 1
                fi
                if [ "$YN" = "n" -o "$YN" = "no" -o "$YN" = "No" -o \
                     "$YN" = "N" -o "$YN" = "NO" ]; then
                        return 0
                fi
                $echocmd "Please enter y or n"
        done
}

cont ()
{
        yorn "Do you want to continue (y/n): \c"
        res=$?
        if [ "$res" != "1" ]; then
                exit 0
        fi
}

sep ()
{
$echocmd
$echocmd '--------------------------------------------------------------------------------'
}

set_java_version()
{
  sep
  yorn "Do you want to make use of COBOL and Java working together? (y/n): \c"
  if test $? -eq 1
  then
	sh $COBDIR/bin/java_setup
	else
		$echocmd "Skipping Java setup"
		$echocmd "Should you want to use Java with COBOL later on"
                # CK changed the comment for J product
		$echocmd "to select the version of Java you wish to use"
		$echocmd "as super user run the following command : "
		$echocmd "  cd $COBDIR; sh ./bin/java_setup"
		cd ${OLDPWD}
		return 0
  fi
}

SCCSid="@(#) install 5.0"

umask 000

#default PATH for COBDIR
DEFCOBDIR=/opt/microfocus/cobol
DEFBIN=/opt/microfocus/bin

BASE=`pwd`
COBDIR=$BASE

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

if [ -f "$COBDIR/etc/mfreg.dat" ]; then
	$echocmd "Install has already been run against this product directory,"
	$echocmd "this is not a valid operation, aborting install..."
	exit 1
fi

unset COBPATH
unset COBDATA

COBSW=""
export COBSW

$echocmd
$echocmd "This script will install Micro Focus Server 5.0 on this computer."
$echocmd
$echocmd "There are two files included in this delivery that contain valuable"
$echocmd "information related to this product. The first (cpi.htm) lists"
$echocmd "all of the issues resolved by this release, and is known as the"
$echocmd "Corrected Product Incident (CPI) report. The other (readme.txt)"
$echocmd "contains details of new features, enhancements and any restrictions"
$echocmd "of which you should be aware. These files are located in :"
$echocmd
$echocmd "     $COBDIR/docs"
$echocmd
$echocmd "We strongly recommend you read both files once the installation"
$echocmd "is complete."
$echocmd
cont

if test ! -f /tmp/mf_lic_acpt
then
sep
$echocmd Before installing and using this software product you must
$echocmd agree to be bound by the terms and conditions of the end user
$echocmd license agreement \(\"License Agreement\"\) which accompanies this product.
$echocmd Please take this time to read the License Agreement. If you are not in
$echocmd agreement with the terms and conditions of the License Agreement, please
$echocmd return the product to your Account Representative and your money will 
$echocmd be refunded.  If you require a replacement copy of the License
$echocmd Agreement, please contact your Account Representative before proceeding 
$echocmd with the install process.
$echocmd
yorn "Do you agree to the terms of the License Agreement? (y/n): \c"
clk_ans=$?
if [ "$clk_ans" != "1" ]; then
$echocmd "We suggest that you remove this product as the installation is incomplete, "
$echocmd "restarting the installation when you are able to accept the terms of"
$echocmd "the license."
exit 0
fi
else
rm /tmp/mf_lic_acpt
sep
fi

if [ "$1" != "noplatformcheck" ]; then
osname=`uname -s`
if [ ".$osname" = "." ]; then
osname=unknown
fi
oslevel=`uname -r 2>/dev/null`
if [ ".$oslevel" = "." ]; then
oslevel=unknown
fi
grep "$osname $oslevel" $COBDIR/docs/env.txt >/dev/null 2>&1
rc=$?
if [ "$rc" != "0" ]; then
builtos=`sed -n '/^Operating System/,/^$/p' $COBDIR/docs/env.txt | sed -n '3,/^$/p'`
compatos=`sed -n '/^Operating System$/,/^$/p' $COBDIR/docs/env.txt | grep -v '^$' | grep -v '^Operating System' | grep -v '^-----'`
sep
$echocmd                 Micro Focus Install
$echocmd
$echocmd "This product was not built or tested on this version of the Operating"
$echocmd "System. The supported versions are listed in docs/env.txt."
# $echocmd "This product was not built or tested on this version"
# $echocmd "of the Operating System."
# $echocmd
# $echocmd "This product was built on Operating System:"
# $echocmd "$builtos"
# $echocmd "and you are installing it on Operating System:"
# $echocmd "$osname $oslevel"
# if [ -f /etc/SuSE-release ]; then
# cat /etc/SuSE-release
# fi
# $echocmd
# $echocmd "Any product issues you report will only be corrected if"
# $echocmd "they can be reproduced on one of our systems running:"
# $echocmd "$compatos"
$echocmd
yorn "Please confirm that you want to continue with this installation (y/n): \c"
decision=$?
if [ "$decision" != "1" ]; then
$echocmd Installation aborted.
exit 98
fi
fi
builtchip=x86_64
compatchip="i686"
chipname=`uname -m 2>/dev/null`
if [ ".$chipname" = "." ]; then
chipname=unknown
fi
if test "$chipname" != "$builtchip" -a "$chipname" != "$compatchip" ; then
sep
$echocmd
$echocmd                 Micro Focus Install
$echocmd
$echocmd "The chiptype of this machine ($chipname) does not match"
$echocmd "that for which this product was created ($builtchip)."
$echocmd
if [ "$chipname" = "x86_64" ]; then
$echocmd
$echocmd "Any product issues you report will only be corrected if"
$echocmd "they can be reproduced on one of our systems running with"
$echocmd "chiptype $builtchip."
$echocmd
yorn "Please confirm that you want to continue with this installation (y/n): \c"
yourchoice=$?
if [ "$yourchoice" != "1" ]; then
$echocmd This installation is aborting.
exit 100
fi
else
$echocmd This installation is aborting.
$echocmd
exit 99
fi
fi
fi

export COBDIR
cd $COBDIR

sep
$echocmd "When you press return you will be shown details of the reference"
$echocmd "environment (and any compatibility environments)."
$echocmd
$echocmd "Please press return when you are ready: \c"
read ready
$echocmd
more docs/env.txt
$echocmd
$echocmd "Please confirm your understanding of the above reference environment"
yorn "details (y/n): \c"
env_ans=$?
if [ "$env_ans" != "1" ]; then
$echocmd "Installation terminated at users request."
exit 0
fi

LD_LIBRARY_PATH=$COBDIR/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
SHLIB_PATH=$COBDIR/lib:$SHLIB_PATH
export SHLIB_PATH
LIBPATH=$COBDIR/lib:$LIBPATH
export LIBPATH

bin/cobconvstrtest -s
case $? in
        0)      ;;
        1)      sep
		$echocmd "Unicode tables not completely available"
                $echocmd "J2EE and Web Services require Unicode and these transaction services"
                $echocmd "will not function correctly"
                $echocmd
		yorn "Please confirm you acknowledgement (y/n): \c" 
                ;;
esac

if test ! -f etc/cobver
then
	sep
	$echocmd "You must have a COBOL system present to install this product"
	exit 1
fi

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

set -e
trap "$echocmd '\ninstall error, check disk space/permissions and try again'; exit 1" 0


DEFLIBDIR=$BASE/lib
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/lib/$shlib*
                then
                        lib_update=yes
                fi
        done
fi
if test $lib_update = yes
then
	$echocmd
        yorn "Would you like to save existing shared libraries\nwhich will be updated? (y/n): \c"
        save_shlibs=$?
        case $save_shlibs in
        1)  SAVDIR=
                while test ".$SAVDIR" = "."
                do
			$echocmd
                        $echocmd "Please enter the full pathname where the"
                        $echocmd "libraries are to be saved."
                        $echocmd
                        $echocmd "q(quit) or <PATH> or RETURN for default($DEFLIBDIR): \c"
			read SAVDIR
                        if test "$SAVDIR" = "q"
                        then
                                $echocmd "\nInstallation abandoned"
                                trap "" 0
                                exit 1
                        fi
                        SAVDIR=`eval $echocmd $SAVDIR`
                        if test "$SAVDIR." = "."
                        then
                                SAVDIR=$DEFLIBDIR
                        fi
                        if expr "$SAVDIR" : '[^\/]' > /dev/null
                        then
                                $echocmd "absolute pathname required (ie must start with '/')"
                                SAVDIR=
                                continue
                        fi
                        if test ! -d "$SAVDIR"
                        then
                                yorn "$SAVDIR does not exist\ndo you want to create it? (y/n): \c"
                                y=$?
                                case "$y" in
                                        1)  mkdir $SAVDIR
                                                chmod 755 $SAVDIR
                                                ;;
                                        *)      SAVDIR=
                                                ;;
                                esac
                                continue
                        fi
                        if test ! -w "$SAVDIR"
                        then
                                $echocmd "cannot write to $SAVDIR"
                                SAVDIR=
                        fi
                done

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

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

		$echocmd
                $echocmd "Old shared libraries saved under"
                $echocmd " $SAVDIR"
                $echocmd
                $echocmd "Please refer to your documentation for instructions"
                $echocmd "on how to continue using your old shared libraries."
                ;;
        esac
fi
cd $COBDIR

cd $COBDIR


LD_LIBRARY_PATH=$COBDIR/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

chmod 755 $COBDIR


if test -d $COBDIR/lib
then
	cd $COBDIR/lib

	SEARCHDIR=/usr/lib
	SEARCHDIR2=/lib



	SEARCHFILE=libc


	if test ! -f $SEARCHDIR/$SEARCHFILE.a -a ! -f $SEARCHDIR2/$SEARCHFILE.a
	then
	    if test ! -f $SEARCHDIR/$SEARCHFILE.so -a ! -f $SEARCHDIR2/$SEARCHFILE.so
	    then
		$echocmd
		$echocmd "WARNING: unable to find sockets library $SEARCHDIR/$SEARCHFILE"
                $echocmd
		$echocmd "CCITCP requires support from this library. Executables linked without this"
		$echocmd "library cannot run applications that use CCITCP functionality."
		$echocmd "If you require this support, you will need to install the"
		$echocmd "TCP/IP Development System libraries prior to installation of your COBOL system."
	    fi

	fi


fi






    GCCINCDIR=""
    GCCLIBDIR=""
    GCCLIBDIR64=""
    if [ -d "/usr/lib/gcc/x86_64-redhat-linux/3.4.3" ]; then
       GCCINCDIR=/usr/lib/gcc/x86_64-redhat-linux/3.4.3/include
       GCCLIBDIR=/usr/lib/gcc/x86_64-redhat-linux/3.4.3/32
       GCCLIBDIR64=/usr/lib/gcc/x86_64-redhat-linux/3.4.3
    elif [ -d "/usr/lib/gcc/i386-redhat-linux/3.4.3" ]; then
       GCCINCDIR=/usr/lib/gcc/i386-redhat-linux/3.4.3/include
       GCCLIBDIR=/usr/lib/gcc/i386-redhat-linux/3.4.3
    elif [ -d "/usr/lib/gcc/x86_64-redhat-linux/4.1.1" ]; then
       GCCINCDIR=/usr/lib/gcc/x86_64-redhat-linux/4.1.1/include
       GCCLIBDIR=/usr/lib/gcc/x86_64-redhat-linux/4.1.1/32
       GCCLIBDIR64=/usr/lib/gcc/x86_64-redhat-linux/4.1.1
    fi
    if [ "x.$GCCLIBDIR" != "x." ]; then
       chmod 644 $COBDIR/etc/cobopt 	 
       ed $COBDIR/etc/cobopt >/dev/null 2>&1 <<EOF
2
s#/usr/lib/gcc/x86_64-redhat-linux/3.4.3/32#$GCCLIBDIR#
w
q
EOF
       chmod 444 $COBDIR/etc/cobopt
    fi
    if [ "x.$GCCLIBDIR64" != "x." ]; then
       chmod 644 $COBDIR/etc/cobopt64
       ed $COBDIR/etc/cobopt64 >/dev/null 2>&1 <<EOF
2
s#/usr/lib/gcc/x86_64-redhat-linux/3.4.3#$GCCLIBDIR64#
w
q
EOF
       chmod 444 $COBDIR/etc/cobopt64
    fi
    if [ "x.$GCCINCDIR" != "x." ]; then 	 
       if [ -f "$COBDIR/demo/c-cobol/ccob1.sh" ]; then 	 
          chmod 755 $COBDIR/demo/c-cobol/ccob1.sh 	 
          ed $COBDIR/demo/c-cobol/ccob1.sh >/dev/null 2>&1 <<EOF
5
s#/usr/lib/gcc/x86_64-redhat-linux/3.4.3/include#$GCCINCDIR#
w
q
EOF
          chmod 555 $COBDIR/demo/c-cobol/ccob1.sh 	 
       fi 	 
    fi 	 


set +e
trap "" 0



cd $COBDIR


set_java_version



if test -d $COBDIR/lmf
then
sep
$echocmd This product is protected using the Micro Focus License Management
$echocmd "Facility (LMF). Please refer to the Development System Licensing Guide"
$echocmd for information relating to the installation of the licensing system
$echocmd and licenses.
$echocmd
# RPI 517299 Ensure that the directory /var/mfaslmf exists before
# any LMF install scripts are run.
mkdir -p /var/mfaslmf
chmod 777 /var/mfaslmf
admindir=
$echocmd
$echocmd If you do not have LMF installed or want to upgrade to the latest version,
$echocmd we recommend you install it now.
$echocmd
yorn "Would you like to install LMF now? (y/n): \c"
lmf_ans=$?
case "$lmf_ans" in
	1)  export COBDIR
		LD_LIBRARY_PATH=$COBDIR/lib:$LD_LIBRARY_PATH
                export LD_LIBRARY_PATH
		PATH=$COBDIR/bin:$PATH
		export PATH
		thisd=`pwd`
                cd $COBDIR/lmf
		sh lmfinstall
                cd $thisd
		if [ -f "/tmp/lmfinstalldir" ]; then
			admindir=`cat /tmp/lmfinstalldir`
			lmfinstallok=1
		else
			lmfinstallok=0
		fi
		;;
	*)      $echocmd
		$echocmd "If you do not already have LMF installed, you can install it later "
		$echocmd "by entering the following :"
                $echocmd "    cd $COBDIR/lmf"
                $echocmd "    sh lmfinstall"
		thisd=`pwd`
		lmfinstallok=-1
                cd $COBDIR/lmf
		admindir=`./lmfgetpv|tail -1|sed "s/Loaded from //"`
		if [ "$admindir" = "License Manager is not running" ]; then
			admindir=
		fi
		cd $thisd
		;;
esac

# Now deal with installation of licenses if we can, and if installer wishes to

$echocmd
$echocmd "Please consult the Development Licensing Guide for detailed information"
$echocmd "on how to install licenses."
$echocmd 
$echocmd "This may be done by changing directory to $COBDIR"
$echocmd "and typing"
$echocmd "        sh ./bin/mflicense"
$echocmd

fi


cd $COBDIR

sep
sh ./aslmf/aslmfinstall


modnum=0
if [ -d $COBDIR/dynload64 ]
then
    modnum=1
fi
if [ -d $COBDIR/dynload ]
then
    modnum=`expr $modnum + 2`
fi


sep
case $modnum in

# 32 bit only
    1)  $echocmd "This product can be used in 32-bit mode only." ;
        $COBDIR/bin/cobmode -s 32 ;
        ;;

# 64 bit only
    2)  $echocmd "This product can be used in 64-bit mode only."
        $COBDIR/bin/cobmode -s 64 ;
        ;;

# 32 and 64 bit.
    3)  $echocmd "This product can be used in either 32-bit or 64-bit modes." ;
        $echocmd "Please enter either 32 or 64 to set the system default mode: \c" ;
        read mod ;
        while [ "$mod" != "32" -a "$mod" != "64" ]
        do
            $echocmd "Invalid response, please enter either 32 or 64."
            read mod
        done ;
        $COBDIR/bin/cobmode -s $mod ;
        $echocmd "System default COBMODE has been set to $mod."
        ;;
esac

if [ -f $COBDIR/docs/html/asdocs.tar ]; then
        sep
        $echocmd "Installing documentation. Please wait..."
	cd $COBDIR/docs/html
	tar xf asdocs.tar
	rm -f asdocs.tar
        cd $COBDIR
fi

cat >$COBDIR/etc/mfreg.dat <<EOF
SX

SX/5.0

SX/5.0/Interface Mapping Toolkit

SX/5.0/Interface Mapping Toolkit/Version
5.0
SX/5.0/Interface Mapping Toolkit/5.0/Install

SX/5.0/Interface Mapping Toolkit/5.0/Install/SCHEMA
$COBDIR/etc/schema
SX/5.0/Interface Mapping Toolkit/5.0/Install/BIN
$COBDIR/lib
EOF

if [ -f $COBDIR/bin/es_install ]; then
$COBDIR/bin/es_install

if [ -d $COBDIR/src/enterpriseserver/xa ]; then
chmod 777 $COBDIR/src/enterpriseserver/xa
fi
fi

if [ "$2" = "configureopenldap" ]; then
if [ -f "$COBDIR/bin/configure_openldap" ]; then
$COBDIR/bin/configure_openldap <<EOF
y

EOF
fi
fi


sep
$echocmd "(remember to set COBDIR to $COBDIR,"
$echocmd "include $COBDIR/lib in LD_LIBRARY_PATH"
$echocmd "and include $COBDIR/bin on your PATH)"
$echocmd

$COBDIR/bin/services

$echocmd
$echocmd "WARNING: Any executables (whether a Run-Time System or an application)"
$echocmd "must be relinked using this new release. Otherwise, the results of"
$echocmd "running the older executables with this new release are undefined."
$echocmd
$echocmd "Installation completed successfully."
$echocmd
$echocmd "The COBOL system is ready to use."
