-
Notifications
You must be signed in to change notification settings - Fork 42
/
install.sh
executable file
·130 lines (113 loc) · 3.69 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
# source: https://github.com/foundObjects/zram-swap
# shellcheck disable=SC2039,SC2162
#[ "$(id -u)" -eq '0' ] || { echo "This script requires root." && exit 1; }
case "$(readlink /proc/$$/exe)" in */bash) set -euo pipefail ;; *) set -eu ;; esac
# ensure a predictable environment
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
\unalias -a
# installer main body:
_main() {
# ensure $1 exists so 'set -u' doesn't error out
{ [ "$#" -eq "0" ] && set -- ""; } > /dev/null 2>&1
case "$1" in
"--uninstall")
# uninstall, requires root
assert_root
_uninstall
;;
"--install" | "")
# install dpkg hooks, requires root
assert_root
_install "$@"
;;
*)
# unknown flags, print usage and exit
_usage
;;
esac
exit 0
}
_install() {
configdiff=''
newconfig=''
if systemctl -q is-active zram-swap.service; then
echo "Stopping zram-swap service"
systemctl stop zram-swap.service
fi
echo "Installing script and service ..."
install -o root zram-swap.sh /usr/local/sbin/zram-swap.sh
install -o root -m 0644 service/zram-swap.service /etc/systemd/system/zram-swap.service
# rename & cleanup old version config file
if [ -f /etc/default/zram-swap-service ]; then
mv -f /etc/default/zram-swap-service /etc/default/zram-swap
chown root:root /etc/default/zram-swap
chmod 0644 /etc/default/zram-swap
fi
if [ -f /etc/default/zram-swap ]; then
{
set +e
configdiff=$(diff -y /etc/default/zram-swap service/zram-swap.config)
set -e
} > /dev/null 2>&1
if [ -n "$configdiff" ]; then
yn=''
echo "Local configuration differs from packaged version"
echo
echo "Install package default configuration? Local config will be saved as /etc/default/zram-swap.oldconfig"
while true; do
echo "(I)nstall package default / (K)eep local configuration / View (D)iff"
printf "[i/k/d]: "
read yn
case "$yn" in
[Ii]*)
echo "Installing package default ..."
install -o root -m 0644 --backup --suffix=".oldconfig" service/zram-swap.config /etc/default/zram-swap
newconfig='y'
break
;;
[Kk]*) break ;;
[Dd]*) printf "%s\n\n" "$configdiff" ;;
esac
done
fi
else
install -o root -m 0644 -b service/zram-swap.config /etc/default/zram-swap
fi
echo "Reloading systemd unit files and enabling boot-time service ..."
systemctl daemon-reload
systemctl enable zram-swap.service
if [ -n "$newconfig" ]; then
cat <<- HEREDOC
Configuration file updated; old config saved as /etc/default/zram-swap.oldconfig
Please review changes between configurations and then start the service with
systemctl start zram-swap.service
HEREDOC
else
echo "Starting zram-swap service ..."
systemctl start zram-swap.service
fi
echo
echo "zram-swap service installed successfully!"
echo
}
_uninstall() {
if systemctl -q is-active zram-swap.service; then
echo "Stopping zram-swap service"
systemctl stop zram-swap.service
fi
echo "Uninstalling script and systemd service."
if [ -f /etc/systemd/system/zram-swap.service ]; then
systemctl disable zram-swap.service || true
rm -f /etc/systemd/system/zram-swap.service
fi
if [ -f /usr/local/sbin/zram-swap.sh ]; then
rm -f /usr/local/sbin/zram-swap.sh
fi
echo "Reloading systemd unit files"
systemctl daemon-reload
echo "zram-swap service uninstalled; remove configuration /etc/default/zram-swap if desired"
}
assert_root() { [ "$(id -u)" -eq '0' ] || { echo "This action requires root." && exit 1; }; }
_usage() { echo "Usage: $(basename "$0") (--install|--uninstall)"; }
_main "$@"