Project

General

Profile

1 8291 aaronmk
#!/bin/bash -e
2 8716 aaronmk
realpath () { readlink -f -- "$1"; }
3 8703 aaronmk
4 8716 aaronmk
include_guard_var () { realpath "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
5
6 8703 aaronmk
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 8793 aaronmk
	alias self_being_included=false
11
	test -z "${!include_guard+t}" && \
12
	{ eval "$include_guard"=1; alias self_being_included=true; }
13 8703 aaronmk
}
14
15 8793 aaronmk
# 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 8704 aaronmk
if self_not_included "${BASH_SOURCE[0]}"; then
22
23 8709 aaronmk
shopt -s expand_aliases
24
25 8854 aaronmk
#### arrays
26 8694 aaronmk
27 8833 aaronmk
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
28 8816 aaronmk
29 8691 aaronmk
reverse () # usage: array=($(reverse args...))
30
{
31
	local i
32
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
33
}
34
35 8854 aaronmk
#### verbose output
36
37 8713 aaronmk
: "${verbosity:=$verbose}" "${verbosity:=0}"
38 8710 aaronmk
39 8278 aaronmk
echo_cmd () { echo "$PS4$*" >&2; }
40 8272 aaronmk
41 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
42
43 8646 aaronmk
canon_rel_path ()
44
{
45
	local path="$1"
46 8716 aaronmk
	path="$(realpath "$path")" # canonicalize
47 8773 aaronmk
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
48 8646 aaronmk
	echo "$path"
49
}
50
51 8463 aaronmk
# usage: echo_func "$@"
52
echo_func ()
53
{
54 8647 aaronmk
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
55
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
56 8463 aaronmk
}
57 8279 aaronmk
58 8702 aaronmk
echo_stdin () # usage: input|echo_stdin|cmd
59
{
60
	echo ----- >&2
61
	tee -a /dev/stderr;
62
	echo ----- >&2
63
}
64 8275 aaronmk
65 8650 aaronmk
echo_vars () { declare -p "${@%%=*}" >&2; } # usage: echo_vars var...
66 8641 aaronmk
67
echo_export ()
68
{
69 8700 aaronmk
	builtin export "$@"
70 8641 aaronmk
	echo_vars "$@"
71
}
72
73 8713 aaronmk
if test "$verbosity" -ge 2; then
74 8711 aaronmk
	alias export="echo_export" # automatically echo env vars when they are set
75
fi
76 8642 aaronmk
77 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
78
79 8854 aaronmk
#### vars
80
81
set_var () { eval "$1"'="$2"'; }
82
83
#### commands
84
85 8272 aaronmk
top_dir="$(dirname "$0")" # outermost script
86
87 8465 aaronmk
run_args_cmd () # runs the command line args command
88 8272 aaronmk
{
89 8292 aaronmk
	test "$?" -eq 0 || return
90 8693 aaronmk
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
91 8290 aaronmk
	test "$#" -ge 1 || set -- all
92 8648 aaronmk
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
93 8272 aaronmk
}
94
95 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
96 8272 aaronmk
{
97 8463 aaronmk
	echo_func "$@"
98 8284 aaronmk
	: "${subdirs?}"
99
100 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
101 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
102 8272 aaronmk
	done
103
}
104
105 8854 aaronmk
#### make
106
107 8280 aaronmk
make ()
108
{
109 8463 aaronmk
	echo_func "$@"
110 8280 aaronmk
	echo_run env make --directory="$top_dir" "$@"
111
}
112 8272 aaronmk
113 8276 aaronmk
if false; then ## usage:
114 8652 aaronmk
inline_make 3<<'EOF'
115 8276 aaronmk
target:
116
	$(self_dir)/cmd >$@
117
EOF
118
# target will be run automatically because it's first in the makefile
119
fi ##
120
inline_make ()
121
{
122 8653 aaronmk
	echo_func "$@"
123 8640 aaronmk
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
124
	local self_dir="$(dirname "$self")"
125 8645 aaronmk
	export self self_dir
126 8640 aaronmk
127 8651 aaronmk
	make --makefile=<((
128 8652 aaronmk
		cat /dev/fd/3
129 8651 aaronmk
		echo -n "
130 8276 aaronmk
.SUFFIXES: # turn off built-in suffix rules
131
.SECONDARY: # don't automatically delete intermediate files
132
.DELETE_ON_ERROR: # delete target if recipe fails
133 8651 aaronmk
"
134
	)|echo_stdin) "$@"
135 8276 aaronmk
}
136 8658 aaronmk
137 8854 aaronmk
#### compression
138
139
### zip
140
141 8658 aaronmk
alias zip="echo_run zip"
142
alias unzip="echo_run unzip"
143 8659 aaronmk
alias zip_newer="zip -u"
144
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
145 8704 aaronmk
146 8775 aaronmk
#### databases
147
148
quote='"'
149
150
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
151
152
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
153
154 8796 aaronmk
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
155
alias mk_table_esc="local table_esc; mk_esc_name table"
156
157
fi # load new aliases
158
if self_being_included; then
159
160
log_sql () { test "$verbosity" -ge 2; }
161
162
### MySQL
163
164 8775 aaronmk
mysql ()
165
{
166
	echo_func "$@"
167 8777 aaronmk
	echo_run env mysql --verbose "$@"
168 8775 aaronmk
}
169
170
mysql_ANSI ()
171
{
172
	echo_func "$@"
173
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
174
}
175
176 8796 aaronmk
### PostgreSQL
177
178
pg_copy_to ()
179
{
180
	echo_func "$@"
181 8805 aaronmk
	if test -z "$source"; then
182
		: "${table:?}"; mk_table_esc
183 8834 aaronmk
		if test -z "$limit"; then local source="$table_esc"
184
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
185 8805 aaronmk
		fi
186
	fi
187 8834 aaronmk
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
188 8805 aaronmk
189 8807 aaronmk
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
190 8796 aaronmk
}
191
192
pg_header ()
193
{
194
	echo_func "$@"
195 8806 aaronmk
	local pg_copy_format="CSV HEADER" limit=0
196 8807 aaronmk
	pg_copy_to "$@"|echo_stdin
197 8796 aaronmk
}
198
199
pg_export_table_no_header ()
200
{
201
	echo_func "$@"
202
	local pg_copy_format="CSV"
203 8807 aaronmk
	pg_copy_to "$@"
204 8796 aaronmk
}
205
206
pg_export_table_to_dir_no_header ()
207
{
208
	echo_func "$@"
209
	local table="$1"; shift; mk_table_esc
210 8807 aaronmk
	local cols="$(pg_header)"
211 8796 aaronmk
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
212
}
213
214 8704 aaronmk
fi