#!/bin/sh
#-------------------------------------------------------------------------
#
# vacuumdb--
#    vacuum a postgres database
#
#    This script runs psql with the "-c" option to vacuum
#    the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/vacuumdb,v 1.1 1999/12/04 04:53:21 momjian Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

PSQLOPT=
verbose=
analyze=
table=

while [ $# -gt 0 ]
do
    case "$1" in
	--help|-\?)
		usage=t
		break
		;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
	--user|--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
		shift;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
	--echo|-e)
		PSQLOPT="$PSQLOPT -e"
		;;
	--quiet|-q)
		PSQLOPT="$PSQLOPT -o /dev/null"
		;;
	--dbname|--database|-d)
		dbname="$2"
		shift;;
# options converted into SQL command
	--analyze|-z)
		analyze="analyze"
		;;
	--table|-t)
		table=$2
		shift;;
	--verbose|-v)
		verbose="verbose"
		;;

	-*)
		echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
		exit 1
		;;

	 *)
		dbname="$1"
		;;
    esac
    shift
done


if [ "$usage" ]; then
	echo "Usage: $CMDNAME [-h <server>] [-p <port>] [-U <username>] [-W] [-d <dbname>] \\"
	echo "       [-z|--analyze] [-v|--verbose] [-t|--table 'table[(columns)]'] [dbname]"
	exit 0
fi

if [ -z "$dbname" ]; then
	echo "$CMDNAME: Missing required argument database name. Try -? for help."
	exit 1
fi

psql $PSQLOPT -d "$dbname" -c "VACUUM $verbose $analyze $table"

if [ $? -ne 0 ]; then
	echo "$CMDNAME: Database vacuum failed."
	exit 1
fi

exit 0
