-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
autoinstall.sh
259 lines (222 loc) · 8.69 KB
/
autoinstall.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
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
# Function to display error message and exit
display_error() {
echo "Error: $1" >&2
exit 1
}
# Function to execute a command and handle errors, with optional internet connectivity check
execute_command() {
local check_internet="$2" # Check for internet if this argument is provided
echo "Executing: $1"
if [ "$check_internet" = "check_internet" ]; then
local max_retries=18 # Total number of retries (18 retries * 10 seconds = 3 minutes)
local retry_interval=10 # Retry interval in seconds
for ((attempt = 1; attempt <= max_retries; attempt++)); do
# Check for internet connectivity
if ping -q -c 1 -W 1 google.com &>/dev/null; then
# Internet is available, execute the command
eval "$1"
local exit_code=$?
if [ $exit_code -eq 0 ]; then
return 0 # Command executed successfully
else
echo "Command failed with exit code $exit_code."
sleep $retry_interval # Wait before retrying
fi
else
echo "Internet not available, retrying in $retry_interval seconds (Attempt $attempt/$max_retries)..."
sleep $retry_interval # Wait before retrying
fi
done
echo "Command failed after $((max_retries * retry_interval)) seconds of retries."
exit 1 # Exit the script after multiple unsuccessful retries
else
eval "$1" # Execute the command without internet connectivity check
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Command failed with exit code $exit_code."
fi
fi
}
# Function to update the OS
update_os() {
execute_command "sudo apt-get update" "check_internet"
execute_command "sudo apt-get upgrade -y"
}
# Function to create and configure the autoconnect script
configure_autoconnect_script() {
# Create connectall.py file
cat <<EOF | sudo tee /usr/local/bin/connectall.py > /dev/null
#!/usr/bin/python3
import subprocess
ports = subprocess.check_output(["aconnect", "-i", "-l"], text=True)
port_list = []
client = "0"
for line in str(ports).splitlines():
if line.startswith("client "):
client = line[7:].split(":",2)[0]
if client == "0" or "Through" in line:
client = "0"
else:
if client == "0" or line.startswith('\t'):
continue
port = line.split()[0]
port_list.append(client+":"+port)
for source in port_list:
for target in port_list:
if source != target:
subprocess.call("aconnect %s %s" % (source, target), shell=True)
EOF
execute_command "sudo chmod +x /usr/local/bin/connectall.py"
# Create udev rules file
echo 'ACTION=="add|remove", SUBSYSTEM=="usb", DRIVER=="usb", RUN+="/usr/local/bin/connectall.py"' | sudo tee -a /etc/udev/rules.d/33-midiusb.rules > /dev/null
# Reload services
execute_command "sudo udevadm control --reload"
execute_command "sudo service udev restart"
# Create midi.service file
cat <<EOF | sudo tee /lib/systemd/system/midi.service > /dev/null
[Unit]
Description=Initial USB MIDI connect
[Service]
ExecStart=/usr/local/bin/connectall.py
[Install]
WantedBy=multi-user.target
EOF
# Reload daemon and enable service
execute_command "sudo systemctl daemon-reload"
execute_command "sudo systemctl enable midi.service"
execute_command "sudo systemctl start midi.service"
}
# Function to enable SPI interface
enable_spi_interface() {
# Edit config.txt file to enable SPI interface
execute_command "sudo sed -i '$ a\dtparam=spi=on' /boot/config.txt"
}
# Function to install required packages
install_packages() {
execute_command "sudo apt-get install -y ruby git python3-pip autotools-dev libtool autoconf libopenblas-dev libasound2-dev libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev python3 libatlas-base-dev libopenjp2-7 libtiff6 libjack0 libjack-dev libasound2-dev fonts-freefont-ttf gcc make build-essential git scons swig libavahi-client3 abcmidi dnsmasq hostapd" "check_internet"
}
# Function to disable audio output
disable_audio_output() {
echo 'blacklist snd_bcm2835' | sudo tee -a /etc/modprobe.d/snd-blacklist.conf > /dev/null
sudo sed -i 's/dtparam=audio=on/#dtparam=audio=on/' /boot/config.txt
}
# Function to install RTP-midi server
install_rtpmidi_server() {
execute_command "cd /home/"
execute_command "sudo wget https://github.com/davidmoreno/rtpmidid/releases/download/v21.11/rtpmidid_21.11_armhf.deb" "check_internet"
execute_command "sudo dpkg -i rtpmidid_21.11_armhf.deb"
execute_command "sudo apt -f install"
execute_command "rm rtpmidid_21.11_armhf.deb"
}
# Function to create Hot-Spot
create_hotspot() {
echo 'interface wlan0 static ip_address=192.168.4.1/24' | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo systemctl daemon-reload
sudo systemctl restart dhcpcd
echo 'interface=wlan0 dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h' | sudo tee --append /etc/dnsmasq.conf > /dev/null
hotspot_config_content=$(cat <<EOT
interface=wlan0
driver=nl80211
ssid=PianoLEDVisualizer
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=visualizer
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOT
)
# Use echo to send the content to the file with sudo
echo "$hotspot_config_content" | sudo tee /etc/hostapd/hostapd.conf > /dev/null
echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' | sudo tee --append /etc/default/hostapd > /dev/null
execute_command "sudo systemctl unmask hostapd"
execute_command "sudo systemctl enable hostapd && sudo systemctl enable dnsmasq"
}
configure_network_interfaces() {
# Edit /etc/network/interfaces file
local interfaces_file="/etc/network/interfaces"
local interfaces_config="
auto wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
"
echo "$interfaces_config" | sudo tee -a "$interfaces_file" > /dev/null
echo "Network interfaces configuration added to $interfaces_file."
}
# Function to install Piano-LED-Visualizer
install_piano_led_visualizer() {
execute_command "cd /home/"
execute_command "sudo git clone https://github.com/onlaj/Piano-LED-Visualizer" "check_internet"
execute_command "sudo chown -R $USER:$USER /home/Piano-LED-Visualizer"
execute_command "sudo chmod -R u+rwx /home/Piano-LED-Visualizer"
execute_command "cd Piano-LED-Visualizer"
execute_command "sudo pip3 install -r requirements.txt --break-system-packages" "check_internet"
execute_command "sudo raspi-config nonint do_boot_behaviour B2"
cat <<EOF | sudo tee /lib/systemd/system/visualizer.service > /dev/null
[Unit]
Description=Piano LED Visualizer
After=network-online.target
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=sudo python3 /home/Piano-LED-Visualizer/visualizer.py
Restart=always
Type=simple
User=plv
Group=plv
EOF
execute_command "sudo systemctl daemon-reload"
execute_command "sudo systemctl enable visualizer.service"
execute_command "sudo systemctl start visualizer.service"
execute_command "sudo chmod a+rwxX -R /home/Piano-LED-Visualizer/"
execute_command "sudo chmod +x /home/Piano-LED-Visualizer/disable_ap.sh"
execute_command "sudo chmod +x /home/Piano-LED-Visualizer/enable_ap.sh"
}
finish_installation() {
echo "------------------"
echo "------------------"
echo "Installation complete. Raspberry Pi will automatically restart in 60 seconds."
echo "If the Raspberry Pi does not restart on its own, please wait for 2 minutes and then manually reboot."
echo "After the reboot, please wait for up to 10 minutes. The Visualizer should start, and the Hotspot 'PianoLEDVisualizer' will become available."
execute_command "sudo shutdown -r +1"
execute_command "sudo /home/Piano-LED-Visualizer/enable_ap.sh"
sleep 60
# Reboot Raspberry Pi
execute_command "sudo reboot"
}
echo "
# _____ _ _ ______ _____
# | __ \\(_) | | | ____|| __ \\
# | |__) |_ __ _ _ __ ___ | | | |__ | | | |
# | ___/| | / _\` || '_ \\ / _ \\ | | | __| | | | |
# | | | || (_| || | | || (_) | | |____ | |____ | |__| |
# |_| |_| \\__,_||_| |_| \\___/ |______||______||_____/
# __ __ _ _ _
# \\ \\ / /(_) | |(_)
# \\ \\ / / _ ___ _ _ __ _ | | _ ____ ___ _ __
# \\ \\/ / | |/ __|| | | | / _\` || || ||_ // _ \\| '__|
# \\ / | |\\__ \\| |_| || (_| || || | / /| __/| |
# \\/ |_||___/ \\__,_| \\__,_||_||_|/___|\\___||_|
#
# Autoinstall script
# - by Onlaj
"
# Main script execution
update_os
configure_autoconnect_script
enable_spi_interface
install_packages
disable_audio_output
install_rtpmidi_server
install_piano_led_visualizer
configure_network_interfaces
create_hotspot
finish_installation