-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalink_install.sh
233 lines (203 loc) · 6.49 KB
/
alink_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
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
#!/bin/sh
# Color-coded echo functions
echo_red() { printf "\033[1;31m$*\033[m\n"; }
echo_green() { printf "\033[1;32m$*\033[m\n"; }
echo_blue() { printf "\033[1;34m$*\033[m\n"; }
# Default configurations
UDP_IP=10.5.0.10
UDP_PORT=9999
LOG_INTERVAL=100
WFBGS_CFG=/etc/wifibroadcast.cfg
WFBGS_CFG2=/home/radxa/gs/wfb.sh
# Variables for repository
REPO_OWNER="sickgreg"
REPO_NAME="OpenIPC-Adaptive-Link"
# Function to fetch the latest release asset URL
github_asset_url() {
FILE_NAME=$1
curl -s "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases" | \
grep -o '"browser_download_url": "[^"]*' | \
grep "$FILE_NAME" | head -n 1 | sed 's/"browser_download_url": "//'
}
# Ensure the script is run as root
if [ $(id -u) -ne 0 ]; then
echo_red "Permission denied. Use sudo to run this script."
echo "Usage: sudo $0 <system> <action>"
exit 1
fi
# Detect system type
gs_check() {
# Check for absence of Buildroot to identify a Ground Station
grep -o "NAME=Buildroot" /etc/os-release > /dev/null 2>&1 || return 0
return 1
}
dr_check() {
# Ensure Buildroot is present, indicating a Drone setup
gs_check || return 0
return 1
}
# Function to update wifibroadcast configuration
update_log_interval() {
if grep -q "log_interval" ${WFBGS_CFG}; then
sed -i 's/log_interval.*/log_interval = '${LOG_INTERVAL}'/' $WFBGS_CFG
else
sed -i '/\[common\]/a log_interval = '${LOG_INTERVAL}'' $WFBGS_CFG
fi
if [ -f "$WFBGS_CFG2" ]; then
# If the file exists, update the log_interval here too (CC's GS)
if grep -q "log_interval" "$WFBGS_CFG2"; then
sed -i 's/log_interval.*/log_interval = '${LOG_INTERVAL}'/' "$WFBGS_CFG2"
else
sed -i '/\[common\]/a log_interval = '${LOG_INTERVAL}'' "$WFBGS_CFG2"
fi
fi
}
# Ground Station setup
gs_setup() {
FILE_NAME=alink_gs
FILE=/usr/bin/$FILE_NAME
# Check if /config directory exists
if [ -d "/config" ]; then
# If /config exists, use it
FILE_CONF="/config/$FILE_NAME.conf"
# Check if /home/radxa directory exists
elif [ -d "/home/radxa" ]; then
# If /home/radxa exists, use it
FILE_CONF="/home/radxa/$FILE_NAME.conf"
else
# If neither /config nor /home/radxa exist, use /etc
FILE_CONF="/etc/$FILE_NAME.conf"
fi
PATH_SERVICE=/etc/systemd/system/$FILE_NAME.service
case "$1" in
install)
echo_blue "Installing Adaptive Link for Ground Station..."
if [ -f $FILE ]; then
echo_red "$FILE_NAME already installed. Use 'remove' first."
exit 1
fi
URL_ALINK_GS=$(github_asset_url "alink_gs")
wget -O $FILE "$URL_ALINK_GS" && chmod +x $FILE
# Create service file
cat <<EOF | tee $PATH_SERVICE
[Unit]
Description=OpenIPC Adaptive_Link
[Service]
ExecStart=${FILE} --config ${FILE_CONF}
Type=idle
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
EOF
# Start service for testing
echo "Starting Adaptive Link temporarily for testing..."
$FILE --config $FILE_CONF &
sleep 5 && kill $!
# Configure
if [ ! -f $FILE_CONF ]; then
echo_red "Error: Missing configuration file ${FILE_CONF}."
exit 1
fi
sed -i 's/udp_ip.*/udp_ip = '$UDP_IP'/' $FILE_CONF
sed -i 's/udp_port.*/udp_port = '$UDP_PORT'/' $FILE_CONF
update_log_interval
# Restart services
systemctl restart wifibroadcast.service
systemctl enable $FILE_NAME.service
systemctl start $FILE_NAME.service
echo_green "Adaptive Link installed successfully."
;;
remove)
# Check and stop the new Ground Station service if its .service file exists
if [ -f "$PATH_SERVICE" ]; then
sudo systemctl stop $FILE_NAME.service
sudo systemctl disable $FILE_NAME.service
sudo rm -f $FILE $FILE_CONF $PATH_SERVICE
echo_green "Adaptive Link ($FILE_NAME) removed."
else
echo_blue "Service file $PATH_SERVICE not found, skipping..."
fi
# Check and stop the old Adaptive Link service if its .service file exists
if [ -f "/etc/systemd/system/adaptive_link.service" ]; then
sudo systemctl stop adaptive_link.service
sudo systemctl disable adaptive_link.service
sudo rm -f /usr/bin/adaptive_link /etc/adaptive_link.conf /etc/systemd/system/adaptive_link.service
echo_green "Old Adaptive Link (adaptive_link) removed."
else
echo_blue "Service file /etc/systemd/system/adaptive_link.service not found, skipping..."
fi
;;
update)
echo_blue "Updating Adaptive Link for Ground Station..."
URL_ALINK_GS=$(github_asset_url "alink_gs")
wget -O $FILE "$URL_ALINK_GS" && chmod +x $FILE
update_log_interval
systemctl restart wifibroadcast.service
systemctl restart $FILE_NAME.service
echo_green "Adaptive Link updated successfully."
;;
*)
echo_red "Invalid action for Ground Station. Use install, remove, or update."
;;
esac
}
# Drone setup
dr_setup() {
FILE_NAME=alink_drone
FILE=/usr/bin/$FILE_NAME
TXPROFILE=/etc/txprofiles.conf
ALINK=/etc/alink.conf
case "$1" in
install)
echo_blue "Installing Adaptive Link for Drone..."
if [ -f $FILE ]; then
echo_red "$FILE_NAME already installed. Use 'remove' first."
exit 1
fi
URL_ALINK_DRONE=$(github_asset_url "alink_drone")
URL_TXPROFILE_CONF=$(github_asset_url "txprofiles.conf")
URL_ALINK_CONF=$(github_asset_url "alink.conf")
curl -L -o $FILE "$URL_ALINK_DRONE"
curl -L -o $TXPROFILE "$URL_TXPROFILE_CONF"
curl -L -o $ALINK "$URL_ALINK_CONF"
chmod +x $FILE
# Add to rc.local
sed -i -e '$i \'$FILE' --ip '$UDP_IP' --port '$UDP_PORT' &' /etc/rc.local
sed -i 's/tunnel=.*/tunnel=true/' /etc/datalink.conf
echo_green "Adaptive Link installed successfully. Restart the drone."
;;
remove)
echo_blue "Removing Adaptive Link for Drone..."
killall $FILE_NAME 2>/dev/null
sed -i '/.*'$FILE_NAME'.*/d' /etc/rc.local
rm -f $FILE $TXPROFILE $ALINK
echo_green "Adaptive Link removed."
;;
update)
echo_blue "Updating Adaptive Link for Drone..."
URL_ALINK_DRONE=$(github_asset_url "alink_drone")
URL_TXPROFILE_CONF=$(github_asset_url "txprofiles.conf")
URL_ALINK_CONF=$(github_asset_url "alink.conf")
curl -L -o $FILE "$URL_ALINK_DRONE"
curl -L -o $TXPROFILE "$URL_TXPROFILE_CONF"
curl -L -o $ALINK "$URL_ALINK_CONF"
echo_green "Adaptive Link updated successfully. Restart the drone."
;;
*)
echo_red "Invalid action for Drone. Use install, remove, or update."
;;
esac
}
# Main script execution
case "$1" in
gs)
gs_check || { echo_red "Error: Not a valid Ground Station environment."; exit 1; }
gs_setup "$2";;
drone)
dr_check || { echo_red "Error: Not a valid Drone environment."; exit 1; }
dr_setup "$2";;
*)
echo_red "Usage: $0 gs|drone install|remove|update"
;;
esac
exit 0