#!/bin/sh
#-------------------------------------------------------------------------
#
# createlang--
#    Remove a procedural language from a database
#
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /home/projects/pgsql/cvsroot/pgsql/src/bin/scripts/droplang,v 1.13 2001/02/18 18:34:01 momjian Exp $
#
#-------------------------------------------------------------------------

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

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
                break
		;;
        --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=//'`
                ;;
	--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"
		;;
	--dbname|-d)
		dbname="$2"
		shift;;
        -d*)
                dbname=`echo "$1" | sed 's/^-d//'`
                ;;
        --dbname=*)
                dbname=`echo "$1" | sed 's/^--dbname=//'`
                ;;

	-*)
		echo "$CMDNAME: invalid option: $1" 1>&2
                echo "Try '$CMDNAME --help' for more information." 1>&2
		exit 1
		;;
	 *)
 		langname="$1"
                if [ "$2" ]; then
                        shift
			dbname="$1"
		fi
                ;;
    esac
    shift
done


if [ "$usage" ]; then
        echo "$CMDNAME removes a procedural language from a database."
	echo
	echo "Usage:"
        echo "  $CMDNAME [options] [langname [dbname]]"
        echo
	echo "Options:"
	echo "  -h, --host=HOSTNAME             Database server host"
	echo "  -p, --port=PORT                 Database server port"
	echo "  -U, --username=USERNAME         Username to connect as"
	echo "  -W, --password                  Prompt for password"
	echo "  -d, --dbname=DBNAME             Database to remove language from"
	echo "  -l, --list                      Show a list of currently installed languages"
        echo
        echo "If 'langname' is not specified, you will be prompted interactively."
        echo "A database name must be specified."
        echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
	exit 0
fi


if [ "$list" ]; then
        ${PATHNAME}psql $PSQLOPT -d "$dbname" -P 'title=Procedural languages' -c "SELECT lanname as \"Name\", lanpltrusted as \"Trusted?\", lancompiler as \"Compiler\" 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" 1>&2
        echo "Try '$CMDNAME -?' for help." 1>&2
	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"
                ;;
	pltclu)
		lancomp="PL/Tcl (untrusted)"
		handler="pltclu_call_handler"
		;;
	plperl)
		lancomp="PL/Perl"
		handler="plperl_call_handler"
                ;;
	*)
		echo "$CMDNAME: unsupported language '$langname'" 1>&2
		echo "Supported languages are 'plpgsql', 'pltcl', 'pltclu', and 'plperl'." 1>&2
		exit 1
                ;;
esac


PSQL="${PATHNAME}psql -A -t -q $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 "$CMDNAME: external error" 1>&2
	exit 1
fi
if [ -z "$res" ]; then
	echo "$CMDNAME: '$langname' is not installed in database $dbname" 1>&2
	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 "$CMDNAME: external error" 1>&2
	exit 1
fi
if [ "$res" -ne 0 ]; then
	echo "$CMDNAME: There are $res functions/trigger procedures declared in language" 1>&2
        echo "$lancomp. Language not removed." 1>&2
	exit 1
fi

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

exit 0
