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

#Order of fields is
#format=account,description,organization
# list account format=account,description,organization --parsable2 --noheader --readonly

verbose_flag=

print_header()
{
	echo "Cluster|Cluster Nodes|CPUs|Duration|End|Event|EventRaw|Node Name|Reason|Start|State|StateRaw|User|"
}

print_event()
{	
	#Clear values
	tmp_clus=
	tmp_cnodes=
	tmp_cpus=
	tmp_dur=
	tmp_end=
	tmp_event=
	tmp_eraw=
	tmp_nname=
	tmp_reas=
	tmp_start=
	tmp_state=
	tmp_sraw=
	tmp_user=

	#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
		    cluster)
			tmp_clus=$val
			;;
		    clusternodes)
			tmp_cnodes=$val
			;;
		    cpus)
			tmp_cpus=$val
			;;
		    duration)
			tmp_dur=$val
			;;
		    end)
			tmp_end=$val
			;;
		    event)
			tmp_event=$val
			;;
		    eventraw)
			tmp_eraw=$val
			;;
		    nodename)
			tmp_nname=$val
			;;
		    reason)
			tmp_reas=$val
			;;
		    start)
			tmp_start=$val
			;;
		    state)
			tmp_state=$val
			;;
		    stateraw)
			tmp_sraw=$val
			;;
		    user)
			tmp_user=$val
			;;
		    *)
			echo >&2 "Unrecognized parm $key, aborting"
			exit 1
			;;
		esac
	done
		    
	echo "$tmp_clus|$tmp_cnodes|$tmp_cpus|$tmp_dur|$tmp_end|$tmp_event|$tmp_eraw|$tmp_nname|$tmp_reas|$tmp_start|$tmp_state|$tmp_sraw|$tmp_user|"

}

print_event_1()
{	print_event \
		cluster='yottascale'  \
		clusternodes='compute-0'  nodename='compute-0.umd.edu' \
		cpus=20  \
		event='Node'  eventraw=2  state='DRAIN'  stateraw=514 \
		start='2016-01-01T00:00:00'  end='2016-01-01T18:00:00'  duration='18:00:00' \
		user='payerle'  reason='bad powersupply' 
}

print_event_2()
{	print_event \
		cluster='yottascale' \
		clusternodes='compute-[11-20]'  \
		cpus=200  \
		event='Cluster'  eventraw=1  state='DRAIN'  stateraw=514 \
		start='2016-01-05T00:00:00'  end='2016-01-05T19:00:00'  duration='19:00:00' \
		user='root'  reason='bad nw card in switch' 
}

print_event_3()
{	print_event \
		cluster='test1'  \
		clusternodes='test-0'  nodename='test-0' \
		cpus=2  \
		event='Node'  eventraw=2  state='DRAIN'  stateraw=514 \
		start='2016-02-05T00:00:00'  end='2016-02-05T13:00:00'  duration='13:00:00' \
		user='payerle'  reason='no reason' 
}


print_specified_event()
{	eid=$1

	case $eid in
	   1)
		print_event_1
		;;
	   2)
		print_event_2
		;;
	   3)
		print_event_3
		;;
	   *)
		x=x
		;;
	esac
}

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

print_all_events()
{	#Must do alphabetically
	print_events 1 2 3 
}


print_ys_events()
{	#Print all events with account=root
	print_events 1 2
}

print_test1_events()
{	#Print all events with account=abc124
	print_events 3 
}

print_root_user_events()
{	#Print all events with user=root
	print_events  2
}

print_payerle_events()
{	#Print all events with user=payerle
	print_events  1 3
}

print_ys_root_events()
{	#Print all events with account=root, user=root
	print_events 2
}

print_ys_payerle_events()
{	#Print all events with account=abc124, user=payerle
	print_events 1
}

print_test1_payerle_events()
{	#Print all events with account=abc124, user=payerle
	print_events 3
}

print_no_events()
{	print_events 'no-such-event'
}

#Parse options
cluster_flag=
user_flag=

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

	case $arg in
	    cluster=* )
		tmp=`echo $arg | sed -e 's/^cluster=//' -e "s/'//g" -e 's/"//g' `
		cluster_flag=$tmp
		;;
	    user=* )
		tmp=`echo $arg | sed -e 's/^user=//' -e "s/'//g" -e 's/"//g' `
		user_flag=$tmp
		;;
	    -v|--verbose)
		verbose_flag=yes
		;;
	esac
done

if [ "x${cluster_flag}" != "x" ]; then
	#cluster requested, possibly with user
	if [ "x${user_flag}" = "x" ]; then
		if [ "x${cluster_flag}" = "xyottascale" ]; then
			print_ys_events
		elif [ "x${cluster_flag}" = "xtest1" ]; then
			print_test1_events
		else
			print_no_events
		fi
	elif [ "x${user_flag}" = "xroot" ]; then
		if [ "x${cluster_flag}" = "xyottascale" ]; then
			print_ys_root_events
		else
			print_no_events
		fi
	elif [ "x${user_flag}" = "xpayerle" ]; then
		if [ "x${cluster_flag}" = "xyottascale" ]; then
			print_ys_payerle_events
		elif [ "x${cluster_flag}" = "xtest1" ]; then
			print_test1_payerle_events
		else
			print_no_events
		fi
	else
		print_no_events
	fi
elif [ "x${user_flag}" = "xroot" ]; then
	print_root_user_events
elif [ "x${user_flag}" = "xpayerle" ]; then
	print_payerle_events
elif [ "x${user_flag}" != "x" ]; then
	print_no_events
else
	#No flags, print all accounts
	print_all_events
fi


