#!/bin/sh
#
#
# Don't edit this file when installing. 
# Everything can be set when the script is run.
#
#######################################################################
# Configuration
#######################################################################
# Release Version
#
VERSION=4

#
# tar.gz file that contains binary installation.
#
TARFILE=ibadmin4.tar.gz

#
# Name of real binary
#
IBADMINBIN=IBAdmin4

#
# Name of run script.
#
IBADMIN=ibadmin4

#
#default installation directory.
#
DEFAULTINSTALLDIR=/opt/ibadmin4

#######################################################################
# some useful functions
# ask displays 1st parameter, and ask new value for variable, whose name is
# in the second parameter.
ask ()
{
askvar=$2
eval old=\$$askvar
eval echo -n \""$1 [$old] : "\" 
read $askvar
eval test -z \"\$$askvar\" && eval $askvar=\'$old\'
}
#
# yesno gives 1 on no, 0 on yes $1 gives text to display.
#
yesno ()
{
  while true; do
  echo -n "$1 (Y/n) ? "
  read ans
  case X$ans in
   X|Xy|XY) return 0;;
   Xn|XN) return 1;;
  esac
  done
}
#
# Untar files ($3,optional) from  file ($1) to the given directory ($2)
#
unztar ()
{
 tar -xzf $HERE/$1 --directory $2 $3
}
#
# Make all the necessary directories to get $1
#
makedirhierarch ()
{
  OLDDIR=`pwd`
  case $1 in
    /*) cd /;;
  esac
  OLDIFS=$IFS;IFS=/;eval set $1; IFS=$OLDIFS
  for i
  do
    test -d $i || mkdir $i || break
    cd $i ||break
  done
  cd $OLDDIR
}
#
# check to see if something is in the path
#
checkpath ()
{
 ARG=$1
 OLDIFS=$IFS; IFS=":";eval set $PATH;IFS=$OLDIFS
 for i
 do
   if [ $i = $ARG ]; then
     return 0
   fi
 done 
 return 1
}

# --------------------------------------------------------------------------
# welcome message.
#

clear
echo "This shell script will attempt to install IBAdmin for linux,"
echo "version $VERSION"
echo 

# Here we start the thing.
HERE=`pwd`

INSTALLDIR=$DEFAULTINSTALLDIR
ask "Install in directory" INSTALLDIR
makedirhierarch $INSTALLDIR
#
# Unpack.
#
echo Unpacking IBAdmin...
unztar $TARFILE $INSTALLDIR

#
# Create run script.
#
echo Creating run script.
cat >${INSTALLDIR}/bin/$IBADMIN <<EOS
#!/bin/sh
# Add installed libraries to path. 
LD_LIBRARY_PATH=${INSTALLDIR}/lib:\$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

${INSTALLDIR}/bin/$IBADMINBIN \$*
# end of script.
EOS
#
# Make script executable
#
chmod 755 ${INSTALLDIR}/bin/$IBADMIN
  
#
# Install symbolic link.
#
if yesno "Install symbolic link in a directory in the PATH"; then
  EXECDIR=/usr/local/bin 
  ask "Install link in directory" EXECDIR
  #
  # Remove any existing link. 
  #
  echo Installing symbolic link...
  rm -f $EXECDIR/$IBADMIN
  #
  # install new link.
  #
  ln -sf ${INSTALLDIR}/bin/$IBADMIN  $EXECDIR/$IBADMIN
fi
#
# All done.
#
echo IBAdmin for Linux version $VERSION is installed. 
echo You can start the program with the $IBADMIN command.
#
#