#!/bin/bash
# $Id: restore_config,v 1.1.4.2 2004/10/20 16:16:31 bwhittak Exp $
#
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
# Restore config from a zip file, converting from 2.x if necessary
# $1 - input zip file
# $2 - optional base directory for restoring config files (instead of /)

if [ -f $NETAWSS/.profile.vars ]; then
    . $NETAWSS/.profile.vars
else
    . /var/NAIENV/.profile.vars
fi
[[ "$PATH" == "$wsPATH":* || -z "$wsPATH" ]] || PATH=$wsPATH:$PATH

shopt -s extglob
shopt -s nullglob

# input parameters
inzip=$1	# input zip
outdir=$2	# output directory for new config files
[[ -s $inzip ]] || exit
[[ -z $outdir || $outdir == /* ]] || outdir=$PWD/$outdir

# temporary directory
tmpdir=/tmp/${0##*/}.$(date -u '+%Y%m%d%H%M%S').$$
trap '[[ -d "$tmpdir" ]] && rm -rf "$tmpdir"' EXIT

# unpack the zip
rm -rf "$tmpdir"
unzip -q -d "$tmpdir" "$inzip" || exit

# v2.x config to convert?
if [[ ! -d $tmpdir/config && -f $tmpdir/WebShield.xml ]]; then
    mkdir -p $tmpdir/config
    update_config "$tmpdir" "$tmpdir/config"
fi

# copy out the new config leaving out the spam activation files
outconf=$outdir$XMLCONFDIR
[[ -d $outconf ]] || mkdir -p "$outconf" &&
    [[ -d "$tmpdir/config" ]] || exit
rm -f \
    "$tmpdir/config/spamconfig/antispam-eval.log" \
    "$tmpdir/config/spamconfig/wsui_spam_activation" \
    "$tmpdir/config/spamconfig/wsui_spam_evaluation" \
    "$tmpdir/config/spamconfig/wsui_spam_can_evaluate"
# apply v3 hotfix updates
for f in "$tmpdir/config/"*.xml; do
    xml-replace $WSMGMT/v3update.xml "$f" "$f" || exit
done
cp -a "$tmpdir/config/." "$outconf" || exit
# copy zebra config
mkdir -p "$outdir/etc/zebra"
[[ ! -d $tmpdir/zebra ]] || cp -a "$tmpdir/zebra/." "$outdir/etc/zebra" || exit

# apply config to system if restoring to main config directory
[[ $XMLCONFDIR -ef $outconf ]] && exec update-xmlconf

exit 0
