@echo off
TITLE EPO download tool

REM Download 7 days of EPO data
SET NUMOFDAYS=7

SET EPOFILE=EPO.BIN

REM If script is executed from Garmin device, update EPO.BIN in correct path
IF EXIST "%~d0\Garmin\GPS\*" SET "EPOFILE=%~d0\Garmin\GPS\EPO.BIN"

REM If getEPO is invoked with argument, use first argument as number of days
IF "%1"=="0" SET NUMOFDAYS=1
IF "%1"=="1" SET NUMOFDAYS=1
IF "%1"=="2" SET NUMOFDAYS=2
IF "%1"=="3" SET NUMOFDAYS=3
IF "%1"=="4" SET NUMOFDAYS=4
IF "%1"=="5" SET NUMOFDAYS=5
IF "%1"=="6" SET NUMOFDAYS=6
IF "%1"=="7" SET NUMOFDAYS=7
IF "%1"=="8" SET NUMOFDAYS=8
IF "%1"=="9" SET NUMOFDAYS=9

REM Start of main script
SET /A EPOLIMIT=%NUMOFDAYS%*9216
SET /A EPOEND=%EPOLIMIT%-1

REM Purge old EPO file
copy /b /y NUL "%EPOFILE%" >NUL 2>&1
echo.

REM Try to find cURL download tool
SET DOWNTOOL=curl.exe
IF EXIST "%SystemRoot%\System32\curl.exe" (
 SET "DOWNTOOL=%SystemRoot%\System32\curl.exe"
 goto USECURL
)
IF EXIST "%SystemRoot%\curl.exe" (
 SET "DOWNTOOL=%SystemRoot%\curl.exe"
 goto USECURL
)
IF EXIST "%~dp0curl.exe" (
 SET "DOWNTOOL=%~dp0curl.exe"
 goto USECURL
)
WHERE curl.exe >NUL 2>&1 && goto USECURL

REM Try to find Wget download tool
SET DOWNTOOL=wget.exe
IF EXIST "%SystemRoot%\System32\wget.exe" (
 SET "DOWNTOOL=%SystemRoot%\System32\wget.exe"
 goto USECURL
)
IF EXIST "%SystemRoot%\wget.exe" (
 SET "DOWNTOOL=%SystemRoot%\wget.exe"
 goto USECURL
)
IF EXIST "%~dp0wget.exe" (
 SET "DOWNTOOL=%~dp0wget.exe"
 goto USEWGET
)
WHERE wget.exe >NUL 2>&1 && goto USEWGET

echo Error!
echo.
echo Cannot find cURL or Wget tool which are used for downloading ephmeris data.
echo.
echo Please download:
echo cURL from     https://curl.se/windows/
echo or Wget from  https://eternallybored.org/misc/wget/
goto END

:USEWGET
echo Fetching ephemeris data from MediaTek with Wget tool...
"%DOWNTOOL%" --max-redirect=2 --tries=1 --timeout=10 -qO "%EPOFILE%" "http://epodownload.mediatek.com/EPO.DAT"

if NOT EXIST "%EPOFILE%" (
 echo.
 echo Error!
 echo.
 echo Cannot download EPO data from MediaTek servers!
 goto END
)

SET EPOSIZE=
FOR %%F IN ("%EPOFILE%") DO SET "EPOSIZE=%%~zF"

if NOT "%EPOSIZE%"=="276480" (
 del /q /f "%EPOFILE%" >NUL 2>&1
 echo.
 echo Error!
 echo.
 echo Cannot download EPO data from MediaTek servers!
 goto END
)

SET CHSIZE=chsize32.exe
IF EXIST "%~dp0chsize32.exe" (
 SET "CHSIZE=%~dp0chsize32.exe"
 goto USECHSIZE
)
WHERE chsize32.exe >NUL 2>&1 && goto USECHSIZE
fsutil file seteof "%EPOFILE%" %EPOLIMIT% >NUL 2>&1
goto USEFSUTIL
:USECHSIZE
"%CHSIZE%" "%EPOFILE%" %EPOLIMIT% >NUL 2>&1
:USEFSUTIL

SET EPOSIZE=
FOR %%F IN ("%EPOFILE%") DO SET "EPOSIZE=%%~zF"

if NOT "%EPOSIZE%"=="%EPOLIMIT%" (
 del /q /f "%EPOFILE%" >NUL 2>&1
 echo.
 echo Error!
 echo.
 echo Cannot adjust EPO file size!
 echo Try to download chsize32 tool from: http://www.ltr-data.se/opencode.html/
 goto END
)

echo.
echo DONE!
echo.
echo Updated file "%EPOFILE%" now contains %NUMOFDAYS% days of fresh EPO data.
echo.
goto END

:USECURL
echo Fetching ephemeris data from MediaTek with cURL tool...
"%DOWNTOOL%" -s -o "%EPOFILE%" -r 0-%EPOEND% --max-redirs 2 -m 10 "http://epodownload.mediatek.com/EPO.DAT"

if NOT EXIST "%EPOFILE%" (
 echo.
 echo Error!
 echo.
 echo Cannot download EPO data from MediaTek servers!
 goto END
)

SET EPOSIZE=
FOR %%F IN ("%EPOFILE%") DO SET "EPOSIZE=%%~zF"

if NOT "%EPOSIZE%"=="%EPOLIMIT%" (
 del /q /f "%EPOFILE%" >NUL 2>&1
 echo.
 echo Error!
 echo.
 echo Cannot download EPO data from MediaTek servers!
 goto END
)

echo.
echo DONE!
echo.
echo Updated file "%EPOFILE%" now contains %NUMOFDAYS% days of fresh EPO data.
echo.

:END
SET NUMOFDAYS=
SET EPOLIMIT=
SET EPOEND=
SET EPOFILE=
SET EPOSIZE=
SET DOWNTOOL=
SET CHSIZE=
echo.
ECHO Press any key to continue...
TIMEOUT /T 10 >nul
