-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·114 lines (103 loc) · 3.06 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/sh
set -e
set -u
cd "$(dirname "$0")"
umask 027
my_find() {
find "$@" \
| awk '($0 ~ /^\.\/\./ && $0 !~ /^\.\/\.git/) { print }'
}
ask_overwrite() {
local fn=$1; shift
local default_answer=${1:-N}
case $default_answer in
Y) local answers="(Y/n)" ;;
*) local answers="(y/N)" ;;
esac
while true; do
echo
grep -viE '\bpass(word|wd|)\b' "$fn" | head -n 10
printf "OK to overwrite this $fn? $answers "
read ans
ans=$(echo "${ans:-$default_answer}" | tr '[:upper:]' '[:lower:]')
case "$ans" in
n|no) return 1 ;;
y|yes) return 0 ;;
esac
done
}
can_overwrite() {
local fn=$1; shift
local target=$1; shift
local overwrite_default=N
test -e "$target" || return 0
case "$fn" in
# Files that start with standard settings
.config/hg/hgrc|.config/mutt/muttrc)
local linecount=$(wc -l <"$fn")
head -n "$linecount" "$target" | cmp -s "$fn" - && return 1
;;
.config/screen/local)
return 1
;;
*)
cmp -s "$fn" "$target" && return 1
overwrite_default=Y
;;
esac
ask_overwrite "$target" $overwrite_default
}
link_if_ok() {
local source="$1"; shift
local target="$1"; shift
if [ -h "$target" ] \
&& [ "$(readlink -e "$source")" = "$(readlink -e "$target")" ]; then
true
elif can_overwrite "$source" "$target"; then
ln -srb "$source" "$target"
fi
}
destdir=${1:-$HOME}
for dn in $(my_find -type d); do
test -d "$destdir/$dn" || mkdir "$destdir/$dn"
done
for fn in $(my_find -type f); do
target="$destdir/$fn"
if can_overwrite "$fn" "$target"; then
cp -b "$fn" "$target"
fi
done
shelldir=".config/bcsh"
cd "$shelldir"
for fn in $(my_find -type f -name .\* ); do
link_if_ok "$destdir/$shelldir/$fn" "$destdir/$fn"
done
for fn in .bash_profile .bashrc; do
link_if_ok "$destdir/$shelldir/bashrc" "$destdir/$fn"
done
link_if_ok "$destdir/.config/emacs" "$destdir/.emacs.d" # Obsolete by Emacs 27
link_if_ok "$destdir/.config/mutt" "$destdir/.mutt" # Obsolete by mutt 2.0
cd - >/dev/null
if [ -z "${1:-}" ] && [ -d /run/systemd/system ] && { \
[ -e "/var/lib/systemd/linger/$(id -nu)" ] \
|| loginctl enable-linger; } ; then
cd .config/systemd/user
systemctl --user daemon-reload
systemctl --user enable --now systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
ls -1 | while read service_name; do
case "$service_name" in
emacs.service|tmpfiles-*.*)
systemctl --user disable "$service_name"
rm "$service_name"
continue
;;
*@.*) pattern='^DefaultInstance *=' ;;
*) pattern='^\[Install\]$' ;;
esac
if grep -qr "$pattern" "$service_name" \
&& [ "$(systemctl --user is-enabled "$service_name" || true)" = disabled ]; then
systemctl --user enable "$service_name"
fi
done
cd - >/dev/null
fi