consistent version of builder.sh
[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 DBHOST=127.0.0.1
7 DBUSER=brisk
8 DBBASE=brisk
9 DBPASS=briskpass
10 PFX="bsk_"
11
12 if [ -f $HOME/.brisk_install ]; then
13    . $HOME/.brisk_install
14 fi
15
16 sqlexe () {
17     local sht
18     sht=$1
19     
20     if [ $sht -eq 1 ];  then 
21         sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE 2>&1 | egrep 'ERROR|^-- MESG' 
22     else
23         sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE
24     fi
25
26     return 0
27 }
28
29 #
30 # MAIN
31 #
32 sht=0
33 if [ -f $HOME/.db.conf ]; then
34     source $HOME/.db.conf
35 fi
36
37 if [ "$1" = "-s" ]; then
38     shift
39     sht=1
40 fi
41
42 if [ "$1" = "create" ]; then
43     echo "su root" 
44     su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\"" 
45 elif [ "$1" = "destroy" ]; then
46     echo "su root" 
47     su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\"" 
48 elif [ "$1" = "clean" ]; then
49     ( echo "-- MESG: clean start" ; cat sql.d/*.sql | grep -i '^drop' | tac ; echo "-- MESG: clean end" ;   ) | sqlexe $sht
50 elif [ "$1" = "build" ]; then
51     ( echo "-- MESG: build start" ; cat sql.d/*.sql | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) | sqlexe $sht
52 elif [ "$1" = "rebuild" ]; then
53     ( echo "-- MESG: clean start" ; cat sql.d/*.sql | grep -i '^drop' | tac ; echo "-- MESG: clean end" ; \
54       echo "-- MESG: build start" ; cat sql.d/*.sql | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
55         | sqlexe $sht
56 elif [ "$1" = "psql" ]; then
57    psql -h $DBHOST -U $DBUSER $DBBASE
58 elif [ "$1" = "dump" ]; then
59    if [ $# -eq 1 ]; then
60       pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
61    else
62       pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
63    fi
64 elif [ "$1" = "dumpall" ]; then
65    if [ $# -eq 1 ]; then
66       pg_dump -h $DBHOST -U $DBUSER $DBBASE
67    else
68       pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
69    fi
70 elif [ "$1" = "add" ]; then
71    cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
72 else
73     echo " USAGE"
74     echo "   ./builder create"
75     echo "   ./builder destroy"
76     echo "   ./builder clean"
77     echo "   ./builder build"
78     echo "   ./builder rebuild"
79     echo "   ./builder psql"
80     echo "   ./builder add <filesql>"
81     echo "   ./builder dump [dumpfile]"
82     echo "   ./builder dumpall [dumpfile]"
83     echo "   ./builder all"
84     echo "   ./builder help"
85 fi