#!/bin/bash

while [ $# -gt 0 ] ; do
  case $1 in
    -sdb)   origdb=$2; shift 2;;              # source db
    -shost) orighost=$2; shift 2;;            # source host
    -spass) origpass=$2; shift 2;;            # source password
    -ddb)   checkdb=$2; shift 2;;             # dest db
    -dhost) checkhost=$2; shift 2;;           # dest host
    -dpass) checkpass=$2; shift 2;;           # dest passwd
    -devhost) develophost=$2; shift 2;;       # develop host
     *)     shift 1 ;;
  esac
done

echo -n "Password for admindb: "
stty -echo < /dev/tty
read checkpass
stty echo < /dev/tty
echo

export PGPASSWORD=$checkpass
get_data()
{
   echo "$1" | sed -e "s@par1@$2@" -e "s@par2@$3@" -e "s@par3@$4@" | psql --tuples-only --no-align -E -h $checkhost -p $checkport -U $checkuser postgres
}

connections=
connections=$( connections=0
  get_data "SELECT client_addr, usename FROM pg_stat_activity where datname = 'par1'" $checkdb | sed -e "s/|/ - /g" | \
    while read line
    do
      let connections=connections+1
      echo $line
    done
) 

if [ "$connections" != "" ]; then
  echo "Database has connections - please disconnect"
  echo "$connections"
  exit 1
fi

savedb="$checkdb"_$(date +"%s")

if [ "$precmd" != "" ]; then
  get_data "$precmd"
fi
 
get_data "ALTER DATABASE par1 RENAME TO par2" "$checkdb" "$savedb" 
get_data 'CREATE DATABASE "'$checkdb'" WITH OWNER = admindb ENCODING = '\'UTF8\'' TEMPLATE = template0 LC_COLLATE = '\'C.UTF-8\'' LC_CTYPE = '\'C.UTF-8\'' CONNECTION LIMIT = -1;' 

pg_dump --username=$origuser --host=$develophost --format=c $origdb | pg_restore --host=$checkhost --username=$checkuser --dbname="$checkdb"

if [ "$postcmd" != "" ]; then
  get_data "$postcmd"
fi
 