#!/bin/bash
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.

shopt -s extglob	# extended globs rule
shopt -s dotglob	# no hiding place
shopt -s nullglob	# no match no token
umask 022		# reasonable umask

# cd to the directory where this script lives
me=${0##*/}
mydir=${0%$me}
[[ -n $mydir ]] && cd "$mydir" || exit

# hotfix name should be the name of this directory
hf=${PWD##*/}

. /var/NAIENV/.profile.vars
PATH=$wsPATH:$PATH

# constants
hf_logs=/logs/hotfixes
hf_dir=$NETAWSS/hotfixes
hf_about=$hf_dir/about

# output to log file
mkdir -p $hf_logs
exec >>$hf_logs/hotfixlog 2>&1
echo -e "\n------- Start $(date)"

# function for progress output
ReportMessage() { :; }	# no-op by default, but source from file if possible...
if [[ -f message_functions.bash ]] && . message_functions.bash; then :
else
    [[ -f $WS_MGMT/message_functions.bash ]] &&
	. $WS_MGMT/message_functions.bash
fi

###############################################################################
# Check for prerequisites
###############################################################################
missing=''
[[ -s prerequisites ]] && for f in $(<prerequisites); do
    x=( $hf_about/$f-+([^.-]).+([^.-]) )
    [[ ${#x[@]} -gt 0 ]] || missing="$missing $f"
done
if [[ -n $missing ]]; then
    echo $'\n---------------------------------------------------------'
    echo "Hotfix $hf $(date) missing prerequisites$missing"
    ReportMessage MSG_prerequisite "${missing# }"
    sleep 10
    exit 1
fi

###############################################################################
# Start for real
###############################################################################

echo $'\n---------------------------------------------------------'
echo "Installing Hotfix $hf $(date)"
ReportMessage MSG_install_hotfix "$hf"

# update grub.conf if on e250
[[ -s /.ws_product_name ]] &&
    . /.ws_product_name &&
    if [[ $WS_PRODUCT_NAME == e250 ]]; then
	mount -o remount,rw /boot &&
	perl -pi -e 's/\bapm=\S+/apm=off/' /boot/grub/grub.conf
    fi
error=$?
mount -o remount,ro /boot

if [[ $error -eq 0 ]]; then
    # update about box
    echo "Updating about box"
    mkdir -p $hf_about && :> $hf_about/$hf
    $WSMGMT/abouthotfixes
    ReportMessage MSG_hotfix_success "$hf"
else
    ReportMessage MSG_hotfix_failed "$hf"
fi

# all done
echo -e "\nCompleted Hotfix $hf $(date) error=$error"
sleep 5
exit $error
