#! /bin/csh -f

#
# if it was an upgrade, merge old configuration files and logs
#
source scripts/aliases

	if ($?upgrade) then
		label "Upgrading from old configuration"
		E -n '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
		if ($status) then
			E ""
			E "Merge Failed. Using default Objects database"
			E ""
			E "Continuing Upgrade..."
		else
			cp $tmpdir/objects.C.merged $FWDIR/conf/objects.C
		endif
		E -n "Restoring rule-bases..."
		cp -p $FWDIR/conf/default.W $FWDIR/conf/default.W.orig
		cp -p $tmpdir/*.W $FWDIR/conf
		if ( -e $tmpdir/rulebases.fws ) \
			cp -p $tmpdir/rulebases.fws $FWDIR/conf
		if ( -e $tmpdir/xlate.conf ) \
			cp -p $tmpdir/xlate.conf $FWDIR/conf
		if ( -e $tmpdir/aftpd.conf ) \
			cp -p $tmpdir/aftpd.conf $FWDIR/conf
		if ( -e $tmpdir/smtp.conf ) \
			cp -p $tmpdir/smtp.conf $FWDIR/conf
		if ( -e $tmpdir/sync.conf ) \
			cp -p $tmpdir/sync.conf $FWDIR/conf
		if ( -e $tmpdir/masters ) \
			cp -p $tmpdir/masters $FWDIR/conf
		if ( -e $tmpdir/clients ) \
			cp -p $tmpdir/clients $FWDIR/conf
		E -n 'Database...'
		if ( -e $tmpdir/fwmusers) \
			cp -p $tmpdir/fwmusers $FWDIR/conf
		if ( -e $tmpdir/gui-clients) \
			cp -p $tmpdir/gui-clients $FWDIR/conf
		if ( -e $tmpdir/fwauth.NDB) \
			cp -p $tmpdir/fwauth.NDB $FWDIR/conf
		if ( -e $tmpdir/fwauth.NDB.database ) \
			cp -p $tmpdir/fwauth.NDB.database $FWDIR/database/fwauth.NDB
		if ( -e $tmpdir/fwuserauth.keys) \
			cp -p $tmpdir/fwuserauth.keys $FWDIR/database
		if ( -e $tmpdir/authkeys.C ) \
			cp -p $tmpdir/authkeys.C $FWDIR/database
		E -n 'Logfile...'
		if ( -e $tmpdir/fw.log) \
			cp -p $tmpdir/fw.log $FWDIR/log/fw.log
		if ( -e $tmpdir/fw.alog) \
			cp -p $tmpdir/fw.alog $FWDIR/log/fw.alog
		E 'Done.'

		if (! $management) goto afterfwmconv

		$FWDIR/bin/fwm -c
		if ($status == 0) then
			E " "
			E "Converting Users Permissions To Read/Write..."
			$FWDIR/bin/fwm -a '*' -ww
		endif

		if ($#argv == 1) goto for_solstice
#		E " "
#		E "The rule bases of Firewall-1 management server are located"
#		E "in a single file called rulebases.fws. You may now merge"
#		E "your existing rule bases into this file."
#		E "At any later time you may do this by running fwm -g file.W."
#		E " "

#		E -n "Do you want to convert your rule bases? (y/n) [y] ? "
#		get yn
for_solstice:
#		if ("X$yn" != Xy && "X$yn" != X) goto afterfwmconv

		if (-f $FWDIR/conf/rulebases.fws) goto afterfwmconv

		$FWDIR/bin/fwm -g $FWDIR/conf/*.W

afterfwmconv:
		rm -rf $tmpdir
	endif

	exit 0
