-
Notifications
You must be signed in to change notification settings - Fork 7
/
pitrex-config.sh
executable file
·246 lines (211 loc) · 6.61 KB
/
pitrex-config.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# This script configures a fresh install of Raspberry Pi OS for
# glitch-free execution of PiTrex software.
#
# Note that HDMI and Composite video output is disabled on start-up,
# until "sudo tvservice -p" is run manually (PiTrex software will be
# glitchy again).
#
# This script must be run under sudo.
expand_tilde()
{
case "$1" in
(\~) echo "$HOME";;
(\~/*) echo "$HOME/${1#\~/}";;
(\~[^/]*/*) local user=$(eval echo ${1%%/*})
echo "$user/${1#*/}";;
(\~[^/]*) eval echo ${1};;
(*) echo "$1";;
esac
}
confirm() {
local _prompt _default _response
if [ "$1" ]; then _prompt="$1"; else _prompt="Are you sure"; fi
_prompt="$_prompt [y/n] ?"
# Loop forever until the user enters a valid response (Y/N or Yes/No).
while true; do
read -r -p "$_prompt " _response
case "$_response" in
[Yy][Ee][Ss]|[Yy]) # Yes or Y (case-insensitive).
echo "y"
return
;;
[Nn][Oo]|[Nn]) # No or N.
echo "n"
return
;;
*) # Anything else (including a blank) is invalid.
;;
esac
done
}
if [ "`whoami`" != "root" ] ; then
echo This script must be run using sudo.
exit 1
fi
if [ "$SUDO_USER" == "" ] ; then
echo This script must be run using sudo, not as root itself.
exit 1
fi
#Process command-line arguments:
CONFIGONLY=
YESTOALL=
until [ -z "$1" ]
do
case "$1" in
"-configonly") CONFIGONLY=1 ;;
"-keephdmi") KEEPHDMI=1 ;;
"-y") YESTOALL=1 ;;
*) echo "Invalid option: $1"; exit 1 ;;
esac
shift
done
echo "Adding configuration to config.txt"
#Edit config.txt:
L1="`grep ^gpio=0-5,16-24,26-29=ip$ /boot/config.txt`"
L2="`grep ^gpio=6-13,25=op$ /boot/config.txt`"
L3="`grep ^gpio=24=np$ /boot/config.txt`"
L4="`grep ^dtoverlay=dwc2,dr_mode=host$ /boot/config.txt`"
L5="`grep ^dtoverlay=dwc /boot/config.txt`"
if [ "$L1" == "" ] || [ "$L2" == "" ] || [ "$L3" == "" ] ; then
echo "[all]" >> /boot/config.txt # just in case ...
echo "# Configure GPIO for PiTrex" >> /boot/config.txt
fi
if [ "$L1" == "" ] ; then
echo "#Inputs" >> /boot/config.txt
echo "gpio=0-5,16-24,26-29=ip" >> /boot/config.txt
echo "added: gpio=0-5,16-24,26-29=ip"
fi
if [ "$L2" == "" ] ; then
echo "#Outputs" >> /boot/config.txt
echo "gpio=6-13,25=op" >> /boot/config.txt
echo "added: gpio=6-13,25=op"
fi
if [ "$L3" == "" ] ; then
echo "#No pull-up/down on RDY" >> /boot/config.txt
echo "gpio=24=np" >> /boot/config.txt
echo "added: gpio=24=np"
fi
if [ "$L4" == "" ] ; then
if [ "$L5" != "" ] ; then
echo "WARNING: check /boot/config.txt for a potential clash between existing:"
echo -n " "; grep ^dtoverlay=dwc /boot/config.txt
echo " and the addition:"
echo " dtoverlay=dwc2,dr_mode=host"
fi
echo "" >> /boot/config.txt
echo "#Use alternative Linux USB driver (no FIQ)" >> /boot/config.txt
echo "#Modes: \"host\", \"peripheral\" or \"otg\"" >> /boot/config.txt
echo "dtoverlay=dwc2,dr_mode=host" >> /boot/config.txt
echo "added: dtoverlay=dwc2,dr_mode=host"
fi
echo "Creating /opt/pitrex folders and linking to /boot"
mkdir -p /opt/pitrex/bin /opt/pitrex/share /boot/roms /boot/settings /boot/ini
[ -e /opt/pitrex/settings ] || ln -s /boot/settings /opt/pitrex/settings
[ -e /opt/pitrex/ini ] || ln -s /boot/ini /opt/pitrex/ini
[ -e /opt/pitrex/roms ] || ln -s /boot/roms /opt/pitrex/roms
chown $SUDO_USER /opt/pitrex/bin
# NOTE: by default this just goes into ROOT's .profile, we need a little
# extra effort to put it in the calling user's .profile as well...
# Might be easier to just add it globally in /etc/rc.local?
P1="`fgrep \"PATH=\\\"/opt/pitrex/bin:\" ~root/.profile`"
if [ "$P1" == "" ] ; then
echo "adding /opt/pitrex/bin to path in ~root/.profile"
cat <<EOF >> ~root/.profile
# set PATH so it includes PiTrex bin if it exists
if [ -d "/opt/pitrex/bin" ] ; then
PATH="/opt/pitrex/bin:\$PATH"
fi
EOF
else
echo "/opt/pitrex/bin already in path in ~root/.profile"
fi
PRO=$(expand_tilde ~$SUDO_USER/.profile)
P2="`fgrep \"PATH=\\\"/opt/pitrex/bin:\" $PRO`"
if [ "$P2" == "" ] ; then
echo "adding /opt/pitrex/bin to path in $PRO"
cat <<EOF >> $PRO
# set PATH so it includes PiTrex bin if it exists
if [ -d "/opt/pitrex/bin" ] ; then
PATH="/opt/pitrex/bin:\$PATH"
fi
EOF
else
echo "/opt/pitrex/bin already in path in $PRO"
fi
echo "Adding pi user to bluetooth group"
adduser pi bluetooth
if [ $CONFIGONLY ]; then
exit 0
fi
echo "Editing rc.local"
#Add commands to rc.local:
R1="`fgrep \"#Keep CPU frequency at maximum:\" /etc/rc.local`"
if [ "$R1" != "" ] ; then
echo "WARNING: rc.local configuration appears to have been done already."
fi
if [ -z "$YESTOALL" ]; then
echo "Do you want to disable HDMI and set performance to maximum at boot time?"
YN=$(confirm "Yes or No?")
else
YN="y";
fi
if [ "$YN" == "y" -a -z "$KEEPHDMI" ] ; then
sed -i '/^exit 0$/i'' \
#Disable HDMI/Composite video output:\
tvservice \-o' /etc/rc.local
echo "adding: tvservice -o to /etc/rc.local"
else
sed -i '/^exit 0$/i'' \
#Disable HDMI/Composite video output:\
# tvservice \-o' /etc/rc.local
fi
if [ "$YN" == "y" ] ; then
sed -i '/^exit 0$/i'' \
#Keep CPU frequency at maximum:\
echo \-n performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' /etc/rc.local
echo "adding: performance mode to /etc/rc.local"
else
sed -i '/^exit 0$/i'' \
#Keep CPU frequency at maximum:\
# echo \-n performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' /etc/rc.local
fi
echo "Enabling rc.local"
#Enable execution of rc.local on start-up with Systemd:
chmod a+x /etc/rc.local
# update if it exists already
echo "Updating /etc/systemd/system/rc-local.service"
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=Runs /etc/rc.local
After=multi-user.target
[Service]
Type=simple
ExecStart=/etc/rc.local
[Install]
WantedBy=rc-local.target
EOF
# update if it exists already
echo "Updating /etc/systemd/system/rc-local.target"
cat > /etc/systemd/system/rc-local.target <<EOF
[Unit]
Description=Run service that runs /etc/rc.local
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes
EOF
if [ -d /etc/systemd/system/rc-local.target.wants ] ; then
echo "WARNING: /etc/systemd/system/rc-local.target.wants already exists"
else
if mkdir /etc/systemd/system/rc-local.target.wants; then
ln -s /etc/systemd/system/rc-local.service /etc/systemd/system/rc-local.target.wants/rc-local.service
systemctl daemon-reload
systemctl set-default rc-local.target
echo "Reboot now to enable new system configuration"
exit 0
else
echo "Failed to set up rc.local with Systemd"
exit 1
fi
fi
exit 0