#!/usr/bin/bash

set -u
umask 0027

## ======================================================================

psql="${MAILMAN_PSQL_COMMAND-/usr/bin/psql}"
psql_history="${MAILMAN_PSQL_HISTORY-/opt/osstech/var/cache/mailman/postgresql/psql_history}"

tty_p=
if [[ -t 0 && -t 1 ]]; then
  tty_p="set"
fi

psql_args=()
if [[ -n $tty_p ]]; then
  psql_args+=(
    --echo-all
    --echo-errors
    --echo-queries
    --echo-hidden
  )
fi

## ======================================================================

export PSQL_HISTORY="$psql_history"

exec "$psql" \
  "${psql_args[@]}" \
  "$@" \
;
