#!/bin/sh

#
# install.sh
#  Shell-scripted Unix installer. Drives the same shell scripts as
#  used by the pkgadd and MSI installers
#

RESULT=0

OURPWD=`pwd`

#
# The user may specify a response file on the command line
# in order to perform unattended/automated installations
#
RESPONSEFILE=$1

#
# Set a 'common' variable, to let scripts beneath know they
# are being run by the common installer, if / when they care
#
COMMONINSTALL=TRUE
export COMMONINSTALL

#
# Find the working directory the installer was run from. Break out
# immediately if it was run from some other directory
#
INST_DATADIR=`pwd`
export INST_DATADIR
echo "Installer invoked in $INST_DATADIR"
if [ ! -s "$INST_DATADIR/install" ]
then
    echo
    echo "Error!"
    echo "Please run nvinstall.sh from the directory that contains it"
    exit 1
fi

#
# Get system type, stuff like the CPIO command to use is platform dependent
#
OS=`uname`
RELEASE=`uname -r`

#
# Establish the users temp directory, defaulting to /tmp
#
if [ "" = "$TMPDIR" ]
then
    echo "Using default /tmp as the temporary directory"
    TMPDIR=/tmp
else
    echo "Using user specified temp directory $TMPDIR"
fi

#
# Expand the tar archive to a directory in the temp dir
#
if [ ! -s "nvdist" ]
then
    echo "Missing distribution file!"
    exit 1
fi

echo "Expanding distribution file"
rm -rf $TMPDIR/bkbinstall
mkdir $TMPDIR/bkbinstall
cd $TMPDIR/bkbinstall
tar -xf $INST_DATADIR/nvdist

#
# Set SERVERCLASSES and CLIENTCLASSES environmental vars
#
SERVERCLASSES=`cat servercomponents.txt`
CLIENTCLASSES=`cat clientcomponents.txt`
export SERVERCLASSES CLIENTCLASSES

PARAMETERFILE=$TMPDIR/bkbinstall/parameters


#
# Run the request script, unless we have been supplied with a
# response file.
#
if [ ! -z "$RESPONSEFILE" ] && [ "nvdist" != "$RESPONSEFILE" ]
then
		echo "Using response file $RESPONSEFILE"
		if [ -s "$OURPWD"/"$RESPONSEFILE" ]
		then
				cp "$OURPWD"/"$RESPONSEFILE" "$PARAMETERFILE"
		else
				echo "Specified response file does not exist!"
				exit 1
		fi
else
		sh request.sh $PARAMETERFILE
		if [ 0 != "$?" ]
		then
				echo "Request script failed!"
				exit 1
		fi
fi

#
# Run the checkinstall script
#
sh checkinstall.sh "$PARAMETERFILE"
if [ 0 != "$?" ]
then
    echo "Check install script failed!"
    exit 1
fi

#
# Source the parameter file, so we know what to copy and to where
#
. "$PARAMETERFILE"

#
# Export variables set by request and checkinstall scripts so that they
# are available in the subshells running later scripts
#
OLDIFS=$IFS
IFS="
"
for line in `cat "$PARAMETERFILE"`
do
  VARNAME=`echo $line | sed 's+\([^=]*\).*+\1+'`
  export $VARNAME
done
IFS=$OLDIFS

#
# Run the preinstall script
#
sh preinstall.sh
if [ 0 != "$?" ]
then
    echo "Preinstall script failed!"
    exit 1
fi

#
# Create the installation directory
#
if [ ! -d $PKG_BASE ]
then
    mkdir $PKG_BASE
fi

#
# For each chosen class, copy in the data from the component subdirectory
#
for component in $CLASSES
do
  if [ -d components/$component ]
  then
      echo "Installing $component"
      cp -R components/$component/* $PKG_BASE
  else
      echo "Corrupt distribution. Cannot find files"
      echo "for component $component"
      rm -rf $PKG_BASE
      exit 1
  fi
done

#
# Run the postinstall script
#
sh postinstall.sh
if [ 0 != "$?" ]
then
    echo "Post install script failed!"
    exit 1
fi

#
# Install the removal scripts in a subdirectory
# beneath the $PKG_BASE
#
if [ ! -d $PKG_BASE/uninstall ]
then
    mkdir $PKG_BASE/uninstall
    if [ 0 != "$?" ]
    then
				echo "Failed to create uninstallation directory"
				exit 1
    fi
fi

echo "Copying uninstallation scripts"
cp preremove.sh $PKG_BASE/uninstall
cp postremove.sh $PKG_BASE/uninstall
cp nvuninstall.sh $PKG_BASE/util/uninstall
cat "$PARAMETERFILE" | grep -v "NDMP_PASSWORD" | grep -v "PASSWORD=" > $PKG_BASE/uninstall/parameters
chmod 700 $PKG_BASE/util/uninstall

#
# Clean up
#
cd "$OURPWD"
rm -rf $TMPDIR/bkbinstall

#
# If we get this far, all was well
#
echo "Installation completed successfully"

exit $RESULT
