#!/bin/sh

FWDIR=$BASEDIR
TMPDIR=$FW_PKGSAV

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

if [ "$VARDIR" = "" ]; then
	VARDIR=$FWDIR
fi

if [ -f $FWDIR/bin/funcchain ]; then
	/bin/rm $FWDIR/bin/funcchain
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/funcchain

if [ -f $FWDIR/bin/fwcisco ]; then
	/bin/rm $FWDIR/bin/fwcisco
fi
ln -s $FWDIR/cisco/fwcisco $FWDIR/bin/fwcisco 

if [ -f $FWDIR/bin/fwcomp ]; then
	/bin/rm $FWDIR/bin/fwcomp
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/fwcomp

if [ -f $FWDIR/bin/fwd ]; then
	/bin/rm $FWDIR/bin/fwd
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/fwd

if [ -f $FWDIR/bin/fwell ]; then
	/bin/rm $FWDIR/bin/fwell
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/fwell

if [ -f $FWDIR/bin/fwlv ]; then
	/bin/rm $FWDIR/bin/fwlv
fi
ln -s $FWDIR/bin/fwuivpn $FWDIR/bin/fwlv

if [ -f $FWDIR/bin/fwm ]; then
	/bin/rm $FWDIR/bin/fwm
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/fwm

if [ -f $FWDIR/bin/fwxauth ]; then
	/bin/rm $FWDIR/bin/fwxauth
fi
ln -s $FWDIR/bin/fwuivpn $FWDIR/bin/fwxauth

if [ -f $FWDIR/bin/fwxlconf ]; then
	/bin/rm $FWDIR/bin/fwxlconf
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/fwxlconf

if [ -f $FWDIR/bin/in.aclientd ]; then
	/bin/rm $FWDIR/bin/in.aclientd
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/in.aclientd

if [ -f $FWDIR/bin/in.aftpd ]; then
	/bin/rm $FWDIR/bin/in.aftpd
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/in.aftpd

if [ -f $FWDIR/bin/in.ahttpd ]; then
	/bin/rm $FWDIR/bin/in.ahttpd
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/in.ahttpd

if [ -f $FWDIR/bin/in.apop3d ]; then
	/bin/rm $FWDIR/bin/in.apop3d
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/in.apop3d

if [ -f $FWDIR/bin/in.atelnetd ]; then
	/bin/rm $FWDIR/bin/in.atelnetd
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/in.atelnetd

if [ -f $FWDIR/bin/snauth ]; then
	/bin/rm $FWDIR/bin/snauth
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/snauth

if [ -f $FWDIR/bin/snmp_trap ]; then
	/bin/rm $FWDIR/bin/snmp_trap
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/snmp_trap

if [ -f $FWDIR/bin/snmpd ]; then
	/bin/rm $FWDIR/bin/snmpd
fi
ln -s $FWDIR/bin/fwvpn $FWDIR/bin/snmpd

if [ -f $FWDIR/lib/wellfleet.C ]; then
	/bin/rm $FWDIR/lib/wellfleet.C
fi
ln -s $FWDIR/well/wellfleet.C $FWDIR/lib/wellfleet.C

if [ -f $FWDIR/modules/fwmod.5.3.o ]; then
	/bin/rm $FWDIR/modules/fwmod.5.3.o
fi
ln -s $FWDIR/modules/fwmodvpn.5.x.o $FWDIR/modules/fwmod.5.3.o

if [ -f $FWDIR/modules/fwmod.5.4.o ]; then
	/bin/rm $FWDIR/modules/fwmod.5.4.o
fi
ln -s $FWDIR/modules/fwmodvpn.5.x.o $FWDIR/modules/fwmod.5.4.o

if [ -f $FWDIR/modules/fwmod.5.5.o ]; then
	/bin/rm $FWDIR/modules/fwmod.5.5.o
fi
ln -s $FWDIR/modules/fwmodvpn.5.x.o $FWDIR/modules/fwmod.5.5.o

if [ -f $FWDIR/well/fwell ]; then
	/bin/rm $FWDIR/well/fwell
fi
ln -s $FWDIR/bin/fwell $FWDIR/well/fwell

if [ $OSREL -eq 5 ]; 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

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

$FWDIR/bin/fw putlic -k

exit 0
