+
+# install location and source files
+: ${DEST:=$(realpath "$HOME")}
+: ${SRC:=$(dirname "$(realpath "$0")")}
+
+# confirm XDG vars we care about are set up
+: ${LOCAL:=.local} # NOTE: needed for bin, which has no xdg var
+: ${XDG_DATA_HOME:=$DEST/$LOCAL/share}
+: ${XDG_CONFIG_HOME:=$DEST/.config}
+: ${XDG_STATE_HOME:=$DEST/$LOCAL/state}
+: ${XDG_CACHE_HOME:=$DEST/.cache}
+
+# these are the vars we actually use
+share="${XDG_DATA_HOME#$DEST/}"
+etc="${XDG_CONFIG_HOME#$DEST/}"
+state="${XDG_STATE_HOME#$DEST/}"
+cache="${XDG_CACHE_HOME#$DEST/}"
+
+# test
+devnull=/dev/null
+if [ "${DOTFILES_TEST+x}" = x ]; then
+ echo "Running test -- no changes will be applied"
+ devnull=/dev/stdout
+ _cd() {
+ echo "-- cd $PWD --> $*";
+ cd $*;
+ }
+ alias cd="_cd"
+ alias mkdir="echo -- mkdir"
+ alias rmdir="echo -- rmdir"
+ alias ln="echo -- ln"
+ alias rm="echo -- rm"
+ alias mv="echo -- mv"
+ alias sh="echo -- sh"
+fi
+
+# ensure ostype
+# NOTE: copied from .zshenv
+if [ -z "$OSTYPE" ]; then
+ OSTYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
+fi
+
+# accomodate multiple userlands
+# XXX: assuming GNU by default
+# TODO: test for GNU, fallback to POSIX instead (no -n)
+lnargs=-sfnv
+case "$OSTYPE" in
+ freebsd*) lnargs=-sFhv;;
+ netbsd|openbsd*) lnargs=-sfhv;;
+esac
+
+# save pwd
+old_pwd="$PWD"
+
+cd "$DEST"
+
+# create preferred folder structure
+mkdir -pm 760 "$share" "$etc" "$state" "$cache"
+ln $lnargs "$DEST/$etc" "$LOCAL/etc"
+mkdir -p "$LOCAL/bin" "$share/fonts" "$share/themes" "$share/icons"
+
+# clean out old links
+# TODO: doesn't remove dead links if realpath fails; this happens if the link
+# target doesn't exist under busybox
+find . "$share" "$etc" \
+ "$LOCAL/bin" "$share/fonts" "$share/themes" "$share/icons" \
+ "$etc/gnupg" .ssh .termux \
+ -xdev -mindepth 1 -maxdepth 1 -type l -exec sh -c '
+ r="$(realpath "{}" 2>/dev/null)"
+ [ "${r#'"$SRC"'/}" != "$r" ] &&
+ '"$([ "${DOTFILES_TEST+x}" = x ] && echo 'echo +++ ')"'unlink "{}"' \;
+
+cd "$SRC" >$devnull
+
+l() {
+ # TODO: use install?
+ d="$DEST/${1:-$x}"
+ [ -L "$d" -o \! -e "$d" ] && ln $lnargs "$SRC/${2:-$x}" "$d" ||
+ echo "!!! File exists and is not a link: $1"
+}
+
+# generic links
+find base -mindepth 1 -maxdepth 1 | while read -r x; do l "${x#base/}"; done
+find bin -mindepth 1 -maxdepth 1 | while read -r x; do l "$LOCAL/$x"; done
+find share -mindepth 1 -maxdepth 1 | while read -r x; do l "$share${x#share}"; done
+find etc -mindepth 1 -maxdepth 1 | while read -r x; do l "$etc${x#etc}"; done
+
+# xorg/wayland resources
+find gui -mindepth 1 -maxdepth 1 | while read -r y; do
+ case "${y#gui/}" in
+ icons|themes)
+ find "$y" -mindepth 1 -maxdepth 2 -type d | while read -r x; do
+ [ ! -e "$x/index.theme" ] && continue
+ command -v gtk-update-icon-cache >$devnull 2>&1 &&
+ gtk-update-icon-cache -f "$x"
+ l "$share${y#gui}/$(basename "$x")"
+ done
+ ;;
+ fonts)
+ find "$y" -mindepth 2 -maxdepth 2 -type d | while read -r x; do
+ command -v mkfontscale >$devnull 2>&1 && mkfontscale "$x"
+ command -v mkfontdir >$devnull 2>&1 && mkfontdir "$x"
+ [ "$(head -1 "$x/fonts.scale" >$devnull 2>&1)" = 0 ] &&
+ rm "$x/fonts.scale"
+ [ "$(head -1 "$x/fonts.dir" >$devnull 2>&1)" = 0 ] &&
+ rm "$x/fonts.dir"
+ l "$share${y#gui}/$(basename "$x")"
+ done
+ ;;
+ *)
+ find "$y" -mindepth 1 -maxdepth 1 | while read -r x; do
+ l "$share${x#gui}"
+ done
+ ;;
+ esac