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