Skip to content

Commit bb138e9

Browse files
Fix & Code Improvements.
- Code changes & improvements to make mounting the WebGUI page more robust. - Fix for calling F/W Update checks when ENABLED.
1 parent a7e495c commit bb138e9

File tree

1 file changed

+129
-80
lines changed

1 file changed

+129
-80
lines changed

MerlinAU.sh

Lines changed: 129 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
#
55
# Original Creation Date: 2023-Oct-01 by @ExtremeFiretop.
66
# Official Co-Author: @Martinski W. - Date: 2023-Nov-01
7-
# Last Modified: 2025-Jan-10
7+
# Last Modified: 2025-Jan-11
88
###################################################################
99
set -u
1010

11-
source /usr/sbin/helper.sh
1211
## Set version for each Production Release ##
1312
readonly SCRIPT_VERSION=1.4.0
1413
readonly SCRIPT_NAME="MerlinAU"
@@ -98,6 +97,8 @@ readonly ORIG_MENU_TREE="/www/require/modules/menuTree.js"
9897
readonly WEBUI_LOCKFD=386
9998
readonly WEBUI_LOCKFILE="/tmp/addonwebui.lock"
10099
readonly TEMPFILE="/tmp/MerlinAU_settings_$$.txt"
100+
readonly webPageFileRegExp="user([1-9]|[1-2][0-9])[.]asp"
101+
readonly webPageLineRegExp="\{url: \"$webPageFileRegExp\", tabName: \"$SCRIPT_NAME\"\}"
101102

102103
# Give FIRST priority to built-in binaries over any other #
103104
export PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
@@ -1673,8 +1674,45 @@ then
16731674
Update_Custom_Settings FW_New_Update_LOG_Directory_Path "$UserPreferredLogPath"
16741675
fi
16751676

1677+
##-------------------------------------##
1678+
## Added by Martinski W. [2025-Jan-11] ##
1679+
##-------------------------------------##
1680+
_GetWebUIPage_()
1681+
{
1682+
local webPageFile webPagePath webPageTemp webPageEntry
1683+
1684+
webPageFile="NONE"
1685+
1686+
if [ -f "$TEMP_MENU_TREE" ]
1687+
then
1688+
webPageEntry="$(grep -E "$webPageLineRegExp" "$TEMP_MENU_TREE")"
1689+
if [ -n "$webPageEntry" ]
1690+
then
1691+
webPageTemp="$(echo "$webPageEntry" | grep -owE "$webPageFileRegExp")"
1692+
[ -n "$webPageTemp" ] && webPageFile="$webPageTemp"
1693+
fi
1694+
fi
1695+
1696+
for index in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1697+
do
1698+
webPageTemp="user${index}.asp"
1699+
webPagePath="${SHARED_WEB_DIR}/$webPageTemp"
1700+
1701+
if [ -f "$webPagePath" ] && \
1702+
[ "$(md5sum < "$1")" = "$(md5sum < "$webPagePath")" ]
1703+
then
1704+
webPageFile="$webPageTemp"
1705+
break
1706+
elif [ "$webPageFile" = "NONE" ] && [ ! -f "$webPagePath" ]
1707+
then
1708+
webPageFile="$webPageTemp"
1709+
fi
1710+
done
1711+
echo "$webPageFile"
1712+
}
1713+
16761714
##----------------------------------------##
1677-
## Modified by Martinski W. [2025-Jan-05] ##
1715+
## Modified by Martinski W. [2025-Jan-11] ##
16781716
##----------------------------------------##
16791717
_Mount_WebUI_()
16801718
{
@@ -1683,100 +1721,79 @@ _Mount_WebUI_()
16831721
Say "${CRITct}**ERROR**${NOct}: The WebUI page file for $SCRIPT_NAME is NOT found."
16841722
return 1
16851723
fi
1686-
local webPageFound=""
1724+
local webPageFile
1725+
1726+
Say "Mounting WebUI page for $SCRIPT_NAME"
16871727

16881728
eval exec "$WEBUI_LOCKFD>$WEBUI_LOCKFILE"
16891729
flock -x "$WEBUI_LOCKFD"
16901730

1691-
# Obtain the first available mount point in $am_webui_page #
1692-
am_get_webui_page "$SCRIPT_WEB_ASP_PATH"
1693-
if [ -z "$am_webui_page" ] || [ "$am_webui_page" = "none" ]
1731+
webPageFile="$(_GetWebUIPage_ "$SCRIPT_WEB_ASP_PATH")"
1732+
if [ -z "$webPageFile" ] || [ "$webPageFile" = "NONE" ]
16941733
then
16951734
Say "${CRITct}**ERROR**${NOct}: Unable to mount the $SCRIPT_NAME WebUI page."
16961735
flock -u "$WEBUI_LOCKFD"
16971736
return 1
16981737
fi
16991738

1700-
##################### TO BE REMOVED #####################
1701-
# Store or update the page name for later use #
1702-
if grep -q "^MerlinAU_uiPage" "$SHARED_SETTINGS_FILE"
1703-
then
1704-
# Replace the existing line with the new value #
1705-
sed -i "s/^MerlinAU_uiPage.*/MerlinAU_uiPage $am_webui_page/" "$SHARED_SETTINGS_FILE"
1706-
else
1707-
echo "MerlinAU_uiPage $am_webui_page" >> "$SHARED_SETTINGS_FILE"
1708-
fi
1709-
##################### TO BE REMOVED #####################
1710-
1711-
# Copy add-on web page to the user's WebUI directory #
1712-
cp -fp "$SCRIPT_WEB_ASP_PATH" "${SHARED_WEB_DIR}/$am_webui_page"
1713-
echo "$SCRIPT_NAME" > "$SHARED_WEB_DIR/$(echo "$am_webui_page" | cut -f1 -d'.').title"
1739+
cp -fp "$SCRIPT_WEB_ASP_PATH" "${SHARED_WEB_DIR}/$webPageFile"
1740+
echo "$SCRIPT_NAME" > "${SHARED_WEB_DIR}/$(echo "$webPageFile" | cut -f1 -d'.').title"
17141741

1715-
# Copy 'menuTree' if not found #
17161742
if [ ! -f "$TEMP_MENU_TREE" ]
1717-
then
1718-
cp -fp "$ORIG_MENU_TREE" "$TEMP_MENU_TREE"
1719-
fi
1720-
sed -i "\\~$am_webui_page~d" "$TEMP_MENU_TREE"
1743+
then cp -fp "$ORIG_MENU_TREE" "$TEMP_MENU_TREE" ; fi
1744+
1745+
sed -i "/url: \"$webPageFile\", tabName: \"$SCRIPT_NAME\"/d" "$TEMP_MENU_TREE"
17211746

1722-
# Insert new entry at the end of the 'Tools' menu #
1723-
sed -i "/url: \"Advanced_FirmwareUpgrade_Content.asp\", tabName:/a {url: \"$am_webui_page\", tabName: \"$SCRIPT_NAME\"}," "$TEMP_MENU_TREE"
1747+
# Insert new page tab in the 'Administration' menu #
1748+
sed -i "/url: \"Advanced_FirmwareUpgrade_Content.asp\", tabName:/a {url: \"$webPageFile\", tabName: \"$SCRIPT_NAME\"}," "$TEMP_MENU_TREE"
17241749

17251750
umount "$ORIG_MENU_TREE" 2>/dev/null
17261751
mount -o bind "$TEMP_MENU_TREE" "$ORIG_MENU_TREE"
17271752
flock -u "$WEBUI_LOCKFD"
1753+
1754+
Say "${GRNct}$SCRIPT_NAME WebUI page was mounted successfully."
17281755
return 0
17291756
}
17301757

17311758
##----------------------------------------##
1732-
## Modified by Martinski W. [2025-Jan-05] ##
1759+
## Modified by Martinski W. [2025-Jan-11] ##
17331760
##----------------------------------------##
17341761
_Unmount_WebUI_()
17351762
{
1736-
if [ ! -f "$TEMP_MENU_TREE" ]
1763+
if [ ! -f "$SCRIPT_WEB_ASP_PATH" ]
17371764
then
1738-
Say "The WebUI Menu Tree file $TEMP_MENU_TREE is NOT found."
1765+
Say "${CRITct}**ERROR**${NOct}: The WebUI page file for $SCRIPT_NAME is NOT found."
17391766
return 1
17401767
fi
1741-
Say "Unmounting WebUI tab for $SCRIPT_NAME"
1768+
local webPageFile
17421769

1743-
# Load the page name we stored during install #
1744-
if [ -f "$SHARED_SETTINGS_FILE" ]
1745-
then
1746-
webui_page="$(grep "^MerlinAU_uiPage" "$SHARED_SETTINGS_FILE" | awk '{print $2}')"
1747-
else
1748-
webui_page="$(grep 'tabName: "MerlinAU"' "$TEMP_MENU_TREE" | sed -n 's/.*url: "\([^"]*\)".*/\1/p')"
1749-
fi
1750-
if [ -z "$webui_page" ]
1751-
then
1752-
Say "$SCRIPT_NAME" "WebGUI ASP page is NOT found to uninstall."
1753-
return 1
1754-
fi
1770+
Say "Unmounting WebUI page for $SCRIPT_NAME"
17551771

17561772
eval exec "$WEBUI_LOCKFD>$WEBUI_LOCKFILE"
17571773
flock -x "$WEBUI_LOCKFD"
17581774

1759-
if [ -f "$TEMP_MENU_TREE" ]
1760-
then
1761-
sed -i "/url: \"$webui_page\", tabName: \"$SCRIPT_NAME\"/d" "$TEMP_MENU_TREE"
1762-
fi
1763-
if [ -f "${SHARED_WEB_DIR}/$webui_page" ]
1775+
webPageFile="$(_GetWebUIPage_ "$SCRIPT_WEB_ASP_PATH")"
1776+
if [ -z "$webPageFile" ] || [ "$webPageFile" = "NONE" ]
17641777
then
1765-
rm -f "${SHARED_WEB_DIR}/$webui_page"
1778+
Say "WebUI page file for $SCRIPT_NAME is NOT found to uninstall."
1779+
flock -u "$WEBUI_LOCKFD"
1780+
return 1
17661781
fi
17671782

1768-
# Remove entry from the SHARED_SETTINGS_FILE #
1769-
if [ -f "$SHARED_SETTINGS_FILE" ]
1783+
if [ -f "$TEMP_MENU_TREE" ]
17701784
then
1771-
sed -i "/^MerlinAU_uiPage\s\+$webui_page$/d" "$SHARED_SETTINGS_FILE"
1785+
sed -i "/url: \"$webPageFile\", tabName: \"$SCRIPT_NAME\"/d" "$TEMP_MENU_TREE"
17721786
fi
1787+
rm -f "${SHARED_WEB_DIR}/$webPageFile"
1788+
rm -f "${SHARED_WEB_DIR}/$(echo "$webPageFile" | cut -f1 -d'.').title"
17731789

1774-
umount "$ORIG_MENU_TREE"
1790+
umount "$ORIG_MENU_TREE" 2>/dev/null
17751791
mount -o bind "$TEMP_MENU_TREE" "$ORIG_MENU_TREE"
17761792
flock -u "$WEBUI_LOCKFD"
17771793

1778-
Say "${GRNct}$SCRIPT_NAME WebUI unmounted successfully."
1794+
Say "${GRNct}$SCRIPT_NAME WebUI page unmounted successfully."
17791795
/sbin/service restart_httpd >/dev/null 2>&1 &
1796+
return 0
17801797
}
17811798

17821799
##----------------------------------------##
@@ -2007,45 +2024,77 @@ _CheckForNewGUIVersionUpdate_()
20072024
}
20082025

20092026
##----------------------------------------##
2010-
## Modified by Martinski W. [2025-Jan-06] ##
2027+
## Modified by Martinski W. [2025-Jan-11] ##
20112028
##----------------------------------------##
20122029
_CurlFileDownload_()
20132030
{
20142031
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ]
20152032
then return 1 ; fi
2016-
local retCode tempFilePathDL="${2}.DL.TMP"
2033+
local retCode=1
2034+
local tempFilePathDL="${2}.DL.TMP"
2035+
local srceFilePathDL="${SCRIPT_URL_REPO}/$1"
20172036

20182037
curl -LSs --retry 4 --retry-delay 5 --retry-connrefused \
2019-
"$1" -o "$tempFilePathDL"
2038+
"$srceFilePathDL" -o "$tempFilePathDL"
20202039
if [ $? -ne 0 ] || [ ! -s "$tempFilePathDL" ] || \
20212040
grep -iq "^404: Not Found" "$tempFilePathDL"
2022-
then rm -f "$tempFilePathDL" ; retCode=1
2023-
else mv -f "$tempFilePathDL" "$2" ; retCode=0
2041+
then
2042+
rm -f "$tempFilePathDL"
2043+
retCode=1
2044+
else
2045+
if [ "$1" = "$SCRIPT_WEB_ASP_FILE" ] && \
2046+
[ -f "$2" ] && [ -f "$TEMP_MENU_TREE" ] && \
2047+
! diff -q "$tempFilePathDL" "$2" >/dev/null 2>&1
2048+
then updatedWebUIPage=true
2049+
else updatedWebUIPage=false
2050+
fi
2051+
mv -f "$tempFilePathDL" "$2"
2052+
retCode=0
20242053
fi
2054+
20252055
return "$retCode"
20262056
}
20272057

20282058
##----------------------------------------##
2029-
## Modified by Martinski W. [2025-Jan-05] ##
2059+
## Modified by Martinski W. [2025-Jan-11] ##
20302060
##----------------------------------------##
20312061
_DownloadScriptFiles_()
20322062
{
2033-
local retCode
2034-
if _CurlFileDownload_ "${SCRIPT_URL_REPO}/version.txt" "$SCRIPT_VERPATH"
2063+
local retCode isUpdateAction updatedWebUIPage theWebPage
2064+
2065+
if [ $# -gt 0 ] && [ "$1" = "update" ]
2066+
then isUpdateAction=true
2067+
else isUpdateAction=false
2068+
fi
2069+
updatedWebUIPage=false
2070+
2071+
if _CurlFileDownload_ "version.txt" "$SCRIPT_VERPATH"
20352072
then
20362073
retCode=0 ; chmod 664 "$SCRIPT_VERPATH"
20372074
else
20382075
retCode=1
20392076
Say "${REDct}**ERROR**${NOct}: Unable to download latest version file for $SCRIPT_NAME."
20402077
fi
2041-
if _CurlFileDownload_ "${SCRIPT_URL_REPO}/$SCRIPT_WEB_ASP_FILE" "$SCRIPT_WEB_ASP_PATH"
2078+
if _CurlFileDownload_ "$SCRIPT_WEB_ASP_FILE" "$SCRIPT_WEB_ASP_PATH"
20422079
then
2043-
retCode=0 ; chmod 664 "$SCRIPT_WEB_ASP_PATH"
2080+
chmod 664 "$SCRIPT_WEB_ASP_PATH"
2081+
if "$inRouterSWmode" && "$updatedWebUIPage"
2082+
then
2083+
theWebPage="$(_GetWebUIPage_ "$SCRIPT_WEB_ASP_PATH")"
2084+
if [ -n "$theWebPage" ] && [ "$theWebPage" != "NONE" ]
2085+
then
2086+
sed -i "/url: \"$theWebPage\", tabName: \"$SCRIPT_NAME\"/d" "$TEMP_MENU_TREE"
2087+
rm -f "${SHARED_WEB_DIR}/$theWebPage"
2088+
rm -f "${SHARED_WEB_DIR}/$(echo "$theWebPage" | cut -f1 -d'.').title"
2089+
fi
2090+
"$isUpdateAction" && _Mount_WebUI_
2091+
fi
2092+
retCode=0
20442093
else
20452094
retCode=1
20462095
Say "${REDct}**ERROR**${NOct}: Unable to download latest WebUI ASP file for $SCRIPT_NAME."
20472096
fi
2048-
if _CurlFileDownload_ "${SCRIPT_URL_REPO}/${SCRIPT_NAME}.sh" "$ScriptFilePath"
2097+
if _CurlFileDownload_ "${SCRIPT_NAME}.sh" "$ScriptFilePath"
20492098
then
20502099
retCode=0 ; chmod 755 "$ScriptFilePath"
20512100
else
@@ -2056,7 +2105,7 @@ _DownloadScriptFiles_()
20562105
}
20572106

20582107
##----------------------------------------##
2059-
## Modified by Martinski W. [2025-Jan-05] ##
2108+
## Modified by Martinski W. [2025-Jan-11] ##
20602109
##----------------------------------------##
20612110
_SCRIPT_UPDATE_()
20622111
{
@@ -2070,7 +2119,7 @@ _SCRIPT_UPDATE_()
20702119
urlScriptVers="$(/usr/sbin/curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_REPO}/version.txt")"
20712120
printf "${CYANct}Downloading latest version ($urlScriptVers) of ${SCRIPT_NAME}${NOct}\n"
20722121

2073-
if _DownloadScriptFiles_
2122+
if _DownloadScriptFiles_ update
20742123
then
20752124
printf "${CYANct}$SCRIPT_NAME files were successfully updated.${NOct}\n\n"
20762125
[ -s "$SCRIPT_VERPATH" ] && urlScriptVers="$(cat "$SCRIPT_VERPATH")"
@@ -2110,7 +2159,7 @@ _SCRIPT_UPDATE_()
21102159
then
21112160
printf "\n\n${CYANct}Downloading $SCRIPT_NAME $DLRepoVersion version.${NOct}\n"
21122161

2113-
if _DownloadScriptFiles_
2162+
if _DownloadScriptFiles_ update
21142163
then
21152164
if "$inRouterSWmode"
21162165
then _SetVersionSharedSettings_ local "$DLRepoVersion" ; fi
@@ -2131,7 +2180,7 @@ _SCRIPT_UPDATE_()
21312180
then
21322181
printf "\n\n${CYANct}Downloading $SCRIPT_NAME $DLRepoVersion version.${NOct}\n"
21332182

2134-
if _DownloadScriptFiles_
2183+
if _DownloadScriptFiles_ update
21352184
then
21362185
if "$inRouterSWmode"
21372186
then _SetVersionSharedSettings_ local "$DLRepoVersion" ; fi
@@ -2155,7 +2204,7 @@ _SCRIPT_UPDATE_()
21552204
}
21562205

21572206
##----------------------------------------##
2158-
## Modified by Martinski W. [2025-Jan-01] ##
2207+
## Modified by Martinski W. [2025-Jan-11] ##
21592208
##----------------------------------------##
21602209
_CheckForNewScriptUpdates_()
21612210
{
@@ -2166,7 +2215,7 @@ _CheckForNewScriptUpdates_()
21662215
[ -s "$SCRIPT_VERPATH" ] && DLRepoVersion="$(cat "$SCRIPT_VERPATH")"
21672216
rm -f "$SCRIPT_VERPATH"
21682217

2169-
if ! _CurlFileDownload_ "${SCRIPT_URL_REPO}/version.txt" "$SCRIPT_VERPATH"
2218+
if ! _CurlFileDownload_ "version.txt" "$SCRIPT_VERPATH"
21702219
then
21712220
Say "${REDct}**ERROR**${NOct}: Unable to download latest version file for $SCRIPT_NAME."
21722221
scriptUpdateNotify=0
@@ -8445,9 +8494,9 @@ _DoStartupInit_()
84458494
fi
84468495
}
84478496

8448-
##-------------------------------------##
8449-
## Added by Martinski W. [2025-Jan-05] ##
8450-
##-------------------------------------##
8497+
##----------------------------------------##
8498+
## Modified by Martinski W. [2025-Jan-11] ##
8499+
##----------------------------------------##
84518500
_DoInstallation_()
84528501
{
84538502
local webguiOK=true
@@ -8459,7 +8508,7 @@ _DoInstallation_()
84598508
_CreateSymLinks_
84608509
_SetVersionSharedSettings_ local "$SCRIPT_VERSION"
84618510
_SetVersionSharedSettings_ server "$SCRIPT_VERSION"
8462-
_DownloadScriptFiles_
8511+
_DownloadScriptFiles_ install
84638512

84648513
if "$inRouterSWmode"
84658514
then
@@ -9246,9 +9295,8 @@ _ShowMainMenuOptions_()
92469295
# Check if router reports a new F/W update is available.
92479296
# If yes, modify the notification settings accordingly.
92489297
#-----------------------------------------------------------#
9249-
FW_NewUpdateVersion="$(_GetLatestFWUpdateVersionFromRouter_)"
9250-
if [ -n "$FW_NewUpdateVersion" ] && \
9251-
[ -n "$FW_InstalledVersion" ] && \
9298+
if FW_NewUpdateVersion="$(_GetLatestFWUpdateVersionFromRouter_)" && \
9299+
[ -n "$FW_NewUpdateVersion" ] && [ -n "$FW_InstalledVersion" ] && \
92529300
[ "$FW_NewUpdateVersion" != "$FW_NewUpdateVerInit" ]
92539301
then
92549302
FW_NewUpdateVerInit="$FW_NewUpdateVersion"
@@ -9924,7 +9972,7 @@ then
99249972
fi
99259973

99269974
##----------------------------------------##
9927-
## Modified by Martinski W. [2024-Jan-05] ##
9975+
## Modified by Martinski W. [2024-Jan-11] ##
99289976
##----------------------------------------##
99299977
if [ $# -gt 0 ]
99309978
then
@@ -9933,6 +9981,7 @@ then
99339981

99349982
inMenuMode=false
99359983
_DoInitializationStartup_ "$1"
9984+
_ConfirmCronJobForFWAutoUpdates_
99369985

99379986
case "$1" in
99389987
run_now)

0 commit comments

Comments
 (0)