#!/bin/sh
#
#Fake sclustermgr show cluster 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 "Class|Cluster|ControlHost|ControlPort|CPUCount|Flags|NodeCount|Cluster Nodes|PluginIDSelect|RPC|"
}

print_cluster()
{	
	#Clear values
	tmp_class=
	tmp_clus=
	tmp_chost=
	tmp_cport=
	tmp_cpus=
	tmp_flags=
	tmp_ncnt=
	tmp_nodes=
	tmp_pis=
	tmp_rpc=

	#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
		    classification)
			tmp_class=$val
			;;
		    cluster)
			tmp_clus=$val
			;;
		    controlhost)
			tmp_chost=$val
			;;
		    controlport)
			tmp_cport=$val
			;;
		    cpucount)
			tmp_cpus=$val
			;;
		    flags)
			tmp_flags=$val
			;;
		    nodecount)
			tmp_ncnt=$val
			;;
		    nodenames)
			tmp_nodes=$val
			;;
		    pluginidselect)
			tmp_pis=$val
			;;
		    rpc)
			tmp_rpc=$val
			;;
		    *)
			echo >&2 "Unrecognized parm $key, aborting"
			exit 1
			;;
		esac
	done
		    
	echo "$tmp_class|$tmp_clus|$tmp_chost|$tmp_cport|$tmp_cpus|$tmp_flags|$tmp_ncnt|$tmp_nodes|$tmp_pis|$tmp_rpc|"

}

print_yotta_cluster()
{	print_cluster \
		cluster='yottascale' classification='imaginary' \
		 controlhost='ys-master1' controlport=6817  \
		flags='virtual'  rpc=7168 pluginidselect=1 \
		nodecount=1000000  nodenames='compute-[0-99999]'  cpucount=20000000 
}

print_test_cluster()
{	print_cluster \
		cluster='test1'  classification='test' \
		 controlhost='test1-master' controlport=6817  \
		flags='debug'  rpc=7168 pluginidselect=1 \
		nodecount=2  nodenames='test-[0-1]'  cpucount=4 
}

print_specified_cluster()
{	cname=$1

	case $cname in
	   yottascale|yotta)
		print_yotta_cluster
		;;
	   test1|test)
		print_test_cluster
		;;
	   *)
		x=x
		#echo >&2 "Unknown cluster $usr"
		;;
	esac
}

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

print_all_clusters()
{	#Must do alphabetically
	print_clusters yottascale test1
}


print_no_clusters()
{	print_clusters 'no-such-cluster'
}

#Parse options
name_flag=

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

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

if [ "x${name_flag}" = "xyottascale" ]; then
	print_clusters yottascale
elif [ "x${name_flag}" = "xtest1" ]; then
	print_clusters test1
elif [ "x${name_flag}" = "x" ]; then
	print_all_clusters
else
	print_no_clusters
fi


