#!/bin/sh

ActNone=0
ActSato=1
ActDato=2
LoggerCmd="logger -p daemon.notice -t haltproc"

echo "Usage: $0 <events folder> [-v]"
echo "events folder - $1"

if [ $# -ne 1 ]; then
	if [ $# -ne 2 ]; then
		echo "Incorrect number of arguments provided, exit"
		exit 1
	else
		if [ "$2" = "-v" ]; then
			LoggerLevV="verbose"
			echo "Set verbose syslog level"
		fi
	fi
fi

if [ ! -d "$1" ]; then
	echo "The folder $1 doesn't exist, exiting"
	exit 1
fi

$LoggerCmd "Process folder $1"

#Start actual operation
echo "\nProcessing list in order:"
ls -lrta "$1"

OurDir=`pwd`
echo "current dir $OurDir"

echo "cd to $1"
cd "$1"

#add all to log in hronological order and set final action
FinalAct=$ActNone
for f in `ls -rt`; do
	case "$f" in
		[0-9]*)
			if [ ! -d "$f" ]; then
				echo "append $f file to log"
				#cat "$f"
				if [ -n "$LoggerLevV" ]; then
					LoggerData=`grep "Action" "$f" | head -n 1`
					#echo "syslog call: $LoggerCmd $f: $LoggerData"
					$LoggerCmd "$f data: $LoggerData"
				fi
				ProcAct=`grep "Action" "$f" | sed 's/.*:\([0-9]\+\).*/\1/g'`
				echo "extracted Action $ProcAct"
				#update final action
				if [ "$ProcAct" -eq "$ActDato" ]; then
					FinalAct=$ActDato
					echo "catched DaTO event"
				else
					if [ "$ProcAct" -eq "$ActSato" ]; then
						if [ "$FinalAct" -ne "$ActDato" ]; then
							FinalAct=$ActSato
						fi
					fi
				fi
			fi
			;;
		halt*)
			if [ ! -d "$f" ]; then
				echo "parse $f file"
				cat "$f"
				if [ -n "$LoggerLevV" ]; then
					LoggerData=`cat "$f" | head -n 1`
					#echo "syslog call: $LoggerCmd $f: $LoggerData"
					$LoggerCmd "$f origin found: $LoggerData"
				fi
				ModuleTypeId=`head -n 1 "$f" | sed 's/.*:\([0-9]\+\).*/\1/g'`
				if [ -n "$ModuleTypeId" ]; then
					ModuleHaltConf="/etc/edge/$ModuleTypeId/haltaction.conf"
					$LoggerCmd "extracted command lines of $ModuleHaltConf"
					if [ -r "$ModuleHaltConf" ]; then
						ActSatoCmd=`grep "$ActSato:" "$ModuleHaltConf" | awk 'NF && $1!~/^#/' | awk -F":" '{print $NF}'`
						echo "extracted SaTO command line: $ActSatoCmd"
						if [ -n "$LoggerLevV" ]; then
							$LoggerCmd "for event $ActSato: $ActSatoCmd"
						fi
						#if [ -n "$ActSatoCmd" ]; then
						#	eval "$ActSatoCmd"
						#fi
						ActDatoCmd=`grep "$ActDato:" "$ModuleHaltConf" | awk 'NF && $1!~/^#/' | awk -F":" '{print $NF}'`
						echo "extracted DaTO command line: $ActDatoCmd"
						if [ -n "$LoggerLevV" ]; then
							$LoggerCmd "for event $ActDato: $ActDatoCmd"
						fi
						#if [ -n "$ActDatoCmd" ]; then
						#	eval "$ActDatoCmd"
						#fi
					fi
				fi
			fi
			;;
	esac
done

echo "removing all the files in $1"
find "$1" -maxdepth 1 -type f -exec rm {} \;

echo "cd back to $OurDir"
cd -

echo "check uni-instance running: start"
MutexName="/var/opt/edge/.haltproc.pid"
if [ -r $MutexName ]; then
	AnotherPid=`cat $MutexName`
	if [ -n "$AnotherPid" ] && [ -r "/proc/$AnotherPid/stat" ]; then
		sleep 3
		if [ -r $MutexName ]; then
			AnotherPid=`cat $MutexName`
			if [ -n "$AnotherPid" ] && [ -r "/proc/$AnotherPid/stat" ]; then
				echo "detected another instance: PID $AnotherPid, Our PID $$"
				$LoggerCmd "detected another instance: PID $AnotherPid, Our PID $$"
				exit 1
			fi
		fi
	else
		echo "some obsolete instance detected: PID $AnotherPid, freed"
	fi
fi

echo $$ > $MutexName
echo "occupied mutex $MutexName with PID $$"
if [ -n "$LoggerLevV" ]; then
	$LoggerCmd "occupied mutex $MutexName with PID $$"
fi

echo "processing action $FinalAct"
if [ $FinalAct -eq $ActSato ]; then
	echo "execute command line of event $ActSato: $ActSatoCmd"
	#echo "syslog call: $LoggerCmd execute SaTO action command line: $ActSatoCmd"
	$LoggerCmd "execute command line $ActSato: $ActSatoCmd"
	if [ -n "$ActSatoCmd" ]; then
		eval "$ActSatoCmd"
	fi
else
	if [ $FinalAct -eq $ActDato ]; then
		echo "execute command line of event $ActDato: $ActDatoCmd"
		#echo "syslog call: $LoggerCmd execute DaTO action command line: $ActDatoCmd"
		$LoggerCmd "execute command line $ActDato: $ActDatoCmd"
		if [ -n "$ActDatoCmd" ]; then
			eval "$ActDatoCmd"
		fi
	fi
fi

echo "check uni-instance running: end"
if [ -r $MutexName ]; then
	MutexPid=`cat $MutexName`
	echo "freed mutex: file PID $MutexPid, OurPID $$"
	if [ -n "$LoggerLevV" ]; then
		$LoggerCmd "freed mutex: file PID $MutexPid, OurPID $$"
	fi
	rm $MutexName
else
	echo "the mutex file is not found: our PID $$"
	if [ -n "$LoggerLevV" ]; then
		$LoggerCmd "the mutex file is not found: our PID $$"
	fi
fi

echo Done

exit 1
