#!/bin/sh
#
#Fake sshare results for testing
#
# I think I got usage numbers to be self-consistent, but I don't really understand
# share numbers, so those are probably inconsistent.
#
# NOTE: any changes here need to be reflected in the Perl test data as well (e.g.
# file parse-help.pl)

#Order of fields is
#We always should be run with --parsable2 --noheaders --long
#Account|User|Raw Shares|Norm Shares|Raw Usage|Norm Usage|Effectv Usage|FairShare|GrpCPUMins|CPURunMins

verbose_flag=
DEBUG=
#Uncomment for help debugging script
#DEBUG=yes

debug()
{	if [ "x$DEBUG" = "xyes" ]; then
		echo >&2 "[DEBUG] $@"
	fi
}

print_header()
{
	echo "Account|User|Raw Shares|Norm Shares|Raw Usage|Norm Usage|Effectv Usage|FairShare|GrpCPUMins|CPURunMins"
}

print_record()
{	
	#Clear values
	tmp_acct=
	tmp_user=
	tmp_rshares=
	tmp_nshares=
	tmp_rusage=
	tmp_nusage=
	tmp_eusage=
	tmp_fshare=
	tmp_gcpumins=
	tmp_crunmins=
	tmpPREFIX=

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

		case $key in
		    account)
			tmp_acct=$val
			;;
		    user)
			tmp_user=$val
			;;
		    rawshares)
			tmp_rshares=$val
			;;
		    normshares)
			tmp_nshares=$val
			;;
		    rawusage)
			tmp_rusage=$val
			;;
		    normusage)
			tmp_nusage=$val
			;;
		    effusage)
			tmp_eusage=$val
			;;
		    fairshare)
			tmp_fshare=$val
			;;
		    grpcpumins)
			tmp_gcpumins=$val
			;;
		    crunmins)
			tmp_crunmins=$val
			;;
		    PREFIX)
			tmpPREFIX=`echo $val | sed -e 's/x/ /g'`
			;;
		    *)
			echo >&2 "Unrecognized parm $key, aborting"
			exit 1
			;;
		esac
	done
		    
	echo "${tmpPREFIX}$tmp_acct|$tmp_user|$tmp_rshares|$tmp_nshares|$tmp_rusage|$tmp_nusage|$tmp_eusage|$tmp_fshare|$tmp_gcpumins|$tmp_crunmins|"

}

sort_words()
{	#We need to sort (and uniq) lists of words
	#This is not the most elegant, but only uses tr, sort, and uniq, so
	#should be pretty portable
	words="$@"

	sorted=`echo $words | tr ' ' '\n' | sort | uniq | tr '\n' ' '`
	echo $sorted
}
	
#Fake output for yottascale cluster
print_recs_for_ys_acct_user()
{	accts=$1
	users=$2

	tmpaccts="$accts"
	tmpusers="$users"

	doroot=no
	#allaccts and allusers MUST be alphabetical
	allaccts="abc124 fbi nsa"
	allusers="george kevin payerle"

	#If no user given, should only print for current user, which we assume is george
	if [ "x$tmpusers" = "x" ]; then
		tmpusers="george"
		#And if no accounts given, all accounts get shown, even root
		if [ "x$tmpaccts" = "x" ]; then
			doroot=yes
			tmpaccts="$allaccts"
		fi
	fi

	#Handle special case for ALL-USERS
	if [ "x$tmpusers" = "xALL-USERS" ]; then
		tmpusers="$allusers"
	fi


	#If no accounts given, do all accounts (including root)
	if [ "x$tmpaccts" = "x" ]; then
		tmpaccts="$allaccts"
		doroot=yes
	fi

	#basePREFIX has an x for each space before userless account entry
	basePREFIX=
	if [ "x$doroot" = "xyes" ]; then
		debug "about to print ys, root,  ROOT"
		print_record \
			account=root \
			normshares=1.000000 \
			rawusage=42000000 \
			effusage=0.000000 \
			fairshare=1.000000 \
			crunmins=5700070
		#If doing root, all non-root accounts will have one space
		basePREFIX=x
	fi

	for acct in $allaccts
	do

		#Skip account if not in our list
		echo "$tmpaccts" | grep $acct > /dev/null
		if [ $? -ne 0 ]; then
			#acct not in tmpaccts, skip it
			continue
		fi

		#Have a valid account

		case $acct in
		   abc124)
			#Print the user-less account record
			debug "about to print ys, abc124, ROOT"
			print_record \
				PREFIX=$basePREFIX \
				account=$acct \
				rawshares=1 \
				normshares=0.333333 \
				rawusage=20000000 \
				normusage=0.476190 \
				effusage=0.476190 \
				fairshare=0.321144 \
				grpcpumins=12321153 \
				crunmins=3156000 
			#Now print user records for each specified user
			for user in $allusers
			do
				
				#Skip user if not in our list
				echo "$tmpusers" | grep $user > /dev/null
				if [ $? -ne 0 ]; then
					continue
				fi

				case $user in
				    george)
					debug "about to print ys, abc124, george"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=5049600 \
						normusage=0.120226 \
						effusage=0.120226 \
						grpcpumins=0.000000 \
						crunmins=315520
					;;
				    kevin)
					debug "about to print ys, abc124, kevin"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=11960320 \
						normusage=0.284770 \
						effusage=0.284770 \
						grpcpumins=0.000000 \
						crunmins=1420240
					;;
				    payerle)
					#Have multiple associations
					debug "about to print ys, abc124, payerle (1)"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=1495040 \
						normusage=0.035596 \
						effusage=0.035596 \
						grpcpumins=0.000000 \
						crunmins=236707
					debug "about to print ys, abc124, payerle (2)"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=996693 \
						normusage=0.023737 \
						effusage=0.023737 \
						grpcpumins=0.000000 \
						crunmins=473413
					debug "about to print ys, abc124, payerle (3)"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=498347 \
						normusage=0.011865 \
						effusage=0.011865 \
						grpcpumins=0.000000 \
						crunmins=710120
					;;
				esac
			done
			;;

		    fbi)
			#Print the user-less account record
			debug "about to print ys, fbi, ROOT"
			print_record \
				PREFIX=$basePREFIX \
				account=$acct \
				rawshares=1 \
				normshares=0.333333 \
				rawusage=16500000 \
				normusage=0.392857 \
				effusage=0.392857 \
				fairshare=0.500000 \
				grpcpumins=30000000 \
				crunmins=1234000
			#Now print user records for each specified user
			for user in $allusers
			do
				#Skip user if not in our list
				echo "$tmpusers" | grep $user > /dev/null
				if [ $? -ne 0 ]; then
					continue
				fi

				case $user in
				    george)
					debug "about to print ys, fbi, george"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=4125000 \
						normusage=0.098214 \
						effusage=0.098214 \
						grpcpumins=0.000000 \
						crunmins=308500
					;;
				    payerle)
					#Have multiple associations
					debug "about to print ys, fbi, payerle (1)"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=4125000 \
						normusage=0.098214 \
						effusage=0.098214 \
						grpcpumins=0.000000 \
						crunmins=308500
					debug "about to print ys, fbi, payerle (2)"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=4125000 \
						normusage=0.098214 \
						effusage=0.098214 \
						grpcpumins=0.000000 \
						crunmins=308500
					debug "about to print ys, fbi, payerle (3)"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=4125000 \
						normusage=0.098214 \
						effusage=0.098214 \
						grpcpumins=0.000000 \
						crunmins=308500
					;;
				esac
			done
			;;

		    nsa)
			#Print the user-less account record
			debug "about to print ys, nsa, ROOT"
			print_record \
				PREFIX=$basePREFIX \
				account=$acct \
				rawshares=1 \
				normshares=0.333333 \
				rawusage=5500000 \
				normusage=0.130952 \
				effusage=0.130952 \
				fairshare=0.500000 \
				grpcpumins=90000000 \
				crunmins=1111111
			#Now print user records for each specified user
			for user in $allusers
			do
				#Skip user if not in our list
				echo "$tmpusers" | grep $user > /dev/null
				if [ $? -ne 0 ]; then
					continue
				fi

				case $user in
				    george)
					debug "about to print ys, nsa, george"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.333333 \
						rawusage=5500000 \
						normusage=0.130952 \
						effusage=0.130952 \
						grpcpumins=0.000000 \
						crunmins=1111111
					;;
				esac
			done
			;;
		esac
	done
}

#Fake output for test cluster
print_recs_for_test_acct_user()
{	accts=$1
	users=$2

	tmpaccts="$accts"
	tmpusers="$users"

	doroot=no
	#allaccts and allusers MUST be alphabetical
	allaccts="abc124"
	allusers="george payerle"

	#If no user given, should only print for current user, which we assume is george
	if [ "x$tmpusers" = "x" ]; then
		tmpusers="george"
		#And if no accounts given, all accounts get shown, even root
		if [ "x$tmpaccts" = "x" ]; then
			doroot=yes
			tmpaccts=$allaccts
		fi
	fi

	#Handle special case for ALL-USERS
	if [ "x$tmpusers" = "xALL-USERS" ]; then
		tmpusers="$allusers"
	fi


	#If no accounts given, do all accounts (including root)
	if [ "x$tmpaccts" = "x" ]; then
		tmpaccts="$allaccts"
		doroot=yes
	fi

	#basePREFIX has an x for each space before userless account entry
	basePREFIX=
	if [ "x$doroot" = "xyes" ]; then
		debug "about to print test, root, ROOT"
		print_record \
			account=root \
			normshares=1.000000 \
			rawusage=12400 \
			effusage=0.000000 \
			fairshare=1.000000 \
			crunmins=248
		#If doing root, all non-root accounts will have one space
		basePREFIX=x
	fi

	for acct in $allaccts
	do
		echo "$tmpaccts" | grep $acct > /dev/null
		if [ $? -ne 0 ]; then
			#acct not in tmpaccts, skip it
			continue
		fi

		#Have a valid account

		case $acct in
		   abc124)
			#Print the user-less account record
			debug "about to print test, abc124, ROOT"
			print_record \
				PREFIX=$basePREFIX \
				account=$acct \
				rawshares=1 \
				normshares=1.000000 \
				rawusage=12400 \
				normusage=1.000000 \
				effusage=1.000000 \
				fairshare=0.321144 \
				grpcpumins=98765432 \
				crunmins=248
			#Now print user records for each specified user
			for user in $allusers
			do
				#Skip user if not in our list
				echo "$tmpusers" | grep $user > /dev/null
				if [ $? -ne 0 ]; then
					continue
				fi

				case $user in
				    george)
					debug "about to print test, abc124, george"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.500000 \
						rawusage=0 \
						normusage=0.000000 \
						effusage=0.000000 \
						grpcpumins=0.000000 \
						crunmins=0
					;;
				    payerle)
					#Have multiple associations
					debug "about to print test, abc124, payerle"
					print_record \
						PREFIX=x$basePREFIX \
						account=$acct \
						user=$user \
						rawshares=1 \
						normshares=0.500000 \
						rawusage=1240 \
						normusage=1.000000 \
						effusage=1.000000 \
						grpcpumins=0.000000 \
						crunmins=248
					;;
				esac
			done
			;;
		esac
	done
}



#Parse options
acct_flag=
user_flag=
clus_flag=
verbose_flag=

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

	case $arg in
	    --accounts=* )
		tmp=`echo $arg | sed -e 's/^--accounts=//' -e "s/'//g" -e 's/"//g' `
		acct_flag=$tmp
		;;
	    --users=* )
		tmp=`echo $arg | sed -e 's/^--users=//' -e "s/'//g" -e 's/"//g' `
		user_flag=$tmp
		;;
	    --clusters=* )
		tmp=`echo $arg | sed -e 's/^--clusters=//' -e "s/'//g" -e 's/"//g' `
		clus_flag=$tmp
		;;
	    --all )
		user_flag='ALL-USERS'
		;;
	    -v|--verbose)
		verbose_flag=yes
		;;
	esac
done


if [ "x$verbose_flag" = "xyes" ]; then
	print_header
fi

case $clus_flag in
    yottascale)
	print_recs_for_ys_acct_user "$acct_flag" "$user_flag"
	;;
    test)
	print_recs_for_test_acct_user "$acct_flag" "$user_flag"
	;;
    *)
	print_recs_for_ys_acct_user "$acct_flag" "$user_flag"
	;;
esac

