if [ "$OALIB" = "" ]
then
	OALIB=/usr/lib/scosh
fi

PATH=$PATH:$OALIB/utilbin
export PATH OALIB

# return codes for functions called from menu
M_READY=0;   M_STAY=1
M_UPONE=2;   M_CONT1=3
M_CONT2=4;   M_UPTWO=5 
M_UPTHREE=6; M_TOP=7

ODTDSMENU=/usr/lib/custom/odtds.menu

cleanup() {
# 	restore any files if necessary
	if [ -d "$SAFEDIR" ]
	then
		cd $SAFEDIR
		for i in *
		do
			case $i in
			Xds|xsightds)
				if [ ! -f /etc/perms/xds ]
				then
					mv $SAFEDIR/$i /etc/perms
				else
					rm -f $i
				fi
				;;
			petkit)
				if [ ! -f /etc/perms/scosmt ]
				then
					mv $SAFEDIR/$i /etc/perms
				else
					rm -f $i
				fi
				;;
			*)
				if [ ! -f /etc/perms/$i ]
				then
					mv $SAFEDIR/$i /etc/perms
				else
					rm -f $i
				fi
				;;
			esac
		done
		cd /
		rmdir $SAFEDIR 2>&1 > /dev/null
	fi
	rm -f /tmp/perms/bundle/odtds /tmp/perms/bundle/prep.odtds
}


fnC() {
	wmove $MainWindow; wclear
	wmove $InfoWindow 0 0; werase
	wrefresh
	echo "Extracting files from floppy .."

	cd /
	oadeinit
	tar xf ${FDEVICE} ./tmp/perms
	ret=$?
	oainit

	if [ $ret -ne 0 -o ! -f /tmp/perms/bundle/odtds ]
	then
		wrefresh
		error "Floppy read error"
		echo "An error was encountered while reading the floppy. Please check"
		echo "the floppy and re-insert it, before re-executing this command."
		return $M_TOP
	fi

# 	execute the prep script
	wmove $InfoWindow 0 0; werase
	echo "Preparing to Update ODT Development System ... "

	wmove $MainWindow; werase
	oadeinit
	if [ -f /tmp/perms/prep.odtds ]
	then
		sh /tmp/perms/prep.odtds
		retval=$?
		oainit
		if [ $retval -ne 0 ]
		then  
			error "Preparation script failed."
			return $M_TOP
		fi
	fi
# 	move the perms files to a "safe" directory.   This will force custom
# 	to extract the perms files from the tape.

	SAFEDIR=/tmp/odtdssave

	if [ -d $SAFEDIR ]
	then
		rm -rf  $SAFEDIR
	fi
	mkdir -p $SAFEDIR

# 	get a list of perms files from the new bundle file
	list=`sed -n '/perms=/s/.*: perms=\([./a-zA-Z0-9]*\) :.*$/\1/p' /tmp/perms/bundle/odtds | \
        sed -n 's!.*/\([a-zA-Z0-9]*\)$!\1!p'`
	list="$list Xds xsightds petkit"

	for i in $list
	do
		if [ -f /etc/perms/$i ] ; then mv /etc/perms/$i $SAFEDIR; fi
	done

# 	now replace the old bundle list with the new bundle list.
# 	not this means users may not be able to re-install components from the
# 	old bundle list.

	if [ ! -d ./etc/perms/bundle ]
	then
		mkdir -p ./etc/perms/bundle
	fi

	mv /tmp/perms/bundle/odtds ./etc/perms/bundle

# 	now invoke custom to install the ODT Dev Sys
	oadeinit
	custom -i $ARGS
	oainit

	return $M_READY
}


fnQ() {
        return $M_READY
}

################
# Main()
P=`pwd`
MainWindow=1
InfoWindow=2
ARGS="$*"
FDEVICE=${FDEVICE:-/dev/install}

if [ -f /tmp/perms/bundle/odtds ]
then
	echo "The installation of Open Desktop Development System is already"
        echo "in progress on another terminal.  Please wait for that installation"
 	echo "to finish before attempting to update it."
	exit 1
fi

# init curses
eval oainit 2>/dev/null || (oash $0; kill $$ )

werase      # these two calls workaround a curses bug that shows up in menu
wrefresh

dateline "Update Open Desktop Development System"

# build main display window,
# this window will be known as '$MainWindow',
# when accessed by window commands
wmopen $MainWindow 18 80 4 0
newwin $InfoWindow 2 80 1 1

wmove $MainWindow; werase

wmove $MainWindow 3 5
echo "    Insert:  Open Desktop Development System"
wmove $MainWindow 4 5
echo "             Floppy Volume 1"
	
menu $ODTDSMENU "Update ODT DS"
retval=$?
	
# end main
oadeinit

cleanup
