123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #!/bin/bash
-
- warn() {
- echo "$@" >&2
- }
-
- think() {
- $Verbose || return
- warn "$@"
- }
-
- die() {
- warn "$@"
- exit 3
- }
-
- usage() {
- warn "usage: $0 init VAULT SUBVAULT"
- warn "usage: $0 explore VAULT SUBVAULT"
- exit 2
- }
-
- debug() {
- $Debug || return
- local msg
- for msg in "$@";
- do
- warn "debug:$msg"
- done
- }
-
- debugv() {
- $Debug || return
- local vn
- local vd
- for vn in "$@";
- do
- if vd=$(declare -p "$vn" 2>/dev/null);
- then
- warn "debug:${vd#declare ?? }"
- else
- warn "debug:$vn #Unset"
- fi
- done
- }
-
- debugc() {
- $Debug || return
- debug "-- cmd begin: $*"
- Debug=false "$@" | sed -e 's/^/debug:/' >&2
- debug "-- cmd end --"
- }
-
- lssv() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- local sv
- test -n "${SubVaults[0]}" && {
- think "using sub-vaults from command line"
- for sv in "${SubVaults[@]}";
- do
- echo "$sv"
- done
- return
- }
- test -f "$Vault/dotvault/subvaults" && {
- think "reading subvaults from file: $Vault/dotvault/subvaults"
- grep . "$Vault/dotvault/subvaults" \
- | grep -v "^#.*"
- return
- }
- think "using hard-coded list of subvaults"
- echo config
- }
-
- lsvault() {
-
-
-
- {
- echo "$Vault"
- lssv \
- | while IFS= read -r sv;
- do
- test -e "$Vault/$sv" || {
- think "ignoring non-existent sub-vault: $sv"
- continue
- }
- test -d "$Vault/$sv" || {
- warn "ignoring non-directory sub-vault: $sv"
- continue
- }
- echo "$Vault/$sv"
- done
- } \
- | LC_ALL=C sort \
- | tac
- }
-
- istarget() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- local item=$1
- debugv item
- Verbose=false lsvault | grep -qxF "$item" && return 1
- Verbose=false lsvault | grep -qx "$item/.*" && return 1
- return 0
- }
-
- pfxpath() {
-
-
-
-
-
-
- local pfx="$1"
- local path
- while IFS= read -r path;
- do
- echo "$pfx$path"
- done
- }
-
- lstarget() {
-
-
-
- local vlt=$1
- debugv vlt
- local item
- find "$vlt" -mindepth 1 -maxdepth 1 -printf "%P\n" \
- | pfxpath "$vlt/" \
- | grep -v "$Vault/dotvault" \
- | while IFS= read -r item;
- do
- istarget "$item" && echo "$item"
- done
- }
-
- make_links() {
-
-
-
- local slpath
- local target
- local vlt
- lsvault \
- | while IFS= read -r vlt;
- do
- lstarget "$vlt" \
- | while IFS= read -r target;
- do
- slpath=$(slpath "$target")
- if test -L "$slpath";
- then
- $ClobberLinks || {
- warn "skipping existing slpath: $slpath"
- continue
- }
- think "removing existing link: $slpath"
- maybe rm "$slpath"
- fi
- test -e "$slpath" && {
- warn "skipping existing non-link: $slpath"
- continue
- }
- maybe ln -sr "$target" "$slpath"
- done
- done
- }
-
- explore() {
-
-
-
- local vlt
- local tgt
- local tgts
- lsvault \
- | sort \
- | while IFS= read -r vlt;
- do
- tgts=$(lstarget "$vlt")
- test -n "$tgts" || continue
- if test "$vlt" == "$Vault";
- then
- echo "VAULT:$vlt:"
- else
- echo
- echo "SUBVAULT:$vlt"
- fi
- sort <<<"$tgts" \
- | while IFS= read -r tgt;
- do
- echo "${tgt}:$(slpath "$tgt")"
- done \
- | sed -e "s|:$HOME|:=> ~|" \
- | column -ts: \
- | sed -e "s/^/ /"
- done
- }
-
- maybe() {
-
-
-
- $Debug && { debug "WOULD: $*"; return; }
- "$@"
- }
-
- slpath() {
-
-
-
- local item=$1
- echo "$HOME/.${item#$Vault/}"
- }
-
- route() {
- local Vault=$1; shift
- local SubVaults=()
- test -n "$Vault" || usage
- test -e "$Vault" || die "vault does not exist: $Vault"
- test -d "$Vault" || die "vault is not a directory: $Vault"
- SubVaults=("$@")
- case $Action in
- init) make_links ;;
- explore) explore ;;
- *) usage ;;
- esac
- }
-
- export LC_ALL=C
-
- main() {
- local Action
- local Debug=false
- local Verbose=false
- local ClobberLinks=false
- while true; do case $1 in
- -d) Debug=true; shift ;;
- -v) Verbose=true; shift ;;
- -f) ClobberLinks=true; shift ;;
- -*) usage ;;
- *) break ;;
- esac done
- Action=$1; shift
- route "$@"
- }
-
- main "$@"
|