shift added to remove 'psql' params passed to psql command
[brisk.git] / sql / builder.sh
1 #! /bin/bash
2
3 #
4 #  all this part is from mopshop and we will use it to construct the brisk database
5 #
6
7 if [ -f $HOME/.db.conf ]; then
8     source $HOME/.db.conf
9 else
10     DBHOST=127.0.0.1
11     DBUSER=brisk
12     DBBASE=brisk
13     DBPASS=briskpass
14     PFX="bsk_"
15 fi
16
17 if [ -f $HOME/.brisk_install ]; then
18     source $HOME/.brisk_install
19 fi
20
21
22 sqlexe () {
23     local sht
24     sht=$1
25     
26     if [ $sht -eq 1 ];  then 
27         sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE 2>&1 | egrep 'ERROR|^-- MESG' 
28     else
29         sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE
30     fi
31
32     return 0
33 }
34
35 one_or_all() {
36     if [ "$1" = "" ]; then
37         cat sql.d/*.sql
38     else
39         cat "$1"
40     fi
41 }
42
43 #
44 # MAIN
45 #
46 sht=0
47
48 if [ "$1" = "-s" ]; then
49     shift
50     sht=1
51 fi
52
53 if [ "$1" = "create" ]; then
54     echo "su root" 
55     su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\"" 
56 elif [ "$1" = "destroy" ]; then
57     echo "su root" 
58     su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\"" 
59 elif [ "$1" = "clean" ]; then
60     ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ;   ) | sqlexe $sht
61 elif [ "$1" = "build" ]; then
62     ( echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) | sqlexe $sht
63 elif [ "$1" = "rebuild" ]; then
64     ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ; \
65         echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
66         | sqlexe $sht
67 elif [ "$1" = "psql" ]; then
68    shift
69    psql -h $DBHOST -U $DBUSER $DBBASE $@
70 elif [ "$1" = "piped" ]; then
71    shift
72    psql -h $DBHOST -U $DBUSER $DBBASE -t -q -A -F '|' $@
73 elif [ "$1" = "dump" ]; then
74     if [ $# -eq 1 ]; then
75         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
76     else
77         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
78     fi
79 elif [ "$1" = "dumpall" ]; then
80     if [ $# -eq 1 ]; then
81         pg_dump -h $DBHOST -U $DBUSER $DBBASE
82     else
83         pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
84     fi
85 elif [ "$1" = "add" ]; then
86     cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
87 else
88     echo " USAGE"
89     echo "   ./builder create"
90     echo "   ./builder destroy"
91     echo "   ./builder clean"
92     echo "   ./builder build"
93     echo "   ./builder rebuild"
94     echo "   ./builder psql"
95     echo "   ./builder piped"
96     echo "   ./builder add <filesql>"
97     echo "   ./builder dump [dumpfile]"
98     echo "   ./builder dumpall [dumpfile]"
99     echo "   ./builder all"
100     echo "   ./builder help"
101 fi