#!/bin/sh
# $Id: preinstall,v 1.1.2.10 2005/09/09 08:14:37 rs131644 Exp $
# preinstall for scs

#
# define variables
#
FALSE="false"
TRUE="true"
BASEDIR="`dirname $0`/.."
PKGDIR=$BASEDIR/pkgs

GREP=/bin/grep
EGREP=/bin/egrep
UNAME=/bin/uname
DF=/bin/df
RPM=/bin/rpm
PERL=/usr/bin/perl
PING=/bin/ping
AWK=/bin/awk
TAIL=/usr/bin/tail

#
# fuctions
#

logError() {
  MSG=$@
  if [ "X$MSG" != "X" ] ; then
    ERROR_MSGS="$ERROR_MSGS\n $MSG" 
  fi
}

logWarn() {
  MSG=$@
  if [ "X$MSG" != "X" ] ; then
    WARN_MSGS="$WARN_MSGS\n $MSG" 
  fi
}
 
printInfo() {
    echo "Info: $@"
}

doesRpmExist() {
  RPM_STR=$@
  EXISTS="$FALSE"
  RETURN=`$RPM -qa | $EGREP -i "^$RPM_STR"`
  if [ "X$RETURN" != "X" ] ; then
    EXISTS="$TRUE"
  fi
  echo "$EXISTS"
}

doesRpmNameExist() {
  RPM_STR=$@
  EXISTS="$FALSE"
  RETURN=`$RPM -qa | $GREP -i "$RPM_STR"`
  if [ "X$RETURN" != "X" ] ; then
    EXISTS="$TRUE"
  fi
  echo "$EXISTS"
}

doesPerlModuleExist() {
  MOD_STR=$@
  EXISTS="$FALSE"
  RETURN=`$PERL -e "use $MOD_STR; exit 0" >/dev/null 2>&1`
  if [ $? = 0 ] ; then
    EXISTS="$TRUE"
  fi
  echo "$EXISTS"
}

# TODO: is port free (netstat -a)
isPortFree() {
  PORT=$@
  echo "$TRUE"
}

checkSCS() {
  if [ "$FORCE_INSTALL" = "$TRUE" ] ; then
    printInfo "Installation forced.  Bypassing check for existing installation."
    return
  fi

  printInfo "Checking for previous installation of Control Station"

  ETCBUILD="/etc/build"
  SCSETCBUILD="/scs/etc/build"

  for location in $ETCBUILD $SCSETCBUILD
  do
    if [ -f $location ] ; then
        SCSVERS=`$AWK '{print $2}' $location`
        SCSMAJOR=`echo $SCSVERS | $AWK 'BEGIN{FS="."}{print $1}'`
        SCSMINOR=`echo $SCSVERS | $AWK 'BEGIN{FS="."}{print $2}'`
        SCSBUGFIX=`echo $SCSVERS | $AWK 'BEGIN{FS="."}{print $3}'`
        SCSBUILD=`echo $SCSVERS | $AWK 'BEGIN{FS="."}{print $4}'`
        printInfo "Found version $SCSVERS installed"

        if [ "$SCSMAJOR.$SCSMINOR" = "2.2" ] ; then
            if [ "$SCSBUGFIX" = "1" ] && [ "$SCSBUILD" -gt 166 ] ; then
                # tell the main installer to update
                RC=3

                logWarn " - Version $SCSVERS of Sun Control Station is already installed"
                logWarn "   Upgrading this existing installation."
            else
                logError " - Version $SCSVERS of Sun Control Station currently installed on"
                logError "  this system is incompatible with this patch."
                logError "  Requires Sun Control Station release 2.2.1.166."
            fi
        else
            logError "- Version $SCSVERS of Sun Control Station currently installed on"
            logError "  this system is incompatible with this patch."
            logError "  Requires Sun Control Station release 2.2.1.166."
        fi
        break
    fi
  done
  if [ "X$SCSVERS" = "X" ]; then
        logError "No Sun Control Station 2.2.1 GA (build 166) installation found. The patch cannot be applied."
  fi
}

checkOS() {
  printInfo "Validating OS"

  OS_OK=$FALSE
  BUILD=""
  OS=""

  # Make sure we have the rpm command
  if [ -x $RPM ] ; then

    # look for the vanilla rh
    if $RPM -q redhat-release > /dev/null 2>&1 ; then 
      RHREL="`$RPM -q redhat-release 2>/dev/null`"
      BUILD="`echo $RHREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
      OS="redhat"
    fi

    if $RPM -q fedora-release > /dev/null 2>&1 ; then 
      RHREL="`$RPM -q fedora-release 2>/dev/null`"
      BUILD="`echo $RHREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
      OS="fedora"
    fi

    # look for non-vanilla versions
    if [ "X$BUILD" = "X" ] ; then
      for i in ws es as 
      do
        if $RPM -q redhat-release-$i > /dev/null 2>&1 ; then
	  RHREL="`$RPM -q redhat-release-$i 2> /dev/null`"
          BUILD="`echo $RHREL | sed -e 's/\-/ /g' | $AWK '{ print $4; }'`"
          OS="redhat"
	  if [ X"$BUILD" != "X" ] ; then
            break;
 	  fi 
        fi
      done
    fi

    # check the suse builds
    if $RPM -q suse-release > /dev/null 2>&1 ; then 
      SUSEREL="`$RPM -q suse-release 2>/dev/null`"
      BUILD="`echo $SUSEREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
      OS="suse"
    fi

    # check the sles builds
    if $RPM -q sles-release > /dev/null 2>&1 ; then 
      SUSEREL="`$RPM -q sles-release 2>/dev/null`"
      BUILD="`echo $SUSEREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
      OS="sles"
    fi
  fi


  # Test for valid BUILD version
  if [ $OS = "redhat" ]; then
    case "$BUILD" in
      2.1[AE]S|8.0|3[AE]S)
        OS_OK=$TRUE
        ;;
      *)
        OS_OK=$FALSE
        ;;
    esac
  elif [ $OS = "fedora" ]; then
    case "$BUILD" in
      1)
        OS_OK=$TRUE
        ;;
      *)
        OS_OK=$FALSE
        ;;
    esac
  elif [ $OS = "suse" ]; then
    case "$BUILD" in
      9.0|9.1|9.2)
        OS_OK=$TRUE
        ;;
      *)
        OS_OK=$FALSE
        ;;
    esac
  elif [ $OS = "sles" ]; then
    case "$BUILD" in
      9)
	OS_OK=$TRUE
        ;;
      *)
        OS_OK=$FALSE
        ;;
    esac
  fi
  if [ "$OS_OK" != "$TRUE" ] ; then
    logError " - Unsupported operating system. (found: $BUILD)";
  fi
}

checkArch() {
  printInfo "Validating architecture"

  ARCH_FLAG="$FALSE"

  # get the architecture 
  ARCH="`$UNAME -m`"

  # Test the arch
  case "$ARCH" in
    i[3-6]86|x86_64) ARCH_FLAG="$TRUE"
  esac
  
  if [ "X$ARCH" = "X" ] ; then
    logError " - Can't determine Architecture" 
  fi

  if [ "$ARCH_FLAG" != "$TRUE" ] ; then     
    logError " - Incompatible arch $ARCH (i[3-6]86 or x86_64 needed)"
    return
  fi
}

checkPerl() {
  printInfo "Validating version of Perl installed"

  PERL_RPM_STR="perl-5.6"
  PERL_MODULES="CGI Digest::MD5"

  # check for perl 5.6 or 5.8 (RH EL 3)

  RETURN=`doesRpmExist "$PERL_RPM_STR"`
  if [ "$RETURN" = "$FALSE" ] ; then 
     PERL_RPM_STR="perl-5.8"
     RETURN=`doesRpmExist "$PERL_RPM_STR"`
     if [ "$RETURN" = "$FALSE" ] ; then 
	logError " - $PERL_RPM_STR.x is not installed."
	return
     fi
     # Digest-MD5 seems to be a part of perl 5.8.x
     PERL_DIGEST=
  fi

  # Check that the binary is in the correct directory
  if [ ! -x "$PERL" ] ; then
    logError " - Perl not found. (expected $PERL)"
    return
  fi

  # Check for perl modules
  for mod in $PERL_MODULES ; do
    RETURN=`doesPerlModuleExist "$mod"`
    if [ "$RETURN" = "$FALSE" ] ; then
      logError " - Perl module $mod is not installed."
      return
    fi
  done
}

checkAllstart() {    
    printInfo "Validating versions of tftp, dhcpd and nfs"
    case "$OS" in
        redhat|fedora)
            ALLSTART_RPMS="tftp-server dhcp nfs-utils anaconda-runtime"
            ;;
        suse|sles)
            ALLSTART_RPMS="tftp dhcp nfs-utils" 
            ;;
    esac
    
    for ALLSTART_RPM in $ALLSTART_RPMS
    do
	RETURN=`doesRpmExist "$ALLSTART_RPM"`
	if [ "$RETURN" = "$FALSE" ] ; then
	    logError " - $ALLSTART_RPM is not installed."
	fi	
    done
	# Check for dhcp version 3
	`$RPM -q --qf "%{version}" dhcp | $EGREP -q ^3`
	if [ $? != 0 ]; then
		logError " - Allstart requires dhcp version 3 or later"
		logError " - For RedHat rpms http://dag.wieers.com/packages/dhcp/"
	fi
}

checkApache() {
  printInfo "Validating version of Apache installed"

  APACHE_LIB="/usr/lib/apache"
  APACHE_RPM="apache-1.3"
  APACHE_BIN="/usr/sbin/httpd"
  APCHE_PORT="80"

  case "$OS" in
    redhat)
      case "$BUILD" in
        8.0|3[AE]S)
          APACHE_LIB="/usr/lib/httpd"
          if [ X"$ARCH" = "Xx86_64" ] ; then
            APACHE_LIB="/usr/lib64/httpd"
          fi
          APACHE_RPM="httpd-2.0"
          ;;
      esac
      ;;
    fedora)
      case "$BUILD" in
        1)
          APACHE_LIB="/usr/lib/httpd"
          if [ X"$ARCH" = "Xx86_64" ] ; then
            APACHE_LIB="/usr/lib64/httpd"
          fi
          APACHE_RPM="httpd-2.0"
          ;;
      esac
      ;;
    suse)
      APACHE_LIB="/usr/lib/apache2-prefork"
      APACHE_BIN="/usr/sbin/httpd2"
      APACHE_RPM="apache2-prefork"
      if [ X"$ARCH" = "Xx86_64" ] ; then
         APACHE_LIB="/usr/lib64/apache2-prefork"
      fi
      ;;
    sles)
      APACHE_LIB="/usr/lib/apache2-prefork"
      APACHE_BIN="/usr/sbin/httpd2"
      APACHE_RPM="apache2-prefork"
      if [ X"$ARCH" = "Xx86_64" ] ; then
         APACHE_LIB="/usr/lib64/apache2-prefork"
      fi
      ;;
  esac

  # Check for a matching apache rpm
  RETURN=`doesRpmExist "$APACHE_RPM"`
  if [ "$RETURN" = "$FALSE" ]; then
    logError " - $APACHE_RPM.x is not installed."
    return
  fi

  # Check that the binary is in the correct directory
  if [ ! -x "$APACHE_BIN" ] ; then
    logError " - Apache not found. (expected $APACHE_BIN)"
    return
  fi

  # Check to see that the lib directory is found
  if [ ! -d "$APACHE_LIB" ] ; then
    logError " - Apache not found: looking for lib dir in $APACHE_LIB."
    return
  fi

  # Verify the port is free
  RETURN=`isPortFree "$APACHE_PORT"`
  if [ "$RETURN" = "$FALSE" ] ; then
    logError " - $APACHE_PORT is not free for Sun Control Station use."
  fi

  # Verify loopback ( 127.0.0.1 localhost ) is set up correctly.
  $PING -w 3 -c 1 -q 127.0.0.1 > /dev/null
  if [ "$?" != "0" ]; then
    logError " - /etc/hosts is misconfigured.  Could not ping loopback (127.0.0.1)."
    return
  fi
  $PING -w 3 -c 1 -q localhost > /dev/null
  if [ "$?" != "0" ] ; then
    logError " - /etc/hosts is misconfigured.  Could not ping localhost."
    return
  fi
}

checkDB() {
  printInfo "Validating version of DB (PostgreSQL/Perl) installed"

  SQLDB_BIN="/usr/bin/postmaster"
  SQLDB_RPM="postgresql-7"
  SQLDB_SERVER_RPM="postgresql-server-7"
  SQLDB_CLIENT_RPM="postgresql-libs-7"
  SQLDB_PERL_MODULES="DBI DBD::Pg"

  for i in $SQLDB_RPM $SQLDB_SERVER_RPM $SQLDB_CLIENT_RPM ; do
    # Check for a matching database rpms
    RETURN=`doesRpmNameExist "$i"`
    if [ "$RETURN" = "$FALSE" ] ; then
      logError " - $i.x is not installed."
      return
    fi
  done

  for mod in $SQLDB_PERL_MODULES ; do
    RETURN=`doesPerlModuleExist "$mod"`
    if [ "$RETURN" = "$FALSE" ] ; then
      logError " - Perl module $mod is not installed."
      return
    fi
  done

  # Check that the binary is in the correct directory
  if [ ! -f "$SQLDB_BIN" ] ; then
    logError " - SQL Server not found. (expected $SQLDB_BIN)"
    return
  fi
}

checkOpenSSH() {
  printInfo "Validating version of OpenSSH installed"

  OPEN_SSH_RPM_STR="openssh-3"

  # Check for matching openSSH rpm
  RETURN=`doesRpmExist "$OPEN_SSH_RPM_STR"`
  if [ "$RETURN" = "$FALSE" ] ; then
    logError " - $OPEN_SSH_RPM_STR.x is not installed."
    return
  fi
}

checkTomcat() {
  printInfo "Checking to see if Tomcat is installed"

  TOMCAT_RPM_STR="tomcat4"
  TOMCAT_FLAG="$FALSE"
  TOMCAT_DIR="/var/tomcat4"
  TOMCAT_PORT="8080"

  # Check for matching tomcat rpm
  RETURN=`doesRpmExist "$TOMCAT_RPM_STR"`
  if [ "$RETURN" = "$TRUE" ] ; then
    TOMCAT_FLAG="$TRUE"
  fi

  # Verify it is installed in the correct location
  if [ "$TOMCAT_FLAG" = "$TRUE" ] && [ ! -d "$TOMCAT_DIR" ] ; then
    logError " - Tomcat is not installed in the expected location. (expected $TOMCAT_DIR)"
    return
  fi

  # Verify the port is free
  if [ "$TOMCAT_FLAG" = "$TRUE" ] ; then
    RETURN=`isPortFree "$TOMCAT_PORT"`
    if [ "$RETURN" = "$FALSE" ] ; then
      logError " - port $TOMCAT_PORT is not available for use by Tomcat."
    fi
  fi
}

checkJDK() {
  printInfo "Checking to see if Java is installed"

  JDK_RPM_STR="j2sdk-1.4.2_03"
  JDK_FLAG="$FALSE"
  JDK_DIR="/usr/java/j2sdk1.4.2_03"

  # Check for matching java rpm
  RETURN=`doesRpmExist "$JDK_RPM_STR"`
  if [ "$RETURN" = "$TRUE" ] ; then
    JDK_FLAG="$TRUE"
  fi

  # Verify it is installed in the correct location
  if [ "$JDK_FLAG" = "$TRUE" ] && [ ! -d "$JDK_DIR" ] ; then
    logError " - Java is not installed in the expected location. (expected $JDK_DIR)"
    return
  fi
}

checkDiskSpace() {

  printInfo "Checking required disk space"

  #
  # Minimum Disk Space Requirement:
  #
  #  /usr 200M ( 120M + 80M buffer )
  #    /usr/mgmt 26688
  #    /usr/java 85924
  #
  #  /var 100M ( 30M + 70M tmp space for mapps )
  #    /var/tomcat4 14004
  #    /var/mgmt    14552
  #
  # TODO: what about tmp?
  #  /tmp ( mapp dir ? )

  # Define out disk space needed on /usr
  if [ "$JDK_FLAG" = "$FALSE" ] ; then
    USR_FREE_SPACE=200000
  else
    USR_FREE_SPACE=120000
  fi

  # Define out disk space needed on /var
  if [ "$TOMCAT_FLAG" = "$FALSE" ] ; then
    VAR_FREE_SPACE=100000
  else
    VAR_FREE_SPACE=85000
  fi
  
  # Verify there is enough disk space on /usr for the install
  FREE_SPACE="`$DF -lk /usr | $TAIL -1 | $AWK '{print $4}'`"
  if [ "$FREE_SPACE" -lt "$USR_FREE_SPACE" ] ; then
    logError " - There is not enough space available on /usr to install. ( needed $USR_FREE_SPACE, found $FREE_SPACE )"
  fi

  # Verify there is enough disk space on /var for the install
  FREE_SPACE="`$DF -lk /var | $TAIL -1 | $AWK '{print $4}'`"
  if [ "$FREE_SPACE" -lt "$VAR_FREE_SPACE" ] ; then
    logError " - There is not enough space available on /var to install. ( needed $VAR_FREE_SPACE, found $FREE_SPACE )"
  fi
}


#
# Main
#

RC=0

#
# Call the initial checks
#
ERROR_MSGS=""
WARN_MSGS=""

checkSCS
checkOS
checkArch
checkPerl
checkAllstart
checkApache
checkDB
checkOpenSSH
checkTomcat
checkJDK
checkDiskSpace

#
# If there are warnings, display them
#
if [ "X$WARN_MSGS" != "X" ] ; then
  echo ""
  echo "Warning: the following issues were found:"
  echo -e "$WARN_MSGS"
  echo ""
fi

#
# If there are errors, display them and fail
#
if [ "X$ERROR_MSGS" != "X" ] ; then
  echo ""
  echo "Installation failed for the following reasons:"
  echo -e "$ERROR_MSGS"
  echo ""
  exit 1
fi

exit $RC
