#!/bin/bash
# $Id: update-xmlconf,v 1.30.2.2 2004/09/23 09:01:18 bwhittak Exp $
#
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
# Apply updated XML configuration files and extra config files zip
#
if [ -f $NETAWSS/.profile.vars ]; then
 . $NETAWSS/.profile.vars
else
 . /var/NAIENV/.profile.vars
fi
PATH=$wsPATH:$PATH
SIGFILE=${XMLCONFDIR}/.config_signatures
shopt -s extglob

declare -r me=${0##*/}
declare -r stamp="$(date -u '+%Y%m%d%H%M%S').$$"
declare -rx AUDIT_ID="$me.$stamp"

Event()		# report an event
{
    evrep -n $me -i AUDIT_ID -v $AUDIT_ID "$@" \
	${WS_SESSION:+-i UI_SESSION_ID -v "$WS_SESSION"} \
	${WS_USER_ID:+-i USER_NAME -v "$WS_USER_ID"} \
	${WS_SOURCE_IP:+-i SOURCE_IP -v "$WS_SOURCE_IP"}
}

ReportProgress ()
{
    # arg1 should be _TR_, arg2 is message id (not used)
    # arg3 is format string (localised) for remaining args
    [[ $# -gt 2 ]] || return 0	# skip if <3 args
    [[ -w /proc/$$/fd/3 ]] || return 0	# skip if no &3
    shift 2
    local f="$1"; shift
    printf "$f\n" "$@" >&3 || exec 3>&-
}

# process options
while [[ $# -gt 0 ]]; do
    case $1 in
    (--zip) file_zip=$2; shift 2;;	# extra zip file
    (*) echo "Unrecognised argument: $1" >&2; shift;;	# ignore unrecognised
    esac
done

tx_changed()
{
    OLDSIG=`grep transparency ${SIGFILE}`
    NEWSIG=`ws_txconfig`
    if [ "${NEWSIG}" !=  "${OLDSIG}" ]
    then
              return 0
    fi
    return 1
}
config_changed()
{
   test -f $SIGFILE
   if [ $? != 0 ]
   then
        return 0
   fi
   for f in ${*}
   do
	CF=${XMLCONFDIR}/${f}
	OLDSIG=`grep $CF ${SIGFILE}`
	NEWSIG=`/usr/bin/md5sum ${CF}`
        if [ "${NEWSIG}" !=  "${OLDSIG}" ]
        then
              return 0
        fi
   done
   return 1
}
nicspeed_changed()
{
  oldsum=`/usr/bin/md5sum /etc/modules.conf | cut -f1 -d" " `
  ${WSMGMT}/systemservices.d/60_nicspeed start
  newsum=`/usr/bin/md5sum /etc/modules.conf | cut -f1 -d" " `
  if [ "${oldsum}" != "${newsum}" ]
  then
      return 1
  fi
  return 0
}
find_restart_type()
{
   # restore optional zip
   RESTART_TYPE=
   if [[ -s $file_zip ]]
   then
        nicspeed_changed
	if [ $? != 0 ]
        then
            RESTART_TYPE=reboot
        else
            RESTART_TYPE=full
        fi
        return
   fi
   config_changed machine.xml system.xml network.xml
   if [ $? = 0 ]
   then
        nicspeed_changed
	if [ $? != 0 ]
        then
            RESTART_TYPE=reboot
        else
            RESTART_TYPE=full
        fi
        return
   fi

   tx_changed
   if [ $? = 0 ]
   then
        RESTART_TYPE=transparent
        return
   fi

   config_changed ftp.xml ftp-logging.xml http.xml http-logging.xml pop3.xml pop3-logging.xml smtp.xml smtp-logging.xml identities.xml ls.xml  
   if [ $? = 0 ]
   then
        RESTART_TYPE=proxy
   fi

   config_changed channels.xml
   if [ $? = 0 ]
   then
        RESTART_TYPE="${RESTART_TYPE} evthandler"
        return
   fi
}
full_restart()
{
    # restore optional zip
    if [[ -s $file_zip ]]; then
        unzip -o -d / "$file_zip" 'etc/zebra/*'
    fi

    # apply the config
    export WS_RESTART_LATER=/tmp/${0##*/}.restart_list.$$
    : >$WS_RESTART_LATER
    /etc/init.d/ws-appliance-services stop
    /etc/init.d/ws-appliance-sys-services reload
    /etc/init.d/ws-appliance-services start
    if [[ -s $WS_RESTART_LATER ]]; then
	(	# deferred restarts in background
	    sleep 5	# wait a second
	    while read f; do
		logger -t ${0##*/} restarting $f
		$f restart
	    done
	) <$WS_RESTART_LATER >/dev/null 2>&1 3<&- &
    fi
    rm -f $WS_RESTART_LATER
}
find_restart_type

for RESTART in ${RESTART_TYPE}
do

   logger -t update-xmlconf Restarting level ${RESTART}

   # See how we were called.
   case "$RESTART" in
      (reboot)
            /usr/bin/reboot
    	;;
      (full)
            full_restart
    	;;
      (transparent)
            /etc/init.d/ws-appliance-services stop
            /etc/init.d/ws-appliance-services start
    	;;

      (proxy)
    	${WSMGMT}/webshieldservices.d/70_proxies reload
    	;;

      (evthandler)
    	${WSMGMT}/webshieldservices.d/10_logging reload
    	;;
    esac
done
${WSMGMT}/webshieldservices.d/99_config  restart

Event -e APPLY_XMLCONF_END
logger -t update-xmlconf applied user configuration
ReportProgress _TR_ "upxml-end" $"Finished applying new configuration"

exit 0
