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