#!/bin/bash
#Обязательные параметры:
#-pwd    - (не обязательный!)пароль пользователя бд, прокидывается в $PGPASSWORD
#-bindir - путь до "родной" утилиты
#Все остальные параметры "прокидываются" без изменений в "родную" утилиту
#Царегородцев А.П. 09.11.2016


unset PGDATABASE
unset PGHOST
unset PGOPTIONS
unset PGPORT
unset PGUSER

until [ -z "$1" ]  #Loop through args
do
    case "$1" in
	-pwd )
            #Catch password
            pwd=$2 
            shift;shift;
            ;;
        -bindir )
            #Catch path to bindir
            bindir=$2
            shift;shift;
	    ;;
	* )
	    #Pass through
	    newargs="$newargs $1"
	    shift
	    ;;
    esac
done

#echo `basename $0`
#echo -n "Catched password is $pwd"
#echo
#echo -n "Catched bindir is $bindir"
#echo
#echo -n "Passed through params is $newargs "
#echo

if [ ! -z "$pwd" ]
then
  export PGPASSWORD=$pwd
  #echo $PGPASSWORD
fi

if [ -z "$bindir" ]
then
  echo "Expected -bindir argument"
  exit 1
else
  $bindir/`basename $0` $newargs
  exit 0
fi
