Project

General

Profile

1
#!/bin/bash -e
2
realpath () { readlink -f -- "$1"; }
3

    
4
include_guard_var () { realpath "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
5

    
6
self_not_included () # usage: if self_not_included; then ... fi
7
{
8
	test "$#" -ge 1 || set -- "${BASH_SOURCE[1]}"
9
	local include_guard="$(include_guard_var "$1")"
10
	alias self_being_included=false
11
	test -z "${!include_guard+t}" && \
12
	{ eval "$include_guard"=1; alias self_being_included=true; }
13
}
14

    
15
# to load newly-defined aliases for use in functions in the same file:
16
## fi # load new aliases
17
## if self_being_included; then
18
# this is needed because aliases defined inside an if statement are not
19
# available inside that if statement
20

    
21
if self_not_included "${BASH_SOURCE[0]}"; then
22

    
23
shopt -s expand_aliases
24

    
25
#### arrays
26

    
27
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
28

    
29
reverse () # usage: array=($(reverse args...))
30
{
31
	local i
32
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
33
}
34

    
35
#### verbose output
36

    
37
: "${verbosity:=$verbose}" "${verbosity:=0}"
38

    
39
echo_cmd () { echo "$PS4$*" >&2; }
40

    
41
echo_run () { echo_cmd "$@"; "$@"; }
42

    
43
if test "$verbosity" -ge 1; then
44
	alias env="echo_run env" # automatically echo commands that use env
45
fi
46

    
47
canon_rel_path ()
48
{
49
	local path="$1"
50
	path="$(realpath "$path")" # canonicalize
51
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
52
	echo "$path"
53
}
54

    
55
# usage: echo_func "$@"
56
echo_func ()
57
{
58
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
59
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
60
}
61

    
62
echo_stdin () # usage: input|echo_stdin|cmd
63
{
64
	echo ----- >&2
65
	tee -a /dev/stderr;
66
	echo ----- >&2
67
}
68

    
69
echo_vars () # usage: echo_vars var...
70
{ { echo -n "$PS4"; declare -p "${@%%=*}";} >&2; }
71

    
72
echo_export ()
73
{
74
	builtin export "$@"
75
	echo_vars "$@"
76
}
77

    
78
if test "$verbosity" -ge 2; then
79
	alias export="echo_export" # automatically echo env vars when they are set
80
fi
81

    
82
usage () { echo "Usage: $1" >&2; (exit 2); }
83

    
84
#### strings
85

    
86
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
87

    
88
sed () { echo_run env sed -"$sed_ere_flag" "$@";}
89

    
90
#### vars
91

    
92
set_var () { eval "$1"'="$2"'; }
93

    
94
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
95

    
96
# usage: local var=...; local_inv
97
alias local_inv='local "no_$var=$(test -n "${!var}" || echo 1)"'
98

    
99
# usage: local prefix=..._; import_vars
100
alias import_vars="$(cat <<'EOF'
101
: "${prefix:?}"
102
local src_var dest_var
103
for src_var in $(eval echo '${!'$prefix'*}'); do
104
	dest_var="${src_var#$prefix}"
105
	local "$dest_var=${!src_var}"; echo_vars "$dest_var"
106
done
107
EOF
108
)"
109

    
110
#### commands
111

    
112
top_dir="$(dirname "$0")" # outermost script
113

    
114
run_args_cmd () # runs the command line args command
115
{
116
	test "$?" -eq 0 || return
117
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
118
	test "$#" -ge 1 || set -- all
119
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
120
}
121

    
122
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
123
{
124
	echo_func "$@"
125
	: "${subdirs?}"
126
	
127
	for subdir in "${subdirs[@]}"; do
128
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
129
	done
130
}
131

    
132
#### make
133

    
134
make ()
135
{
136
	echo_func "$@"
137
	echo_run env make --directory="$top_dir" "$@"
138
}
139

    
140
if false; then ## usage:
141
inline_make 3<<'EOF'
142
target:
143
	$(self_dir)/cmd >$@
144
EOF
145
# target will be run automatically because it's first in the makefile
146
fi ##
147
inline_make ()
148
{
149
	echo_func "$@"
150
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
151
	local self_dir="$(dirname "$self")"
152
	export self self_dir
153
	
154
	make --makefile=<((
155
		cat /dev/fd/3
156
		echo -n "
157
.SUFFIXES: # turn off built-in suffix rules
158
.SECONDARY: # don't automatically delete intermediate files
159
.DELETE_ON_ERROR: # delete target if recipe fails
160
"
161
	)|echo_stdin) "$@"
162
}
163

    
164
#### compression
165

    
166
### zip
167

    
168
alias zip="echo_run zip"
169
alias unzip="echo_run unzip"
170
set_inv force
171
alias zip_newer="zip${no_force:+ -u}"
172
alias unzip_newer="unzip${no_force:+ -u} -o"
173
	# -o is safe because -u only extracts newer files
174

    
175
#### databases
176

    
177
# using prefixed connection vars
178
alias use_local="local prefix=local_; import_vars"
179
alias use_remote="local prefix=remote_; import_vars"
180
alias use_local_remote="use_local; use_remote"
181

    
182
quote='"'
183

    
184
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
185

    
186
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
187

    
188
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
189
alias mk_table_esc="local table_esc; mk_esc_name table"
190

    
191
fi # load new aliases
192
if self_being_included; then
193

    
194
log_sql () { test "$verbosity" -ge 2; }
195

    
196
### MySQL
197

    
198
# auto-adds connection/login opts when specified
199
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
200
{
201
	echo_func "$@"
202
	if test _"$ssh_server" = _"$(hostname -f)"; then local ssh_server=; fi
203
	if test -n "$ssh_server"; then
204
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
205
	fi
206
	
207
	local var=ssh_dest; local_inv
208
	echo_run ${no_ssh_dest:+env }${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
209
${server:+ --host="$server" }${user+--user="$user" }\
210
--password${password+="$password"} "$@"
211
}
212

    
213
mysql ()
214
{
215
	echo_func "$@"
216
	mysql_cmd --verbose "$@"
217
}
218

    
219
mysql_ANSI ()
220
{
221
	echo_func "$@"
222
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
223
}
224

    
225
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
226
{
227
	echo_func "$@"
228
	mysql_cmd --quick --lock-tables=false --set-charset \
229
${postgres_compat:+--compatible=postgresql --add-locks=false }\
230
${schema:+--no-data }${data:+--no-create-info }"$@"
231
}
232

    
233
mysqldump_diffable ()
234
{
235
	echo_func "$@"
236
	mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
237
}
238

    
239
### PostgreSQL
240

    
241
pg_copy_to ()
242
{
243
	echo_func "$@"
244
	if test -z "$source"; then
245
		: "${table:?}"; mk_table_esc
246
		if test -z "$limit"; then local source="$table_esc"
247
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
248
		fi
249
	fi
250
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
251
	
252
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
253
}
254

    
255
pg_header ()
256
{
257
	echo_func "$@"
258
	local pg_copy_format="CSV HEADER" limit=0
259
	pg_copy_to "$@"|echo_stdin
260
}
261

    
262
pg_export_table_no_header ()
263
{
264
	echo_func "$@"
265
	local pg_copy_format="CSV"
266
	pg_copy_to "$@"
267
}
268

    
269
pg_export_table_to_dir_no_header ()
270
{
271
	echo_func "$@"
272
	local table="$1"; shift; mk_table_esc
273
	local cols="$(pg_header)"
274
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
275
}
276

    
277
fi
(45-45/50)