#!/bin/sh
#-------------------------------------------------------------------------
#
# createlang--
#    Remove a procedural language from a database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/droplang,v 1.4 1999/12/16 20:10:02 momjian Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

PSQLOPT=
dbname=
langname=
echo=
list=

# Check for echo -n vs echo \c

if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi


# ----------
# Get options, language name and dbname
# ----------
while [ $# -gt 0 ]
do
    case "$1" in 
	--help|-\?)
		usage=t
		;;
        --list|-l)
                list=t
                ;;
# 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=//'`
                ;;
	--user|--username|-U)
		PSQLOPT="$PSQLOPT -U '$2'"
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --user=*)
                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--user=//'`
                ;;
        --username=*)
                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
                ;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
	--echo|-e)
                echo=t
		;;
	--dbname|--database|-d)
		dbname="$2"
		shift;;
        -d*)
                dbname=`echo $1 | sed 's/^-d//'`
                ;;
        --dbname=*)
                dbname=`echo $1 | sed 's/^--dbname=//'`
                ;;
        --database=*)
                dbname=`echo $1 | sed 's/^--database=//'`
                ;;

	 *)
 		langname="$1"
                if [ "$2" ]; then
                        shift
			dbname="$1"
		fi
                ;;
    esac
    shift
done


if [ "$usage" ]; then
	echo ""
	echo "Usage: $CMDNAME [options] [language [dbname]]"
	echo ""
	echo "    -h HOSTNAME, --host=HOSTNAME     "
	echo "    -p PORT,     --port=PORT         "
	echo "    -u USERNAME, --username=USERNAME "
	echo "    -W,          --password          "
	echo "    -d DBNAME    --database=DBNAME   "
	echo "    -e,          --echo              "
        echo "    -q,          --quiet             "   
	echo "    -?,          --help              "
	echo ""
	exit 1
fi


if [ "$list" ]; then
        psql $PSQLOPT -d "$dbname" -c "SELECT lanname, lanpltrusted, lancompiler FROM pg_language WHERE lanispl = 't'"
        exit $?
fi


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


# ----------
# If not given on the commandline, ask for the language
# ----------
if [ -z "$langname" ]; then
	$ECHO_N "Language to remove from database $dbname: "$ECHO_C
	read langname
fi

# ----------
# Check if supported and set related values
# ----------
case "$langname" in
	plpgsql)
                lancomp="PL/pgSQL"
		handler="plpgsql_call_handler"
                ;;
	pltcl)
		lancomp="PL/Tcl"
		handler="pltcl_call_handler"
                ;;
	*)
		echo "$CMDNAME: Unsupported language '$langname'."
		echo "          Supported languages are 'plpgsql' and 'pltcl'."
		exit 1
                ;;
esac


if [ "$echo" ]; then
        PSQLOPT="$PSQLOPT -e"
else
        PSQLOPT="$PSQLOPT -q"
fi

PSQL="psql -A -t $PSQLOPT -d $dbname -c"


# ----------
# Make sure the language is installed
# ----------
res=`$PSQL "SELECT oid FROM pg_language WHERE lanname = '$langname'"`
if [ $? -ne 0 ]; then
	echo "Language removal failed."
	exit 1
fi
if [ -z "$res" ]; then
	echo "The language '$langname' isn't installed in database $dbname."
	exit 1
fi


# ----------
# Check that there are no functions left defined in that language
# ----------
res=`$PSQL "SELECT COUNT(proname) FROM pg_proc P, pg_language L WHERE P.prolang = L.oid AND L.lanname = '$langname'"`
if [ $? -ne 0 ]; then
	echo "Language removal failed."
	exit 1
fi
if [ $res -ne 0 ]; then
	echo "There are $res functions/trigger procedures declared in language"
        echo "$lancomp."
        echo "Language not removed."
	exit 1
fi

# ----------
# Drop the language and the call handler function
# ----------
$PSQL "DROP PROCEDURAL LANGUAGE '$langname'"
if [ $? -ne 0 ]; then
	echo "Language removal failed."
	exit 1
fi
$PSQL "DROP FUNCTION $handler()"
if [ $? -ne 0 ]; then
	echo "Language removal failed."
	exit 1
fi

exit 0
