#!/bin/ksh
#
# ident "@(#)bbmergeprefs.sh 1.2     00/12/06 SMI"
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All rights reserved.
#                       
# upload: 
#   subtract the fixed pref settings from preferences.js, so the
#   upload contains only user setable prefs
# download:
#   merge the down loaded prefs with the local preset values
#  
# args:
# $1  direction ( up/down load )
# $2  universal user identification
# $3  local source/target up/down load directory
# $4  dest/src dircetory
# $5  file to handle

. $BBROOT/etc/opt/SUNWbb/blackbox.rc
. $BBPATH/config/browser.rc

PREFS=prefs.def
LOCKS=locks.def

export PATH=$BBPATH/bin:/bin:. 

if [ $# != 5 ]; then
    echo "usage: $0"
    exit 1
fi

case $1 in 
	"up")
		egrep -v -f $PATTERN $3/$4/$5 > $3/$4/${5}.tmp 
		mv $3/$4/${5}.tmp $3/$4/${5}.local
		if [ -x $BROWSERAGENT ]; then
                    $BROWSERAGENT $1 $2 $3 $4 ${5}.local
		fi
		rm -f $PATTERN $3/$4/$5
	 	;;
	"down")
		if [ ! -d $3/$4 ]; then
			mkdir -p $3/$4
		fi
		if [ -x $BROWSERAGENT ]; then
		    $BROWSERAGENT $1 $2 $3 $4 ${5}.tmp
		    if [ $? != 0 ]; then
			# create a default file
			bbprefs $BROWSERPREFDEFAULT user_pref > $3/$4/${5}.tmp
		    fi
		else
		    if [ -f $3/$4/${5}.local ]; then 
		    	cp $3/$4/${5}.local $3/$4/${5}.tmp
		    else
			bbprefs $BROWSERPREFDEFAULT user_pref > $3/$4/${5}.tmp
		    fi
		fi
		bbprefs $PREFS user_pref >> $3/$4/${5}.tmp
		bbprefs $LOCKS lockPref  >> $3/$4/${5}.tmp
		mv $3/$4/${5}.tmp $3/$4/$5
		;;
	*)
		echo "$0: unknown direction: $1"
		exit 2
esac

