#!/bin/sh
# Upgrade script from Zeus 3.1.x to Zeus 3.3.x
# Will migrate websites from a Zeus 3.1.x server a Zeus 3.3.x server
# Basically it just creates files in $ZEUSHOME/webadmin/conf/sites
#
# Usage:
#   z31upgrade.sh <oldz31directory>

# ----------------------------------------------------------
# $1 = the virtualserver are are trying to migrate
# $2 = the filename of the registry dump file

upgrade_server()
{
    SERVER=$1
    FILE="$ZEUSHOME/webadmin/conf/sites/$SERVER"
  
    if [ -f $FILE ]
    then
       echo "   WARNING: skipped as \$ZEUSHOME/webadmin/conf/sites/$SERVER already exists"
       return 
    fi
    
    # Merge current skel, and old config files
    cp $ZEUSHOME/webadmin/conf/skel $FILE

    # This is the same as grepping for conf!web!servers!$SERVER and pipeing
    # into regcmd, but is about three times faster

    grep '^add' $2 | cut -d'!' -f2- | grep '^web!servers!'$SERVER'!' | cut -d'!' -f4- | cut -d' ' -f1,3- >> $FILE

    # 'condense' the two files, so that latest settings override the
    # former settings
    $ZEUSHOME/perl/miniperl - $FILE > $FILE._$$_ <<'EOF'
while(<>) {
    ($k,$v) = split( / /, $_, 2 );
    $p{$k} = $v;
}

while( ($k,$v) = each %p ) { print "$k $v"; }
EOF

    # Some directory structures have moved since 3.1, and some settings we
    # need to remove
    sed \
    -e 's@^modules!errors!directory %zeushome%/web/errpages$@modules!errors!directory %zeushome%/web/etc/errpages@' \
    -e 's@^modules!map!alias!/icons/!filepath %zeushome%/web/icons/$@modules!map!alias!/icons/!filepath %zeushome%/web/etc/icons/@' \
    -e 's@^modules!search!input_template %zeushome%/web/misc/search_form.html$@modules!search!input_template %zeushome%/web/etc/search_form.html@' \
    -e 's@^modules!search!output_template %zeushome%/web/misc/search_output.html$@modules!search!output_template %zeushome%/web/etc/search_output.html@' \
    -e '/^running /d' \
    -e '/^modified /d' \
    < $FILE._$$_ > $FILE
    rm $FILE._$$_
}

# ----------------------------------------------------------

if [ "$#" != "1" ]
then
    echo "Zeus 3.1.x -> 3.3.x configuration migration"
    echo "Usage:"
    echo "  $0 <old Zeus 3.1 installation directory>"
    exit 1
fi

# Ensure ZEUSHOME is setup to point to our 3.3 installation
if [ -z "$ZEUSHOME" ]
then
    # Have a good guess, we live in $ZEUSHOME/webadmin/bin
    cd `dirname $0`/../..
    ZEUSHOME=`pwd`
    export ZEUSHOME
fi

if [ -z "$ZEUSHOME" -o ! -d "$ZEUSHOME/webadmin" ]
then
  echo 'FATAL: $ZEUSHOME is not set, or is set incorrectly'
  echo 'It should point to the new Zeus 3.3 installation directory'
  exit 1
fi

OLDZEUSHOME=$1
# Ensure that this is the old Zeus 3.1 installation
if [ ! -d "$OLDZEUSHOME" -o \
     ! -f "$OLDZEUSHOME/etc/reg.data" -o \
     ! -x "$OLDZEUSHOME/bin/regdump" ]
then
   echo "FATAL: $OLDZEUSHOME is not a valid Zeus 3.1 installation"
   exit 1
fi

echo "Extracting old server configuration"
DUMP="/tmp/z31dump.$$"
# Copy file first, as it might be currently running
cp $OLDZEUSHOME/etc/reg.data $DUMP.reg
$OLDZEUSHOME/bin/regdump $DUMP.reg > $DUMP
rm -f $DUMP.reg

echo "Generating list of virtual servers"
SERVERS=`cat $DUMP | grep 'conf!web!servers![^!]*$' | cut -d'!' -f4`

echo "Upgrading server configuration"
for S in $SERVERS
do
    echo " Importing server $S"
    upgrade_server $S $DUMP
done

# Check for any special tuneables and give a warning for these
grep '^add[ ]*conf!web!tuning!' $DUMP >/dev/null
if [ $? -eq 0 ]
then
    echo
    echo "Warning: the following program tuneables were previously set:"
    grep '^add[ ]*conf!web!tuning!' $DUMP | sed 's/^add[ ]*/    /';
    echo "These have not been migrated, they may not be appropriate for v3.3."
    echo "Please check the documentation or contact technical support."
    echo
fi

# Remove our dump file
rm -f $DUMP

echo "Migration complete"
exit 0
