Project

General

Profile

1
#!/bin/bash -e
2
include_guard_var () { readlink -f -- "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
3

    
4
self_not_included () # usage: if self_not_included; then ... fi
5
{
6
	test "$#" -ge 1 || set -- "${BASH_SOURCE[1]}"
7
	local include_guard="$(include_guard_var "$1")"
8
	test -z "${!include_guard+t}" && eval "$include_guard"=1
9
}
10

    
11
if self_not_included "${BASH_SOURCE[0]}"; then
12

    
13
shopt -s expand_aliases
14

    
15
set_var () { eval "$1"'="$2"'; }
16

    
17
reverse () # usage: array=($(reverse args...))
18
{
19
	local i
20
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
21
}
22

    
23
: "${verbosity:=$verbose}" "${verbosity:=0}"
24

    
25
echo_cmd () { echo "$PS4$*" >&2; }
26

    
27
echo_run () { echo_cmd "$@"; "$@"; }
28

    
29
canon_rel_path ()
30
{
31
	local path="$1"
32
	path="$(readlink -f -- "$path")" # canonicalize
33
	path="${path#$PWD/}" # remove any shared prefix with the current dir
34
	echo "$path"
35
}
36

    
37
# usage: echo_func "$@"
38
echo_func ()
39
{
40
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
41
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
42
}
43

    
44
echo_stdin () # usage: input|echo_stdin|cmd
45
{
46
	echo ----- >&2
47
	tee -a /dev/stderr;
48
	echo ----- >&2
49
}
50

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

    
53
echo_export ()
54
{
55
	builtin export "$@"
56
	echo_vars "$@"
57
}
58

    
59
if test "$verbosity" -ge 2; then
60
	alias export="echo_export" # automatically echo env vars when they are set
61
fi
62

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

    
65
top_dir="$(dirname "$0")" # outermost script
66

    
67
run_args_cmd () # runs the command line args command
68
{
69
	test "$?" -eq 0 || return
70
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
71
	test "$#" -ge 1 || set -- all
72
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
73
}
74

    
75
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
76
{
77
	echo_func "$@"
78
	: "${subdirs?}"
79
	
80
	for subdir in "${subdirs[@]}"; do
81
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
82
	done
83
}
84

    
85
make ()
86
{
87
	echo_func "$@"
88
	echo_run env make --directory="$top_dir" "$@"
89
}
90

    
91
if false; then ## usage:
92
inline_make 3<<'EOF'
93
target:
94
	$(self_dir)/cmd >$@
95
EOF
96
# target will be run automatically because it's first in the makefile
97
fi ##
98
inline_make ()
99
{
100
	echo_func "$@"
101
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
102
	local self_dir="$(dirname "$self")"
103
	export self self_dir
104
	
105
	make --makefile=<((
106
		cat /dev/fd/3
107
		echo -n "
108
.SUFFIXES: # turn off built-in suffix rules
109
.SECONDARY: # don't automatically delete intermediate files
110
.DELETE_ON_ERROR: # delete target if recipe fails
111
"
112
	)|echo_stdin) "$@"
113
}
114

    
115
alias zip="echo_run zip"
116
alias unzip="echo_run unzip"
117
alias zip_newer="zip -u"
118
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
119

    
120
fi
(48-48/53)