From 8747c72b953e5eadf25677e1d0bb9003bfcdc211 Mon Sep 17 00:00:00 2001
From: Sven Hoexter <sven@stormbind.net>
Date: Thu, 13 Dec 2018 17:58:56 +0100
Subject: [PATCH] prototype of a thin shell wrapper around pass to access
 multiple repository

---
 home/mpass.sh | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)
 create mode 100755 home/mpass.sh

diff --git a/home/mpass.sh b/home/mpass.sh
new file mode 100755
index 0000000..8d88b6d
--- /dev/null
+++ b/home/mpass.sh
@@ -0,0 +1,114 @@
+#!/bin/bash
+
+set -eu
+
+# since we're running with nounset we've to ensure all variables exist
+repo=""
+
+function help {
+    cat <<EOF
+    ${0} [ -r repository ] [ pull | push | pass-command ]
+
+    mpass is a thin wrapper around pass to ease working with
+    multiple repository.
+
+    Before you get started create a configuration file in
+    ${HOME}/.mpass. This fill must contain an associative bash array
+    named "REPO_STORE_DIR[reponame]" to define your repository location.
+    The default repoistory should be named "default" is used when no
+    other repository name is defined with "-r" on the mpass invocation.
+
+    Example:
+      REPO_STORE_DIR[default]="/home/sven/pass"
+      REPO_STORE_DIR[foo]="/home/sven/pass-foo"
+
+    
+    Commands:
+
+    pull: executes a "git pull" in the configured REPO_STORE_DIR[reponame]
+    push: executes a "git push" in the configured REPO_STORE_DIR[reponame]
+
+EOF
+    exit 42
+}
+
+function initialize {
+    # try to source our configuration
+    if [ -f "${HOME}/.mpass" ]; then
+	# declare all known configuration options as an associative array first
+	declare -A REPO_STORE_DIR
+	source ${HOME}/.mpass
+    else
+	echo "ERROR: Could not find mpass configuration in ${HOME}/.mpass" >&2
+	help
+    fi
+
+    # in case no repository is defined, assume it's "default"
+    if [ -z "${repo}" ]; then
+	repo="default"
+    fi
+
+    # define pass repo location
+    # this variable is also used by pass to decide where to operate
+    export PASSWORD_STORE_DIR="${REPO_STORE_DIR[${repo}]}"
+
+    # check if we actually have something that could be a repository configuraton
+    if [ -z "${PASSWORD_STORE_DIR}" ]; then
+	echo "ERROR: No valid repository configuration found for repo ${repo}" >&2
+	help
+    fi
+
+    # try to find out if we've a repository at hand, otherwise issue a warning
+    # this could still result in an error, but is perfectly fine on an "init" command
+    if ! [ -d "${PASSWORD_STORE_DIR}" ]; then
+	echo "WARNING: Given pass repo ${PASSWORD_STORE_DIR} does not exist" >&2
+    fi
+}
+
+while getopts ":r:" option; do
+  case $option in
+    r)
+	repo="${OPTARG}"
+	shift $((OPTIND-1))
+	;;
+    \?)
+	echo "ERROR: Invalid option: -$OPTARG" >&2
+	help
+	;;
+    :)
+	echo "ERROR: Option -$OPTARG requires an argument" >&2
+	help
+	;;
+  esac
+done
+
+if [[ -z "${@}" ]]; then
+    echo "ERROR: no commands given" >&2
+    help
+fi
+
+# read our configuration
+initialize
+
+case $1 in
+    pull)
+	echo "Trying to update pass repo ${PASSWORD_STORE_DIR}"
+	cd ${PASSWORD_STORE_DIR}
+	set +e
+	git pull
+	set -e
+	cd -
+	;;
+    push)
+	echo "Trying to push pass repo ${PASSWORD_STORE_DIR}"
+	cd ${PASSWORD_STORE_DIR}
+	set +e
+	git push
+	set -e
+	cd -
+	;;
+    *)
+	echo "Remaining pass commands: $@"
+	pass ${@}
+	;;
+esac
-- 
2.39.5