+#
+# set up git-annex aliases and functions
+#
+# Copyright © 2018 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+# Source repository: http://git.madduck.net/v/etc/zsh.git
+#
+
+alias gx='git annex'
+alias gx\?='alias -rm gx gx[a-z]'
+alias gxa='gx add'
+alias gxg='gx get'
+alias gxs='gx sync'
+alias gxd='gx drop'
+alias gxj='gx adjust'
+alias gxu='gx unlock'
+alias gxk='gx lock'
+
+function gxi() {
+ local cdup=$(git rev-parse --show-cdup 2>/dev/null || git init --quiet)
+ [[ -n $cdup ]] && builtin cd "$cdup"
+ gx init
+}
+
+function gxe() {
+ setopt localoptions xtrace
+ [[ -z "$1" ]] && { echo >&2 E: missing file; return 1; }
+ local -A files
+ for f; do
+ if [[ -f "$f" ]]; then
+ gxu "$f" || return 1
+ files[$f]=$(md5sum "$f")
+ fi
+ done
+ ${EDITOR:-editor} "$@"
+ for f; do
+ [[ -f "$f" ]] || continue
+ [[ $files[$f] == "$(md5sum "$f")" ]] || gxa "$f"
+ gxk "$f"
+ done
+}
+compdef -a _files gxe
+
+function gxtestfile() {
+ local fname="${1:-$(tempfile -d ${PWD})}"
+ local fname="${fname##*/}"
+ echo "${(%):-"%D{%c}"}" >| "$fname"
+ gxa "$fname"
+ gc -m"add test file $fname"
+}
+
+function gxl() {
+ setopt localoptions xtrace
+ local rloc="$1"
+ local dest="${2:-.}"
+
+ if [[ -z $rloc ]]; then
+ echo >&2 "E: missing remote location."
+ return 1
+ fi
+
+ local host="${rloc%%:*}"
+ local rdest="${rloc#*:}"
+ local origin="${host%%.*}"
+ local localhost="${(%):-%m}"
+ git clone --origin "$origin" "$rloc" "$dest"
+ builtin cd "$dest"
+ git annex --quiet init
+ ssh "$host" "builtin cd '$rdest' && git remote add $localhost $localhost:${PWD#${HOME}/} && git annex --quiet sync"
+ git annex sync
+}
+compdef gxl=scp
+
+function gxx() {
+ #setopt localoptions xtrace
+ local mode
+ [[ $1 =~ (start|stop|q) ]] && { mode=$1; shift; }
+
+ local dir="${${1:a}:-${PWD}}"
+ if [[ ! -d $dir ]]; then
+ echo >&2 "E: directory does not exist: $dir"
+ return 1
+ elif [[ ! -d $dir/.git ]]; then
+ echo >&2 "E: not a Git repository: $dir"
+ return 1
+ fi
+
+ local escaped="$(systemd-escape "$dir")"
+ local service="git-annex-assistant@${escaped}.service"
+ local function sc() { systemctl --quiet --user "$@" "$service"; }
+ case "${mode:-start}" in
+ (start)
+ sc enable
+ sc start
+ ;;
+ (*)
+ sc is-failed && sc reset-failed
+ sc stop
+ sc disable
+ ;;
+ esac
+ sc --no-pager status
+}
+
+# vim:ft=zsh