summaryrefslogtreecommitdiff
path: root/wrapper/inner.sh.m4
blob: bd67f0279377d81c1ebcdef0f27c601f8fdb5467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
# rvs inner.sh - The main RVS program
# Copyright (C) 2009-2010, 2015  Luke Shumaker
#
# This file is part of rvs.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
set -u

m4_include(config.sh)

declare -r varname_REPO="${PACKAGE^^}_REPO"
declare -r varname_EXEC_PATH="${PACKAGE^^}_EXEC_PATH"

declare -r program_name="$1"; shift

#

_() {
	if type gettext &>/dev/null; then
		TEXTDOMAINDIR=$localedir gettext "$pkgtextdomain" "$1";
	else
		printf '%s' "$1";
	fi
}

sighandler_exit() {
	local signal=$1; shift
	echo
	error 0 "$@"
	trap -- "$signal"
	kill -"$signal" "$$"
}

install_sighandlers() {
	set -E
	for signal in TERM HUP QUIT; do
		trap "sighandler_exit $signal '%s signal cought.  Exiting...'" $signal
	done
	trap 'sighandler_exit INT "Aborted by user.  Exiting..."' INT
	trap 'kill -USR1 "$$"' ERR
}

# Like GLibC's error(3), but call gettext on the format string
error() {
	>&2 printf "%s: $(_ "$2")\n" "$program_name" "${@:3}"
	[[ $1 -eq 0 ]] || exit $1
}

errusage() {
	>&2 printf "$(_ "$2")\n" "$program_name" "${@:3}"
	[[ $1 -eq 0 ]] || exit $1
}

_runcmd() {
	[[ $# -ge 1 ]] || errusage 1 'Usage: %q [<module>/]<command> [<args>]'
	local mod=''
	local cmd=$1; shift
	local args_str=''
	[[ $# -eq 0 ]] || printf -v args_str '%q ' "$@"

	local exec_path="${!varname_EXEC_PATH:-$pkglibexecdir}"

	if [[ "$cmd" == */* ]]; then
		if ! [[ -f "${exec_path}/modules/$cmd" ]]; then
			error 127 'Module/Command not found: %s' "$cmd"
		fi
		mod="${cmd%%/*}"
		cmd="${cmd#*/}"
	fi
	shopt -s nullglob
	local files=("${exec_path}"/modules/*/"$cmd")
	if [[ ${#files[@]} -eq 0 ]]; then
		error 127 'Command not found: %s' "$cmd"
	fi
	files=("${files[@]#"${exec_path}/modules/"}")

	local tmpdir
	trap '[ -z "${tmpdir:-}" ] || rm -rf -- "$tmpdir"' EXIT
	tmpdir="$(mktemp -dt "${PACKAGE}.XXXXXXXXXX")"
	mkdir -- "$tmpdir/output"

	local repo
	repo="$(_repo)"
	export "${varname_REPO}=${repo}"

	local cwd
	printf -v cwd '%q' "$PWD"

	make --no-print-directory --quiet \
	     -f "$exec_path/runcmd.mk" \
	     -C "$tmpdir/output" \
	     CWD="$cwd" \
	     ARGS="${args_str//'$'/'$$'}" \
	     EXEC_PATH="$exec_path" \
	     TMPDIR="$tmpdir" \
	     -- "${files[@]}"
	local r=$?
	cd "$tmpdir/output"
	if [[ -n "$mod" ]]; then
		cat "$mod/$cmd"
	else
		grep -r ^ *
	fi
	return $r
}

_repo() {
	[[ $# -eq 0 ]] || errusage 1 'Usage: %q repo'
	if [ -z "${!varname_REPO:-}" ]; then # we aren't getting a value from then env
		local repo=".${PACKAGE,,}"

		OLDPWD=''
		#     [------can ascend-----] && ! [-not found repo--]
		while [ "$PWD" != "$OLDPWD" ] && ! [ -d "$PWD/$repo" ]; do
			cd ..
		done

		if [ -d "$PWD/$repo" ]; then
			# we found a repository
			printf '%s\n' "$PWD/$repo"
		else
			# we didn't find a repository
			error 128 "No %s repository found" "$PACKAGE"
		fi
	else
		printf '%s\n' "${!varname_REPO}"
	fi
}

_init() {
	[[ $# -le 1 ]] || errusage 1 'Usage: %q init [directory]'
	local dir="${1:-$PWD}"
	mkdir -p -- "$dir"
	cd "$dir"
	repo="$(_repo 2> /dev/null)" || true
	if [ -n "${repo:-}" ]; then
		error 129 'Repository already exists: %s' "$repo"
	fi
	export "$varname_REPO=$PWD/.${PACKAGE,,}"
	test -d "${!varname_REPO}" || mkdir "${!varname_REPO}"
	_runcmd init "$dir"
}

main() {
	install_sighandlers
	[[ $# -ge 1 ]] || error 2 'No command specified';
	export "${PACKAGE^^}=$program_name"

	local cmd=$1; shift
	case "$cmd" in
		repo) _repo "$@";;
		init) _init "$@";;
		*) _runcmd "$cmd" "$@";;
	esac
}

main "$@"

# Copy/Paste Virus 1.3c Please copy and paste this text anywhere. Track
# its progress by searching for this MD5#f7eac285ebfe21c4587bfebb9582f90d