#!/bin/sh
#
# $Id: install.sh 66 2005-04-19 21:22:47Z decibel $
#
# Copyright (c) 2004, Jim C. Nasby (decibel@rrs.decibel.org)
# All rights reserved.

# See if we can find out where dblink is
sharedir=`pg_config --sharedir`

if [ x$sharedir == x ]; then
    echo "Unable to find location of PostgreSQL shared directory"
    echo "(is pg_config in your $PATH?)"
    exit 1
fi

dblink=$sharedir/contrib/dblink.sql

if [ ! -r $dblink ]; then
    echo "Unable to read dblink.sql ($dblink)"
    exit 1
fi

rrs=`psql -qtc "SELECT EXISTS( SELECT * FROM pg_namespace WHERE nspname='rrs' )" $@`
if [ $? -ne 0 ]; then
    echo "ERROR: Unable to determine if RRS is installed" 1>&2
    exit 1
fi
if [ $rrs == f ]; then
    echo Installing RRS
    rrs/install $@ || exit 1
fi

plpgsql=`psql -qtc "SELECT EXISTS( SELECT * FROM pg_language WHERE lanname='plpgsql' )" $@`
if [ $? -ne 0 ]; then
    echo "ERROR: Unable to determine if plpgsql is installed" 1>&2
    exit 1
fi
if [ $plpgsql == f ]; then
    psql -qc 'CREATE LANGUAGE plpgsql' $@ >/dev/null || exit 1
fi

cd `dirname $0`

echo Installing PGStats
psql -qv dblink=$dblink -f install.sql $@ || exit 1

echo If you want to tighten security, run
echo    psql -f security.sql $@
