#! /bin/sh

FWDIR=$BASEDIR
TMPDIR=$FW_PKGSAV

PATH=$FWDIR/bin:$FWDIR/cisco:/usr/bin:/usr/ucb:/usr/sbin

fw1_firewall=`cat $FWDIR/conf/product.conf | grep -i FireWall | awk -F\= '{print $2}'`

if [ $OSREL -eq 5 ]; then
  if [ X$fw1_firewall = X1 ]; then
# kernel module installation
# remove old kernel module if present
    echo "Removing old fw kernel module (if present)..."
    egrep -s fw /etc/name_to_major
    if [ $? -eq 0 ]; then
	/usr/sbin/rem_drv fw 2>&1 > /dev/null
    fi
    /bin/rm -f /usr/kernel/drv/fw /usr/kernel/drv/fw.conf 2>&1 > /dev/null
    echo "Installing new kernel module..."
    ln -s $FWDIR/modules/fwmod.5.4.o /usr/kernel/drv/fw
    ln -s $FWDIR/modules/fw.conf /usr/kernel/drv/fw.conf
    if [ "$Group_name" = "" ]; then
	 mode="* 0600 root bin"
    else
	 mode="* 0660 root $Group_name"
    fi
    /usr/sbin/add_drv -m "$mode" fw
  
    cp /etc/devlink.tab /etc/devlink.tab.bak
    egrep -s 'name=fw[	]' /etc/devlink.tab
    if [ $? -eq 0 ]; then
      ed - /etc/devlink.tab << EOEX
g/name=fw[	]/d
w
q
EOEX
    fi
    echo "type=ddi_pseudo;name=fw	fw\A0" >> /etc/devlink.tab
    /usr/sbin/devlinks
    if [ -r /dev/fw0 ]; then
	  :
    else
	  echo "*** Failed to create /dev/fw0, contact technical support"
	  exit 1
    fi
  fi
fi

if [ "$FW_UPGRADE" = "1" ]; then
	echo "Upgrading from old configuration:"
	echo "	Merging objects database..."
	cp -p $FWDIR/conf/objects.C $FWDIR/conf/objects.C.generic
	cp -p $TMPDIR/objects.C $FWDIR/conf/objects.C.orig
	$FWDIR/bin/fw confmerge $TMPDIR/objects.C $FWDIR/conf/objects.C > $TMPDIR/objects.C.merged 2> /dev/null
	if [ $? -ne 0 ]; then
		echo ""
		echo "	Merge failed. Using default objects database..."
		echo "	Continuing upgrade..."
	else
		cp $TMPDIR/objects.C.merged $FWDIR/conf/objects.C
	fi
	echo "	Restoring rule bases..."
	cp -p $FWDIR/conf/default.W $FWDIR/conf/default.W.orig
	cp -p $TMPDIR/*.W $FWDIR/conf
# Fix 2.0 bug
	if [ -f $TMPDIR/xlate.conf ]; then \
		cp -p $TMPDIR/xlate.conf $FWDIR/conf
	fi
	if [ -f $TMPDIR/external.if ]; then \
		cp -p $TMPDIR/external.if $FWDIR/conf
	fi
# End 2.0 bug fix
	echo "	Database..."
# 2.1 Addition
	if [ -f $TMPDIR/fwmusers ]; then
		cp -p $TMPDIR/fwmusers $FWDIR/conf
	fi
	if [ -f $TMPDIR/gui-clients ]; then
		cp -p $TMPDIR/gui-clients $FWDIR/conf
	fi
	if [ -f $TMPDIR/fw.license ]; then
	  cp -p $TMPDIR/fw.license $FWDIR/conf
	  if [ $OSREL -eq 5 ]; then
	    echo "	Reinstalling the license..."
	    $FWDIR/bin/fw putlic 0 0-0-0 0 2> /dev/null
	  else
	    $FWDIR/bin/fw putlic 0 0-0-0 0 2> /dev/null
	  fi
	fi
# End 2.1 Addition
	if [ -f $TMPDIR/fwauth.NDB ]; then
		cp -p $TMPDIR/fwauth.NDB $FWDIR/conf
	fi
	if [ -f $TMPDIR/fwauth.NDB.database ]; then
		cp -p $TMPDIR/fwauth.NDB.database $FWDIR/database/fwauth.NDB
	fi
	if [ -f $TMPDIR/fwuserauth.keys ]; then
		cp -p $TMPDIR/fwuserauth.keys $FWDIR/database
	fi
	if [ -f $TMPDIR/authkeys.C ]; then
		cp -p $TMPDIR/authkeys.C $FWDIR/database
	fi
	echo "	Logfile..."
	if [ -f $TMPDIR/fw.log ]; then
		cp -p $TMPDIR/fw.log $FWDIR/log/fw.log
	fi
	echo "Done upgrading from old configuration."
	rm -rf $TMPDIR/*

# 2.1 Addition
	if [ "$Merge_W" = "y" ]; then
	  echo "Merging .W files..."
	  touch $FWDIR/conf/rulebases.fws
	  $FWDIR/bin/fwm -g $FWDIR/conf/*.W 2> $FWDIR/log/fwmconvert_log
	fi
fi

# End 2.1 Addition

set fw1_firewall = `cat $FWDIR/conf/product.conf | grep -i firewall | awk -F\= '{print $2}'`

if [ X$fw1_firewall = X1 ]; then
  if [ -f /etc/fw.boot/default.bin ]; then rm /etc/fw.boot/default.bin; fi
  if [ -f $FWDIR/state/default.bin ]; then
    echo regenerating the default filter...
    $FWDIR/bin/fw defaultgen
    cp $FWDIR/state/default.bin /etc/fw.boot/default.bin 
  fi
  echo Performing fw putlic -K...
  $FWDIR/bin/fw putlic -K 2> /dev/null
  echo "Done."
fi

$FWDIR/bin/fw putlic -k
exit 0

