#!/bin/ksh

#
# Maps onspaces to onutil
# Ver 1.0
#

#
# Add mirroring
#
#	add_mirror ${DBSPACE} \
#		${PATHNAME} ${OFFSET} ${SIZE} \
#		${MIRRORPATH} \
#		${MIRROROFFSET}
#
function add_mirror
{
set -x
echo $*
	onutil -e <<EOF
		ALTER
			DBSPACE ${1}
			START MIRRORING
			CHUNK "${2}"
			OFFSET ${3}
			MIRROR "${5}"
			OFFSET ${6}

		;

EOF
}


# Mirror space
#	mirror_off ${DBSPACE}
#
function mirror_off
{
	onutil -e <<EOF
		ALTER
			DBSPACE ${1}
			STOP MIRRORING
		;

EOF
}

#
# Add a chunk to a DBspace or BLOBspace
#	create_space ${SPACENAME} \
#			${TEMPFLAG} \
#			${PATHNAME} ${OFFSET} ${SIZE} \
#			${MIRRORPATH} ${MIRROROFFSET}
#
function create_space
{
	if [ "${2}" = "1" ]
	then
		TEMPSPACE="TEMP"
	else
		TEMPSPACE=""
	fi

	if [ "${6}" = "" ]
	then
		onutil -e <<EOF
			CREATE ${TEMPSPACE}
				DBSPACE ${1}
				CHUNK "${3}"
				OFFSET ${4}
				SIZE ${5}
				COSERVER 1
			;

EOF
	else
		onutil -e <<EOF
			CREATE ${TEMPSPACE}
				DBSPACE ${1}
				CHUNK "${3}"
				OFFSET ${4}
				SIZE ${5}
				MIRROR "${6}"
				OFFSET ${7}
				COSERVER 1
			;

EOF
	fi
}

#
# Add a chunk to a DBspace or BLOBspace
#	add_space ${SPACENAME} \
#			${PATHNAME} ${OFFSET} ${SIZE} \
#			${MIRRORPATH} ${MIRROROFFSET}
#
function add_space
{
	if [ "${5}" = "" ]
	then
		onutil -e <<EOF
			ALTER DBSPACE ${1}
				ADD CHUNK "${2}"
				OFFSET ${3}
				SIZE ${4}
			;

EOF
	else
		onutil -e <<EOF
			ALTER DBSPACE ${1}
				ADD CHUNK "${2}"
				OFFSET ${3}
				SIZE ${4}
				MIRROR "${5}"
				OFFSET ${6}
		;
EOF
	fi
}

#
# Drop DBSpace
# drop_dbspace ${DBSPACE} ${YES}
# alter cogroup cogroup_all reset backup;
#
function drop_dbspace
{
	onutil -e <<EOF
		DROP DBSPACE ${1}
		;
		alter cogroup cogroup_all reset backup;

EOF
}

#
# Drop Chunk
# drop_chunk ${DBSPACE} ${PATHNAME} ${OFFSET} ${Y_FLAG}
#
function drop_chunk
{
	onutil -e <<EOF
		ALTER DBSPACE ${1}
			DROP CHUNK "${2}"
			OFFSET ${3}
		;
		alter cogroup cogroup_all reset backup;

EOF
}

#
# Print out the usage
#
function usage
{
cat <<EOF
Usage:
 ${0}  { -a spacename -p pathname -o offset -s size [-m path offset]  |

             -c { -d DBspace [-t] |  -b BLOBspace -g pagesize }
                     -p pathname -o offset -s size [-m path offset] |

             -d spacename [-p pathname -o offset] [-f] [-y] |

             -f[y] off [DBspace-list] | on [DBspace-list] |

             -m spacename {-p pathname -o offset -m path offset [-y] |
                           -f filename} |

             -r spacename [-y] |

             -s spacename -p pathname -o offset {-O | -D} [-y] }

    -a - Add a chunk to a DBspace or BLOBspace
    -c - Create a DBspace or BLOBspace
    -d - Drop a DBspace, BLOBspace or chunk
    -f - Change dataskip default for specified DBspaces
    -m - Add mirroring to an existing DBspace or BLOBspace
    -r - Turn mirroring off for a DBspace or BLOBspace
    -s - Change the status of a chunk

EOF
}

#
# Main
#
TEMPFLAG=0
F_FLAG=0
O_FLAG=0
D_FLAG=0
Y_FLAG=0
MIRRORPATH=""
MIRROROFFSET=""

if [ $# = 0 ]
then
	#
	# Call the usage function
	#
	usage

	#
	# Exit: 0
	#
	exit 0
fi

#
# Process arguments
#
case ${1} in

#
# -a spacename -p pathname -o offset -s size [-m path offset]
#
"-a")
	while getopts a:p:o:s:m: ARG
	do
		case ${ARG} in
		#
		# -a spacename
		#
		"a")
			SPACENAME=${OPTARG}
			;;

		#
		# -p pathname
		#
		"p")
			PATHNAME=${OPTARG}
			;;

		#
		# -o offset
		#
		"o")
			OFFSET=${OPTARG}
			;;

		#
		# -s size
		#
		"s")
			SIZE=${OPTARG}
			;;

		#
		# -m path offset
		#
		"m")
			MIRRORPATH=${OPTARG}
			MIRROROFFSET=${11}
			;;
		esac
	done
	add_space ${SPACENAME} \
			${PATHNAME} ${OFFSET} ${SIZE} \
			${MIRRORPATH} ${MIRROROFFSET}
	;;

#
# -c
#
"-c")
	while getopts cd:tb:g:p:o:s:m: ARG
	do
		case ${ARG} in
		#
		# -c
		#
		"c")
			;;

		#
		# -d DBSpace
		#
		"d")
			DBSPACE=${OPTARG}
			;;

		#
		# -t
		#
		"t")
			TEMPFLAG=1
			;;

		#
		# -b BlobSpace
		#
		"b")
			BLOBSPACE=${OPTARG}
			echo "BlobSpaces not implemented"
			exit -1
			;;

		#
		# -g Pagesize
		#
		"g")
			PAGESIZE=${OPTARG}
			;;

		#
		# -p path
		#
		"p")
			PATHNAME=${OPTARG}
			;;

		#
		# -o offset
		#
		"o")
			OFFSET=${OPTARG}
			;;

		#
		# -s size
		#
		"s")
			SIZE=${OPTARG}
			;;

		#
		# -m path offset
		#
		"m")
			MIRRORPATH=${OPTARG}
			case $# in
			9)
				MIRROROFFSET=${9}
				;;
			10)
				MIRROROFFSET=${10}
				;;
			11)
				MIRROROFFSET=${11}
				;;
			12)
				MIRROROFFSET=${12}
				;;
			13)
				MIRROROFFSET=${13}
				;;
			14)
				MIRROROFFSET=${14}
				;;
			esac
			;;
		esac
	done
	create_space ${DBSPACE} \
			${TEMPFLAG} \
			${PATHNAME} ${OFFSET} ${SIZE} \
			${MIRRORPATH} ${MIRROROFFSET}
	;;

#
# -d - Drop a DBspace, BLOBspace or chunk
# -d spacename [-p pathname -o offset] [-f] [-y]
#
"-d")
	while getopts d:p:o:fy ARG
	do
		case ${ARG} in
		#
		# -d DBSpace
		#
		"d")
			DBSPACE=${OPTARG}
			;;

		#
		# -p path
		#
		"p")
			PATHNAME=${OPTARG}
			;;

		#
		# -o offset
		#
		"o")
			OFFSET=${OPTARG}
			;;

		#
		# -f
		#
		"f")
			F_FLAG=1
			;;

		#
		# -y
		#
		"y")
			Y_FLAG=1
			;;

		esac
	done

	if [ ${#} -ge 6 ]
	then
		drop_chunk ${DBSPACE} ${PATHNAME} ${OFFSET} ${Y_FLAG}
	else
		drop_dbspace ${DBSPACE}
	fi
	exit
	;;

#
# -f - Change dataskip default for specified DBspaces
# -f[y] off [DBspace-list]
# -f[y] on [DBspace-list]
#
"-f"|"-fy")
	case ${2} in
	"on"|"ON")
		DATASKIP=1
		;;
	"off"|"OFF")
		DATASKIP=0
		;;
	*)
		usage
		exit -1
		;;
	esac
	DBSPACE_LIST=${3}
	echo "Not implemented..."
	exit -1
	;;

#
# -m - Add mirroring to an existing DBspace or BLOBspace
# -m spacename {-p pathname -o offset -m path offset [-y] | -f filename}
#
"-m")
	DBSPACE=""

	while getopts m:p:s:o:yf: ARG
	do
		case ${ARG} in
		#
		# -m dbspace
		#
		"m")
			if [ "${DBSPACE}" = "" ]
			then
				DBSPACE=${OPTARG}
			else
				#
				# TODO: Get next arg
				# Constant '11' won't work
				#
				MIRRORPATH=${OPTARG}
				case $# in
				11)
					MIRROROFFSET=${11}
					;;
				esac
			fi
			;;

		#
		# -p path
		#
		"p")
			PATHNAME=${OPTARG}
			;;

		#
		# -o size
		#
		"o")
			OFFSET=${OPTARG}
			;;

		#
		# -s size
		#
		"s")
			SIZE=${OPTARG}
			;;

		#
		# -y
		#
		"y")
			Y_FLAG=1
			;;

		#
		# -f filename
		#
		"f")
			FILENAME=${OPTARG}
			;;
		esac
	done
	add_mirror ${DBSPACE} \
		${PATHNAME} ${OFFSET} ${SIZE} \
		${MIRRORPATH} \
		${MIRROROFFSET}
	;;

#
# -r - Turn mirroring off for a DBspace or BLOBspace
# -r spacename [-y]
#
"-r")
	while getopts r:y ARG
	do
		case ${ARG} in
		#
		# -r DBSpace
		#
		"r")
			DBSPACE=${OPTARG}
			;;

		#
		# -y
		#
		"y")
			Y_FLAG=1
			;;

		esac
	done
	mirror_off ${DBSPACE}
	;;

#
# -s - Change the status of a chunk
# -s spacename -p pathname -o offset {-O | -D} [-y]
#
"-s")
	while getopts s:p:o:s:ODy ARG
	do
		case ${ARG} in
		#
		# -s DBSpace
		#
		"s")
			DBSPACE=${OPTARG}
			;;

		#
		# -p path
		#
		"p")
			PATHNAME=${OPTARG}
			;;

		#
		# -o offset
		#
		"o")
			OFFSET=${OPTARG}
			;;

		#
		# -s size
		#
		"s")
			SIZE=${OPTARG}
			;;

		#
		# -y
		#
		"y")
			Y_FLAG=1
			;;

		#
		# -O
		#
		"O")
			O_FLAG=1
			;;
		#
		# -D
		#
		"D")
			D_FLAG=1
			;;
		esac
	done
	echo "Not implemented..."
	exit -1
	;;

\?)
	usage
	exit -1
	;;

*)
	usage
	exit -1
	;;
esac
exit


