#!/bin/ksh

#
# check_catalogs ${DBMS}
#
function check_catalogs
{
	onutil -e <<-EOF
		CHECK CATALOGS ${1}
		;

	EOF
}

#
# dump_page ${TABLESPACE} ${PAGE}
#
function dump_page
{
	if [ "${2}" = "" ]
	then
		PAGE=""
	else
		PAGE=" PAGE ${2} "
	fi

	onutil -e <<-EOF
		CHECK TABLE DATA
			WITH BLOBS
			IN TABLESPACE ${1}
			${PAGE}
			DISPLAY DATA
		;

	EOF
}

#
# check_reserved {check|print}
#
function check_reserved
{
	DISPLAY_DATA=""
	if [ "${1}" = "print" ]
	then
		DISPLAY_DATA=" DISPLAY DATA "
	fi
	onutil -e <<-EOF
		CHECK RESERVED ${DISPLAY_DATA}
		;

	EOF
}

#
# check_space {check|print}
#
function check_space
{
	DISPLAY_DATA=""
	if [ "${1}" = "print" ]
	then
		DISPLAY_DATA=" DISPLAY DATA "
	fi
	onutil -e <<-EOF
		CHECK SPACE ${DISPLAY_DATA}
		;

	EOF
}

#
# check_index_keys check {i|I} ${DBMS} ${OWNER} ${TABLE} ${INDEX}
#                  1     2     3       4        5       6
#
function check_index_keys
{
#set -x
	DISPLAY_DATA=""
	if [ "${1}" = "print" ]
	then
		DISPLAY_DATA=" DISPLAY DATA "
	fi

	WITH_DATA=""
	if [ "${2}" = "I" ]
	then
		WITH_DATA=" WITH DATA "
	fi

	#
	# All NULL
	#
	#echo NULL
	if 	[ "${3}" = "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" = "" ] &&
	 	[ "${6}" = "" ]
	then

		onutil -e <<-EOF
			CHECK INDEX ${WITH_DATA} ${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS
	#
	#echo DBMS
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" = "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK INDEX ${WITH_DATA}
			DATABASE ${3}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:TABLE
	#
	#echo DBMS:TABLE
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK INDEX ${WITH_DATA}
			${3}:${5}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:OWNER.TABLE
	#
	#echo DBMS:OWNER.TABLE
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" <> "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK INDEX ${WITH_DATA}
			${3}:${4}.${5}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:TABLE#IDX
	#
	#echo DBMS:TABLE#IDX
	if 	[ "${3}" <> "" ] &&
	 	# [ "${4}" <> "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" <> "" ]
	then
		onutil -e <<-EOF
			CHECK INDEX ${WITH_DATA}
			DATABASE ${3} INDEX ${6}
			${DISPLAY_DATA}
		;
		EOF
		return
	fi

}

#
# check_table ${PC_FLAG} ${T_FLAG} "${DBMS}" "${OWNER}" "${TABLE}"
"${FRAGDBS}"
#                  1     2          3         4          5          6
#
function check_table
{
#set -x
	DISPLAY_DATA=""
	if [ "${1}" = "print" ]
	then
		DISPLAY_DATA=" DISPLAY DATA "
	fi

	ALLOCATION=""
	if [ "${2}" = "T" ]
	then
		ALLOCATION=" ALLOCATION "
	fi

	#
	# All NULL
	#
	#echo NULL
	if 	[ "${3}" = "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" = "" ] &&
	 	[ "${6}" = "" ]
	then
		usage
		return
	fi

	#
	# DBMS
	#
	#echo DBMS
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" = "" ] &&
	 	[ "${6}" = "" ]
	then
		usage
		return
	fi

	#
	# DBMS:TABLE
	#
	#echo DBMS:TABLE
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK TABLE ${ALLOCATION} INFO
			${3}:${5}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:OWNER.TABLE
	#
	#echo DBMS:OWNER.TABLE
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" <> "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK TABLE ${ALLOCATION} INFO
			${3}:${4}.${5}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:TABLE#IDX
	#
	#echo DBMS:TABLE#IDX
	if 	[ "${3}" <> "" ] &&
	 	# [ "${4}" <> "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" <> "" ]
	then
		onutil -e <<-EOF
			CHECK TABLE ${ALLOCATION} INFO
			${3}:${4}.${5},${6}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

}

#
# check_data ${PC_FLAG} ${D_FLAG} "${DBMS}" "${OWNER}" "${TABLE}"
"${FRAGDBS}"
#                  1     2          3         4          5          6
#
function check_data
{
#set -x
	DISPLAY_DATA=""
	if [ "${1}" = "print" ]
	then
		DISPLAY_DATA=" DISPLAY DATA "
	fi

	REPAIR=""
	if [ "${1}" = "check" ]
	then
		DISPLAY_DATA=" "
		REPAIR=" REPAIR "
	fi

	WITH_BLOBS=""
	if [ "${2}" = "D" ]
	then
		WITH_BLOBS=" WITH BLOBS "
	fi

	#
	# All NULL
	#
	#echo NULL
	if 	[ "${3}" = "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" = "" ] &&
	 	[ "${6}" = "" ]
	then
		usage
		return
	fi

	#
	# DBMS
	#
	#echo DBMS
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" = "" ] &&
	 	[ "${6}" = "" ]
	then
		usage
		return
	fi

	#
	# DBMS:TABLE
	#
	#echo DBMS:TABLE
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" = "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK TABLE DATA ${WITH_BLOBS}
			${3}:${5}
			${REPAIR}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:OWNER.TABLE
	#
	#echo DBMS:OWNER.TABLE
	if 	[ "${3}" <> "" ] &&
	 	[ "${4}" <> "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" = "" ]
	then
		onutil -e <<-EOF
			CHECK TABLE DATA ${WITH_BLOBS}
			${3}:${4}.${5}
			${REPAIR}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

	#
	# DBMS:TABLE#IDX
	#
	#echo DBMS:TABLE,FRAGDBS
	if 	[ "${3}" <> "" ] &&
	 	# [ "${4}" <> "" ] &&
	 	[ "${5}" <> "" ] &&
	 	[ "${6}" <> "" ]
	then
		onutil -e <<-EOF
			CHECK TABLE DATA ${WITH_BLOBS}
			${3}:${4}.${5},${6}
			${REPAIR}
			${DISPLAY_DATA}
			;

		EOF
		return
	fi

}

#
# Print out the usage
#
function usage
{
cat <<-EOF
ONCHECK
Usage:  oncheck {-cCheckOption | -pPrintOption} [-y | -n] [-q]
                [ { database[:[owner.]table[,fragdbs|#index]] | TBLspace
number

                | Chunk number } { rowid | page number } ]

-c      - CheckOption
    r   - reserved pages
    R   - reserved pages including logical and physical logs
    e   - extents
    c   - database catalogs [database]
    i   - table indexes database[:[owner.]table[#index]]
    I   - table indexes and rowids in index database[:[owner.]table[#index]]
    w   - Do not place share lock on table during index check
    d   - TBLspace data rows including bitmaps
          database[:[owner.]table[,fragdbs]]
    D   - TBLspace data rows including bitmaps, remainder pages and BLOBs
          database[:[owner.]table[,fragdbs]]

-p      - PrintOption
    r   - reserved pages (-cr)
    R   - reserved pages including logical and physical logs (-cR)
    e   - extents report (-ce)
    c   - catalog report (-cc) [database]
    k   - keys in index (-ci) database[:[owner.]table[#index]]
    K   - keys and rowids in index (-cI) database[:[owner.]table[#index]]
    l   - leaf node keys only (-ci) database[:[owner.]table[#index]]
    L   - leaf node keys and rowids (-cI) database[:[owner.]table[#index]]
    w   - Do not place share lock on table during index check
    d   - TBLspace data rows (-cd) database[:[owner.]table[,fragdbs]]
[rowid]
    D   - TBLspace data rows including bitmaps, remainder pages and BLOBs
(-cD)
          database[:[owner.]table[,fragdbs]] [page number]
    t   - TBLspace report database[:[owner.]table[,fragdbs]]
    T   - TBLspace disk utilization report
database[:[owner.]table[,fragdbs]]
    p   - Dump page for the given
          [table[,fragdbs] and rowid | TBLspace and page number]
    P   - Dump page for the given chunk number and page number
          [chunk num and page number]
    B   - BLOBspace utilization for given table(s)
          database[:[owner.]table[,fragdbs]]

-q      - Quiet mode - print only error messages
-n      - Answer NO to all questions
-y      - Answer YES to all questions
EOF
}

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

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

	#
	# Exit: 0
	#
	exit 0
fi

#
# Process arguments
#
case ${1} in

#
#CHECK TABLE DATA
#    d   - TBLspace data rows including bitmaps
#          database[:[owner.]table[,fragdbs]]
#    D   - TBLspace data rows including bitmaps, remainder pages and BLOBs
#          database[:[owner.]table[,fragdbs]]
#
#CHECK SPACE
#	-e
#CHECK RESERVED
#    r   - reserved pages
#    R   - reserved pages including logical and physical logs
#
#DISPLAY PAGE
#    p   - Dump page for the given
#          [table[,fragdbs] and rowid | TBLspace and page number]
#    P   - Dump page for the given chunk number and page number
#          [chunk num and page number]

#
# CHECK CATALOGS
#
"-cc"|"-pc")
	if [ ${#} -eq 2 ]
	then
		DBMS=${2}
	fi
	check_catalogs ${DBMS}
	;;

#
# CHECK RESERVED
#
"-cr"|"-cR")
	check_reserved check
	;;

#
# PRINT RESERVED
#
"-pr"|"-pR")
	check_reserved print
	;;

#
# CHECK SPACE
#
"-ce")
	check_space check
	;;

#
# PRINT SPACE
#
"-pe")
	check_space print
	;;

#
# PRINT RESERVED
#
"-pr"|"-pR")
	check_reserved print
	;;

#
# CHECK INDEX KEYS [ WITH DATA ]
# database
# database:table
# database:owner.table
# database:table#idxname
# database:owner.table#idxname
#
"-ci"|"-cI"|"-pi"|"-pI")
	case ${1} in
	"-ci")
		I_FLAG="i"
		PC_FLAG="check"
		;;
	"-pi")
		I_FLAG="i"
		PC_FLAG="print"
		;;
	"-cI")
		I_FLAG="I"
		PC_FLAG="check"
		;;
	"-pI")
		I_FLAG="I"
		PC_FLAG="print"
		;;
	*)
		I_FLAG="i"
		PC_FLAG="print"
	esac

	DBMSCNT=`echo ${2} | grep -c ":"`
	if [ ${DBMSCNT} -ne 0 ]
	then
		DBMS=`echo ${2} | sed -e "s/:.*$//"`
		OWNCNT=`echo ${2} | grep -c "\."`
		if [ ${OWNCNT} -ne 0 ]
		then
			OWNER=`echo ${2} | \
				sed -e "s/\..*$//" | \
				sed -e "s/.*://" `
			TABLE=`echo ${2} | sed -e "s/.*\.//"`
		else
			TABLE=`echo ${2} | sed -e "s/.*://"`
			OWNER=""
		fi

		IDXCNT=`echo ${2} | grep -c "#"`
		if [ ${IDXCNT} -ne 0 ]
		then
			INDEX=`echo ${2} | \
				sed -e "s/.*#//" `
			TABLE=`echo ${TABLE} | sed -e "s/#.*//"`
		else
			INDEX=""
		fi
	else
		DBMS=${2}
		OWNER=""
		TABLE=""
		INDEX=""
	fi

	#echo D $DBMS
	#echo O $OWNER
	#echo T $TABLE
	#echo I $INDEX
	#exit
	check_index_keys ${PC_FLAG} ${I_FLAG} "${DBMS}" "${OWNER}" "${TABLE}"
"${INDEX}"
	;;

#
# CHECK TABLE
# database
# database:table
# database:owner.table
# database:table,fragdbs
# database:owner.table,fragdbs
#
"-pt"|"-pT")
	case ${1} in
	"-pt")
		T_FLAG="t"
		PC_FLAG="print"
		;;
	"-pT")
		T_FLAG="I"
		PC_FLAG="print"
		;;
	*)
		T_FLAG="t"
		PC_FLAG="print"
	esac

	DBMSCNT=`echo ${2} | grep -c ":"`
	if [ ${DBMSCNT} -ne 0 ]
	then
		FRAGCNT=`echo ${2} | grep -c ","`
		if [ ${FRAGCNT} -ne 0 ]
		then
			FRAGDBS=`echo ${2} | \
				sed -e "s/.*,//" `
			TEMP=`echo ${2} | \
				sed -e "s/,.*//" `
		else
			FRAGDBS=""
			TEMP=${2}
		fi

		DBMS=`echo ${TEMP} | sed -e "s/:.*$//"`
		OWNCNT=`echo ${TEMP} | grep -c "\."`
		if [ ${OWNCNT} -ne 0 ]
		then
			OWNER=`echo ${TEMP} | \
				sed -e "s/\..*$//" | \
				sed -e "s/.*://" `
			TABLE=`echo ${TEMP} | sed -e "s/.*\.//"`
		else
			TABLE=`echo ${TEMP} | sed -e "s/.*://"`
			OWNER=""
		fi
	else
		DBMS=${2}
		OWNER=""
		TABLE=""
		FRAGDBS=""
	fi

	#echo D $DBMS
	#echo O $OWNER
	#echo T $TABLE
	#echo F $FRAGDBS
	#exit
	check_table ${PC_FLAG} ${T_FLAG} "${DBMS}" "${OWNER}" "${TABLE}"
"${FRAGDBS}"
	;;

#
# CHECK DATA
# database
# database:table
# database:owner.table
# database:table,fragdbs
# database:owner.table,fragdbs
#
"-pd"|"-pD"|"-cd"|"-cD")
	case ${1} in
	"-pd")
		D_FLAG="d"
		PC_FLAG="print"
		;;
	"-pD")
		D_FLAG="D"
		PC_FLAG="print"
		;;
	"-cd")
		D_FLAG="d"
		PC_FLAG="check"
		;;
	"-cD")
		D_FLAG="D"
		PC_FLAG="check"
		;;
	*)
		D_FLAG="d"
		PC_FLAG="print"
	esac

	DBMSCNT=`echo ${2} | grep -c ":"`
	if [ ${DBMSCNT} -ne 0 ]
	then
		FRAGCNT=`echo ${2} | grep -c ","`
		if [ ${FRAGCNT} -ne 0 ]
		then
			FRAGDBS=`echo ${2} | \
				sed -e "s/.*,//" `
			TEMP=`echo ${2} | \
				sed -e "s/,.*//" `
		else
			FRAGDBS=""
			TEMP=${2}
		fi

		DBMS=`echo ${TEMP} | sed -e "s/:.*$//"`
		OWNCNT=`echo ${TEMP} | grep -c "\."`
		if [ ${OWNCNT} -ne 0 ]
		then
			OWNER=`echo ${TEMP} | \
				sed -e "s/\..*$//" | \
				sed -e "s/.*://" `
			TABLE=`echo ${TEMP} | sed -e "s/.*\.//"`
		else
			TABLE=`echo ${TEMP} | sed -e "s/.*://"`
			OWNER=""
		fi
	else
		DBMS=${2}
		OWNER=""
		TABLE=""
		FRAGDBS=""
	fi

	#echo D $DBMS
	#echo O $OWNER
	#echo T $TABLE
	#echo F $FRAGDBS
	#exit
	check_data ${PC_FLAG} ${D_FLAG} "${DBMS}" "${OWNER}" "${TABLE}"
"${FRAGDBS}"
	;;

#
# DUMP PAGE
# database
# database:table
# database:owner.table
# database:table,fragdbs
# database:owner.table,fragdbs
#
"-pp")

	case  ${#} in
	1)
		usage
		exit
		;;
	2)
		TABLESPACE=${2}
		PAGE=""
		;;
	3)
		TABLESPACE=${2}
		PAGE=${3}
		;;
	*)
		usage
		exit
		;;
	esac

	dump_page "${TABLESPACE}" "${PAGE}"
	;;


\?)
	usage
	exit -1
	;;

*)
	usage
	exit -1
	;;
esac
exit


