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

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

verbose_flag=

print_header()
{
	echo "Action|Actor|Info|Time|Where|"
}


print_transaction()
{	action="$1"
	actor="$2"
	info="$3"
	timestamp="$4"
	where="$5"
	echo "$action|$actor|$info|$timestamp|$where|"
}

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

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

print_trans_3()
{	print_transaction \
	 	'Add Associations' \
		'root' \
		"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,'" \
		'2016-01-08T09:54:25' \
		'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
	    actor=* )
		tmp=`echo $arg | sed -e 's/^actor=//' -e "s/'//g" -e 's/"//g' `
		actor_flag=$tmp
		;;
	    -v|--verbose )
		verbose_flag=yes
		;;
	esac
done

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

