#!/bin/sh
#-------------------------------------------------------------------------
#
# clusterdb--
#    cluster a postgres database
#
#    This script runs psql with the "-c" option to cluster
#    the requested database.
#
# Copyright (c) 2002, PostgreSQL Global Development Group
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/Attic/clusterdb,v 1.8 2002/10/21 20:32:33 momjian Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename "$0"`
PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`

PSQLOPT=
table=
dbname=
alldb=
quiet=0

while [ "$#" -gt 0 ]
do
	case "$1" in
	--help|-\?)
		usage=t
		break
		;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
	-h*)
		PSQLOPT="$PSQLOPT $1"
		;;
	--host=*)
		PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
		;;
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
	-p*)
		PSQLOPT="$PSQLOPT $1"
		;;
	--port=*)
		PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
		;;
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
		shift;;
	-U*)
		PSQLOPT="$PSQLOPT $1"
		;;
	--username=*)
		PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
		;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
	--echo|-e)
		ECHOOPT="-e"
		;;
	--quiet|-q)
		ECHOOPT="$ECHOOPT -o /dev/null"
		quiet=1
		;;
	--dbname|-d)
		dbname="$2"
		shift;;
	-d*)
		dbname=`echo $1 | sed 's/^-d//'`
		;;
	--dbname=*)
		dbname=`echo $1 | sed 's/^--dbname=//'`
		;;
	-a|--alldb)
		alldb=1
		;;
# options converted into SQL command
	--table|-t)
		table="$2"
		shift;;
	-t*)
		table=`echo $1 | sed 's/^-t//'`
		;;
	--table=*)
		table=`echo $1 | sed 's/^--table=//'`
		;;
	-*)
		echo "$CMDNAME: invalid option: $1" 1>&2
		echo "Try '$CMDNAME --help' for more information." 1>&2
		exit 1
		;;
	*)
		dbname="$1"
		if [ "$#" -ne 1 ]; then
			echo "$CMDNAME: invalid option: $2" 1>&2
			echo "Try '$CMDNAME --help' for more information." 1>&2
			exit 1
		fi
		;;
	esac
	shift
done

if [ "$usage" ]; then	
	echo "$CMDNAME cluster all previously clustered tables in a database."
	echo
	echo "Usage:"
	echo "  $CMDNAME [OPTION]... [DBNAME]"
	echo
	echo "Options:"
	echo "  -a, --all                 cluster all databases"
	echo "  -d, --dbname=DBNAME       database to cluster"
	echo "  -t, --table='TABLE'       cluster specific table only"
	echo "  -e, --echo                show the command being sent to the backend"
	echo "  -q, --quiet               don't write any output"
	echo "  -v, --verbose             write a lot of output"
	echo " --help                     show this help, then exit"
	echo
	echo "Connection options:"
	echo "  -h, --host=HOSTNAME       database server host"
	echo "  -p, --port=PORT           database server port"
	echo "  -U, --username=USERNAME   user name to connect as"
	echo "  -W, --password            prompt for password"
	echo
	echo "Read the description of the SQL command CLUSTER for details."
	echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
	exit 0
fi

if [ "$alldb" ]; then
	if [ "$dbname" -o "$table" ]; then
		echo "$CMDNAME: cannot cluster all databases and a specific one at the same time" 1>&2
		exit 1
	fi
	dbname=`${PATHNAME}psql $PSQLOPT -q -t -A -d template1 -c 'SELECT datname FROM pg_database WHERE datallowconn'`
	[ "$?" -ne 0 ] && exit 1

elif [ -z "$dbname" ]; then
	if [ "$PGDATABASE" ]; then
		dbname="$PGDATABASE"
	elif [ "$PGUSER" ]; then
		dbname="$PGUSER"
	else
		dbname=`${PATHNAME}pg_id -u -n`
	fi
	[ "$?" -ne 0 ] && exit 1
fi

for db in $dbname
do
	[ "$alldb" ] && echo "Clustering $db"
	query="SELECT nspname, pg_class.relname, pg_class_2.relname FROM pg_class, pg_class AS pg_class_2 JOIN pg_namespace ON (pg_namespace.oid=relnamespace), pg_index WHERE pg_class.oid=pg_index.indrelid AND pg_class_2.oid=pg_index.indexrelid AND pg_index.indisclustered AND pg_class.relowner=(SELECT usesysid FROM pg_user WHERE usename=current_user)"
	if [ -z "$table" ]; then
		tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query"`
		[ "$?" -ne 0 ] && exit 1
	else
		# if tablename has a dot, use it as namespace separator
		if echo $table | grep -s '\.' 2>&1 >/dev/null
		then
			tbl=`echo $table | cut -d. -f2`
			nspc=`echo $table | cut -d. -f1`
			tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$tbl' AND nspname='$nspc'"`
			echo $tables
		else
			tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$table'"`
		fi
	fi
	query=
	for tabs in $tables
	do
		nspc=`echo $tabs | cut -d: -f1`
		tab=`echo $tabs | cut -d: -f2`
		idx=`echo $tabs | cut -d: -f3`
		query="$query CLUSTER $idx ON $nspc.$tab;"
	done
	${PATHNAME}psql $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';$query" -d $db
	if [ "$?" -ne 0 ]
	then
		echo "$CMDNAME: While clustering $db, the following failed: $query" 1>&2
	fi
done

exit 0
