pkg_unpack_package ()
{
	PKG=$1
	PKG_FILE=$2
	PKG_NAME=$3
	XPATH=$4

	case "$PKG" in
	   common | netscape_sh)        ;;
	   jdbc)
		if package_to_install $PKG
		then
			:
		elif package_to_install rdbms
		then
			# if there is rdbms, we need jdbc so that
			# it can run empjavasql.
			:
		else
			return
		fi
		;;
	   *)   package_to_install $PKG || return ;;
	esac

	test "$XPATH" = "" && XPATH=$INSTALL_DIR

	if test -f $PKG_FILE.tar
	then
		PKG_FILE=$PKG_FILE.tar
	elif test -f $PKG_FILE.tgz
	then
		PKG_FILE=$PKG_FILE.tgz
	else
		return
	fi

	test -d "$XPATH" || mkdir -p $XPATH

	test "$PKG_NAME" = "" || \
		echo_message "$MSG_INFO_UNPACK_PACKAGE" "$PKG_NAME" 

	case "$PKG_FILE" in
	   *.tgz)
		tar xvzf $PKG_FILE -C $XPATH | $DOTS
		;;
	   *.tar)
		case "$PKG_FILE" in
		  /*)	;;
		  *)	PKG_FILE=`pwd`/$PKG_FILE
			;;
		esac

		(cd $XPATH; tar xvf $PKG_FILE) | $DOTS
		;;
	esac

	test "$PKG_NAME" = "" || echo_message "$MSG_INFO_DONE"
}

pkg_unpack_all_packages ()
{
	pkg_unpack_package "common" "$PKG_COMMON" "$MSG_PKG_CORE"
	pkg_unpack_package "rdbms" "$PKG_RDBMS" "$MSG_PKG_RDBMS"
	pkg_unpack_package "rdbms_rt" "$PKG_RDBMS" "$MSG_PKG_RDBMS_RT"
	pkg_unpack_package "docs" "$PKG_DOCS" "$MSG_PKG_DOCS"
	pkg_unpack_package "docsvr" "$PKG_DOCSVR" "$MSG_PKG_DOCSVR"
	pkg_unpack_package "odbccl" "$PKG_ODBCCL" "$MSG_PKG_ODBCCL"
	pkg_unpack_package "jdbc" "$PKG_JDBC" "$MSG_PKG_JDBC"
	pkg_unpack_package "rtdccl_dev" "$PKG_RTDCCL_DEV" "$MSG_PKG_RTDCCL_DEV"

	extracted_hypmedia=no

	if package_to_install ehtml
	then
		echo_message "$MSG_INFO_UNPACK_PACKAGE" "$MSG_PKG_EHTML"
		pkg_unpack_package "common" "$PKG_HYPMEDIA" ""
		extracted_hypmedia=yes

		pkg_unpack_package "ehtml" "$PKG_EHTML" ""
		pkg_unpack_package "ehtml" "$PKG_EHTML_APACHE" "" \
				"$INSTALL_DIR/hypmedia"
		pkg_unpack_package "ehtml" \
				"$INSTALL_DIR/hypmedia/docroot/empapps.tgz" \
				"" \
				"$INSTALL_DIR/hypmedia/docroot"
		pkg_unpack_package "ehtml" \
				"$INSTALL_DIR/hypmedia/docroot/examples.tgz" \
				"" \
				"$INSTALL_DIR/hypmedia/docroot"

		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install empperl
	then
		echo_message "$MSG_INFO_UNPACK_PACKAGE" "$MSG_PKG_EMPPERL"
		test "$extracted_hypmedia" = "no" && \
			pkg_unpack_package "common" "$PKG_HYPMEDIA" ""
		extracted_hypmedia=yes

		pkg_unpack_package "empperl" "$PKG_EMPPERL" ""
		pkg_unpack_package "empperl" "$PKG_PERL_STD" "" \
				"$INSTALL_DIR/hypmedia/empperl"
		
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install emptcltk
	then
		echo_message "$MSG_INFO_UNPACK_PACKAGE" "$MSG_PKG_EMPTCLTK"
		test "$extracted_hypmedia" = "no" && \
			pkg_unpack_package "common" "$PKG_HYPMEDIA" ""
		extracted_hypmedia=yes

		pkg_unpack_package "emptcltk" "$PKG_EMPTCLTK" ""
		pkg_unpack_package "emptcltk" "$PKG_TCLTK_STD" "" \
				"$INSTALL_DIR/hypmedia/emptcltk"
		
		echo_message "$MSG_INFO_DONE"
	fi
}

change_file ()
{
	file=$1
	label=$2
	value=$3

	xxTMP=$TMPDIR/xxtemp.$$

	sed -e "s|$label=.*|$label=$value|" < $file > $xxTMP
	if cmp -s $file $xxTMP
	then
		rm -fr $xxTMP
	else
		# FreeBSD prints error if mv from /tmp
		cp $xxTMP $file
		rm $xxTMP
	fi
}

pkg_config_common ()
{
	# copying from EMPRESSPATH is not a typo.
	cp $EMPRESSPATH/config/initfile $INSTALL_DIR/config

	cp $INSTALL_DIR/config/options._ $INSTALL_DIR/config/options
	cp $INSTALL_DIR/config/nettype.cfg._ $INSTALL_DIR/config/nettype.cfg
	cp $INSTALL_DIR/config/netserver.cfg._ $INSTALL_DIR/config/netserver.cfg
	X=`getuname`
	cat > $INSTALL_DIR/config/password <<EOM
# Empress password file. This is a generated file! Do not edit. Rev#0
$X::NN:::
EOM

	X=`licutil_licenced_evaluation_deadline`
	test "$X" = "" || X="'$X'"
	test "$X" = "" && X=never
	change_file $INSTALL_DIR/config/options MSINST_EXPIRY "$X"

	X=`licutil_licenced_product evaluation`
	test "$X" = "" || X=standard
	test "$X" = "" && X=evaluation
	change_file $INSTALL_DIR/config/options MSINST_TYPE "$X"

	sed -e "s;^#setenv EMPRESSPATH.*;setenv EMPRESSPATH $INSTALL_DIR;" \
		< $INSTALL_DIR/common/bin_pro/setenv.csh \
		> $INSTALL_DIR/bin/setenv.csh

	sed -e "s;^#EMPRESSPATH.*;EMPRESSPATH=$INSTALL_DIR;" \
		< $INSTALL_DIR/common/bin_pro/setenv.sh \
		> $INSTALL_DIR/bin/setenv.sh
}

pkg_config_rdbms ()
{
	X=`licutil_licenced_product evaluation`
	test "$X" = "yes" && X=X
	test "$X" = "no" && X=

	# change_file $INSTALL_DIR/config/initfile MSMKDBSHMEM $X

	X=`licutil_licenced_product mb`
	if test "$X" = "yes"
	then
		X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
		test -d $INSTALL_DIR/rdbms/4gl && cd $INSTALL_DIR/rdbms/4gl && \
			./mkdbs -native
		EMPRESSPATH=$X
	fi

	X=`licutil_licenced_product guidev`
	if test "$X" = "yes"
	then
		X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
		test -d $INSTALL_DIR/rdbms/gui && cd $INSTALL_DIR/rdbms/gui && \
			./mkguidb -native
		EMPRESSPATH=$X
	fi

	X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
	$EMPRESSPATH/rdbms/util_bin/setup_default_connsv
	EMPRESSPATH=$X

	cd

	LIST="
		$INSTALL_DIR/rdbms/testdbs
		$INSTALL_DIR/rdbms/testwork
		$INSTALL_DIR/rdbms/testwork/_testtmp/testdir
		$INSTALL_DIR/rdbms/testwork/_nettmp/testdir
		$INSTALL_DIR/rdbms/spool
		$INSTALL_DIR/rdbms/spool/log
		$INSTALL_DIR/rdbms/spool/shm
		$INSTALL_DIR/rdbms/spool/gui
		$INSTALL_DIR/rdbms/spool/odbc
		"

	for I in $LIST
	do
		test -d $I && rm -fr $I
		mkdir -p $I
	done

	chmod 777 $INSTALL_DIR/rdbms/spool/log \
			$INSTALL_DIR/rdbms/spool/shm \
			$INSTALL_DIR/rdbms/spool/gui \
			$INSTALL_DIR/rdbms/spool/odbc
}

pkg_config_rdbms_rt ()
{
	pkg_config_rdbms

	RMLIST="
		rdbms/4gl/apfmdb.ex
		rdbms/4gl/apfmdb00.ex
		rdbms/4gl/termdb.ex
		bin/empc++
		bin/emp*cc
		bin/emp*f77
		bin/emp4bldx 
		bin/empgbld* 
		rdbms/exec/emp4ful*
		rdbms/exec/empesql*
		rdbms/conf_bin/*test*
		rdbms/conf_bin/mk*
		rdbms/custom/src
		rdbms/gui/*.c
		rdbms/gui/*.c_
		rdbms/gui/*.eg
		rdbms/gui/empguidb.ex
		include
		rdbms/lib
		rdbms/test
		rdbms/testdbs
		rdbms/testwork
		"

	for I in $RMLIST
	do
		rm -fr $INSTALL_DIR/$I > /dev/null 2>&1
	done
}

pkg_config_docsvr ()
{
	: X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
	: $EMPRESSPATH/docsvr/etc/setupsv
	: EMPRESSPATH=$X
}

pkg_config_ehtml ()
{
	X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
	cd $EMPRESSPATH/hypmedia/docroot
	./setup.sh -rtm
	EMPRESSPATH=$X
	cd
}

pkg_config_empperl ()
{
	X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
	cd $EMPRESSPATH/hypmedia/empperl/bin
	cp $EMPRESSPATH/bin/empperl .
	./setup

	if test -d $INSTALL_DIR/rdbms/custom
	then
		cd $INSTALL_DIR/hypmedia/empperl/DBD-Empress
		./install.sh $INSTALL_DIR/hypmedia/empperl
	fi

	cd $INSTALL_DIR/hypmedia/empperl/DBD-EmpressNet
	./install.sh $INSTALL_DIR/hypmedia/empperl

	EMPRESSPATH=$X
	cd
}

pkg_config_emptcltk ()
{
	X=$EMPRESSPATH; EMPRESSPATH=$INSTALL_DIR; export EMPRESSPATH
	cd $EMPRESSPATH/hypmedia/emptcltk/emptcltk/src
	./install.sh -f -q
	EMPRESSPATH=$X
	cd
}

pkg_config_all_packages ()
{
	echo_message "$MSG_INFO_SETUP_COMMON"
	pkg_config_common 2>&1 | tee -a $INSTLOG | $DOTS 5
	echo_message "$MSG_INFO_DONE"

	if package_to_install rdbms
	then
		echo_message "$MSG_INFO_SETUP_RDBMS"
		pkg_config_rdbms 2>&1 | tee -a $INSTLOG | $DOTS 5
		echo_message "$MSG_INFO_DONE"
	elif package_to_install rdbms_rt
	then
		echo_message "$MSG_INFO_SETUP_RDBMS_RT"
		pkg_config_rdbms_rt 2>&1 | tee -a $INSTLOG | $DOTS 5
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install docs
	then
		echo_message "$MSG_INFO_SETUP_DOCS"
		# nothing to setup
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install docsvr
	then
		echo_message "$MSG_INFO_SETUP_DOCSVR"
		pkg_config_docsvr 2>&1 | tee -a $INSTLOG | $DOTS 5
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install odbccl
	then
		echo_message "$MSG_INFO_SETUP_ODBCCL"
		# nothing to setup
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install jdbc
	then
		echo_message "$MSG_INFO_SETUP_JDBC"
		# nothing to setup
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install ehtml
	then
		echo_message "$MSG_INFO_SETUP_EHTML"
		pkg_config_ehtml 2>&1 | tee -a $INSTLOG | $DOTS 5
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install empperl
	then
		echo_message "$MSG_INFO_SETUP_EMPPERL"
		pkg_config_empperl 2>&1 | tee -a $INSTLOG | $DOTS 5
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install emptcltk
	then
		echo_message "$MSG_INFO_SETUP_EMPTCLTK"
		pkg_config_emptcltk 2>&1 | tee -a $INSTLOG | $DOTS 5
		echo_message "$MSG_INFO_DONE"
	fi

	if package_to_install netscape && test -f $PKG_NETSCAPE_INFO
	then
		echo_message "$MSG_INFO_SETUP_NETSCAPE"
		. $PKG_NETSCAPE_INFO
		X=$CDROM_DIR/$NETSCAPE_FILE
		sh $INSTUTIL_DIR/ns-inst.sh $X \
			$INSTALL_DIR/netscape 2>&1 | tee -a $INSTLOG | $DOTS 5
		pkg_unpack_package "netscape_sh" "$PKG_NETSCAPE_SH" ""
		echo_message "$MSG_INFO_DONE"
	fi
}
