:	'(c) Copyright	Empress Software Inc.	1983, 2000'

if test "$EMPRESSPATH" = "" 
then
	echo "EMPRESSPATH not set"
	exit 1

elif test ! -d $EMPRESSPATH
then
	echo "invalid EMPRESSPATH"
	exit 1
else
	RDBMSPATH=$EMPRESSPATH/rdbms
	export RDBMSPATH
fi

PROGNAME=`$EMPRESSPATH/common/sys_bin/basename $0`
USAGE="*** Usage ***  $PROGNAME  db"
TMP=`$RDBMSPATH/util_bin/tmpdir`/temp$$

. $RDBMSPATH/util_bin/banner

echo "**********************************************************************"
echo
echo "		GUICNVT converts Empress v6.4 GUI v1.0 Beta databases"
echo "	    to a form compatible with Empress v6.6 GUI v1.0 cut 2 Beta."
echo 
echo "			Usage: guicnvt database_name"
echo
echo "**********************************************************************"
echo

DB=$1

echo "Checking the database version. Please wait ..."
echo

if test ! -d $DB
then
	echo "$DB is not a database"
	exit 1
fi 

if test ! -f $DB/tabzero
then
	echo "$DB is not a database"
	exit 1
fi

$EMPRESSPATH/bin/empcmd "$DB" "display sys_gui_application" > $TMP 2>&1

if grep "User Error" $TMP > /dev/null 2>&1
then
	rm $TMP
	echo "$DB is not an Empress GUI database"
	exit 1
fi

$EMPRESSPATH/bin/empcmd "$DB" "display sys_gui_registry" > $TMP 2>&1

if grep "User Error" $TMP > /dev/null 2>&1
then
	echo "It seems that $DB is an Empress GUI Beta 1.0 database."
	convert=yes
else
	rm $TMP
	echo "$DB is an up-to-date Empress GUI database."
	echo "No conversion is required."
	echo
	convert=no
	echo -n "Do you want to compile and link applications in $DB ? :"
	while :
	do
		read ANS
		case $ANS in
		 y* | Y* )
			break
			;;
		 n* | N* )
			echo
			echo "Finished guicnvt."
			exit 0
			break
			;;
		*)
			echo -n "Please enter yes or no : "
			;;
		esac
	done
fi

case $convert in
 yes )	
	echo
	echo "Converting the database. Please wait  ..."

	$EMPRESSPATH/bin/empbatch "$DB" < $RDBMSPATH/gui/guicnvt/guicnvt.dat
	echo
	echo "Database $DB converted."
	;;
 * )
	;;
esac

echo
echo "Checking for Global variables. Please wait  ..."
echo

$EMPRESSPATH/bin/empcmd "$DB" "select count from sys_gui_module, sys_gui_action dump where (sys_gui_module.action_global = NULL or sys_gui_action.name <> sys_gui_module.action_global) and text_string match '*global*' and sys_gui_action.id = sys_gui_module.id" > $TMP

GLOBAL_CNT=`cat $TMP`

case $GLOBAL_CNT in
 0)	
    echo "No global variables definition found in script other than the global script."
    echo
	;;
 *)
    echo "Global variable declarations are no longer allowed in Script actions"
    echo "other than the global script."
    echo "The following actions contain the word 'global'"
    echo

   $EMPRESSPATH/bin/empcmd "$DB" "select '   ' print '' width 8, sys_gui_module.name print 'Module Name', sys_gui_action.name print 'Action Name' from sys_gui_module, sys_gui_action where (sys_gui_module.action_global = NULL or sys_gui_action.name <> sys_gui_module.action_global) and text_string match '*global*' and sys_gui_action.id = sys_gui_module.id order by sys_gui_module.name" 

    echo
    echo "Please modify all global variable declaration before continuing !"
    echo
    echo "                     *** WARNING ***"
    echo "If global variables are defined in script action other than"
    echo "the global script, compilation of that module will fail!"
    echo
    echo -n "Do you want to continue (y/n) ? : "
    while :
    do
	read ANS
	case $ANS in
	 y* | Y* )
		break
		;;
	 n* | N* )
		echo
		echo "After modifying the global variable declarations, run"
		echo "guicnvt again to compile and link the applications."
		echo
		echo "Finished guicnvt."
		exit 0
		break
		;;
	*)
		echo -n "Please enter yes or no : "
		;;
	esac
    done
	;;
esac

echo
echo "Compiling modules. Please wait ..."
echo

$EMPRESSPATH/bin/empcmd "$DB" "select name from sys_gui_module dump" > $TMP

while read NAME
do
	echo "	Compiling Module $NAME"
	$EMPRESSPATH/bin/empgui -c -q $DB "$NAME"
done < $TMP


echo
echo "Linking applications. Please wait ..."
echo

$EMPRESSPATH/bin/empcmd "$DB" "select name from sys_gui_application dump" > $TMP

while read NAME
do
	echo "	Linking Application $NAME"
	$EMPRESSPATH/bin/empgui -l -q $DB "$NAME"
done < $TMP

echo
echo "Cleaning up. Please wait ..."
echo

$EMPRESSPATH/bin/empcmd "$DB" "empty sys_gui_registry"

echo "Finished guicnvt"
