Skip to content

Commit 7692a2a

Browse files
Code Improvements for Script Downloads
Some code improvements when downloading script files.
1 parent 0720405 commit 7692a2a

File tree

2 files changed

+63
-41
lines changed

2 files changed

+63
-41
lines changed

MerlinAU.sh

Lines changed: 62 additions & 40 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-Dec-31
7+
# Last Modified: 2025-Jan-01
88
###################################################################
99
set -u
1010

@@ -475,9 +475,9 @@ Toggle_LEDs_PID=""
475475
FW_UpdateCheckState="TBD"
476476
FW_UpdateCheckScript="/usr/sbin/webs_update.sh"
477477

478-
##--------------------------------------##
479-
## Added by Martinski W. [22023-Nov-24] ##
480-
##--------------------------------------##
478+
##-------------------------------------##
479+
## Added by Martinski W. [2023-Nov-24] ##
480+
##-------------------------------------##
481481
#---------------------------------------------------------#
482482
# The USB-attached drives can have multiple partitions
483483
# with different file systems (NTFS, ext3, ext4, etc.),
@@ -1521,29 +1521,48 @@ _CheckForNewGUIVersionUpdate_()
15211521
return "$retCode"
15221522
}
15231523

1524+
##-------------------------------------##
1525+
## Added by Martinski W. [2025-Jan-01] ##
1526+
##-------------------------------------##
1527+
_CurlFileDownload_()
1528+
{
1529+
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ]
1530+
then return 1 ; fi
1531+
local retCode tempFilePathDL="${2}.DL.TMP"
1532+
1533+
curl -LSs --retry 4 --retry-delay 5 --retry-connrefused \
1534+
"$1" -o "$tempFilePathDL"
1535+
if [ $? -ne 0 ] && [ ! -s "$tempFilePathDL" ] || \
1536+
grep -iq "^404: Not Found" "$tempFilePathDL"
1537+
then rm -f "$tempFilePathDL" ; retCode=1
1538+
else mv -f "$tempFilePathDL" "$2" ; retCode=0
1539+
fi
1540+
return "$retCode"
1541+
}
1542+
15241543
##----------------------------------------##
1525-
## Modified by Martinski W. [2024-Dec-31] ##
1544+
## Modified by Martinski W. [2025-Jan-01] ##
15261545
##----------------------------------------##
15271546
_SCRIPTUPDATE_()
15281547
{
15291548
local urlScriptVers theScriptVers extraParam=""
15301549

15311550
_DownloadScriptFiles_()
15321551
{
1533-
local retCode ScriptFileDL="${ScriptFilePath}.DL"
1534-
1535-
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_REPO}/version.txt" -o "$SCRIPTVERPATH"
1536-
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_REPO}/${SCRIPT_NAME}.sh" -o "$ScriptFileDL"
1537-
1538-
if [ $? -eq 0 ] && [ -s "$ScriptFileDL" ]
1552+
local retCode
1553+
if _CurlFileDownload_ "${SCRIPT_URL_REPO}/version.txt" "$SCRIPTVERPATH"
15391554
then
1540-
mv -f "$ScriptFileDL" "$ScriptFilePath"
1541-
chmod 755 "$ScriptFilePath"
1542-
retCode=0
1555+
retCode=0 ; chmod 664 "$SCRIPTVERPATH"
15431556
else
1544-
rm -f "$ScriptFileDL"
1545-
printf "\n${REDct}Download failed.${NOct}\n"
15461557
retCode=1
1558+
Say "${REDct}**ERROR**${NOct}: Unable to download latest version file for $SCRIPT_NAME."
1559+
fi
1560+
if _CurlFileDownload_ "${SCRIPT_URL_REPO}/${SCRIPT_NAME}.sh" "$ScriptFilePath"
1561+
then
1562+
retCode=0 ; chmod 755 "$ScriptFilePath"
1563+
else
1564+
retCode=1
1565+
Say "${REDct}**ERROR**${NOct}: Unable to download latest script file for $SCRIPT_NAME."
15471566
fi
15481567
return "$retCode"
15491568
}
@@ -1558,8 +1577,9 @@ _SCRIPTUPDATE_()
15581577

15591578
if _DownloadScriptFiles_
15601579
then
1561-
printf "${CYANct}$SCRIPT_NAME successfully updated.${NOct}\n\n"
1580+
printf "${CYANct}$SCRIPT_NAME was successfully updated.${NOct}\n\n"
15621581
sleep 1
1582+
[ -s "$SCRIPTVERPATH" ] && urlScriptVers="$(cat "$SCRIPTVERPATH")"
15631583
if [ $# -gt 1 ] && [ "$2" = "newgui" ] && \
15641584
_CheckForNewGUIVersionUpdate_ "$theScriptVers" "$urlScriptVers"
15651585
then extraParam="forceupdate" ; fi
@@ -1584,7 +1604,7 @@ _SCRIPTUPDATE_()
15841604
echo -e "${CYANct}This will overwrite your currently installed version.${NOct}"
15851605
if _WaitForYESorNO_
15861606
then
1587-
printf "\n\n${CYANct}Downloading $SCRIPT_NAME ${CYANct}v${DLRepoVersion}${NOct}\n"
1607+
printf "\n\n${CYANct}Downloading $SCRIPT_NAME $DLRepoVersion version.${NOct}\n"
15881608

15891609
if _DownloadScriptFiles_
15901610
then
@@ -1603,12 +1623,13 @@ _SCRIPTUPDATE_()
16031623
echo -e "${CYANct}Bingo! New version available! Would you like to update now?${NOct}"
16041624
if _WaitForYESorNO_
16051625
then
1606-
printf "\n\n${CYANct}Downloading $SCRIPT_NAME ${CYANct}v${DLRepoVersion}${NOct}\n"
1626+
printf "\n\n${CYANct}Downloading $SCRIPT_NAME $DLRepoVersion version.${NOct}\n"
16071627

16081628
if _DownloadScriptFiles_
16091629
then
16101630
printf "\n$(date) - $SCRIPT_NAME - Successfully downloaded $SCRIPT_NAME v${DLRepoVersion}\n"
16111631
printf "${CYANct}Update successful! Restarting script...${NOct}\n"
1632+
sleep 1
16121633
_CheckForNewGUIVersionUpdate_ && extraParam="forceupdate"
16131634
_ReleaseLock_
16141635
exec "$ScriptFilePath" $extraParam
@@ -1626,27 +1647,28 @@ _SCRIPTUPDATE_()
16261647
}
16271648

16281649
##----------------------------------------##
1629-
## Modified by Martinski W. [2024-Dec-31] ##
1650+
## Modified by Martinski W. [2025-Jan-01] ##
16301651
##----------------------------------------##
16311652
_CheckForNewScriptUpdates_()
16321653
{
16331654
local extraParam=""
16341655

16351656
echo
1657+
DLRepoVersion="$SCRIPT_VERSION"
16361658
[ -s "$SCRIPTVERPATH" ] && DLRepoVersion="$(cat "$SCRIPTVERPATH")"
16371659
rm -f "$SCRIPTVERPATH"
16381660

1639-
# Download the latest version file from the source repository
1640-
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_REPO}/version.txt" -o "$SCRIPTVERPATH"
1641-
1642-
if [ $? -ne 0 ] || [ ! -s "$SCRIPTVERPATH" ]
1643-
then scriptUpdateNotify=0 ; return 1 ; fi
1661+
if ! _CurlFileDownload_ "${SCRIPT_URL_REPO}/version.txt" "$SCRIPTVERPATH"
1662+
then
1663+
Say "${REDct}**ERROR**${NOct}: Unable to download latest version file for $SCRIPT_NAME."
1664+
scriptUpdateNotify=0
1665+
return 1
1666+
fi
16441667

1645-
# Read in its contents for the current version file
16461668
DLRepoVersion="$(cat "$SCRIPTVERPATH")"
16471669
if [ -z "$DLRepoVersion" ]
16481670
then
1649-
echo "Variable for downloaded version is empty."
1671+
Say "${REDct}**ERROR**${NOct}: Variable for downloaded version is empty."
16501672
scriptUpdateNotify=0
16511673
return 1
16521674
fi
@@ -1656,16 +1678,16 @@ _CheckForNewScriptUpdates_()
16561678

16571679
if [ "$DLRepoVersionNum" -gt "$ScriptVersionNum" ]
16581680
then
1659-
scriptUpdateNotify="New script update available.
1681+
scriptUpdateNotify="New script update available.
16601682
${REDct}v${SCRIPT_VERSION}${NOct} --> ${GRNct}v${DLRepoVersion}${NOct}"
1661-
Say "$myLAN_HostName - A new script version update (v$DLRepoVersion) is available to download."
1662-
if [ "$ScriptAutoUpdateSetting" = "ENABLED" ]
1663-
then
1664-
_CheckForNewGUIVersionUpdate_ && extraParam="newgui"
1665-
_SCRIPTUPDATE_ force $extraParam
1666-
fi
1683+
Say "$myLAN_HostName - A new script version update (v$DLRepoVersion) is available to download."
1684+
if [ "$ScriptAutoUpdateSetting" = "ENABLED" ]
1685+
then
1686+
_CheckForNewGUIVersionUpdate_ && extraParam="newgui"
1687+
_SCRIPTUPDATE_ force $extraParam
1688+
fi
16671689
else
1668-
scriptUpdateNotify=0
1690+
scriptUpdateNotify=0
16691691
fi
16701692
return 0
16711693
}
@@ -3124,7 +3146,7 @@ _GetNodeInfo_()
31243146
return 1
31253147
fi
31263148

3127-
# Perform login request
3149+
# Perform login request #
31283150
curl -s -k "${NodeURLstr}/login.cgi" \
31293151
--referer "${NodeURLstr}/Main_Login.asp" \
31303152
--user-agent 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0' \
@@ -3142,7 +3164,7 @@ _GetNodeInfo_()
31423164
return 1
31433165
fi
31443166

3145-
# Run the curl command to retrieve the HTML content
3167+
# Retrieve the HTML content #
31463168
htmlContent="$(curl -s -k "${NodeURLstr}/appGet.cgi?hook=nvram_get(productid)%3bnvram_get(asus_device_list)%3bnvram_get(cfg_device_list)%3bnvram_get(firmver)%3bnvram_get(buildno)%3bnvram_get(extendno)%3bnvram_get(webs_state_flag)%3bnvram_get(odmpid)%3bnvram_get(wps_modelnum)%3bnvram_get(model)%3bnvram_get(build_name)%3bnvram_get(lan_hostname)%3bnvram_get(webs_state_info)%3bnvram_get(label_mac)" \
31473169
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0' \
31483170
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
@@ -3182,7 +3204,7 @@ _GetNodeInfo_()
31823204
# Combine extracted information into one string #
31833205
Node_combinedVer="${node_firmver}.${node_buildno}.$node_extendno"
31843206

3185-
# Perform logout request
3207+
# Perform logout request #
31863208
curl -s -k "${NodeURLstr}/Logout.asp" \
31873209
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0' \
31883210
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
@@ -7088,9 +7110,9 @@ Please manually update to version ${GRNct}${MinSupportedFirmwareVers}${NOct} or
70887110
fi
70897111
fi
70907112

7091-
# Extracting the F/W Update codebase number to use in the curl #
7113+
# Extracting the F/W Update codebase number #
70927114
fwUpdateBaseNum="$(echo "$release_version" | cut -d'.' -f1)"
7093-
# Inserting dots between each number
7115+
# Inserting dots between each number #
70947116
dottedVersion="$(echo "$fwUpdateBaseNum" | sed 's/./&./g' | sed 's/.$//')"
70957117

70967118
## Check for Login Credentials ##

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MerlinAU - AsusWRT-Merlin Firmware Auto Updater
22
## v1.3.9
3-
## 2024-Dec-21
3+
## 2025-Jan-02
44

55
![image](https://github.com/user-attachments/assets/185f9fe4-acdb-419a-8154-ab6fa6e0fd46)
66
![image](https://github.com/user-attachments/assets/8f22818d-2118-4c47-a54a-e54fb147fd6b)

0 commit comments

Comments
 (0)