]> git.sev.monster Git - dotfiles.git/blame_incremental - install.sh
major rework
[dotfiles.git] / install.sh
... / ...
CommitLineData
1#!/bin/sh
2
3# install location and source files
4: ${DEST:=$(realpath "$HOME")}
5: ${SRC:=$(dirname "$(realpath "$0")")}
6
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}
13
14# these are the vars we actually use
15share="${XDG_DATA_HOME#$DEST/}"
16etc="${XDG_CONFIG_HOME#$DEST/}"
17state="${XDG_STATE_HOME#$DEST/}"
18cache="${XDG_CACHE_HOME#$DEST/}"
19
20# test
21devnull=/dev/null
22if [ "$1" = "test" ]; then
23 echo "Running test -- no changes will be applied"
24 devnull=/dev/stdout
25 _cd() {
26 cd $*;
27 echo "-- cd $* --> $PWD";
28 }
29 alias cd="_cd"
30 alias mkdir="echo -- mkdir"
31 alias ln="echo -- ln"
32 alias rm="echo -- rm"
33 alias sh="echo -- sh"
34fi
35
36# ensure ostype
37# NOTE: copied from .zshenv
38if [ -z "$OSTYPE" ]; then
39 OSTYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
40fi
41
42# accomodate multiple userlands
43# XXX: assuming GNU by default
44# TODO: test for GNU, fallback to POSIX instead (no -n)
45lnargs=-sfnv
46case "$OSTYPE" in
47 freebsd*) lnargs=-sFhv;;
48 netbsd|openbsd*) lnargs=-sfhv;;
49esac
50
51cd "$DEST"
52
53# create preferred folder structure
54mkdir -pm 760 "$share" "$etc" "$state" "$cache"
55ln $lnargs "$DEST/$etc" "$LOCAL/etc"
56mkdir -p "$LOCAL/bin" "$share/fonts" "$share/themes" "$share/icons"
57
58# clean out old links
59# TODO: doesn't remove dead links if realpath fails; this happens if the link
60# target doesn't exist under busybox
61find . "$share" "$etc" \
62 "$LOCAL/bin" "$share/fonts" "$share/themes" "$share/icons" \
63 "$etc/gnupg" .ssh .termux \
64 -xdev -mindepth 1 -maxdepth 1 -type l -exec sh -c '
65 r="$(realpath "{}")"
66 [ "${r#'"$SRC"'/}" != "$r" ] &&
67 '"$([ "$1" = test ] && echo 'echo +++ ')"'unlink "{}"' \;
68
69cd "$SRC" >$devnull
70
71l() {
72 # TODO: use install?
73 d="$DEST/${1:-$x}"
74 [ -L "$d" -o \! -e "$d" ] && ln $lnargs "$SRC/${2:-$x}" "$d" ||
75 echo "!!! File exists and is not a link: $1"
76}
77
78# generic links
79find base -mindepth 1 -maxdepth 1 | while read -r x; do l "${x#base/}"; done
80find bin -mindepth 1 -maxdepth 1 | while read -r x; do l "$LOCAL/$x"; done
81find share -mindepth 1 -maxdepth 1 | while read -r x; do l "$share${x#share}"; done
82find etc -mindepth 1 -maxdepth 1 | while read -r x; do l "$etc${x#etc}"; done
83
84# xorg/wayland resources
85find gui -mindepth 1 -maxdepth 1 | while read -r y; do
86 case "${y#gui/}" in
87 icons|themes)
88 find "$y" -mindepth 1 -maxdepth 2 -type d | while read -r x; do
89 [ ! -e "$x/index.theme" ] && continue
90 command -v gtk-update-icon-cache >$devnull 2>&1 &&
91 gtk-update-icon-cache -f "$x"
92 l "$share${y#gui}/$(basename "$x")"
93 done
94 ;;
95 fonts)
96 find "$y" -mindepth 1 -maxdepth 2 -type d | while read -r x; do
97 command -v mkfontscale >$devnull 2>&1 && mkfontscale "$x"
98 command -v mkfontdir >$devnull 2>&1 && mkfontdir "$x"
99 [ "$(head -1 "$x/fonts.scale" >$devnull 2>&1)" = 0 ] &&
100 rm "$x/fonts.scale"
101 [ "$(head -1 "$x/fonts.dir" >$devnull 2>&1)" = 0 ] &&
102 rm "$x/fonts.dir"
103 l "$share${y#gui}/$(basename "$x")"
104 done
105 ;;
106 *)
107 find "$y" -mindepth 1 -maxdepth 1 | while read -r x; do
108 l "$share${x#gui}"
109 done
110 ;;
111 esac
112done
113
114# gpg
115if command -v gpg >$devnull 2>&1; then
116 mkdir -pm 700 "$DEST/$etc/gnupg"
117 find gnupg -mindepth 1 -maxdepth 1 \! -name '*.gpg' | while read -r x; do
118 l "$etc/$x"
119 done
120 find gnupg -mindepth 1 -maxdepth 1 -name '*.gpg' | while read -r x; do
121 gpg --import "$x"
122 done
123fi
124
125# ssh
126find ssh -mindepth 1 -maxdepth 1 | while read -r x; do
127 l ".$x"; done
128
129# termux, assume it if android
130if [ $(uname -o) = 'Android' ]; then
131 mkdir -p "$DEST/.termux"
132 find termux -mindepth 1 -maxdepth 1 | while read -r x; do l ".$x"; done
133 l '.termux/font.ttf' "$share/fonts/Dina-ttf/Dina.ttf"
134fi
135
136# vim: sts=4 sw=4 et
This page took 0.028913 seconds and 4 git commands to generate.