3 # install location and source files
4 : ${DEST:=$(realpath "$HOME")}
5 : ${SRC:=$(dirname "$(realpath "$0")")}
7 # confirm XDG vars we care about are set up
8 : ${LOCAL:=.local} # NOTE: needed for bin, which has no xdg var
9 : ${XDG_DATA_HOME:=$DEST/$LOCAL/share}
10 : ${XDG_CONFIG_HOME:=$DEST/.config}
11 : ${XDG_STATE_HOME:=$DEST/$LOCAL/state}
12 : ${XDG_CACHE_HOME:=$DEST/.cache}
14 # these are the vars we actually use
15 share="${XDG_DATA_HOME#$DEST/}"
16 etc="${XDG_CONFIG_HOME#$DEST/}"
17 state="${XDG_STATE_HOME#$DEST/}"
18 cache="${XDG_CACHE_HOME#$DEST/}"
22 if [ "$1" = "test" ]; then
23 echo "Running test -- no changes will be applied"
26 echo "-- cd $* --> $PWD";
30 alias mkdir="echo -- mkdir"
31 alias rmdir="echo -- rmdir"
39 # NOTE: copied from .zshenv
40 if [ -z "$OSTYPE" ]; then
41 OSTYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
44 # accomodate multiple userlands
45 # XXX: assuming GNU by default
46 # TODO: test for GNU, fallback to POSIX instead (no -n)
49 freebsd*) lnargs=-sFhv;;
50 netbsd|openbsd*) lnargs=-sfhv;;
58 # create preferred folder structure
59 mkdir -pm 760 "$share" "$etc" "$state" "$cache"
60 ln $lnargs "$DEST/$etc" "$LOCAL/etc"
61 mkdir -p "$LOCAL/bin" "$share/fonts" "$share/themes" "$share/icons"
64 # TODO: doesn't remove dead links if realpath fails; this happens if the link
65 # target doesn't exist under busybox
66 find . "$share" "$etc" \
67 "$LOCAL/bin" "$share/fonts" "$share/themes" "$share/icons" \
68 "$etc/gnupg" .ssh .termux \
69 -xdev -mindepth 1 -maxdepth 1 -type l -exec sh -c '
71 [ "${r#'"$SRC"'/}" != "$r" ] &&
72 '"$([ "$1" = test ] && echo 'echo +++ ')"'unlink "{}"' \;
79 [ -L "$d" -o \! -e "$d" ] && ln $lnargs "$SRC/${2:-$x}" "$d" ||
80 echo "!!! File exists and is not a link: $1"
84 find base -mindepth 1 -maxdepth 1 | while read -r x; do l "${x#base/}"; done
85 find bin -mindepth 1 -maxdepth 1 | while read -r x; do l "$LOCAL/$x"; done
86 find share -mindepth 1 -maxdepth 1 | while read -r x; do l "$share${x#share}"; done
87 find etc -mindepth 1 -maxdepth 1 | while read -r x; do l "$etc${x#etc}"; done
89 # xorg/wayland resources
90 find gui -mindepth 1 -maxdepth 1 | while read -r y; do
93 find "$y" -mindepth 1 -maxdepth 2 -type d | while read -r x; do
94 [ ! -e "$x/index.theme" ] && continue
95 command -v gtk-update-icon-cache >$devnull 2>&1 &&
96 gtk-update-icon-cache -f "$x"
97 l "$share${y#gui}/$(basename "$x")"
101 find "$y" -mindepth 1 -maxdepth 2 -type d | while read -r x; do
102 command -v mkfontscale >$devnull 2>&1 && mkfontscale "$x"
103 command -v mkfontdir >$devnull 2>&1 && mkfontdir "$x"
104 [ "$(head -1 "$x/fonts.scale" >$devnull 2>&1)" = 0 ] &&
106 [ "$(head -1 "$x/fonts.dir" >$devnull 2>&1)" = 0 ] &&
108 l "$share${y#gui}/$(basename "$x")"
112 find "$y" -mindepth 1 -maxdepth 1 | while read -r x; do
120 if command -v gpg >$devnull 2>&1; then
121 mkdir -pm 700 "$DEST/$etc/gnupg"
122 find gnupg -mindepth 1 -maxdepth 1 \! -name '*.gpg' | while read -r x; do
125 find gnupg -mindepth 1 -maxdepth 1 -name '*.gpg' | while read -r x; do
131 if command -v ssh >$devnull 2>&1; then
132 mkdir -pm 700 "$DEST/.ssh"
133 find ssh -mindepth 1 -maxdepth 1 | while read -r x; do
137 # termux, assume it if android
138 if [ $(uname -o) = 'Android' ]; then
139 mkdir -p "$DEST/.termux"
140 find termux -mindepth 1 -maxdepth 1 | while read -r x; do l ".$x"; done
141 l '.termux/font.ttf' "gui/fonts/Dina-ttf/Dina.ttf"
146 # ensure xdg user dirs, and move old to new while we're at it
147 source $etc/user-dirs.dirs
148 function fixup_xdg_home {
149 if [ -z "$1" -o "${1#/}" = "$1" -o "$(realpath "$1")" = "$(realpath "$HOME")" ]; then
153 if [ $? -eq 0 -a -n "$2" -a -d "$2" ]; then
158 fixup_xdg_home $XDG_DESKTOP_DIR Desktop
159 fixup_xdg_home $XDG_DOWNLOAD_DIR Downloads
160 fixup_xdg_home $XDG_TEMPLATES_DIR
161 fixup_xdg_home $XDG_PUBLICSHARE_DIR Public
162 fixup_xdg_home $XDG_DOCUMENTS_DIR Documents
163 fixup_xdg_home $XDG_MUSIC_DIR Music
164 fixup_xdg_home $XDG_PICTURES_DIR Pictures
165 fixup_xdg_home $XDG_VIDEOS_DIR Videos