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
canon_rel_path ()
44
{
45
	local path="$1"
46
	path="$(realpath "$path")" # canonicalize
47
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
48
	echo "$path"
49
}
50

    
51
# usage: echo_func "$@"
52
echo_func ()
53
{
54
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
55
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
56
}
57

    
58
echo_stdin () # usage: input|echo_stdin|cmd
59
{
60
	echo ----- >&2
61
	tee -a /dev/stderr;
62
	echo ----- >&2
63
}
64

    
65
echo_vars () { declare -p "${@%%=*}" >&2; } # usage: echo_vars var...
66

    
67
echo_export ()
68
{
69
	builtin export "$@"
70
	echo_vars "$@"
71
}
72

    
73
if test "$verbosity" -ge 2; then
74
	alias export="echo_export" # automatically echo env vars when they are set
75
fi
76

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

    
79
#### vars
80

    
81
set_var () { eval "$1"'="$2"'; }
82

    
83
#### commands
84

    
85
top_dir="$(dirname "$0")" # outermost script
86

    
87
run_args_cmd () # runs the command line args command
88
{
89
	test "$?" -eq 0 || return
90
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
91
	test "$#" -ge 1 || set -- all
92
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
93
}
94

    
95
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
96
{
97
	echo_func "$@"
98
	: "${subdirs?}"
99
	
100
	for subdir in "${subdirs[@]}"; do
101
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
102
	done
103
}
104

    
105
#### make
106

    
107
make ()
108
{
109
	echo_func "$@"
110
	echo_run env make --directory="$top_dir" "$@"
111
}
112

    
113
if false; then ## usage:
114
inline_make 3<<'EOF'
115
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
	echo_func "$@"
123
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
124
	local self_dir="$(dirname "$self")"
125
	export self self_dir
126
	
127
	make --makefile=<((
128
		cat /dev/fd/3
129
		echo -n "
130
.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
"
134
	)|echo_stdin) "$@"
135
}
136

    
137
#### compression
138

    
139
### zip
140

    
141
alias zip="echo_run zip"
142
alias unzip="echo_run unzip"
143
alias zip_newer="zip -u"
144
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
145

    
146
#### 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
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
mysql ()
165
{
166
	echo_func "$@"
167
	echo_run env mysql --verbose "$@"
168
}
169

    
170
mysql_ANSI ()
171
{
172
	echo_func "$@"
173
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
174
}
175

    
176
### PostgreSQL
177

    
178
pg_copy_to ()
179
{
180
	echo_func "$@"
181
	if test -z "$source"; then
182
		: "${table:?}"; mk_table_esc
183
		if test -z "$limit"; then local source="$table_esc"
184
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
185
		fi
186
	fi
187
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
188
	
189
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
190
}
191

    
192
pg_header ()
193
{
194
	echo_func "$@"
195
	local pg_copy_format="CSV HEADER" limit=0
196
	pg_copy_to "$@"|echo_stdin
197
}
198

    
199
pg_export_table_no_header ()
200
{
201
	echo_func "$@"
202
	local pg_copy_format="CSV"
203
	pg_copy_to "$@"
204
}
205

    
206
pg_export_table_to_dir_no_header ()
207
{
208
	echo_func "$@"
209
	local table="$1"; shift; mk_table_esc
210
	local cols="$(pg_header)"
211
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
212
}
213

    
214
fi
(45-45/50)