Skip to content

Commit 2a954f8

Browse files
Merge pull request #215 from Martinski4GitHub/dev
Minor code fixes & improvements.
2 parents 07d8c57 + 986ec0b commit 2a954f8

File tree

1 file changed

+87
-60
lines changed

1 file changed

+87
-60
lines changed

MerlinAU.sh

Lines changed: 87 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Original Creation Date: 2023-Oct-01 by @ExtremeFiretop.
66
# Official Co-Author: @Martinski W. - Date: 2023-Nov-01
7-
# Last Modified: 2024-May-21
7+
# Last Modified: 2024-May-23
88
###################################################################
99
set -u
1010

@@ -25,9 +25,10 @@ readonly FW_URL_RELEASE_SUFFIX="Release"
2525
##---------------------------------------##
2626
## Added by ExtremeFiretop [2024-May-03] ##
2727
##---------------------------------------##
28-
# Changelog URL Info #
28+
# Changelog Info #
2929
readonly CL_URL_NG="${FW_URL_BASE}/Documentation/Changelog-NG.txt/download"
3030
readonly CL_URL_386="${FW_URL_BASE}/Documentation/Changelog-386.txt/download"
31+
readonly high_risk_terms="factory default reset|features are disabled|break backward compatibility|must be manually|strongly recommended"
3132

3233
# For new script version updates from source repository #
3334
UpdateNotify=0
@@ -98,8 +99,9 @@ then inRouterSWmode=true
9899
else inRouterSWmode=false
99100
fi
100101

101-
mainMenuReturnPromptStr="Press <Enter> to return to the Main Menu..."
102-
advnMenuReturnPromptStr="Press <Enter> to return to the Advanced Menu..."
102+
readonly mainMenuReturnPromptStr="Press <Enter> to return to the Main Menu..."
103+
readonly advnMenuReturnPromptStr="Press <Enter> to return to the Advanced Menu..."
104+
readonly logsMenuReturnPromptStr="Press <Enter> to return to the Logs Menu..."
103105

104106
[ -t 0 ] && ! tty | grep -qwi "NOT" && isInteractive=true
105107

@@ -201,10 +203,10 @@ _AcquireLock_()
201203
then
202204
Say "Stale lock found (older than $LockFileMaxSecs secs.) Reset lock file."
203205
oldPID="$(cat "$LockFilePath")"
204-
if [ -n "$oldPID" ] && kill -EXIT $oldPID 2>/dev/null && \
205-
echo "$(pidof "$ScriptFileName")" | grep -qow "$oldPID"
206+
if [ -n "$oldPID" ] && kill -EXIT "$oldPID" 2>/dev/null && \
207+
pidof "$ScriptFileName" | grep -qow "$oldPID"
206208
then
207-
kill -TERM $oldPID ; wait $oldPID
209+
kill -TERM "$oldPID" ; wait "$oldPID"
208210
fi
209211
rm -f "$LockFilePath"
210212
echo "$$" > "$LockFilePath"
@@ -506,7 +508,7 @@ logo() {
506508
echo -e " | |\/| |/ _ | '__| | | '_ \ / /\ \| | | |"
507509
echo -e " | | | | __| | | | | | | |/ ____ | |__| |"
508510
echo -e " |_| |_|\___|_| |_|_|_| |_/_/ \_\____/ ${GRNct}v${SCRIPT_VERSION}"
509-
echo -e " ${NOct}"
511+
echo -e "${NOct}"
510512
}
511513

512514
##-----------------------------------------------##
@@ -1286,12 +1288,12 @@ _CreateEMailContent_()
12861288
;;
12871289
STOP_FW_UPDATE_APPROVAL)
12881290
emailBodyTitle="WARNING"
1289-
high_risk_terms="factory default reset|features are disabled|break backward compatibility|must be manually|strongly recommended"
1290-
if $isEMailFormatHTML; then
1291-
# Highlight high-risk terms using HTML with a yellow background
1291+
if $isEMailFormatHTML
1292+
then
1293+
# Highlight high-risk terms using HTML with a yellow background #
12921294
highlighted_changelog_contents=$(echo "$changelog_contents" | sed -E "s/($high_risk_terms)/<span style='background-color:yellow;'>\1<\/span>/gi")
12931295
else
1294-
# Highlight high-risk terms in plain text using asterisks
1296+
# Highlight high-risk terms in plain text using asterisks #
12951297
highlighted_changelog_contents=$(echo "$changelog_contents" | sed -E "s/($high_risk_terms)/*\1*/gi")
12961298
fi
12971299
{
@@ -2052,15 +2054,15 @@ _TestLoginCredentials_()
20522054
}
20532055

20542056
##----------------------------------------##
2055-
## Modified by Martinski W. [2024-Apr-15] ##
2057+
## Modified by Martinski W. [2024-May-23] ##
20562058
##----------------------------------------##
20572059
_GetPasswordInput_()
20582060
{
20592061
local PSWDstrLenMIN=1 PSWDstrLenMAX=64
20602062
local PSWDstring PSWDtmpStr PSWDprompt
20612063
local retCode charNum pswdLength showPSWD
2062-
# Added for TAB keypress debounce #
2063-
local lastTabTime=0 currentTime
2064+
# For more responsive TAB keypress debounce #
2065+
local tabKeyDebounceSem="/tmp/var/tmp/${ScriptFNameTag}_TabKeySEM.txt"
20642066

20652067
if [ $# -eq 0 ] || [ -z "$1" ]
20662068
then
@@ -2078,6 +2080,13 @@ _GetPasswordInput_()
20782080
stty "$savedSettings"
20792081
}
20802082

2083+
_TabKeyDebounceWait_()
2084+
{
2085+
touch "$tabKeyDebounceSem"
2086+
usleep 300000 #0.3 sec#
2087+
rm -f "$tabKeyDebounceSem"
2088+
}
2089+
20812090
_ShowAsterisks_()
20822091
{
20832092
if [ $# -eq 0 ] || [ "$1" -eq 0 ]
@@ -2137,12 +2146,11 @@ _GetPasswordInput_()
21372146
## TAB keypress as toggle with debounce ##
21382147
if [ "$charNum" -eq 9 ]
21392148
then
2140-
currentTime="$(date +%s)"
2141-
if [ "$((currentTime - lastTabTime))" -gt 0 ]
2149+
if [ ! -f "$tabKeyDebounceSem" ]
21422150
then
21432151
showPSWD="$((! showPSWD))"
2144-
lastTabTime="$currentTime" # Update TAB keypress time #
21452152
_ShowPSWDPrompt_
2153+
_TabKeyDebounceWait_ &
21462154
fi
21472155
continue
21482156
fi
@@ -2634,12 +2642,12 @@ change_build_type()
26342642
doReturnToMenu=false
26352643
while true
26362644
do
2637-
printf "\n${SEPtop}"
2645+
printf "\n${SEPstr}"
26382646
printf "\nChoose your preferred option for the build type to flash:\n"
26392647
printf "\n ${GRNct}1${NOct}. Original ${REDct}ROG${NOct} themed user interface${NOct}\n"
26402648
printf "\n ${GRNct}2${NOct}. Pure ${GRNct}non-ROG${NOct} themed user interface ${GRNct}(Recommended)${NOct}\n"
26412649
printf "\n ${GRNct}e${NOct}. Exit to Advanced Menu\n"
2642-
printf "${SEPtop}\n"
2650+
printf "${SEPstr}\n"
26432651
printf "[$display_choice] Enter selection: "
26442652
read -r choice
26452653

@@ -3640,7 +3648,7 @@ _Toggle_FW_UpdateEmailNotifications_()
36403648

36413649
if ! _WaitForYESorNO_ "Do you want to ${emailNotificationNewStateStr} F/W Update email notifications?"
36423650
then
3643-
_RunEMailNotificationTest_ && _WaitForEnterKey_ "$mainMenuReturnPromptStr"
3651+
_RunEMailNotificationTest_ && _WaitForEnterKey_ "$advnMenuReturnPromptStr"
36443652
return 1
36453653
fi
36463654

@@ -3657,7 +3665,7 @@ _Toggle_FW_UpdateEmailNotifications_()
36573665
printf "F/W Update email notifications are now ${emailNotificationNewStateStr}.\n"
36583666

36593667
_RunEMailNotificationTest_
3660-
_WaitForEnterKey_ "$mainMenuReturnPromptStr"
3668+
_WaitForEnterKey_ "$advnMenuReturnPromptStr"
36613669
}
36623670

36633671
##------------------------------------------##
@@ -4143,26 +4151,26 @@ Please manually update to version $minimum_supported_version or higher to use th
41434151
release_version_regex="$formatted_release_version \([0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\)"
41444152
current_version_regex="$formatted_current_version \([0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\)"
41454153

4146-
# Check if the current version is present in the changelog
4154+
# Check if the current version is present in the changelog #
41474155
if ! grep -Eq "$current_version_regex" "$changeLogFile"; then
41484156
Say "Current version not found in change-log. Bypassing change-log verification for this run."
41494157
else
4150-
# Extract log contents between two firmware versions
4158+
# Extract log contents between two firmware versions #
41514159
changelog_contents="$(awk "/$release_version_regex/,/$current_version_regex/" "$changeLogFile")"
4152-
# Define high-risk terms as a single string separated by '|'
4153-
high_risk_terms="factory default reset|features are disabled|break backward compatibility|must be manually|strongly recommended"
41544160

4155-
# Search for high-risk terms in the extracted log contents
4156-
if echo "$changelog_contents" | grep -Eiq "$high_risk_terms"; then
4157-
if [ "$inMenuMode" = true ]; then
4158-
printf "\n ${REDct}Warning: Found high-risk phrases in the change-log.${NOct}"
4161+
# Search for high-risk terms in the extracted log contents #
4162+
if echo "$changelog_contents" | grep -Eiq "$high_risk_terms"
4163+
then
4164+
if [ "$inMenuMode" = true ]
4165+
then
4166+
printf "\n ${REDct}*WARNING*: Found high-risk phrases in the change-log.${NOct}"
41594167
printf "\n ${REDct}Would you like to continue anyways?${NOct}"
41604168
if ! _WaitForYESorNO_ ; then
41614169
Say "Exiting for change-log review."
41624170
_DoCleanUp_ 1 ; return 1
41634171
fi
41644172
else
4165-
Say "Warning: Found high-risk phrases in the change-log."
4173+
Say "*WARNING*: Found high-risk phrases in the change-log."
41664174
Say "Please run script interactively to approve the upgrade."
41674175
_SendEMailNotification_ STOP_FW_UPDATE_APPROVAL
41684176
_DoCleanUp_ 1
@@ -4225,22 +4233,38 @@ Please manually update to version $minimum_supported_version or higher to use th
42254233
firmware_file="$pure_file"
42264234
fi
42274235

4228-
##------------------------------------------##
4229-
## Modified by ExtremeFiretop [2024-May-21] ##
4230-
##------------------------------------------##
4236+
##----------------------------------------##
4237+
## Modified by Martinski W. [2024-May-23] ##
4238+
##----------------------------------------##
4239+
# Fetch the latest SHA256 checksums from ASUSWRT-Merlin website #
4240+
checksums="$(curl --silent --connect-timeout 10 --retry 4 --max-time 12 https://www.asuswrt-merlin.net/download | sed -n '/<pre>/,/</pre>/p' | sed -e 's/<[^>]*>//g')"
42314241

4232-
# Fetch the latest checksums from ASUSWRT-Merlin website
4233-
checksums=$(curl --silent --connect-timeout 10 --retry 4 --max-time 12 https://www.asuswrt-merlin.net/download | sed -n '/<pre>/,/</pre>/p' | sed -e 's/<[^>]*>//g')
4242+
if [ -z "$checksums" ]
4243+
then
4244+
Say "${REDct}**ERROR**${NOct}: Could not download the firmware SHA256 signatures from the website."
4245+
_DoCleanUp_ 1
4246+
if [ "$inMenuMode" = true ]
4247+
then
4248+
_WaitForEnterKey_ "$mainMenuReturnPromptStr"
4249+
return 1
4250+
else
4251+
# Assume non-interactive mode; perform exit.
4252+
_DoExit_ 1
4253+
fi
4254+
fi
42344255

4235-
if [ -f "$firmware_file" ]; then
4256+
if [ -f "$firmware_file" ]
4257+
then
42364258
fw_sig="$(openssl sha256 "$firmware_file" | cut -d' ' -f2)"
4237-
# Extract the corresponding checksum for the firmware file from the fetched data
4259+
# Extract the corresponding signature for the firmware file from the fetched checksums #
42384260
dl_sig="$(echo "$checksums" | grep "$(basename $firmware_file)" | cut -d' ' -f1)"
4239-
if [ "$fw_sig" != "$dl_sig" ]; then
4240-
Say "${REDct}**ERROR**${NOct}: Extracted firmware does not match the SHA256 signature from the website!"
4261+
if [ "$fw_sig" != "$dl_sig" ]
4262+
then
4263+
Say "${REDct}**ERROR**${NOct}: SHA256 signature from extracted firmware file does not match the SHA256 signature from the website."
42414264
_DoCleanUp_ 1
42424265
_SendEMailNotification_ FAILED_FW_CHECKSUM_STATUS
4243-
if [ "$inMenuMode" = true ]; then
4266+
if [ "$inMenuMode" = true ]
4267+
then
42444268
_WaitForEnterKey_ "$mainMenuReturnPromptStr"
42454269
return 1
42464270
else
@@ -4249,9 +4273,10 @@ Please manually update to version $minimum_supported_version or higher to use th
42494273
fi
42504274
fi
42514275
else
4252-
Say "${REDct}**ERROR**${NOct}: Firmware file not found!"
4276+
Say "${REDct}**ERROR**${NOct}: Firmware image file NOT found!"
42534277
_DoCleanUp_ 1
4254-
if [ "$inMenuMode" = true ]; then
4278+
if [ "$inMenuMode" = true ]
4279+
then
42554280
_WaitForEnterKey_ "$mainMenuReturnPromptStr"
42564281
return 1
42574282
else
@@ -4545,12 +4570,12 @@ _SetEMailFormatType_()
45454570
doReturnToMenu=false
45464571
while true
45474572
do
4548-
printf "\n${SEPtop}"
4573+
printf "\n${SEPstr}"
45494574
printf "\nChoose the format type for email notifications:\n"
45504575
printf "\n ${GRNct}1${NOct}. HTML\n"
45514576
printf "\n ${GRNct}2${NOct}. Plain Text\n"
45524577
printf "\n ${GRNct}e${NOct}. Exit to Advanced Menu\n"
4553-
printf "${SEPtop}\n"
4578+
printf "${SEPstr}\n"
45544579
printf "[$currFormatStr] Enter selection: "
45554580
read -r userInput
45564581

@@ -4885,8 +4910,10 @@ fi
48854910
# menu setup variables #
48864911
theExitStr="${GRNct}e${NOct}=Exit to Main Menu"
48874912
theADExitStr="${GRNct}e${NOct}=Exit to Advanced Menu"
4913+
theLGExitStr="${GRNct}e${NOct}=Exit to Logs Menu"
4914+
48884915
padStr=" "
4889-
SEPtop="----------------------------------------------------------"
4916+
SEPstr="----------------------------------------------------------"
48904917

48914918
FW_RouterProductID="${GRNct}${PRODUCT_ID}${NOct}"
48924919
if [ "$PRODUCT_ID" = "$MODEL_ID" ]
@@ -4930,10 +4957,10 @@ _GetFileSelectionIndex_()
49304957
if [ $# -lt 2 ] || [ "$2" != "-MULTIOK" ]
49314958
then
49324959
multiIndexListOK=false
4933-
promptStr="Enter selection [${selectStr}] [${theExitStr}]?"
4960+
promptStr="Enter selection [${selectStr}] [${theLGExitStr}]?"
49344961
else
49354962
multiIndexListOK=true
4936-
promptStr="Enter selection [${selectStr} | ${theAllStr}] [${theExitStr}]?"
4963+
promptStr="Enter selection [${selectStr} | ${theAllStr}] [${theLGExitStr}]?"
49374964
fi
49384965
fileIndex=0 multiIndex=false
49394966
numRegEx="([1-9]|[1-9][0-9])"
@@ -5236,22 +5263,22 @@ _ShowMainMenu_()
52365263
##----------------------------------------##
52375264
## Modified by Martinski W. [2024-May-19] ##
52385265
##----------------------------------------##
5239-
printf "${SEPtop}"
5266+
printf "${SEPstr}"
52405267
if [ "$HIDE_ROUTER_SECTION" = "false" ]
52415268
then
52425269
if ! FW_NewUpdateVersion="$(_GetLatestFWUpdateVersionFromRouter_ 1)"
52435270
then FW_NewUpdateVersion="${REDct}NONE FOUND${NOct}"
52445271
else FW_NewUpdateVersion="${GRNct}${FW_NewUpdateVersion}${NOct}$arrowStr"
52455272
fi
5246-
printf "\n Router's Product Name/Model ID: $FW_RouterModelID${padStr}(H)ide"
5273+
printf "\n Router's Product Name/Model ID: ${FW_RouterModelID}${padStr}(H)ide"
52475274
printf "\n USB-Attached Storage Connected: $USBConnected"
52485275
printf "\n F/W Version Currently Installed: $FW_InstalledVersion"
52495276
printf "\n F/W Update Version Available: $FW_NewUpdateVersion"
52505277
printf "\n F/W Update Estimated Run Date: $ExpectedFWUpdateRuntime"
52515278
else
5252-
printf "\n Router's Product Name/Model ID: $FW_RouterModelID${padStr}(S)how"
5279+
printf "\n Router's Product Name/Model ID: ${FW_RouterModelID}${padStr}(S)how"
52535280
fi
5254-
printf "\n${SEPtop}"
5281+
printf "\n${SEPstr}"
52555282

52565283
printf "\n ${GRNct}1${NOct}. Run F/W Update Check Now\n"
52575284
printf "\n ${GRNct}2${NOct}. Set Router Login Credentials\n"
@@ -5294,7 +5321,7 @@ _ShowMainMenu_()
52945321

52955322
printf "\n ${GRNct}un${NOct}. Uninstall\n"
52965323
printf "\n ${GRNct}e${NOct}. Exit\n"
5297-
printf "${SEPtop}\n"
5324+
printf "${SEPstr}\n"
52985325
}
52995326

53005327
##------------------------------------------##
@@ -5305,7 +5332,7 @@ _ShowAdvancedOptionsMenu_()
53055332
clear
53065333
logo
53075334
printf "================== Advanced Options Menu =================\n"
5308-
printf "${SEPtop}\n"
5335+
printf "${SEPstr}\n"
53095336

53105337
printf "\n ${GRNct}1${NOct}. Set Directory for F/W Update ZIP File"
53115338
printf "\n${padStr}[Current Path: ${GRNct}${FW_ZIP_DIR}${NOct}]\n"
@@ -5399,7 +5426,7 @@ _ShowAdvancedOptionsMenu_()
53995426
fi
54005427

54015428
printf "\n ${GRNct}e${NOct}. Return to Main Menu\n"
5402-
printf "${SEPtop}"
5429+
printf "${SEPstr}"
54035430
}
54045431

54055432
##---------------------------------------##
@@ -5410,7 +5437,7 @@ _ShowNodesMenu_()
54105437
clear
54115438
logo
54125439
printf "================= AiMesh Node(s) Info Menu ================\n"
5413-
printf "${SEPtop}\n"
5440+
printf "${SEPstr}\n"
54145441

54155442
if ! node_online_status="$(_NodeActiveStatus_)"
54165443
then node_online_status="" ; fi
@@ -5426,7 +5453,7 @@ _ShowNodesMenu_()
54265453
echo ""
54275454

54285455
printf "\n ${GRNct}e${NOct}. Return to Main Menu\n"
5429-
printf "${SEPtop}"
5456+
printf "${SEPstr}"
54305457
}
54315458

54325459
_ShowNodesMenuOptions_()
@@ -5485,7 +5512,7 @@ _DownloadChangelogs_()
54855512
less "$changeLogFile"
54865513
fi
54875514
rm -f "$changeLogFile" "$wgetLogFile"
5488-
"$inMenuMode" && _WaitForEnterKey_ "$mainMenuReturnPromptStr"
5515+
"$inMenuMode" && _WaitForEnterKey_ "$logsMenuReturnPromptStr"
54895516
return 1
54905517
}
54915518

@@ -5497,7 +5524,7 @@ _ShowLogsMenu_()
54975524
clear
54985525
logo
54995526
printf "======================== Logs Menu =======================\n"
5500-
printf "${SEPtop}\n"
5527+
printf "${SEPstr}\n"
55015528

55025529
printf "\n ${GRNct}1${NOct}. Set Directory for F/W Update Log Files"
55035530
printf "\n${padStr}[Current Path: ${GRNct}${FW_LOG_DIR}${NOct}]\n"
@@ -5510,7 +5537,7 @@ _ShowLogsMenu_()
55105537
printf "\n ${GRNct}cl${NOct}. View latest F/W Changelog\n"
55115538

55125539
printf "\n ${GRNct}e${NOct}. Return to Main Menu\n"
5513-
printf "${SEPtop}"
5540+
printf "${SEPstr}"
55145541
}
55155542

55165543
##----------------------------------------##

0 commit comments

Comments
 (0)