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

USAGE="*** Usage ***  $0  : This script is for Empress internal use only"

case "$CNVDB" in 
"")
	echo "$USAGE"
	exit 1
	;;
esac

case "$CNVDB_VER" in
 v2.4) ;;
 *) exit 0 ;;
esac

. $EMPRESSPATH/config/options

UE_E0_CVV2_INFO1=10150
UE_E0_CVV2_INFO5=10175
UE_E0_CVV2_INFO6=10176
UI_E0_CONV4GLFORMS=10061
UI_E0_CONVER4GL=10059
UI_E0_COPYPRIV=10060
UI_E0_NO4GLTABORAPPL=10058

case "$MSINST_OLDVERS" in
 v2*) 	;;
 "")	$NLSECHO "
	Sorry, during installation you did not mention that your current version
	of Empress is an upgrade from an older version. You should:
	   change directory to %s, and
	   run the script \'%s\'
	to configure the conversion utilities.
	Aborting.
	" $UE_E0_CVV2_INFO6 $EMPRESSPATH/rdbms empugprd
	exit 1
	;;
 *) 	$NLSECHO "
This is a version 2 database. Your older version of Empress is
%s 
" $UE_E0_CVV2_INFO5 $MSINST_OLDVERS
	exit 1
	;;
esac

case "$EMP4GL" in
   no)
	exit 0
	;;
esac

case "$MSINST_OLDVERSPATH" in
 "")
	$NLSECHO "Older version of Empress installation is not valid" $UE_E0_CVV2_INFO1
	exit 1
	;;
esac

:	"Test to see if the database contains v2.4 Empress 4GL system tables"


$EMPRESSPATH/bin/empcmd "$CNVDB" "display sys_4gl_link" > $TMP 2>&1

if grep "User Error" $TMP > /dev/null 2>&1
then
	rm $TMP
       	$NLSECHO  "The database does not contain any Empress 4GL \
			tables/applications" $UI_E0_NO4GLTABORAPPL
	exit 0
fi

rm -f $TMP
	
:	"MS environment that I do not want to change "
MSDBPRIVS="creator creator dba; creator user user grant"
export MSDBPRIVS

$EMPRESSPATH/bin/empclrlk "$CNVDB"

:	"Convert v2.4 to v4.0"

$NLSECHO  "Converting Empress 4GL" $UI_E0_CONVER4GL
$EMPRESSPATH/bin/empbatch "$CNVDB" < $EMPRESSPATH/rdbms/cnvt_bin/empcvv2.ms

$NLSECHO  "Copying privileges from 'sys_4gl_application' to the new \
	tables:" $UI_E0_COPYPRIV

$EMPRESSPATH/bin/empcmd "$CNVDB" "insert into sys_4gl_tmp
	select from sys_privs where priv_tabnum = (select tab_number \
	from sys_tables where tab_name = 'sys_4gl_application')"

for I in  \
	sys_4gl_domain \
	sys_ag_definition \
	sys_ag_parameter \
	sys_ag_table \
	sys_ag_attribute \
	sys_ag_screen \
	sys_ag_screen_attr \
	sys_ag_function \
	sys_ag_qualification \
	sys_ag_qual_list \
	sys_ag_menu \
	sys_ag_menu_item \

do
	echo "$I"
	TABNUM=`$EMPRESSPATH/bin/empcmd "$CNVDB" \
		"select tab_number from sys_tables where tab_name = '$I' dump"`
	$EMPRESSPATH/bin/empbatch "$CNVDB" <<@
grant alter on $I to dba as dba;
update sys_4gl_tmp set priv_tabnum = $TABNUM;
delete from sys_privs where priv_tabnum = $TABNUM;
insert into sys_privs select * from sys_4gl_tmp;
@
	$EMPRESSPATH/bin/empcmd "$CNVDB" "alter $I" <<@ > /dev/null 2>&1
.q
@

done

$EMPRESSPATH/bin/empcmd "$CNVDB" "drop sys_4gl_tmp"

$NLSECHO  "Converting Empress 4GL forms" $UI_E0_CONV4GLFORMS
$EMPRESSPATH/rdbms/exec/emputil3 -CNVTDB_SUPPORT -CNVTAPFM "$CNVDB"

