-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathRMS_Update.sh
executable file
·219 lines (171 loc) · 6.33 KB
/
RMS_Update.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
#!/bin/bash
# This script is used for updating the RMS code from GitHub
# WARNING: The update might fail when new dependencies (libraries)
# are introduced! Further steps might have to be undertaken.
RMSSOURCEDIR=~/source/RMS
RMSBACKUPDIR=~/.rms_backup
# File for indicating that the update is in progress
UPDATEINPROGRESSFILE=$RMSBACKUPDIR/update_in_progress
echo "Updating RMS code..."
# Make the backup directory
mkdir $RMSBACKUPDIR
# Check if the update was interrupted while it was in progress
UPDATEINPROGRESS="0"
if [ -f $UPDATEINPROGRESSFILE ]; then
echo "Reading update in progress file..."
UPDATEINPROGRESS=$(cat $UPDATEINPROGRESSFILE)
echo "Update interuption status: $UPDATEINPROGRESS"
fi
# If an argument (any) is given, then the config and mask won't be backed up
# Also, don't back up the files if the update script was interrupted the last time
if [ $# -eq 0 ] && [ "$UPDATEINPROGRESS" = "0" ]; then
echo "Backing up the config and mask..."
# Back up the config and the mask
cp $RMSSOURCEDIR/.config $RMSBACKUPDIR/.
cp $RMSSOURCEDIR/mask.bmp $RMSBACKUPDIR/.
fi
cd $RMSSOURCEDIR
# Activate the virtual environment
source ~/vRMS/bin/activate
# Remove the build dir
echo "Removing the build directory..."
rm -r build
# Perform cleanup before installations
echo "Running pyclean for thorough cleanup..."
pyclean . -v --debris all
# Cleanup for *.so files in the repository folder
echo "Cleaning up *.so files in the repository..."
find . -name "*.so" -type f -delete
# Set the flag indicating that the RMS dir is reset
echo "1" > $UPDATEINPROGRESSFILE
# Stash the cahnges
git stash
# Pull new code from github
git pull
### Install potentially missing libraries ###
# Function to check if a package is installed
isInstalled() {
dpkg -s "$1" >/dev/null 2>&1
}
# Function to attempt passwordless sudo
tryPasswordlessSudo() {
if sudo -n true 2>/dev/null; then
return 0
else
return 1
fi
}
# Function to prompt for sudo password with timeout
sudoWithTimeout() {
local timeout_duration=30
local attempts=3
local prompt="[sudo] password for $USER (timeout in ${timeout_duration}s): "
local sudo_keep_alive_duration=$((timeout_duration / 2))
echo "Please enter your sudo password. You have $attempts attempts, with a ${timeout_duration}-second timeout."
for ((i=1; i<=attempts; i++)); do
# Use read with timeout to get the password securely
read -s -t "$timeout_duration" -p "$prompt" password
echo # Move to a new line after password input
# Check if password is empty (timeout or Ctrl+D)
if [[ -z "$password" ]]; then
return 1
fi
# Validate the password
if echo "$password" | sudo -S true 2>/dev/null; then
# Keep sudo token alive in background
(while true; do sudo -v; sleep $sudo_keep_alive_duration; done) &
KEEP_SUDO_PID=$!
return 0
else
if [ $i -lt $attempts ]; then
echo "Sorry, try again. You have $((attempts - i)) attempts remaining."
else
echo "sudo: $attempts incorrect password attempts"
return 1
fi
fi
done
# If we've exhausted all attempts
return 1
}
# List of packages to check/install
packages=(
"gobject-introspection"
"libgirepository1.0-dev"
"gstreamer1.0-libav"
"gstreamer1.0-plugins-bad"
)
# Check if any package is missing
missing_packages=()
for package in "${packages[@]}"; do
if ! isInstalled "$package"; then
missing_packages+=("$package")
fi
done
# If all packages are installed, inform and continue
if [ ${#missing_packages[@]} -eq 0 ]; then
echo "All required packages are already installed."
else
# Some packages are missing, so we need to update and install
echo "The following packages need to be installed: ${missing_packages[*]}"
# First, try passwordless sudo
if tryPasswordlessSudo; then
echo "Passwordless sudo available. Proceeding with installation."
sudo apt-get update
all_installed=true
for package in "${missing_packages[@]}"; do
echo "Installing $package..."
if ! sudo apt-get install -y "$package"; then
echo "Failed to install $package"
all_installed=false
fi
done
if $all_installed; then
echo "All required packages have been successfully installed."
else
echo "Some packages failed to install. Please check the output above for details."
fi
else
# Passwordless sudo not available, prompt for password
echo "Passwordless sudo not available. Prompting for password."
if ! sudoWithTimeout; then
echo "Password entry timed out or was incorrect. Skipping package installation."
else
# Password entered successfully, proceed with update and install
sudo apt-get update
# Install missing packages
all_installed=true
for package in "${missing_packages[@]}"; do
echo "Installing $package..."
if ! sudo apt-get install -y "$package"; then
echo "Failed to install $package"
all_installed=false
fi
done
if $all_installed; then
echo "All required packages have been successfully installed."
else
echo "Some packages failed to install. Please check the output above for details."
fi
# Kill the background sudo-keeping process
kill $KEEP_SUDO_PID 2>/dev/null
fi
fi
fi
### ###
# make sure the correct requirements are installed
pip install -r requirements.txt
# Run the python setup
python setup.py install
# Create a template file from the source config and copy the user config and mask files back
if [ $# -eq 0 ]; then
# Rename the existing source .config file to .configTemplate
mv $RMSSOURCEDIR/.config $RMSSOURCEDIR/.configTemplate
# Copy the user config and mask files back
cp $RMSBACKUPDIR/.config $RMSSOURCEDIR/.
cp $RMSBACKUPDIR/mask.bmp $RMSSOURCEDIR/.
fi
# Set the flag that the update is not in progress
echo "0" > $UPDATEINPROGRESSFILE
echo "Update finished! Update exiting in 5 seconds..."
sleep 5