#!/bin/sh
#
#Fake sacctmgr show transaction results for testing

#Order of fields is
#format=adminlevel,defaultaccount,transaction 

#Updated to handle preTRES (Slurm v 14.x.y) and postTRES (Slurm 15.x.y) data
#USe env var FAKE_SLURM_VERSION to set this
#Also accepts --version and format= arguments

#Order of fields is now controllable via format option
#Below is the default if no format given
FORMAT_FIELDS="timestamp action actor where info"

verbose_flag=

print_version()
{	version=$1
	cat - <<EOF
slurm $version
EOF
	exit 0
}

print_header()
{
	tmptext=
	for fld in $FORMAT_FIELDS
	do
		case $fld in
		#-----	Common stuff
		    action)
			tmptext="${tmptext}Action"
			;;
		    actor)
			tmptext="${tmptext}Actor"
			;;
		    info)
			tmptext="${tmptext}Info"
			;;
		    timestamp)
			tmptext="${tmptext}Time"
			;;
		    where)
			tmptext="${tmptext}Where"
			;;
		#-----	no preTRES stuff
		#-----	no postTRES stuff
		#-----	ERROR
		    *)
			echo >&2 "Unrecognized field name $fld in format string, aborting"
			exit 1;
			;;
		esac
		tmptext="${tmptext}|"
	done
	echo $tmptext
}


print_transaction()
{	
	#Clear values
	#	common
	tmp_action=
	tmp_actor=
	tmp_info=
	tmp_timestamp=
	tmp_where=

	#no preTRES stuff

	#no postTRES stuff


	#Set values
	while [ $# -gt 0 ]
	do
		key=$1
		val=$1
		shift
		key=`echo $key | sed -e 's/=.*$//'`
		val=`echo $val | sed -e 's/^[^=]*=//'`
		#echo >&2 "$key = $val"

		case $key in
		#----	Common fields
		    action)
			tmp_action=$val
			;;
		    actor)
			tmp_actor=$val
			;;
		    info)
			tmp_info=$val
			;;
		    timestamp)
			tmp_timestamp=$val
			;;
		    where)
			tmp_where=$val
			;;

		#----	no preTRES fields
		#----	no postTRES fields
		#----	ERROR
		    *)
			echo >&2 "Unrecognized parm $key, aborting"
			exit 1
			;;
		esac
	done
		    
	#Print values
	tmptext=
	for fld in $FORMAT_FIELDS
	do
		case $fld in
		#----	common fields
		    action)
			tmptext="${tmptext}${tmp_action}"
			;;
		    actor)
			tmptext="${tmptext}${tmp_actor}"
			;;
		    info)
			tmptext="${tmptext}${tmp_info}"
			;;
		    timestamp)
			tmptext="${tmptext}${tmp_timestamp}"
			;;
		    where)
			tmptext="${tmptext}${tmp_where}"
			;;

		#----	no preTRES fields

		#----	no postTRES fields

		#----	ERROR
		    *)
			echo >&2 "Unrecognized field name $fld in format string, aborting"
			exit 1;
			;;
		esac
		tmptext="${tmptext}|"
	done
	echo $tmptext


}

print_trans_1()
{	print_transaction \
		action='Modify Clusters' \
		actor='slurm' \
		info="control_host='10.99.1.1', control_port=6817, last_port=6817, rpc_version=7168, dimensions=1, plugin_id_select=101, flags=0" \
		timestamp='2016-01-01T10:00:00' \
		where="(name='yottascale')"
}

print_trans_2()
{	print_transaction \
		action='Modify Associations' \
		actor='root' \
		info='grp_cpu_mins=1000000' \
		timestamp='2016-01-02T11:30:31' \
		where="(id_assoc=107)" 
}

print_trans_3()
{	print_transaction \
	 	action='Add Associations' \
		actor='root' \
		info="mod_time=1447858465, acct='physics', user='alavirad', \`partition\`='standard', shares=1, grp_cpu_mins=NULL, grp_cpu_run_mins=NULL, grp_cpus=NULL, grp_jobs=NULL, grp_mem=NULL, grp_nodes=NULL, grp_submit_jobs=NULL, grp_wall=NULL, is_def=1, max_cpu_mins_pj=NULL, max_cpu_run_mins=NULL, max_cpus_pj=NULL, max_jobs=NULL, max_nodes_pj=NULL, max_submit_jobs=NULL, max_wall_pj=NULL, def_qos_id=NULL, qos=',6,8,7,9,5,4,'" \
		timestamp='2016-01-08T09:54:25' \
		where='id_assoc=4741' 
}

print_specified_transaction()
{	tid=$1

	case $tid in
	   1)
		print_trans_1
		;;
	   2)
		print_trans_2
		;;
	   3)
		print_trans_3
		;;
	   *)
		x=x
		#echo >&2 "Unknown transaction $usr"
		;;
	esac
}

print_transactions()
{	#We always have --noheader
	if [ "x$verbose_flag" = "xyes" ]; then
		print_header
	fi
	while [ $# -gt 0 ]
	do
		tid=$1
		shift
		print_specified_transaction $tid
	done
}

print_all_transactions()
{	
	print_transactions 1 2 3
}

print_no_transactions()
{	
	print_transactions no-such-transaction
}

#Parse options
actor_flag=

while [ $# -gt 0 ]
do
	arg=$1
	shift

	case $arg in
	    --version)
		#Print version and exit
		if [ "x$FAKE_SLURM_VERSION" = "x" ]; then
			print_version 14
		else
			print_version $FAKE_SLURM_VERSION
		fi
		exit 0
		;;
	    format=*)
		#Set our format string
		tmp=`echo $arg | sed -e 's/^format=//' -e "s/'//g" -e 's/"//g' -e 's/,/ /g'`
		FORMAT_FIELDS=$tmp
		;;
	    actor=* )
		tmp=`echo $arg | sed -e 's/^actor=//' -e "s/'//g" -e 's/"//g' `
		actor_flag=$tmp
		;;
	    action=* )
		tmp=`echo $arg | sed -e 's/^action=//' -e "s/'//g" -e 's/"//g' `
		action_flag=$tmp
		;;
	    -v|--verbose )
		verbose_flag=yes
		;;
	esac
done

if [ "x${actor_flag}" != "x" ]; then
	#actor requested
	case $actor_flag in
	    root)
		if [ "x${action_flag}" = "x" ]; then
			print_transactions 2 3
		else
			case $action_flag in
			   'Modify Associations')
				print_transactions 2
				;;
			   'Add Associations')
				print_transactions 3
				;;
			   *)
				print_no_transactions
				;;
			esac
		fi
		;;
	    slurm)
		if [ "x${action_flag}" = "x" ]; then
			print_transactions 1
		else
			case $action_flag in
			   'Modify Clusters')
				print_transactions 1
				;;
			   *)
				print_no_transactions
				;;
			esac
		fi
		;;
	    *)
		print_no_transactions
		;;
	esac
elif [ "x${action_flag}" != "x" ]; then
	case $action_flag in
	    'Modify Clusters')
		print_transactions 1
		;;
	    'Modify Associations')
		print_transactions 2
		;;
	    'Add Associations')
		print_transactions 3
		;;
	    *)
		print_no_transactions
		;;
	esac
else
	print_all_transactions
fi

