#! /usr/bin/env bash

# file      : build/pgsql/pgsql
# copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
# license   : GNU GPL v2; see accompanying LICENSE file

#
# PostgreSQL driver wrapper.
#

driver=
opt=

while [ $# -gt 0 ]; do
  case $1 in
    --driver)
      driver=$2
      shift 2
      ;;
    --user)
      opt="$opt --user=$2"
      shift 2
      ;;
    --database)
      opt="$opt --dbname=$2"
      shift 2
      ;;
    --host)
      opt="$opt --host=$2"
      shift 2
      ;;
    --port)
      opt="$opt --port=$2"
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

if [ -z "$driver" ]; then
  driver=psql
fi

opt="--quiet $opt"

export PGOPTIONS=--client-min-messages=warning

if [ -n "$1" ]; then
  exec $driver $opt --set ON_ERROR_STOP=1 -f $1
else
  exec $driver $opt
fi