#!/bin/sh
# $Id: installercore,v 1.1.2.2 2005/04/08 20:52:46 rs131644 Exp $
# install.sh - scs installer script

BASEDIR="`dirname $0`/.."
PKGDIR=$BASEDIR/pkgs
INSTDIR=$BASEDIR/install
PREINST=$INSTDIR/preinstall
POSTINST=$INSTDIR/postinstall
PKGLIST=$INSTDIR/install_list

PKGADD="/bin/rpm -U --replacepkgs --replacefiles --nopreun --nopostun"
PKGCHK="/bin/rpm -q"
LS=/bin/ls
HEAD=/usr/bin/head
AWK=/bin/awk
DF=/bin/df
TAIL=/usr/bin/tail
ID=/usr/bin/id
USERADD=/usr/sbin/useradd
GROUPADD=/usr/sbin/groupadd
GROUPMOD=/usr/sbin/groupmod

TC_USER=tomcat4

TRUE="true"
FALSE="false"


#
# functions
#

printError() {
    echo "ERROR: $@" 1>&2
}

printInfo() {
    echo "Info: $@" 
}

printWarn() {
    echo "Warn: $@" 
}

errorExit() {
    printError "$@"
    exit 1;
}


#
# Main
#


#
# Run the preinstall script
#
printInfo "Executing preinstall"
/bin/sh "$PREINST"
PREINSTALLSTATUS=$?
if [ $PREINSTALLSTATUS == 1 ] ; then
    errorExit "Preinstall failed"
elif [ $PREINSTALLSTATUS == 3 ] ; then
    printInfo "Updating installed version of SCS"
    UPDATE=$TRUE
    export UPDATE
fi
printInfo "Preinstall complete"


#
# Due to a bug in RH we need to create the tomcat4 user and group.
# It seems the tomcat4.rpm might not create these.
#
TC_USER="tomcat4"
if $GROUPMOD $TC_USER > /dev/null 2>&1 ; then
    printInfo "Using existing tomcat4 group"
else
    printInfo "Adding tomcat4 group"
    $GROUPADD -r $TC_USER || \
        errorExit "Can't add tomcat4 group"
fi
	
if $ID "$TC_USER" > /dev/null 2>&1 ; then 
    printInfo "Using existing tomcat4 user"
else
    printInfo "Adding tomcat4 user"
    $USERADD -g $TC_USER -d "/var/tomcat4" -s /bin/false \
        -c "Tomcat4" "$TC_USER" || \
        errorExit "Can't add tomcat4 user"
fi 


#
# Hack /etc/rc.d/init.d on SuSE so that our tomcat4 RPM intended for
# Redhat will install properly.
#
if $PKGCHK suse-release > /dev/null 2>&1 ; then
	# Do some dirty hacks here
        if [ ! -e "/etc/rc.d/init.d" ]; then
		/bin/ln -s /etc/init.d /etc/rc.d/init.d
	fi
fi

if $PKGCHK sles-release > /dev/null 2>&1 ; then
	# Do some dirty hacks here
        if [ ! -e "/etc/rc.d/init.d" ]; then
		/bin/ln -s /etc/init.d /etc/rc.d/init.d
	fi
fi

#
# Add the packages ( skip the package if it already is installed )
#
printInfo "Adding packages"
echo
while read LINE
do
  case "$LINE" in 
      [A-Za-z]*) : ;;
      *) continue ;;
  esac
  
  PKG="`echo $LINE | $AWK -F'|' '{ print $1; }' | sed -e 's/ //g'`"
  if [ "X$PKG" = "X" ] ; then
      continue;
  fi
  
  # get the specific version of the rpm to install
  FILENAME="`cd $PKGDIR; $LS ${PKG}*.rpm | $HEAD -1`"

  PKGNAME="`echo $FILENAME | \
      $AWK -F. '{for(i=1;i<NF-2;i++){printf("%s.",$i);} \
      print $(NF-2)}'`"

#  if [ "X$PKGNAME" != "X" ] ; then
#      if $PKGCHK $PKGNAME > /dev/null 2>&1 ; then
#	  printInfo "Package $PKGNAME is already installed"
#	  continue;
#      fi
#  fi
  
  OPTS="`echo $LINE | $AWK -F'|' '{ print $2; }' | sed -e 's/ //g'`"   
  if [ "X$OPTS" != "X" ] ; then  
      printInfo "Installing $FILENAME with '$OPTS'"
  else
      printInfo "Installing $FILENAME"
  fi
  
  if $PKGADD $OPTS "$PKGDIR/$FILENAME" ; then
      printInfo "Installed $FILENAME" ;
  else 
      errorExit "Couldn't install $FILENAME"
  fi
done < "$PKGLIST"
printInfo "Package addition complete"


#
# Run the postinstall script
#
printInfo "Executing postinstall"
/bin/sh "$POSTINST" "$FACTORY_INST" || errorExit "Postinstall failed"	
printInfo "Postinstall complete"


# Verify there is enough recommended disk space left on the devices.
# If not, warn the user.

  # Recommended Disk Space: ( stuff that grows )
  # /var
  #   /var/log/tomcat4 --> /usr/log/tomcat4
  #   /var/log/mgmt --> /usr/log/mgmt
  #   /var/mgmt ( backups and pkgs )
  #   /var/tomcat4/work
  #   /var/tomcat4/web/pkgs
  #   /var/tmp 

  REC_FREE_SPACE=300000

  # Verify there is enough disk space on /var to run properly
  FREE_SPACE="`$DF -lk /var | $TAIL -1 | $AWK '{print $4}'`"
  if [ "$FREE_SPACE" -lt "$REC_FREE_SPACE" ] ; then
    echo ""
    echo ""
    printWarn "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    printWarn "There does not appear to be enough free disk space on /var for"
    printWarn "the Control Station to function properly.  It is recommended"
    printWarn "you follow the instructions for creating links from various"
    printWarn "directories that contain files that grow as the Control Station"
    printWarn "is used.  You can find these instructions in the faq."
    printWarn ""
    printWarn "found $FREE_SPACE, needed $REC_FREE_SPACE"
    printWarn "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    echo ""
    echo ""
  fi

#
# We made it...notify and exit
#
echo
printInfo "Installation Complete"

exit 0
