Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 75 additions & 20 deletions MerlinAutoUpdate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
URL_BASE="https://sourceforge.net/projects/asuswrt-merlin/files"
URL_RELEASE_SUFFIX="Release"

##-------------------------------------##
## Added by Martinski W. [2023-Oct-05] ##
##-------------------------------------##
##----------------------------------------------##
## Added/Modified by Martinski W. [2023-Oct-05] ##
##----------------------------------------------##
_GetRouterModel_()
{
local retCode=1 routerModelID=""
local nvramModelKeys="odmpid productid wps_modelnum wps_device_name model"
local nvramModelKeys="productid build_name odmpid"
for nvramKey in $nvramModelKeys
do
routerModelID="$(nvram get "$nvramKey")"
Expand All @@ -27,6 +27,12 @@ SETTINGSFILE="$SETTINGS_DIR/custom_settings.txt"
CRON_SCHEDULE="0 0 * * 0"
COMMAND="sh /jffs/scripts/MerlinAutoUpdate.sh run_now"

##-------------------------------------##
## Added by Martinski W. [2023-Oct-05] ##
##-------------------------------------##
_WaitForEnterKey_()
{ printf "\nPress Enter to continue..." ; read EnterKEY ; }

# Function to get LAN IP
get_lan_ip() {
local lan_ip
Expand Down Expand Up @@ -128,26 +134,40 @@ Update_Custom_Settings() {
esac
}

##----------------------------------------##
## Modified by Martinski W. [2023-Oct-05] ##
##----------------------------------------##
get_current_firmware() {
local current_version="$(nvram get buildno)_$(nvram get extendno)"
local current_version="$(nvram get buildno).$(nvram get extendno)"
echo "$current_version"
}

##----------------------------------------##
## Modified by Martinski W. [2023-Oct-05] ##
##----------------------------------------##
get_latest_firmware() {
local url="$1"
local page=$(curl -s "$url")

local links_and_versions=$(echo "$page" | grep -o 'href="[^"]*'"$MODEL"'[^"]*\.zip' | sed 's/amp;//g; s/href="//' |
awk -F'[_\.]' '{print $3"."$4"."$5" "$0}' | sort -t. -k1,1n -k2,2n -k3,3n)
local links_and_versions="$(curl -s "$url" | grep -o 'href="[^"]*'"$MODEL"'[^"]*\.zip' | sed 's/amp;//g; s/href="//' |
awk -F'[_\.]' '{print $3"."$4"."$5" "$0}' | sort -t. -k1,1n -k2,2n -k3,3n)"

if [ -z "$links_and_versions" ]
then echo "**ERROR** **NO_URL**" ; return 1 ; fi

local latest=$(echo "$links_and_versions" | tail -n 1)
local version=$(echo "$latest" | cut -d' ' -f1 | awk -F. '{print $(NF-1)"."$NF}')
local link=$(echo "$latest" | cut -d' ' -f2-)
local latest="$(echo "$links_and_versions" | tail -n 1)"
local linkStr="$(echo "$latest" | cut -d' ' -f2-)"
local fileStr="$(echo "$linkStr" | grep -oE "/${MODEL}_[0-9]+.*.zip$")"
local versionStr

if [ -z "$fileStr" ]
then versionStr="$(echo "$latest" | cut -d ' ' -f1)"
else versionStr="$(echo ${fileStr%.*} | sed "s/\/${MODEL}_//" | sed 's/_/./g')"
fi

# Extracting the correct link from the page
local correct_link=$(echo "$link" | sed 's|^/|https://sourceforge.net/|')
local correct_link="$(echo "$linkStr" | sed 's|^/|https://sourceforge.net/|')"

echo "$version"
echo "$versionStr"
echo "$correct_link"
}

Expand Down Expand Up @@ -196,7 +216,7 @@ pure_file=$(ls | grep -i '_pureubi.w' | grep -iv 'rog')
Update_Custom_Settings "local" "n"
fi

read -p "Press Enter to continue..."
_WaitForEnterKey_
}

# Function to translate cron schedule to English
Expand Down Expand Up @@ -284,22 +304,57 @@ else
echo "Cron job 'MerlinAutoUpdate' already exists."
fi

##-------------------------------------##
## Added by Martinski W. [2023-Oct-06] ##
##-------------------------------------##
_VersionFormatToNumber_()
{
if [ $# -eq 0 ] || [ -z "$1" ] || [ -z "$2" ]
then echo "" ; return 1 ; fi

local versionNum versionStr="$1"

if [ "$(echo "$1" | awk -F '.' '{print NF}')" -lt "$2" ]
then versionStr="$(nvram get firmver | sed 's/\.//g').$1" ; fi

if [ "$2" -lt 4 ]
then versionNum="$(echo "$versionStr" | awk -F '.' '{printf ("%d%03d%03d\n", $1,$2,$3);}')"
else versionNum="$(echo "$versionStr" | awk -F '.' '{printf ("%d%d%03d%03d\n", $1,$2,$3,$4);}')"
fi

echo "$versionNum" ; return 0
}

##----------------------------------------##
## Modified by Martinski W. [2023-Oct-06] ##
##----------------------------------------##
# Embed functions from second script, modified as necessary.
run_now() {

Say "Running the task now...Checking for updates..."

# Get current firmware version
current_version=$(get_current_firmware)
current_version="$(get_current_firmware)"
#current_version="388.3"

# Use set to read the output of the function into variables
set -- $(get_latest_firmware "$URL_RELEASE")
release_version=$1
release_link=$2
release_version="$1"
release_link="$2"

if [ "$1" = "**ERROR**" ] && [ "$2" = "**NO_URL**" ]
then
echo "**ERROR**: No firmware release URL was found for [$MODEL] router model."
_WaitForEnterKey_
return 1
fi

local numFields="$(echo "$release_version" | awk -F '.' '{print NF}')"
local numCurrentVers="$(_VersionFormatToNumber_ "$current_version" "$numFields")"
local numReleaseVers="$(_VersionFormatToNumber_ "$release_version" "$numFields")"

# Compare versions before deciding to download
if [ "$release_version" \> "$current_version" ]; then
if [ "$numReleaseVers" -gt "$numCurrentVers" ]; then
Say "Latest release version is $release_version, downloading from $release_link"
wget -O "${MODEL}_firmware.zip" "$release_link"
else
Expand Down Expand Up @@ -445,7 +500,7 @@ else
Say "Login failed. Please confirm credentials by selecting: 1. Configure Credentials"
fi

read -p "Press Enter to continue..."
_WaitForEnterKey_
}

if [[ -n "$1" && "$1" == "run_now" ]]; then
Expand Down Expand Up @@ -507,7 +562,7 @@ while true; do
4) change_schedule ;;
e) exit ;;
*) echo "Invalid choice. Please try again."
read -p "Press Enter to continue..." # Pauses script until Enter is pressed
_WaitForEnterKey_ # Pauses script until Enter is pressed
;;
esac
done