#!/bin/ksh
#
# If MU93xx is updated: This script will be run before the update
#
# ATTENTION! Be sure that no CR LF is in the script (only LF is allowed)!
#            Else it will not run!

# begin (erase OrphanHeaders)
# this block is not allowed to delete, must also be used at production builds

function eraseOrphanedHeaders
{
	echo SourcePath = $1

	echo $1/metainfo2.txt > /tmp/SwdlMetainfoPath.txt

	echo Now we are runing our tools $1/$MUName/tools/EIH

	# The tool couldn't open /HBpersistence/SWDL/update.txt, thus we copy it to tmp
	cp /HBpersistence/SWDL/update.txt /tmp/update.txt
	
	cp $1/$MUName/tools/EIH /tmp
	chmod a+x /tmp/EIH
	/tmp/EIH
}

# Switch the lines if you want to test with another file
MetainfoRoot=/HBpersistence/SWDL
# MetainfoRoot=.

Source=`grep 'Source = ' $MetainfoRoot/update.txt`

case $Source in
	"Source = CD")
		echo We got the source CD
		sourcePath=/fs/cd0
		;;
	"Source = SD1")
		echo We got the source SD1
		sourcePath=/fs/sd0
		;;
	"Source = SD2")
		echo We got the source SD2
		sourcePath=/fs/sd1
		;;
	"Source = USB")
		echo We got the source USB
		sourcePath=/fs/usb0
		;;
	*)
		echo Source is unknown $Source
		exit;
		;;
esac

ReleasePath=`grep 'ReleasePath = ' $MetainfoRoot/update.txt`

case $ReleasePath in
	"ReleasePath = ")
		echo Empty release path
		;;
	"ReleasePath = ./"*)
		echo Relative release path $ReleasePath
		sourcePath=$sourcePath${ReleasePath##"ReleasePath = ."}
		;;
	*)
		echo Absolute release path $ReleasePath
		sourcePath=$ReleasePath
		;;
esac

SubDir=`grep 'UseSubDir = ' $MetainfoRoot/update.txt`

case $SubDir in
	"UseSubDir = "*)
		echo SubDir $SubDir
		sourcePath=$sourcePath/${SubDir##"UseSubDir = "}
		;;
	*)
		echo No subdir
		;;
esac

MUName=`grep [[]MU9.*=TODO $MetainfoRoot/update.txt`

MUName=${MUName##"["}
MUName=${MUName%%\\*}
echo MUName: $MUName

if [[ $MUName == "MU9307" ]]; then
   MUName=MU9308
fi

if [[ $MUName == "MU9303" ]]; then
   MUName=MU9304
fi

if [[ -a $sourcePath/$MUName/tools/run_EIH_always ]] ; then
	echo We run EIH always!
	eraseOrphanedHeaders $sourcePath
else	
	# Switch the lines if you want to test with another version and/or file
	 Version=`grep 'version = 0003' /etc/version/MainUnit-version.txt`
	# Version=`grep 'version = xxxx' ./MainUnit-version.txt`

	# Switch the lines if you want to test with another version
	if [[ $Version = "version = 0003" ]];then
	# if [[ $Version = "version = xxxx" ]];then
	 echo Erase
	 eraseOrphanedHeaders $sourcePath
	else
	  echo Erasing is done by the app
	  exit
	fi
fi

# End (erase OrphanHeaders)


# Temp. workaround for removing obsolete language
# Can be removed after 14.02.2008
mount -u /mnt/sss
rm -R /mnt/sss/recog/ru_RU*
mount -u -r /mnt/sss


# Copy persistent data to tmp in case the persistence is flashed
#
# TODO!!! Before activating: Make sure that copy succeeded!
#
cp /HBpersistence/oadb.dbf /tmp
cp /HBpersistence/PDL.dat /tmp
cp /HBpersistence/bss.ddb /tmp
cp /HBpersistence/addressbook.db /tmp

# FPlummer 2008/09/15
# limiting of errlog entries of J9 does not work.
# So provide a possibillity to delete any entry in errlog temporarilly.
rm -fr /mnt/efs-persist/syslog/errlog/*

# Delete all JXE files at /HBpersistence and /HBextended
rm -f /HBpersistence/lsd.jxe /HBextended/lsd.jxe /HBextended/lsd2.jxe >/dev/null

echo scriptOK > /tmp/SWDLScript.Result
