#!/bin/bash
# $Header: /cvs/WebShield/wstmp/w100/src/HotFix/SpamUpdate/Attic/actionscript,v 1.1.2.1 2005/04/14 10:28:09 ebaulk Exp $
# Copyright (C) 2005 McAfee Inc. All rights reserved.

if [ -f $NETAWSS/.profile.vars ]; then
 . $NETAWSS/.profile.vars
else
 . /var/NAIENV/.profile.vars
fi

shopt -s nullglob
shopt -s extglob

#*****************************************************
# Definitions
#*****************************************************

# Which hotfix is this?
HFN=Streaming Spam Update Protection 

# this is the directory from where this hotfix will run
PKDIR=$(dirname $0)
[[ "$PKDIR" == /* ]] || PKDIR=$PWD/$PKDIR

# Specify the directory to hold the hotfix progress log
HOTFIXLOGDIR=/logs/hotfixes

# The name of the hotfix progress log is hotfixlog
LOGFILE=$HOTFIXLOGDIR/activatelog

# Specify the general area where hotfix backup directories
# are to be created
HOTFIXDIR=$NETAWSS/hotfixes

# The name of the roll back script is
ROLLBACK=$HOTFIXDIR/uninstallac${HFN}

# Specify where any XML files are to land up
XML_DIR=$NETAWSS/UI/xml


#******************************************************
# Create the directories needed for this install
#******************************************************

# make the log file directory
if [ ! -d $HOTFIXLOGDIR ]; then
    mkdir $HOTFIXLOGDIR
fi

# make the general hotfix backup file directory
if [ ! -d $HOTFIXDIR ]; then
    mkdir $HOTFIXDIR
fi


# status is used to show if the install went ok, it is
# written into the progress log
status="No Status Available"

# retVal is the return value of this install. 0 is good
retVal=0

# Send all the output from this script to the progress log
exec >> ${LOGFILE} 2>&1

# What are we supposed to be doing?
case $0 in
    (?(*/)install*) install=true desc="Install";;
    (?(*/)uninstall*) install=false desc="Uninstall";;
    (*) echo "$0: Neither install nor uninstall - giving up";
	exit 1;;
esac

if $install; then
    # copy this script as the uninstall script
    echo Creating an uninstall script called $ROLLBACK
    cp "$0" $ROLLBACK
    chmod +x $ROLLBACK
fi


#*********************************************************
# MakeBak - This creates a .bak version of the passed file
# and adds the undo lines to the rollback file
# Usage: MakeBak MyDir/Myfile
# This will result in a file called MyDir/Myfile.bak and the
# rollback file will contain the following lines
# ...
# if [ -f MyDir/MyFile.bak ]; then
# mv -f MyDir/MyFile.bak MyDir/MyFile
# fi
# ...
#*********************************************************
MakeBak()
{
    cp -f $1 $1.bak
}

#********************************************************
# ChangeWSxmlFile - This function takes a path to a
# WebShield.xml file and applies changes:
#*********************************************************
pb='/<PropertyBag cname="DenyFeature1"/,/<\/PropertyBag>/'
paf='<Property cname="activex-allow-flash"'
pnf='<Property cname="activex"'
pe='</Property>'
if $install; then
    pold=$pnf pnew=$paf pv=true
    msg='Applying the activex-allow-flash patch to'
else # uninstall
    pnew=$pnf pold=$paf pv=
    msg='Removing the activex-allow-flash patch from'
fi
fgrep -qs "$pold" $NETAWSS/proto/xmlconfdir/WebShield.xml &&
    sed_af="${pb}s%$pold\([[:space:]]*/>\|[^<]*$pe\)%$pnew>$pv$pe%"
ChangeWSxmlFile()
{
    test -f $1 || return 0	# don't bother if it's not there
    [[ -z "$sed_af" ]] && return 0	# nothing to do
    echo "$msg" $1
    if sed -e "$sed_af" $1 > changedfile &&
	    [[ -s changedfile ]] && ! cmp -s changedfile $1; then
	echo Backing up original file
	MakeBak $1
	[[ -s changedfile ]] && cp -f changedfile $1 || retVal=1
	rm -f changedfile
    else
	echo "... failed!"
	retVal=1
    fi
}


#*********************************************************
# ChangeWSxmls - Function to change all the WebShield.xml
# files using ChangeWSxmlFile
#*********************************************************
ChangeWSxmls()
{
    # first change the config directory version
    ChangeWSxmlFile $XMLCONFDIR/WebShield.xml
    # change the original version
    ChangeWSxmlFile $NETAWSS/proto/xmlconfdir/WebShield.xml

    #run through each version contained in /tmp/sessions/sessionid
    for xmlfile in $(find /tmp/sessions -name WebShield.xml)
    do
	ChangeWSxmlFile $xmlfile
    done
}


#**********************************************************
# UpdateHistories - Function to update the WebShieldStatus.xml
# file, to indicate that there has been a hotfix installed
#**********************************************************
$install && perladdfrag=' $lt .= "$frag$date$etext";'
perlprog='
    undef $/; use IO::File; use strict;
    sub Suck { if(my $f=IO::File->new(shift(@_), "r" ) ) { return <$f> } };
    sub Blow { (IO::File->new(shift(@_), "w") or return undef)->print(@_) };

    my ($date, $xml_dir, $pfile, $efile) = splice @ARGV, 0, 4;
    unless (@ARGV > 0) {exit 0};

    my $ptext = Suck $pfile; my $etext = Suck $efile;

    my $WSf = "$xml_dir/WebShieldStatus.xml";
    my $WLf = "$xml_dir/WebShieldStatusLoading.xml";

    # patterns`
    my $epan = qr{[ \t]*\Q</UIPanel>\E(?:\s*?\n)?};
    my $p_hacked = qr{^(.*?\Q$ptext\E)(.*?)($epan.*)}s;
    my $p_nohack = qr{^(.*?\Qcnameref="FullVersion"\E
			.*?\Q</UIRow>\E.*?($epan))(.*)}sx;
    my ($st, $lt, $ft);	# start, list, and finish sections

    # read and split WebShieldStatus.xml
    my $xt = Suck($WSf) or die "read error $WSf: $!\n";
    if ($xt =~ $p_hacked) { ($st, $lt, $ft) = ( $1, $2, $3 ) }
    elsif ($xt =~ $p_nohack) { $st=$1.$ptext; $ft=$2.$3; $lt="" }
    else { die "parse error $WSf\n" };

    # edit the hotfix list
    while (my $frag = <>) {
	next if ($frag eq "");
	$frag =~ s/\n$//s;
	$lt =~ s/\Q$frag\E.*?\Q$etext\E//gs;'"$perladdfrag"'
    }
    # write out WebShieldStatus.xml
    Blow $WSf, $st, $lt, $ft or die "write error $WSf: $!\n";

    # edit list into WebShieldStatusLoading.xml
    if ( -f $WLf ) {
	$xt = Suck($WLf) or die "read error $WLf: $!\n";
	if ($xt =~ $p_hacked) { ($st, $ft) = ( $1, $3 ) }
	elsif ($xt =~ $p_nohack) { $st=$1.$ptext; $ft=$2.$3 }
	else { die "parse error $WSf\n" };
	Blow $WLf, $st, $lt, $ft or die "write error $WLf: $!\n";
    }
'
UpdateHistories()
{
    for pfile in $PKDIR/*/WebShieldStatusPrefixXML; do
	local langdir=${pfile%/WebShieldStatusPrefixXML}
	local xml_dir=$XML_DIR/${langdir##*/}
	local efile=$langdir/WebShieldStatusFragmentEndXML
	[[ -f "$efile" && -f "$xml_dir/WebShieldStatus.xml" ]] || continue
	perl -we "$perlprog" "$(date)" "$xml_dir" "$pfile" "$efile" \
	    $langdir/@(WebShieldStatusFragmentXML)
	rm -f $xml_dir/WebShieldStatusInfixXML \
	    $xml_dir/WebShieldStatusPrefixXML	# we don't use these
    done
}



#***********************************************************
# DoFile - Function to make a backup of a file and install a
# new version
#***********************************************************
DoFile()
{
    local msg=$1
    local old=$2
    local new=$3
    # check we've got a change
    [[ ! -f "$new" ]] ||
	cmp -s "$old" "$new" && echo "No change to $msg" && return 0
    # Copy new file
    echo "Installing the new $msg" &&
    cp -f "$new" "$old" && return 0
    # oops
    retVal=1
}

#**************************************************
# Main part of the script
#**************************************************

echo -e "\n------- Start $(date)"
echo ${desc}ing Activator ${HFN}

# install the crontab
if $install; then
DoFile "spam cron file" \
	/etc/cron.d/spam_uricf_upd.cron $PKDIR/spam_uricf_upd.cron
chmod 0555 /etc/cron.d/spam_uricf_upd.cron
touch /etc/cron.d
else
# remove the crontab
rm -f /etc/cron.d/spam_uricf_upd.cron
fi


# Update Hotfix History
UpdateHistories
status="${desc}ed Successfully"

echo "Finished ${desc} of Activator ${HFN}. $status"
echo -e "------- End $(date)\n"
exit $retVal
