#!/bin/bash

# Defaults
D_parentDir="./"

G_SYNOPSIS="

 NAME

 	dijk_meta.bash

 SYNOPSIS

 	dijk_meta.bash		

 DESCRIPTION

 	'dijk_meta.bash' is a simple example template describing a 
	possible mechanism in which to iteratively control a 'dijkstra_p1'
	process.
	
	It is designed to demonstrate a technique for creating several 
	label files on a given surface. Typically, a user/programmer
	would modify this file to suit their particular needs.
	
	Note that 'dijk_meta.bash' creates a dsh script file that
	it passes to a python controller, 'dijk_dscript.py'. This
	python script in turn creates the actual 'dijkstra_p1' process
	and terminates the same when necessary.	
	
 OPTIONS
 
	(none)	

 PRECONDITIONS
 
 	o You will need a legal 'options.txt' file in your working
	  directory. This file is read by 'dijkstra_p1' and is used
	  to define the basic environment.
	  
	o You will need the following files in your PATH:
	
		* 'SSocket_client'
		  Used to communicate to with 'dijkstra_p1'
		
		* 'dijk_dscript.py'
		  The python intermediary that starts / stops
		  'dijkstra_p1'
 POSTCONDITIONS

 	o A set of FreeSurfer label files will be created, one for
	  each seed vertex (specified in this file). These label
	  files are projected onto the 'curvatureFile' as specified
	  in 'options.txt'.
	
 HISTORY
 
 10 March 2005
 o Initial design and coding.

"

###\\\
# Global variables
###///
SELF=`basename $0`
PID=$$
DSHSCRIPT="/tmp/vertexArea.dsh.$PID"

# Actions
A_fileCheck="checking for prerequisite files" 

# Error messages
EM_nofile="I couldn't find a required file."

# Error codes
EC_nofile="1"

###\\\
# Function definitions
###///

function shutdown
# $1: Exit code
{
        rm -f $DSHSCRIPT
        echo -e "\n$SELF:\n\tShutting down with code $1.\n"
        exit $1
}

function synopsis_show
{
	echo "USAGE:"
	echo "$G_SYNOPSIS"
	shut_down 0
}
                   
function error
# $1: Action
# $2: Error string
# $3: Exit code
{
	echo -e "\n$SELF:\n\tSorry, but there seems to be an error." 	>&2
	echo -e "\tWhile $1,"                                      	>&2
	echo -e "\t$2\n"                                           	>&2
	echo -e "\n\tUse $SELF -x for help"			 	>&2
	shut_down $3
}                

function warn
# $1: Action
# $2: Warn string
# $3: Default value
{
	echo -e "\n$SELF: WARNING\n" 			>&2
	echo -e "\tWhile $1,"                           >&2
	echo -e "\t$2\n"                                >&2
	echo -e "\tSetting default to '$3'\n"		>&2
}        

function ret_check
{
	# ARGS
	# $1 		in		return value to check
	#
	# DESC
	# Checks for the passed return value and prints
	# a notification.
	#
	
	local ret=$1
	
	if [[ $ret != "0" ]] ; then
		echo -e "[ failure ]"
		return 1
	else
		echo -e "[ ok ]"
		return 0
	fi
}

function file_checkOnPath
{
	# ARGS
	# $1 		in		file to check
	# $2		in		error action
	# $3		in		error message
	# $4		in		error code
	#
	# DESC
	# Checks for the existence of a file. If not found,
	# uses arguments ${2-4} for a call to error_exit
	#
	
	local file=$1
	local action=$2
	local message=$3
	local code=$4
	
	echo -en "\t\t\t"
	type -all $file 2>/dev/null >/dev/null
	notFound=$?
	
	if (( notFound ))  ; then
		echo -e "[ failure ]"
		return 1
	else
		echo -e "[ ok ]"
	fi
	return 0
}

REQUIREDFILES="SSocket_client dijk_dscript.py dijkstra_p1"
for file in $REQUIREDFILES ; do
	echo -en "Checking for $file...\t\t"
	file_checkOnPath $file || \
	error "$A_fileCheck" "$EM_nofile" "$EC_nofile"
done

### Main:

function dshScript_generate
{
	#
	# ARGS
	#	$1		in		the seed vertex
	#	$2		in		distance
	#
	# DESC
	#	Generates a dsh script that produces a region label
	#	which is centered on a passed seed vertex and extends
	#	for a given distance.
	#
	
	local	seed=$1
	local	distance=$2
		
	echo "SURFACE active set 0"				> $DSHSCRIPT
	echo "LABEL activeSurface singleVertexSet $seed"	>> $DSHSCRIPT
	echo "LABEL ply set $distance"				>> $DSHSCRIPT
	echo "LABEL ply functionAssign 2"			>> $DSHSCRIPT
	echo "LABEL ply do"					>> $DSHSCRIPT
	echo "LABEL ply save area-${seed}-"			>> $DSHSCRIPT
}

# Generate a set of dsh scripts about each vertex
#	and execute them
seedVertices="40764 65557 20780 95730"
distance=30
for vertex in $seedVertices ; do
	echo -en "Generating a script about vertex $vertex, distance $distance..."
	dshScript_generate $vertex $distance
	echo -e "\t\t[ ok ]"
	echo -en "\tExecuting script...\t\t\t\t\t"
	dijk_dscript.py -s $DSHSCRIPT 2>/dev/null >/dev/null
	ret=$?
	ret_check $ret || error "$A_executing" "Some error occurred" 2
done

# Clean up all the spawned processes
dijk_dscript.py -c "TERM"

shutdown 0