diff options
Diffstat (limited to 'wrapper/inner.sh')
-rw-r--r-- | wrapper/inner.sh | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/wrapper/inner.sh b/wrapper/inner.sh new file mode 100644 index 0000000..b1dc718 --- /dev/null +++ b/wrapper/inner.sh @@ -0,0 +1,145 @@ +#!/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 + echo "$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 "signalhandler_exit $signal '%s signal cought. Exiting...'" $signal + done + trap 'signalhandler_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" "$RVS" "${@:3}" + [[ $1 -eq 0 ]] || exit $1 +} + +_runcmd() { + [[ $# -ge 1 ]] || error 1 'Usage: %s <command> [<args>]' "$program_name" + local cmd=$1 + + local exec_path="${!varname_EXEC_PATH:-$pkglibexecdir}" + + if [ ! -e "$exec_path/$cmd" ] || [ "$cmd" = "${PACKAGE}" ]; then + error 127 '%s: Not a %s command' "$cmd" "$PACKAGE" + else + trap '[ -z "${TMPDIR:-}" ] || rm -rf -- "$TMPDIR"' EXIT + export TMPDIR="$(mktemp -dt "${PACKAGE}.XXXXXXXXXX")" + + repo="$(_repo)" + export "$varname_REPO=$repo" + + set -o pipefail + + if [ -f "$exec_path/$cmd" ]; then + "$exec_path/$cmd" "$@" | tee -- "$TMPDIR/$cmd" + elif [ -d "$exec_path/$cmd" ]; then + mkdir -p -- "$TMPDIR/$cmd" + local file + for file in "$exec_path/$command"/*; do + "$file" "$@" | tee -- "$TMPDIR/$command/${file##*/}" || + error 127 'Aborting: error encountered while running: %s' "$file" + done + else + error 127 '%s: Could not execute' "$exec_path/$cmd" + fi + fi +} + +_repo() { + [[ $# -ne 0 ]] || error 1 'Usage: %s repo' "$program_name" + if [ -z "${!varname_REPO:-}" ]; then # we aren't getting a value from then env + local repo=".${PACKAGE,,}" + + # [------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() { + [[ $# -gt 1 ]] || error 1 'Usage: %s init [directory]' "$program_name" + local dir="${1:-$PWD}" + mkdir -p -- "$dir" + cd "$dir" + repo="$(_repo 2> /dev/null)" || true + if [ -n "${repo:-}" ]; then + _error 129 "Repository already exists at \`%s'" "$repo" + fi + export "$varname_REPO=$PWD/.${PACKAGE,,}" + mkdir "${!varname_REPO}" + _runcmd init "$dir" +} + +main() { + install_sighandlers + [[ $# -ge 1 ]] || error '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 |