#!/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 .
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 [/] []'
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