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
fi # load new aliases
85
if self_being_included; then
86

    
87
#### strings
88

    
89
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
90

    
91
sed () { env sed -"$sed_ere_flag" "$@";}
92

    
93
#### vars
94

    
95
set_var () { eval "$1"'="$2"'; }
96

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

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

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

    
113
#### commands
114

    
115
top_dir="$(dirname "$0")" # outermost script
116

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

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

    
135
#### make
136

    
137
make ()
138
{
139
	echo_func "$@"
140
	env make --directory="$top_dir" "$@"
141
}
142

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

    
167
#### compression
168

    
169
### zip
170

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

    
178
#### databases
179

    
180
# using prefixed connection vars
181
alias use_local="local prefix=local_; import_vars"
182
alias use_remote="local prefix=remote_; import_vars"
183
alias use_local_remote="use_local; use_remote"
184

    
185
quote='"'
186

    
187
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
188

    
189
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
190

    
191
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
192
alias mk_table_esc="local table_esc; mk_esc_name table"
193

    
194
fi # load new aliases
195
if self_being_included; then
196

    
197
log_sql () { test "$verbosity" -ge 2; }
198

    
199
### MySQL
200

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

    
217
mysql ()
218
{
219
	echo_func "$@"
220
	mysql_cmd --verbose "$@"
221
}
222

    
223
mysql_ANSI ()
224
{
225
	echo_func "$@"
226
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
227
}
228

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

    
237
mysqldump_diffable ()
238
{
239
	echo_func "$@"
240
	mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
241
}
242

    
243
### PostgreSQL
244

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

    
259
pg_header ()
260
{
261
	echo_func "$@"
262
	local pg_copy_format="CSV HEADER" limit=0
263
	pg_copy_to "$@"|echo_stdin
264
}
265

    
266
pg_export_table_no_header ()
267
{
268
	echo_func "$@"
269
	local pg_copy_format="CSV"
270
	pg_copy_to "$@"
271
}
272

    
273
pg_export_table_to_dir_no_header ()
274
{
275
	echo_func "$@"
276
	local table="$1"; shift; mk_table_esc
277
	local cols="$(pg_header)"
278
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
279
}
280

    
281
fi
(45-45/50)