:	'(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  olddb newdb"

. $RDBMSPATH/util_bin/banner
. $RDBMSPATH/internal/compat

NLSECHO="$EMPRESSPATH/common/nls/nlsecho"
UE_E0_CVV2_INFO4=10174
UE_E0_CV_LOGS=10316
UE_E0_CV_NDBA=10315
UE_E0_CV_TRANS=10317
UE_E0_DIREXIST=10034
UE_E0_NOTADB=10016
UI_E0_4GLTABINDBORNOT=10051
UI_E0_CNVTV8_CNVTMSG=10394
UI_E0_CNVTV8_MSG3=10393
UI_E0_CNVT_4GL_DATE1=10142
UI_E0_CNVT_4GL_DATE2=10143
UI_E0_CNVT_4GL_DATE3=10144
UI_E0_CNVT_4GL_DATE4=10145
UI_E0_COMPATCHK=10147
UI_E0_EMPSHPRT_TRYAGAIN=10195
UI_E0_INFOMSG3=10068
UI_E0_PROCCOMPLINK=10062
UI_EI_CV_ABORTBYUSER=10149
UI_EI_CV_IMPEXP1=10310
UI_EI_CV_IMPEXP2=10311
UI_EI_CV_IMPEXP3=10312
UI_EI_CV_INFO1=10148
UI_EI_CV_INFO2=10177
UI_EI_YESORNOORQUIT=10097

while :
do
	case "$1" in 
	 "-testmode")
		TESTMODE=yes
		export TESTMODE
		shift
		;;
	  -* )
		echo "$USAGE";
		exit 1;
		;;
	   * )
		break
		;;
	esac
done

VER=`$RDBMSPATH/exec/emputil -MSDBVERSION`

#This part is moved from $RDBMSPATH/bin_pro/empcnvt
test $2 = "" || test "$TESTMODE" = "yes" || $NLSECHO "

***************************************************************************

This command will continue to convert your old Empress database so that
Empress V%s can access it. It requires your old Empress version to be
available on this system.

           *********** Warning ***********

Once the old Empress is converted, the old Empress version will NOT be
able to access this database anymore. You are recommended to back up the
database before you use this command.

You must ensure nobody is accessing the database.

***************************************************************************
" $UI_E0_CNVTV8_MSG3 $VER

while :
do
	$NLSECHO -n "
This command will continue to convert the following database:
   %s
Are you sure (Yes/No)? " $UI_E0_CNVTV8_CNVTMSG $1
	read ANS

	case $ANS in
	   Yes | yes)	break ;;
	   N* | n*)	exit 0 ;;
	   *)		$NLSECHO "Invalid response, try again" \
				$UI_E0_EMPSHPRT_TRYAGAIN
			;;
	esac
done

case $# in
 2)
	CNVDB=$1
	if test -f "$CNVDB"/tablezero
	then
		TABZERO="$CNVDB"/tablezero
	else
		if test -f "$CNVDB"/tabzero
		then
			TABZERO="$CNVDB"/tabzero
		else
		   	$NLSECHO  "*** User Error ***  %s is not a database" \
				$UE_E0_NOTADB $CNVDB
			exit 1
		fi
	fi

	DBA=`$EMPRESSPATH/common/sys_bin/getuname`;
	TMP=./tmp$$
	grep MSDBADMINISTRATOR $TABZERO > $TMP
	. $TMP
	rm -f $TMP
	#	Ignore blanks
	if test !  $MSDBADMINISTRATOR = $DBA 
	then
		$NLSECHO "*** User Error *** you are not the DBA" $UE_E0_CV_NDBA
		exit 1
	fi

	grep MSDBAUDITTRAIL $TABZERO > $TMP
	grep MSDBRECOVERYLOG $TABZERO >> $TMP
	. $TMP
	rm -f $TMP
	if test  "$MSDBAUDITTRAIL1" = "" -a  \
		 "$MSDBAUDITTRAIL2" = "" -a  \
		 "$MSDBRECOVERYLOG1" = "" -a  \
		 "$MSDBRECOVERYLOG2" = ""
	then
		:
	else
		$NLSECHO "*** User Error *** Please disable all the audit trails and recovery logs and try again" $UE_E0_CV_LOGS
		exit 1
	fi

	if (test -f "$CNVDB"/_trn/*trn* )
	then
		$NLSECHO "*** User Error *** Please resolve leftover transactions and try again" $UE_E0_CV_TRANS
		exit 1
	fi

	NEWDB=$2
	if (test ! -f $CNVDB/tablezero &&  test ! -f $CNVDB/tabzero )
	then
	        $NLSECHO "*** User Error *** %s is not a database" \
	                   $UE_E0_NOTADB $CNVDB
	     	exit 1
	fi

	if (test ! -d $NEWDB)
	then
		mkdir $NEWDB
		if ( test -d $NEWDB)
		then
			rm -r $NEWDB > /dev/null
		else
			$NLSECHO "Can not create working directory " $UI_EI_CV_IMPEXP2
			exit 1
		fi
	else
		$NLSECHO "*** Directory exists *** '%s'" $UE_E0_DIREXIST $NEWDB
		exit 1
	fi

	export CNVDB NEWDB
	;;
 *)
	echo "$USAGE"
	exit 1
	;;
esac

TRUE=yes
FALSE=no
	
. $EMPRESSPATH/config/options

case "$TESTMODE" in
 yes)
	 TESTOLDVER=v4.3
	 TESTOLDPATH=$RDBMSPATH
	 TESTVER=vx
	 export TESTOLDVER TESTOLDPATH TESTVER
	  
  	 MSINST_OLDVERS=$TESTOLDVER
	 MSINST_OLDVERSPATH=$TESTOLDPATH
	;;
esac

case "$MSINST_OLDVERS" in
 "")	
	cat <<EOM

 If your old version of Empress is one of the following:
	$COMPAT_VERSION

 you can run:
 	empcnvt $CNVDB

 Otherwise, please edit \$EMPRESSPATH/config/options file and set:

   MSINST_OLDVERS to the version number of your older version of Empress.
   For example,
	MSINST_OLDVERS=v4.4

   MSINST_OLDVERSPATH to your old RDBMSPATH.
   For example,
	MSINST_OLDVERSPATH=/usr/empress/v4.4

 Then run:
	empcnvt $CNVDB $NEWDB

EOM
	exit 1
	;;
esac

case "$MSINST_OLDVERSPATH" in
 "")
	cat <<'EOM'

 If your old version of Empress is one of the following:
	$COMPAT_VERSION

 you can run:
 	empcnvt $CNVDB

 Otherwise, please edit \$EMPRESSPATH/config/options file and set:

   MSINST_OLDVERS to the version number of your older version of Empress.
   For example,
	MSINST_OLDVERS=v4.4

   MSINST_OLDVERSPATH to your old RDBMSPATH.
   For example,
	MSINST_OLDVERSPATH=/usr/empress/v4.4

 Then run:
	empcnvt $CNVDB $NEWDB

EOM
	exit 1
	;;
esac

EXEC=$RDBMSPATH/exec/emputil3
if test ! -f $EXEC
then
	$NLSECHO "The file %s can not be found." $UE_E0_CVV2_INFO4 $EXEC
	exit 1
fi

echo
$NLSECHO -n "
  +=============================================================+
  |  Empcnvt requires a working directory for temporary files.  |
  |  Please specify a working directory. Press return if you    |
  |  want to use the current directory.                         | 
  +=============================================================+
   Working directory ? : " $UI_EI_CV_IMPEXP1

read ANS

case "$ANS" in 
 "" )
	WDIR=`pwd` ;;
 * )
	WDIR=$ANS ;;
esac

MSTMPDIR=$WDIR
export MSTMPDIR

MAXTRY=100

while MAXTRY=`expr $MAXTRY - 1 `
do
	WORKD=$WDIR/_CNVT_WORK$MAXTRY

	if (test ! -d $WORKD)
	then
		mkdir $WORKD

		if ( test -d $WORKD)
		then
			WDIR=$WORKD
			break
		else
			$NLSECHO "Can not create working directory !" $UI_EI_CV_IMPEXP2
			exit 1
		fi
	fi
done


MSGLOBALDATADICTIONARY=
export MSGLOBALDATADICTIONARY

TMP=$WDIR/temp
OUTFILE=_outcnvt

: get current version
grep MSDBVERSION $RDBMSPATH/custom/tabzero > $TMP
. $TMP

CURRENT_VERSION=`$RDBMSPATH/exec/emputil -MSDBVERSION`

OLDMSPATH="$MSINST_OLDVERSPATH"
NEWMSPATH="$RDBMSPATH"
export OLDMSPATH NEWMSPATH

case "$MSINST_OLDVERSBLKSIZE" in
	512 ) MSBLKSIZE=512 ;;
	*) MSBLKSIZE=1024 ;;
esac

rm -fr $TMP

export CNVDB NEWDB NLSECHO TMP TRUE FALSE EMP4GL OUTFILE
export DATE_FORMAT MS4GLCOMPATCHK COMPILE_LINK BACKGROUND RDBMSPATH
export CURRENT_VERSION MSINST_OLDVERSPATH
export DBLOCK  WDIR

$NLSECHO -n  "
  +=============================================================+
  | The conversion can  be started as a  background  process.   |
  | If you choose to start the conversion as a background       |
  | process, the result will be output to the file _outcnvt.    |
  +=============================================================+

  Do you want to start conversion in background (y/n/q) ? " $UI_EI_CV_INFO1

while :
do
	read ANS
	case $ANS in
	 y* | Y* )
		BACKGROUND=$TRUE
		break
		;;
	 n* | N* )
		BACKGROUND=$FALSE
		break
		;;
	 q*|Q*) $NLSECHO  "***  Conversion Aborted By User  ***" $UI_EI_CV_ABORTBYUSER
		rm -rf $WDIR > /dev/null 2>&1
		exit 1 
		;;
	 *) $NLSECHO -n  "Please enter yes, no or quit: " $UI_EI_YESORNOORQUIT
		;;
	esac
done

echo
$NLSECHO -n  "
  +=============================================================+
  | Empress recommends that your converted database should have |
  | record level locking on the data dictionary tables.         |
  +=============================================================+

  Do you want record level locking (y/n/q) ? " $UI_EI_CV_IMPEXP3

while :
do
	read ANS
	case $ANS in
	 y* | Y* )
		DBLOCK=$TRUE
		break
		;;
	 n* | N* )
		DBLOCK=$FALSE
		break
		;;
	 q*|Q*) $NLSECHO  "***  Conversion Aborted By User  ***" $UI_EI_CV_ABORTBYUSER
		rm -fr $WDIR > /dev/null 2>&1
		exit 1 
		;;
	 *) $NLSECHO -n  "Please enter yes, no or quit: " $UI_EI_YESORNOORQUIT
		;;
	esac
done


echo
$NLSECHO -n "Do you have Empress 4GL tables/applications in your \
database? 
Please answer 'y' if you are not sure. " $UI_E0_4GLTABINDBORNOT

while :
do
	read ANSWER
	case "$ANSWER" in
	 y*|Y*)
		EMP4GL=$TRUE
		break
		;;
	 n*|N*)
		EMP4GL=$FALSE
		case "$BACKGROUND" in
		 yes)	
			$NLSECHO -n  "
  +=============================================================+
  | The conversion will be started as a  background  process.   |
  | Please check _outcnvt for the result.			|
  +=============================================================+
" $UI_EI_CV_INFO2
			trap "" 1 15
			exec $RDBMSPATH/cnvt_bin/empcvgo  &
			exit 0
			;;
		esac
		if $RDBMSPATH/cnvt_bin/empcvgo
		then
			exit 0
		else
			exit 1
		fi
		;;
	 q*|Q*) $NLSECHO  "***  Conversion Aborted By User  ***" $UI_EI_CV_ABORTBYUSER
		rm -rf $WDIR > /dev/null 2>&1
		exit 1 
		;;
	 *) $NLSECHO -n  "Please enter yes, no or quit: " $UI_EI_YESORNOORQUIT
		;;
	esac
done

echo
$NLSECHO "
Since v4.5, the 4GL date field uses Universal Date Format, "yyyymmdd".
In order to use the date format other than Universal Date Format, the
4GL date field must be changed to Empress Date format.
There are 9 difference Empress Date format:
" $UI_E0_CNVT_4GL_DATE1

	$NLSECHO "
	Type	Format			Year range	Example
	0	yyyymmdd		0000-9999	19820627
	1	dd aaaaaaaaa yyyy	0000-9999	27 June 1982
	2	aaaaaaaaa dd, yy	0000-9999	June 27, 1982
	3	mm/dd/yy		1900-1999	06/27/82
	4	dd/mm/yy		1900-1999	27/06/82
	5	dd aaa yy		1900-1999	06 Jun 82
	6	aaa dd, yy		1900-1999	Jun 27, 82
	7	mm/dd/yyyy		0000-9999	06/27/1982
	8	dd/mm/yyyy		0000-9999	27/06/1982
" $UI_E0_CNVT_4GL_DATE2
	
$NLSECHO -n "
Do you want to change the datatype of all 4GL date field to Empress date 
field ? " $UI_E0_CNVT_4GL_DATE3

while :
do
  read ANSWER
  case $ANSWER in
    y*|Y* )
	while :
	do
  		$NLSECHO  -n "Empress date format (0-8) ? " $UI_E0_CNVT_4GL_DATE4
  		read ANSWER
  		case $ANSWER in
  	 	  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8)  	DATE_FORMAT=$ANSWER
							break 
							;;
  	 	  *) ;;
  		esac
	done
	break
  	;;
    n*|N*) DATE_FORMAT= 
	   break
	   ;;
    q*|Q*) $NLSECHO  "***  Conversion Aborted By User  ***" $UI_EI_CV_ABORTBYUSER
	   rm -rf $WDIR > /dev/null 2>&1
	   exit 1 
     	   ;;
    *) $NLSECHO -n  "Please enter yes, no or quit: " $UI_EI_YESORNOORQUIT 
	   ;;
   esac	
done

while :
do
	echo 
	$NLSECHO -n  "Do you wish to proceed to compile and link \
	tables/applications ? " $UI_E0_PROCCOMPLINK
	read ANSWER
	case $ANSWER in
		y* | Y* )
			$NLSECHO  "
+=============================================================+
|                                                             |
|   If any error occurs while compiling or  linking, you do   |
|   not have to reconvert your database.  Simply proceed to   |
|   correct the error and recompile or relink.                |
|                                                             |
+=============================================================+
" $UI_E0_INFOMSG3
			COMPILE_LINK=$TRUE
			break
			;;
		n* | N*)
			COMPILE_LINK=$FALSE
			break
			;;
    		q*|Q*) $NLSECHO  "***  Conversion Aborted By User  ***" $UI_EI_CV_ABORTBYUSER
	   		rm -rf $WDIR > /dev/null 2>&1
	   		exit 1 
			;;
		*) $NLSECHO -n  "Please enter yes, no or quit: " $UI_EI_YESORNOORQUIT
			;;
	esac	
done

while :
do
	case $COMPILE_LINK in
	  n* | N* )
	           break;;
	esac

	echo
	$NLSECHO -n  "
Do you wish the compiler issue warning when it finds version 2 incompatible 
4GL code ? " $UI_E0_COMPATCHK
	read ANSWER
	case $ANSWER in
 		y* | Y* ) MS4GLCOMPATCHK=X
			break
			;;
 		n* | N* ) MS4GLCOMPATCHK=
			break
			;;
    		q*|Q*) $NLSECHO  "***  Conversion Aborted By User  ***" $UI_EI_CV_ABORTBYUSER
	   		rm -rf $WDIR > /dev/null 2>&1
	   		exit 1 
			;;
		*) $NLSECHO -n  "Please enter yes, no or quit: " $UI_EI_YESORNOORQUIT
			;;
	esac	
done

case "$BACKGROUND" in
 yes)
	$NLSECHO -n  "
  +=============================================================+
  | The conversion will be started as a  background  process.   |
  | Please check _outcnvt for the result.			|
  +=============================================================+
" $UI_EI_CV_INFO2
	trap "" 1 15
	exec $RDBMSPATH/cnvt_bin/empcvgo  &
	exit 0
	;;
esac

if $RDBMSPATH/cnvt_bin/empcvgo
then
	exit 0
else
	exit 1
fi
