#!/bin/sh
#
#Script: bin/gen_tm_tables
#Usage:  generate tm_tables.sh file which contains all database tables 
#        and sequences that will be managed by slony database replication 
#        manager.
#Author: ken
#Date:   03-25 16:55:56 CST 2015
#
usage(){
    echo "usage:  $0 DBHOST DBNAME USERNAME"
    echo "sample: $0 avalon-c5a tm10 ctu"
    exit
}

if [ $# -ne 3 ]
then
	usage
fi

DBHOST=$1
DBNAME=$2
USERNAME=$3

find_tables(){
	psql -A -t -c "select tablename from pg_tables where schemaname='public'" -h $DBHOST $DBNAME -U $USERNAME | sort
}

find_sequences(){
	psql -A -t -c "select distinct c.relname FROM pg_class c WHERE c.relkind = 'S'" -h $DBHOST $DBNAME -U $USERNAME | sort
}

cat > /dev/stdout  <<EOF
#
# This file defines two ksh variables that
#	keep all crm tables. 
# This is to be used by dbmirror scripts to
#	set/unset db triggers
#
#
tm_tables="
EOF

find_tables

cat > /dev/stdout  <<EOF
"

tm_sequences="
EOF

find_sequences

echo "\""


