#
#
#

instpath_check_install_dir ()
{
	test "$INSTALL_DIR" = "" && return 1

	case "$INSTALL_DIR" in
	  ~/*)
		INSTALL_DIR=`echo $INSTALL_DIR | sed "s:~:${HOME}:"`
		;;
	esac

	if test "$INSTALL_DIR" = "/"
	then
		echo_message "$MSG_INSTALL_IN_ROOT"
		return 1

	fi

	if test -d $INSTALL_DIR
	then
		INSTALL_DIR=`cd $INSTALL_DIR; pwd`

		# check permission
		TESTFILE=$INSTALL_DIR/temp.$$
		echo > $TESTFILE 2>&1

		if test ! -f $TESTFILE
		then
			echo_message "$MSG_INSTALL_WRITE_PERMISSION"
			return 1
		fi

		rm -fr $TESTFILE

		X=`cd $INSTALL_DIR; ls`

		case $X in
		  "")	# an empty directory; good
			INSTALL_DIR_OPT=empty		
			return 0;;
		esac

		while :
		do
			echo_message "$MSG_INSTALL_DIRECTORY_OPTIONS"

			read ANS

			case $ANS in
			   M | m | "")	INSTALL_DIR_OPT=move
					return 0 ;;
			   I | i)	INSTALL_DIR_OPT=overwrite
					return 0 ;;
			   E | e)	return 1 ;;
			   q | Q | quit | QUIT)
					installation_cleanup
					exit 1;;
			   *)	echo_message "$MSG_M_I_E_OR_Q" ;;
			esac
		done
	fi

	mkdir -p $INSTALL_DIR 2> /dev/null

	if test ! -d $INSTALL_DIR
	then
		echo_message "$MSG_FAIL_CREAT_DIR" $INSTALL_DIR
		return 1
	fi

	INSTALL_DIR=`cd $INSTALL_DIR; pwd`

	#
	#	remove the directory for now. In case, the
	#	user quit before the actual install.
	#
	rm -fr $INSTALL_DIR
	INSTALL_DIR_OPT=new
	return 0
}

instpath_ask_installation_path ()
{
	#
	#	Global variable set by this routine
	#
	#	INSTALL_DIR		where Empress will be installed
	#
	#
	#	If directory exists and not empty,
	#		If eval mode, refuse to use that.
	#		elif full mode, move all files/dirs to olddir.
	#		elif custom mode, move affected files/dirs to olddir.
	#

	OUT=`$EMPRESSPATH/empinstl/instbin/inst -CHKVER`

	if test "$MSPACKAGE" = "eXcalibur"
	then
		DEFAULT_DIR="$HOME/eXcalibur/v${OUT}/installation"
	else
		DEFAULT_DIR="$HOME/Empress/v${OUT}/installation"
	fi
	
	test "$INSTALL_DIR" = "" || DEFAULT_DIR=$INSTALL_DIR

	while :
	do
		echo_message "$MSG_INSTALL_DIR_QUERY" "$DEFAULT_DIR"

		read INSTALL_DIR

		case $INSTALL_DIR in
		  "")	INSTALL_DIR=$DEFAULT_DIR ;;
		  q | Q | quit | QUIT)	installation_cleanup
					exit 1
					;;
		esac

		if instpath_check_install_dir
		then
			return
		fi
	done
}

rm_old_dir ()
{
	PKG=$1
	TARGET_DIR=$2

	X=`licutil_licenced_product $PKG`
	test "$X" = "yes" && rm -fr $INSTALL_DIR/$TARGET_DIR
}

instpath_cleanup_install_path ()
{
	test -d $INSTALL_DIR || return

	cd $INSTALL_DIR
	test "$OLD_EXTENSION" = "" && OLD_EXTENSION=`date "+%Y-%m-%d-%T"`

	LIST=`ls`
	test "$LIST" = "" && return

	case "$INSTALL_DIR_OPT" in
	   move)
		mv $INSTALL_DIR $INSTALL_DIR.$OLD_EXTENSION
		;;
	   overwrite)
		rm_old_dir rdbms rdbms
		rm_old_dir rdbms_rt rdbms
		rm_old_dir docs docs
		rm_old_dir docsvr docsvr
		rm_old_dir jdbc jdbc
		rm_old_dir ehtml hypmedia
		rm_old_dir empperl hypmedia
		rm_old_dir emptcltk hypmedia
		;;
	esac
		
}
