#!/bin/sh

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 wish to continue (y/n): \c"
        res=$?
        if [ "$res" != "1" ]; then
                exit 0
        fi
}

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

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

unset COBPATH
unset COBDATA

COBSW=""
export COBSW

if [ "$COBDIR" = "" ]; then
	$echocmd "You must have COBDIR set to run this script."
	exit 1
fi

if [ -f $COBDIR/bin/sde ]; then
grep  RXCTT/AA.:9l.a5.50 $COBDIR/etc/cobver >/dev/null
RC=$?
if [ "$RC" = "1" ]; then
	$echocmd "This install can only be applied to an existing Server Express Development System installation."
	exit 1
fi
grep "PTI=ES" $COBDIR/etc/cobver >/dev/null
RC=$?
if [ "$RC" = "0" ]; then
	$echocmd "The Enterprise Server Development System has already been installed."
	exit 1
fi
fi

cd $COBDIR
if [ -f docs/html/esdocs.tar ]; then
      sep
      $echocmd "Installing documentation. Please wait..."
      cd docs/html
      tar xf esdocs.tar
      rm -f esdocs.tar
      cd $COBDIR
fi
if [ -f etc/cciusers.dat ]; then
   if [ ! -f /etc/cciusers.dat ]; then
      cp etc/cciusers.dat /etc
   fi
fi

if [ ! -d /var/mfcobol/logs ]; then
    mkdir -p /var/mfcobol/logs
    chmod 777 /var/mfcobol/logs
fi

host=`uname -n`

cat >>$COBDIR/etc/mf-server.dat <<EOF
name=ESDEMO
mfds=mrpi://$host:86
logdir=/var/mfcobol/logs
EOF

if [ ! -d $COBDIR/deploy ]; then
   mkdir $COBDIR/deploy
fi
chmod 777 $COBDIR/deploy

cat >>$COBDIR/deploy/.mfdeploy <<EOF
MFDS=localhost
MFUS_SERVER=ESDEMO
MFUS_LISTENER=Web Services
EOF
chmod 444 $COBDIR/deploy/.mfdeploy

if [ ! -d /var/mfcobol/logs ]; then
   mkdir -p /var/mfcobol/logs
   chmod 777 /var/mfcobol/logs
fi

sep
$echocmd Enterprise Server provides a scalable, managed and high-performance
$echocmd transactional environment for the deployment of COBOL applications
$echocmd and services, COBOL/J2EE applications and direct COBOL Web Services.
$echocmd
$echocmd "Your Enterprise Server requires configuration. You can either do it now "
$echocmd "or later. To do it now you need to know the alphanumeric user ID of the "
$echocmd "Enterprise Server System Administrator."
$echocmd "To do it later, enter the following commands whilst logged in as root :"
$echocmd "     cd $COBDIR"
$echocmd "     sh ./bin/casperm"
$echocmd "     sh ./bin/eslminstall\n"
yorn "Do you wish to configure Enterprise Server now? (y/n): \c"
res=$?
if [ "$res" = "1" ]; then
  $COBDIR/bin/eslminstall
  $COBDIR/bin/casperm
else
for i in cas3270 cascnvim casrdtex imtkmake mfcs casfile casstart cobimtk mfdepinst casmq casrdtup eslm casdump casstop mfds casinfo cassi casdup 
do
if [ -f "$COBDIR/bin/$i" ]; then
mv $COBDIR/bin/$i $COBDIR/bin/.$i
cat > $COBDIR/bin/$i <<EOF
#!/bin/sh
echo "Enterprise Server has not yet been configured. This tool cannot be run "
echo "without that being done. If you wish to configure now, enter the "
echo "following commands whilst logged in as root :"
echo " cd $COBDIR "
echo " sh ./bin/casperm "
echo " sh ./bin/eslminstall\n"
EOF
chmod 755 $COBDIR/bin/$i
fi
done
fi

cat <<++EOF >>$COBDIR/etc/cobver
PTI=ES
++EOF
