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
#### strings
36

    
37
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
38

    
39
sed () { echo_run env sed -"$sed_ere_flag" "$@";}
40

    
41
#### verbose output
42

    
43
: "${verbosity:=$verbose}" "${verbosity:=0}"
44

    
45
echo_cmd () { echo "$PS4$*" >&2; }
46

    
47
echo_run () { echo_cmd "$@"; "$@"; }
48

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

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

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

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

    
74
echo_export ()
75
{
76
	builtin export "$@"
77
	echo_vars "$@"
78
}
79

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

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

    
86
#### vars
87

    
88
set_var () { eval "$1"'="$2"'; }
89

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

    
92
#### commands
93

    
94
top_dir="$(dirname "$0")" # outermost script
95

    
96
run_args_cmd () # runs the command line args command
97
{
98
	test "$?" -eq 0 || return
99
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
100
	test "$#" -ge 1 || set -- all
101
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
102
}
103

    
104
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
105
{
106
	echo_func "$@"
107
	: "${subdirs?}"
108
	
109
	for subdir in "${subdirs[@]}"; do
110
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
111
	done
112
}
113

    
114
#### make
115

    
116
make ()
117
{
118
	echo_func "$@"
119
	echo_run env make --directory="$top_dir" "$@"
120
}
121

    
122
if false; then ## usage:
123
inline_make 3<<'EOF'
124
target:
125
	$(self_dir)/cmd >$@
126
EOF
127
# target will be run automatically because it's first in the makefile
128
fi ##
129
inline_make ()
130
{
131
	echo_func "$@"
132
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
133
	local self_dir="$(dirname "$self")"
134
	export self self_dir
135
	
136
	make --makefile=<((
137
		cat /dev/fd/3
138
		echo -n "
139
.SUFFIXES: # turn off built-in suffix rules
140
.SECONDARY: # don't automatically delete intermediate files
141
.DELETE_ON_ERROR: # delete target if recipe fails
142
"
143
	)|echo_stdin) "$@"
144
}
145

    
146
#### compression
147

    
148
### zip
149

    
150
alias zip="echo_run zip"
151
alias unzip="echo_run unzip"
152
alias zip_newer="zip -u"
153
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
154

    
155
#### databases
156

    
157
quote='"'
158

    
159
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
160

    
161
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
162

    
163
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
164
alias mk_table_esc="local table_esc; mk_esc_name table"
165

    
166
fi # load new aliases
167
if self_being_included; then
168

    
169
log_sql () { test "$verbosity" -ge 2; }
170

    
171
### MySQL
172

    
173
mysql ()
174
{
175
	echo_func "$@"
176
	echo_run env mysql --verbose "$@"
177
}
178

    
179
mysql_ANSI ()
180
{
181
	echo_func "$@"
182
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
183
}
184

    
185
### PostgreSQL
186

    
187
pg_copy_to ()
188
{
189
	echo_func "$@"
190
	if test -z "$source"; then
191
		: "${table:?}"; mk_table_esc
192
		if test -z "$limit"; then local source="$table_esc"
193
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
194
		fi
195
	fi
196
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
197
	
198
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
199
}
200

    
201
pg_header ()
202
{
203
	echo_func "$@"
204
	local pg_copy_format="CSV HEADER" limit=0
205
	pg_copy_to "$@"|echo_stdin
206
}
207

    
208
pg_export_table_no_header ()
209
{
210
	echo_func "$@"
211
	local pg_copy_format="CSV"
212
	pg_copy_to "$@"
213
}
214

    
215
pg_export_table_to_dir_no_header ()
216
{
217
	echo_func "$@"
218
	local table="$1"; shift; mk_table_esc
219
	local cols="$(pg_header)"
220
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
221
}
222

    
223
fi
(45-45/50)