387af9a8ecd851380e4921c78be98f5210ad4175
[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     DBPORT=5432
13     DBBASE=brisk
14     DBPASS=briskpass
15     PFX="bsk_"
16 fi
17
18 if [ -f $HOME/.brisk_install ]; then
19     source $HOME/.brisk_install
20 fi
21
22 pg_args=""
23 test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
24 test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
25 test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
26 test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
27         
28 sqlexe () {
29     local sht
30     sht=$1
31     
32     if [ $sht -eq 1 ];  then 
33         sed "s/#PFX#/$PFX/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG' 
34     else
35         sed "s/#PFX#/$PFX/g" | psql -a $pg_args
36     fi
37
38     return 0
39 }
40
41 one_or_all() {
42     if [ "$1" = "" ]; then
43         cat sql.d/*.sql
44     else
45         cat "$1"
46     fi
47 }
48
49 #
50 # MAIN
51 #
52 sht=0
53
54 if [ "$1" = "-s" ]; then
55     shift
56     sht=1
57 fi
58
59 if [ "$1" = "create" ]; then
60     echo "su root" 
61     su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\"" 
62 elif [ "$1" = "destroy" ]; then
63     echo "su root" 
64     su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\"" 
65 elif [ "$1" = "clean" ]; then
66     ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ;   ) | sqlexe $sht
67 elif [ "$1" = "build" ]; then
68     ( echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) | sqlexe $sht
69 elif [ "$1" = "rebuild" ]; then
70     ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ; \
71         echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
72         | sqlexe $sht
73 elif [ "$1" = "psql" ]; then
74    shift
75    psql $pg_args $@
76 elif [ "$1" = "piped" ]; then
77    shift
78    psql $pg_args -t -q -A -F '|' $@
79 elif [ "$1" = "dump" ]; then
80     if [ $# -eq 1 ]; then
81         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
82     else
83         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
84     fi
85 elif [ "$1" = "dumpall" ]; then
86     if [ $# -eq 1 ]; then
87         pg_dump -h $DBHOST -U $DBUSER $DBBASE
88     else
89         pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
90     fi
91 elif [ "$1" = "add" ]; then
92     cat "$2" | psql $pg_args
93 else
94     echo " USAGE"
95     echo "   ./builder create"
96     echo "   ./builder destroy"
97     echo "   ./builder clean"
98     echo "   ./builder build"
99     echo "   ./builder rebuild"
100     echo "   ./builder psql"
101     echo "   ./builder piped"
102     echo "   ./builder add <filesql>"
103     echo "   ./builder dump [dumpfile]"
104     echo "   ./builder dumpall [dumpfile]"
105     echo "   ./builder all"
106     echo "   ./builder help"
107 fi