bin sh usage echo Usage COMMAND ARGS echo COMMAND may be echo create e

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/sh
usage() {
echo "Usage: $0 COMMAND [ARGS]"
echo "COMMAND may be:"
echo "create"
echo " initiate database and load all data"
echo "explain TASK_NUMBER"
echo " show solution of specified task"
echo "start"
echo " start database server"
echo "stop"
echo " stop database server"
echo "run"
echo " run database command-line interface"
echo "renew [TASK]"
echo " drop and re-insert data for specified task (or all data)"
echo "gzip"
echo " archive current directory"
}
# ----------- Функции управления базой ----------------------------------------
PORT=7107
DIRPATH=`pwd`
DBDIR=db
DBPATH=$DIRPATH/$DBDIR
LOGFILE=$DBDIR/db.log
DBNAME=test
MYUSER=`whoami`
srv_ctl() {
pg_ctl -D $DBPATH -l $LOGFILE -o "-i -p $PORT -D $DBPATH" $1
}
run() {
psql -p $PORT $@ $DBNAME
}
installdb() {
if [ -f $DBPATH/postmaster.pid ]; then
srv_ctl stop
fi
rm -rf $DBPATH
mkdir -p $DBPATH
initdb -D $DBPATH
srv_ctl start
sleep 20
createdb -p $PORT $DBNAME
createlang -p $PORT plpgsql $DBNAME
}
rebuild_pghba() {
mv $DBPATH/pg_hba.conf $DBPATH/pg_hba.conf.bak
head --lines=-6 $DBPATH/pg_hba.conf.bak>$DBPATH/pg_hba.conf
printf "local\tall\t%s\t\ttrust\n">>$DBPATH/pg_hba.conf $MYUSER
printf "host\tall\t%s\t127.0.0.1/32\ttrust\n">>$DBPATH/pg_hba.conf $MYUSER
printf "host\tall\t%s\t::1/128\ttrust\n">>$DBPATH/pg_hba.conf $MYUSER
}
installmyuser() {
printf "host\t%s\t%s\t192.168.0.0\t255.255.0.0\ttrust\n" $DBNAME $MYUSER>>$DBPATH/pg_hba.conf
}
create() {
installdb
rebuild_pghba
installmyuser
srv_ctl reload
insert
}
# -----------------------------------------------------------------------------
DATADIR=data
DATAPATH=$DIRPATH/$DATADIR
TASKSDIR=tasks
TASKSPATH=$DIRPATH/$TASKSDIR
gzip() {
DIRNAME=`basename $DIRPATH`
DIRZIPNAME=$DIRNAME.tar.gz
tar czfv ../$DIRZIPNAME --exclude="$DBDIR" -C $DIRPATH/.. $DIRNAME
}
db_check() {
if [ ! -d $DBPATH ]; then
echo "You need to create database first:"
echo "$0 create"
exit 1
fi
}
server_check() {
db_check
if [ ! -f $DBPATH/postmaster.pid ]; then
echo "You need to start server first:"
echo "$0 start"
exit 1
fi
}
search_data() {
find $DATAPATH -mindepth 2 -name $1
}
tasks_list() {
find $TASKSPATH -mindepth 1 -type d -printf "%f\n"
}
explain() {
server_check
if [ -z "$1" -o ! -f $TASKSPATH/$1/query.sql ]; then
echo "Usage: $0 explain TASK"
if [ ! -z $1 ]; then
echo "Task '$1' not found"
fi
echo "Available tasks:"
tasks_list
exit 0
fi
cd $TASKSPATH/$1
if [ ! -f task.txt ]; then
echo "File 'task.txt' not found in $TASKSDIR/$1"
exit 1
fi
cat task.txt
echo
if [ -f script.sql ]; then
run -e -f script.sql
else
run -e -f query.sql
fi
}
get_datapath() {
sed -n '/--/p' $TASKSPATH/$1/query.sql | head -n 1
}
insert() {
if [ ! -z $1 ]; then
filenames=$DATAPATH/$1/create.sql
filenames="$filenames $DATAPATH/$1/fill.sql"
else
filenames=`search_data create.sql`
filenames="$filenames `search_data fill.sql`"
fi
cat $filenames | run
}
drop() {
if [ ! -z $1 ]; then
filenames=$DATAPATH/$1/drop.sql
else
filenames=`search_data drop.sql`
fi
cat $filenames | run
}
renew() {
server_check
if [ ! -z "$1" -a ! -f $TASKSPATH/$1/query.sql ]; then
echo "Task '$1' not found"
echo "Available tasks:"
tasks_list
exit 1
fi
if [ -z "$1" ]; then
echo "Updating all data..."
drop
insert
else
str=`get_datapath $1`
if [ -z "$str" ]; then
echo "No information supplied to '$1'"
exit 1
fi
dtdir=`echo $str | sed 's/[- ]//g'`
echo "Updating '$dtdir' stuff..."
drop $dtdir
insert $dtdir
fi
}
# -----------------------------------------------------------------------------
case $1 in
create|run|gzip) $1;;
explain|renew) $1 $2;;
start|stop) srv_ctl $1;;
*) usage;;
esac