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
	test -z "${!include_guard+t}" && eval "$include_guard"=1
11
}
12
13 8704 aaronmk
if self_not_included "${BASH_SOURCE[0]}"; then
14
15 8709 aaronmk
shopt -s expand_aliases
16
17 8694 aaronmk
set_var () { eval "$1"'="$2"'; }
18
19 8691 aaronmk
reverse () # usage: array=($(reverse args...))
20
{
21
	local i
22
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
23
}
24
25 8713 aaronmk
: "${verbosity:=$verbose}" "${verbosity:=0}"
26 8710 aaronmk
27 8278 aaronmk
echo_cmd () { echo "$PS4$*" >&2; }
28 8272 aaronmk
29 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
30
31 8646 aaronmk
canon_rel_path ()
32
{
33
	local path="$1"
34 8716 aaronmk
	path="$(realpath "$path")" # canonicalize
35 8773 aaronmk
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
36 8646 aaronmk
	echo "$path"
37
}
38
39 8463 aaronmk
# usage: echo_func "$@"
40
echo_func ()
41
{
42 8647 aaronmk
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
43
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
44 8463 aaronmk
}
45 8279 aaronmk
46 8702 aaronmk
echo_stdin () # usage: input|echo_stdin|cmd
47
{
48
	echo ----- >&2
49
	tee -a /dev/stderr;
50
	echo ----- >&2
51
}
52 8275 aaronmk
53 8650 aaronmk
echo_vars () { declare -p "${@%%=*}" >&2; } # usage: echo_vars var...
54 8641 aaronmk
55
echo_export ()
56
{
57 8700 aaronmk
	builtin export "$@"
58 8641 aaronmk
	echo_vars "$@"
59
}
60
61 8713 aaronmk
if test "$verbosity" -ge 2; then
62 8711 aaronmk
	alias export="echo_export" # automatically echo env vars when they are set
63
fi
64 8642 aaronmk
65 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
66
67
top_dir="$(dirname "$0")" # outermost script
68
69 8465 aaronmk
run_args_cmd () # runs the command line args command
70 8272 aaronmk
{
71 8292 aaronmk
	test "$?" -eq 0 || return
72 8693 aaronmk
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
73 8290 aaronmk
	test "$#" -ge 1 || set -- all
74 8648 aaronmk
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
75 8272 aaronmk
}
76
77 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
78 8272 aaronmk
{
79 8463 aaronmk
	echo_func "$@"
80 8284 aaronmk
	: "${subdirs?}"
81
82 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
83 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
84 8272 aaronmk
	done
85
}
86
87 8280 aaronmk
make ()
88
{
89 8463 aaronmk
	echo_func "$@"
90 8280 aaronmk
	echo_run env make --directory="$top_dir" "$@"
91
}
92 8272 aaronmk
93 8276 aaronmk
if false; then ## usage:
94 8652 aaronmk
inline_make 3<<'EOF'
95 8276 aaronmk
target:
96
	$(self_dir)/cmd >$@
97
EOF
98
# target will be run automatically because it's first in the makefile
99
fi ##
100
inline_make ()
101
{
102 8653 aaronmk
	echo_func "$@"
103 8640 aaronmk
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
104
	local self_dir="$(dirname "$self")"
105 8645 aaronmk
	export self self_dir
106 8640 aaronmk
107 8651 aaronmk
	make --makefile=<((
108 8652 aaronmk
		cat /dev/fd/3
109 8651 aaronmk
		echo -n "
110 8276 aaronmk
.SUFFIXES: # turn off built-in suffix rules
111
.SECONDARY: # don't automatically delete intermediate files
112
.DELETE_ON_ERROR: # delete target if recipe fails
113 8651 aaronmk
"
114
	)|echo_stdin) "$@"
115 8276 aaronmk
}
116 8658 aaronmk
117
alias zip="echo_run zip"
118
alias unzip="echo_run unzip"
119 8659 aaronmk
alias zip_newer="zip -u"
120
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
121 8704 aaronmk
122 8775 aaronmk
#### databases
123
124
quote='"'
125
126
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
127
128
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
129
130
mysql ()
131
{
132
	echo_func "$@"
133 8777 aaronmk
	echo_run env mysql --verbose "$@"
134 8775 aaronmk
}
135
136
mysql_ANSI ()
137
{
138
	echo_func "$@"
139
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
140
}
141
142 8704 aaronmk
fi