#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/ws_upgrade_sw,v 1.5.2.2 2005/01/13 13:44:48 bwhittak Exp $
#
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
# Author :	Simon Crowe
#
# Apply a Webshield configuration update
#
# Arg1: filename
# Arg2: if present, indicates the file was read off a CD
#
if [ -f $NETAWSS/.profile.vars ]; then
 . $NETAWSS/.profile.vars
else
 . /var/NAIENV/.profile.vars
fi
PATH=$wsPATH:$PATH
SWDIR=/tmp/swupgd
LOGFILE=/tmp/upgrade.log
UPGRADEFILENAME=`/bin/basename $1`
ONCD=$2

Report()
{
    /usr/bin/logger -t ws_upgrade "$@"
}

leave()
{
    /bin/echo "$1" >> ${LOGFILE}
    if [ $2 != 0 ]
    then
        /usr/bin/logger	-t ws_upgrade "WebShield update from ${UPGRADEFILENAME} failed - ${1}"
    else
        /usr/bin/logger -t ws_upgrade	"WebShield update from ${UPGRADEFILENAME} OK"
    fi
    cd /
    /bin/rm -rf $SWDIR
    exit $2
}

unpack_upgd()
{

    /bin/rm -rf ${SWDIR}
    /bin/mkdir ${SWDIR}
    if [ $? != 0 ]
    then
        # Could not create temporary directory
        leave "Could no create temporary directory $SWDIR" 1
    fi
    TGZ_FILE=$1

    if [ -z $1 ]
    then
        # No Name specified
        leave "No file specified" 2
    fi

    if [ ! -r $1 ]
    then
        #
        leave "$1 not found" 3
    fi


    cd $SWDIR
    if [ $? != 0 ]
    then
        # Could not go to the temporary directory
        leave "Could not go into temp dir" 4
    fi

    if unzip -l >/dev/null 2>&1 $TGZ_FILE
    then
          /usr/bin/unzip -L ${TGZ_FILE} > /dev/null 2>&1
          RET=$?
    else
          /bin/tar xvzf $TGZ_FILE > /dev/null 2>&1
          RET=$?
    fi
    if [ $RET != 0 ]
    then
        # Error unpacking the CD file
        /bin/rm $TGZ_FILE
        leave "Error unpacking $TGZ_FILE" 5
    fi
    cd /
    /bin/rm $TGZ_FILE
}

installsw()
{
    if [ -r ${1}/previous ]
    then
         VS=`/bin/cat ${1}/previous`
         if [ "$VS" != "${SYSTEM_VERSION}" ]
         then
             echo $VS not installed >> /tmp/upgrade.log
             leave "${VS} not installed" 6
         fi
    fi
	if [ -r ${1}/cdrequired ]
	then
		if [ "$ONCD" == "" ]
		then
			leave "Must install from CD" 10
		fi
	fi
    if [ -r ${1}/script ]
    then
        WSPKNAME=`/bin/basename ${1}`
        /usr/bin/logger -t ws_upgrade	"WebShield update from ${UPGRADEFILENAME} running - upgrade ${WSPKNAME}"
        while read ACTION FILENAME
        do
            case ${ACTION} in
                remove)
                      /usr/bin/logger -t ws_upgrade	"WebShield update ${UPGRADEFILENAME}:${WSPKNAME} removing rpm - ${FILENAME}"
                      /bin/rpm -e ${FILENAME} >> ${LOGFILE} 2>&1
                      if [ $? != 0 ]
                      then
                            leave "Removal of $FILENAME failed" 9
                      fi
                      ;;
                 run)
                      NAME=`/bin/basename ${FILENAME}`
                      /usr/bin/logger -t ws_upgrade	"WebShield update ${UPGRADEFILENAME}:${WSPKNAME} running - ${NAME}"
                      ${1}/${NAME} >> ${LOGFILE} 2>&1
                      if [ $? != 0 ]
                      then
                            leave "${NAME} failed " 7
                      fi
                     ;;
                 install)
                      OPT=ihv
                      PKG=`/bin/echo $FILENAME | /usr/bin/cut -f1 -d"-"`
                      /usr/bin/logger -t ws_upgrade	"WebShield update ${UPGRADEFILENAME}:${WSPKNAME} installing rpm - ${PKG}"
                      /bin/rpm -qv $PKG > /dev/null  2>&1
                      if [ $? = 0 ]
                      then
                          OPT=Fhv
                      fi
                      /bin/rpm -${OPT} ${1}/${FILENAME}  >>${LOGFILE}
                      if [ $? != 0 ]
                      then
                            leave "Installation of ${FILENAME} failed " 8
                     fi
                     ;;
              esac
        /usr/bin/logger -t ws_upgrade	"WebShield update from ${UPGRADEFILENAME} finished - upgrade ${WSPKNAME}"
        done < ${1}/script
    else	# no script - this isn't right
	return 1
    fi
}

version_ok()
{	# check versions
    local sysver=$1 verfile=$2
    # empty system version string or no file - we pass
    [[ -n $sysver && -f $verfile ]] || return 0
    for v in $(<$verfile); do
	[[ $v == $sysver ]] && return 0	# matched version - pass
    done
    return 1	# nothing matched - fail
}

install_upgrades()
{
    local -i doneright=0
    # Get the software version from the System
    if [ -f /.version ]
    then
         SYSTEM_VERSION=`/bin/cat /.version`
    else
         SYSTEM_VERSION=000000
         /bin/echo 000000 > /.version
    fi

    for UPGRADE in ${SWDIR}/*
    do
         if [ "${UPGRADE}" != "${SWDIR}/*" ]
         then
	      # check applicable to this system or skip
	      [[ -f $UPGRADE/cdrequired ]] &&
		version_ok 001001 $UPGRADE/version ||
		  version_ok $SYSTEM_VERSION $UPGRADE/version || continue

              # Install the software
              installsw ${UPGRADE} && (( ++doneright ))
              /bin/basename ${UPGRADE} >> /.upgrade_history
         fi
    done
    [[ $doneright -gt 0 ]]
    return
}

if [ "$0" != "/tmp/ws_upgrade" ]
then
     /bin/cp -f $0 /tmp/ws_upgrade > /dev/null 2>&1
     /bin/chmod +x /tmp/ws_upgrade > /dev/null 2>&1
     /tmp/ws_upgrade $1 $2
     ECODE=$?
     cd /
     /bin/rm -rf /tmp/ws_upgrade ${SWDIR}
     exit $ECODE
fi
# Unpack the  file
unpack_upgd $1

#install the sofware
install_upgrades || leave "Nothing to install!" 11

/usr/bin/logger -t ws_upgrade	"WebShield update from ${UPGRADEFILENAME} OK"
exit 0

