-
Notifications
You must be signed in to change notification settings - Fork 0
/
x735v1_install.sh
executable file
·151 lines (131 loc) · 3.73 KB
/
x735v1_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/sh
# Custom implementation for power management by addon board x735v1
# uses *gpio-watch* from https://github.com/larsks/gpio-watch.git
# _Requires_ additional connection of the power button to pin 16
if ! command -v git > /dev/null; then
sudo apt install -y git
fi
if ! command -v make > /dev/null; then
sudo apt install -y build-essential
fi
poweroff_path="/usr/local/bin/x735_poweroff.sh"
get_gpio_watch()
{
local builddir; builddir=$(mktemp -d)
cd "$builddir" && \
git clone https://github.com/larsks/gpio-watch.git && \
cd gpio-watch && \
make && \
sudo make install && \
sudo mkdir -p /etc/gpio-scripts && \
rm -Rf "$builddir"
}
install_startup_script()
{
if [ -z "$1" ]; then
echo "No startup script path provided, breaking up!"
exit 1
fi
local targetfn; targetfn="$1"
local tempfn; tempfn="$(mktemp)"
cat > "$tempfn" << EOF
#!/bin/sh
PINBTN=16
echo "\$PINBTN" > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio\$PINBTN/direction
# create shutdown script
scriptfn="/etc/gpio-scripts/\$PINBTN"
# shutdown calls x735 poweroff script via systemd runonce service
(echo "#!/bin/sh";
echo "sudo /sbin/shutdown -h now";) > "\$scriptfn" && \
chmod 755 "\$scriptfn"
PINBOOT=17
echo "\$PINBOOT" > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio\$PINBOOT/direction
echo 1 > /sys/class/gpio/gpio\$PINBOOT/value
#gpio-watch -l /var/log/gpio-watch_\$PINBTN.log \$PINBTN:switch &
# on button press, custom pin goes from 1->0->1 (on pushing down)
gpio-watch -l /var/log/gpio-watch_\$PINBTN.log \$PINBTN:falling &
EOF
if [ ! -f "$tempfn" ]; then
echo "Could not create x735 startup script!"
return
fi
chmod 755 "$tempfn" && \
sudo mv "$tempfn" "$targetfn" && \
sudo chown root.root "$targetfn" && \
sudo sed -i -e "\#^$targetfn# d" /etc/rc.local && \
sudo sed -i -e "$ i $targetfn" /etc/rc.local
}
install_poweroff_script()
{
if [ -z "$1" ]; then
echo "No power off script path provided, breaking up!"
exit 1
fi
local targetfn; targetfn="$1"
local tempfn; tempfn="$(mktemp)"
cat > "$tempfn" << EOF
#!/bin/sh
# largely identical to https://github.com/geekworm-com/x730-script
BUTTON=18
echo "\$BUTTON" > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio\$BUTTON/direction
echo 1 > /sys/class/gpio/gpio\$BUTTON/value
TIMEOUT=\${1:-4}
TIMEOUT=3
if ! echo "\$TIMEOUT" | egrep -q '^[0-9\\.]+$'; then
echo "ERROR: Sleep timeout '\$TIMEOUT' not a number!" >&2
exit 1
fi
echo "X730 Shutting down in \$TIMEOUT seconds ..."
/bin/sleep \$TIMEOUT
# restore GPIO 18
echo 0 > /sys/class/gpio/gpio\$BUTTON/value
EOF
if [ ! -f "$tempfn" ]; then
echo "Could not create x735 poweroff script!"
return
fi
chmod 755 "$tempfn" && \
sudo mv "$tempfn" "$targetfn" && \
sudo chown root.root "$targetfn"
}
install_systemd_service()
{
if [ -z "$1" ]; then
echo "No power off script path provided, breaking up!"
exit 1
fi
local poweroff_path; poweroff_path="$1"
local targetfn; targetfn=/etc/systemd/system/x735_before_shutdown.service
local service_name; service_name="$(basename "$targetfn")"
local tempfn; tempfn="$(mktemp)"
cat > "$tempfn" << EOF
[Unit]
Description=Before Shutting Down
After=reboot.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=$poweroff_path
[Install]
WantedBy=multi-user.target
EOF
if [ ! -f "$tempfn" ]; then
echo "Could not create x735 systemd service!"
return
fi
chmod 644 "$tempfn" && \
sudo mv "$tempfn" "$targetfn" && \
sudo chown root.root "$targetfn" && \
sudo systemctl daemon-reload && \
sudo systemctl enable "$service_name" && \
sudo systemctl start "$service_name"
}
get_gpio_watch
install_startup_script "/usr/local/bin/x735_onStartup.sh"
install_poweroff_script "$poweroff_path"
install_systemd_service "$poweroff_path"
# vim: set ts=4 sts=4 sw=4 tw=0: