dot configurations file management fixed
[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     psql -h $DBHOST -U $DBUSER $DBBASE
69 elif [ "$1" = "dump" ]; then
70     if [ $# -eq 1 ]; then
71         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
72     else
73         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
74     fi
75 elif [ "$1" = "dumpall" ]; then
76     if [ $# -eq 1 ]; then
77         pg_dump -h $DBHOST -U $DBUSER $DBBASE
78     else
79         pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
80     fi
81 elif [ "$1" = "add" ]; then
82     cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
83 else
84     echo " USAGE"
85     echo "   ./builder create"
86     echo "   ./builder destroy"
87     echo "   ./builder clean"
88     echo "   ./builder build"
89     echo "   ./builder rebuild"
90     echo "   ./builder psql"
91     echo "   ./builder add <filesql>"
92     echo "   ./builder dump [dumpfile]"
93     echo "   ./builder dumpall [dumpfile]"
94     echo "   ./builder all"
95     echo "   ./builder help"
96 fi