+
+if [ -f $HOME/.brisk_install ]; then
+ source $HOME/.brisk_install
+fi
+
+pg_args=""
+test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
+test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
+test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
+test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
+
+
+case $CMD in
+ "create")
+ echo "su root"
+ su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\""
+ ;;
+
+ "destroy")
+ echo "su root"
+ su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\""
+ ;;
+ "clean")
+ ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ; ) | sqlexe
+ ;;
+ "build")
+ ( echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ; ) | sqlexe
+ ;;
+ "rebuild")
+ ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ; \
+ echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ; ) \
+ | sqlexe
+ ;;
+ "psql")
+ psql $pg_args $@
+ ;;
+
+ "piped")
+ psql $pg_args -t -q -A -F '|' $@
+ ;;
+ "dump")
+ if [ $# -eq 1 ]; then
+ pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
+ else
+ pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $1
+ fi
+ ;;
+ "dumpall")
+ if [ $# -eq 1 ]; then
+ pg_dump -h $DBHOST -U $DBUSER $DBBASE
+ else
+ pg_dump -h $DBHOST -U $DBUSER $DBBASE > $1
+ fi
+ ;;
+ "add")
+ ( echo "-- MESG: add start" ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: add end" ; ) | sqlexe
+ ;;
+ "del")
+ ( echo "-- MESG: del start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: del end" ; ) | sqlexe
+ ;;
+ "res")
+ ( echo "-- MESG: res start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: del end" ; ) | sqlexe
+ ;;
+ "help"|"-h"|"--help")
+ usage 0
+ ;;
+ *)
+ usage 1
+ ;;
+esac
+
+exit 0