#!/bin/ksh
#
# script to migrate conformance data

# setup directories - absolute paths
base=`pwd`
cmd=$(basename $0)
cmdBase=$(dirname $0)
cd $cmdBase
cmdBase=`pwd`
cd $base

dumpSql=$cmdBase/dumpSql

#
# create SQL
#   $1 - name of test case
#
function createSQL {

	typeset tc=$1
	typeset destDir ftype fname fsrc

	cd $dumpSql
	rm -f core.sql domain.sql local_auth_context.sql.tmpl
	if [[ $tc = 'base' ]]
	then
		./dumpSql
	else
			# pick range so as not to export base, if loaded
		./dumpSql 32000 999999999
	fi

	if [[ ! -f core.sql ]]
	then
		echo "SQL dump failed"
		exit 2
	fi

		# copy files
	destDir=$confBase
	for ftype in domain core local_auth_context
	do
		fname=$confBase/${ftype}_${tc}.sql
		fsrc=$ftype.sql
			
		if [[ $ftype = local_auth_context ]]
		then
			fname=${fname}.tmpl
			fsrc=${fsrc}.tmpl
		fi

			# only copy generated files if original exists
		if [[ -f $fname ]]
		then
				# save old versions
			if [[ ! -f $fname.orig ]]
			then
				cp -p $fname $fname.orig
			fi

			cp $fsrc $fname
		fi
	done

	cd $base
}

#
# load the data for an individual test case
#
#	$1 - testcase identifier
function loadData {


	typeset tc=$1
	typeset delFile=delFile_$$.sql
	typeset tab fname ftype

		# delete old data
	for tab in  \
			policy_selection_rule network_selection_policy \
			ringing_schedule_entry ringing_device_set_entry \
			ringing_device_set mcs_identity_pref mcs_subscriber_pref \
			mcs_subscription status_subscription local_auth_context \
			endpoint device voice_mail_box identity subscriber \
			conference_factory meet_me_conference meet_me_conference_pool \
			conference_resource derived_association domain_replace_chars_rule \
			domain_rule_sequence domain \
			mcs_service_definition 
	do
		echo "delete from $tab;" >>$delFile
	done

	echo "exit;" >>$delFile

	sqlplus -S $CSN_DBUSER/$CSN_DBPASS@$CSN_DBSERVICE @$delFile 
	rm -f $delFile

	sqlplus -S $CSN_DBUSER/$CSN_DBPASS@$CSN_DBSERVICE \
										< $confBase/mcs_service_definition.sql

	for ftype in domain core local_auth_context
	do
		fname=$confBase/${ftype}_${tc}.sql

		if [[ $ftype = local_auth_context ]]
		then
			if [[ -f $fname.tmpl ]]
			then
				sed -e 's/md5(.*),/null,/' $fname.tmpl >$fname

				sqlplus -S $CSN_DBUSER/$CSN_DBPASS@$CSN_DBSERVICE <$fname

				rm -f $fname
			fi
		else
			if [[ -f $fname ]]
			then
				sqlplus -S $CSN_DBUSER/$CSN_DBPASS@$CSN_DBSERVICE <$fname
				#echo "@$fname" | \
				# sqlplus $CSN_DBUSER/$CSN_DBPASS@$CSN_DBSERVICE

			fi
		fi
	done
}

#
# scan/parse arguments
#
function scanArgs {

	if [[ $# = 0 ]]
	then
		usage
	fi

	loadOnly=0
	allSuites=0
	plugIn=''

	while getopts :alp: option
	do
		case $option in
			"a")
				allSuites=1
				;;

			"l")
				loadOnly=1
				;;

			"p")
				plugIn=$OPTARG
				;;

			"b")
				installTomcatBase=1
				;;

			"?")
				echo "$cmd: bad option ($OPTARG)"
				usage
				exit 1
				;;
		esac
	done

		# finish command scan
	shift $(($OPTIND - 1))

		# directory checks
	if [[ -z ${REL_ROOT:-} ]]
	then
		echo "REL_ROOT not defined"
		exit 2
	fi

	confBase=$REL_ROOT/tools/src/systemTest/conformance/provisioning
	if [[ ! -d $confBase ]]
	then
		echo '$REL_ROOT/tools/src/systemTest/conformance does not exist'
		exit 2
	fi

		# verify suites
	if [[ $# = 0 ]]
	then
		if [[ $allSuites = 0 ]]
		then
			usage
		fi
		suiteList="$(ls $confBase/core*.sql)"
	else
		if [[ $allSuites = 1 ]]
		then
			usage
		fi
		suiteList="$*"
	fi

		# verify plugin
	if [[ $loadOnly = 0 ]]
	then
		if [[ -z $plugIn ]]
		then
			usage
		else
			plugInScript=$cmdBase/plugin/$plugIn/migrate
			if [[ ! -x $plugInScript ]]
			then
				echo "Can't find plugIn script ($plugInScript)"
				exit 2
			fi
		fi
	else
		if [[ ! -z $plugIn ]]
		then
			usage
		fi
	fi

}

#
# usage
#
function usage {
	cat <<EOF
      usage: $cmd {-p plugIn | -l} {-a | test1 ...}
            -p plugIn - plugIn name to be used for conversion
            -l - load data only
            -a - process all test suites in \$CSE_UNITTEST_DIR
            test1 ... - list of test suites with respect to \$CSE_UNITTEST_DIR
EOF

	exit 1
}

#
# main starts here
#

scanArgs $@

	# redirect output

for testCase in $suiteList
do
		# validate file
	if [[ ! -f $testCase ]]
	then
		testCase=$confBase/core_${testCase}.sql
		if [[ ! -f $testCase ]]
		then
			echo "Cannot find suite $testCase"
			continue
		fi
	fi

		# convert to suite name
	testCase=${testCase#$confBase/core_}
	testCase=${testCase%.sql}

	echo "== processing ${testCase#$confBase/} "

	if [[ $loadOnly = 0 ]]
	then
		echo "=== loading data"
		cd $(dirname $plugInScript)
		$plugInScript $testCase before
		cd $base
		loadData $testCase

		echo "=== migrate"
		cd $(dirname $plugInScript)
		$plugInScript $testCase migrate
		retCode=$?
		cd $base
		if [[ $retCode != 0 ]]
		then
			echo "migration fails; returns $retCode"
		else
			echo "=== dumpSql"
			createSQL $testCase	 
		fi

		cd $(dirname $plugInScript)
		$plugInScript $testCase after
		cd $base
	else
		echo "=== loading data"
		loadData $testCase
	fi
done

echo "== Done"


