11@ echo off
2- setlocal
2+ setlocal enableExtensions enableDelayedExpansion
33
4- rem --- Configuration ---
5- set NAME = APP
6- set " APP_HOME = %HOME_DIR% \. %NAME% "
4+ rem --- Configuration (Non-Overridable) ---
5+ set APP_NAME = APP
6+ set APP_DIR = .APP
77set RELEASE_URL = https://example.com/releases/foo-latest.zip
8- rem ---------------------
8+ rem ---------------------------------------
99
10- rem --- Environment Setup ---
10+ rem 1. Setup essential variables needed for config loading
1111set " HOME_DIR = %USERPROFILE% "
12+ set " APP_HOME = %HOME_DIR% \.%APP_DIR% "
13+
14+ rem 2. Define default overridable variables
15+ rem The update period in days (e.g., 3 means check if the last_checked file is older than 3 days)
16+ set UPDATE_PERIOD = 3
17+
18+ rem --- Configuration Loading ---
19+ rem Helper subroutine to load configuration from a file
20+ :LOAD_CONFIG
21+ set " CONFIG_FILE = %~1 "
22+ if exist " %CONFIG_FILE% " (
23+ call :LOG " Loading configuration from %CONFIG_FILE% ..."
24+ rem /F "tokens=1,2 delims==" splits lines by '='. 'skip=0' is default. 'eol=#' handles comments.
25+ for /f " tokens=1* delims== eol=#" %%a in ('type " %CONFIG_FILE% " ') do (
26+ rem %%a is the key (before '=') and %%b is the value (after '=')
27+ rem Handle specific known variable overrides here
28+ if /I " %%a " == " UPDATE_PERIOD" (
29+ set UPDATE_PERIOD = %%b
30+ )
31+ rem Add other overridable variables here if needed
32+ )
33+ )
34+ goto :eof
35+
36+ rem Load user-wide configuration (if exists)
37+ call :LOAD_CONFIG " %APP_HOME% \bootstrap.cfg"
38+
39+ rem Load local configuration (if exists)
40+ call :LOAD_CONFIG " .\%APP_DIR% \bootstrap.cfg"
41+
42+ rem -----------------------------
43+
44+ rem 3. Define the remaining path variables using the (potentially overridden) NAME/UPDATE_PERIOD
1245set " CACHE_DIR = %APP_HOME% \cache"
1346set " BIN_DIR = %APP_HOME% \bin"
14- set " APP_EXE = %BIN_DIR% \%NAME % .cmd"
47+ set " APP_EXE = %BIN_DIR% \%APP_NAME % .cmd"
1548set " ARCHIVE_FILE = %CACHE_DIR% \release.zip"
1649set " LAST_CHECKED_FILE = %CACHE_DIR% \last_checked"
1750
@@ -59,12 +92,6 @@ rem --- Core Logic Functions ---
5992 )
6093
6194 rem Move contents from the temporary unpack location to the application home
62- rem This requires navigating the directory structure, which is complex in Batch.
63- rem We will assume the archive root needs to be moved into APP_HOME.
64-
65- rem Move everything from temp_install (including the content of any single root folder)
66- rem Note: This assumes the ZIP contains a single root folder or direct files.
67-
6895 rem Use XCOPY to move files/directories
6996 call :LOG " Moving contents to %APP_HOME% "
7097
@@ -83,35 +110,29 @@ rem --- Core Logic Functions ---
83110:HANDLE_UPDATE_CHECK
84111 set " UPDATE_NEEDED = false"
85112
86- rem Check file age: older than 3 days? (or doesn't exist)
113+ rem Check file age: older than %UPDATE_PERIOD% days? (or doesn't exist)
87114 rem forfiles exits with ERRORLEVEL 0 if files matching the criteria are found.
88- rem /D -3 means files older than 3 days ago.
89- forfiles /P " %CACHE_DIR% " /M last_checked /D -3 /C " cmd /c echo Found > nul" 2 > nul
115+ rem /D -N means files older than N days ago.
116+ forfiles /P " %CACHE_DIR% " /M last_checked /D -%UPDATE_PERIOD% /C " cmd /c echo Found > nul" 2 > nul
90117 if errorlevel 1 (
91- rem ERRORLEVEL 1 means no files older than 3 days were found (either file is new, or file doesn't exist)
92- rem We need the inverse logic: We want to update if it DOESN'T exist OR if it IS older.
118+ rem ERRORLEVEL 1 means no files older than %UPDATE_PERIOD% days were found (either file is new, or file doesn't exist)
93119 if not exist " %LAST_CHECKED_FILE% " (
94120 set " UPDATE_NEEDED = true"
95121 ) else (
96- rem Re-check: forfiles /D -N checks for files older than N days.
97- rem If no file is found, the file is either missing or newer than N days.
98- rem The initial check is flawed for the "newer than" case.
99-
100- rem Simpler, more reliable check: find if any file named last_checked *exists*.
101- rem If it exists, use forfiles to check if it's OLDER than 3 days.
102- set " IS_OLD = true"
103- forfiles /P " %CACHE_DIR% " /M last_checked /D -3 /C " cmd /c set IS_OLD=false" 2 > nul
122+ rem Check for files older than %UPDATE_PERIOD% days
123+ set " IS_OLD = false"
124+ forfiles /P " %CACHE_DIR% " /M last_checked /D -%UPDATE_PERIOD% /C " cmd /c set IS_OLD=true" 2 > nul
104125 if " %IS_OLD% " == " true" (
105- call :LOG " Checking for updates (last check older than 3 days or file missing)..."
126+ call :LOG " Checking for updates (last check older than %UPDATE_PERIOD% days or file missing)..."
106127 set " UPDATE_NEEDED = true"
107128 ) else (
108- call :LOG " Skipping update check (last check within 3 days)."
129+ call :LOG " Skipping update check (last check within %UPDATE_PERIOD% days)."
109130 goto :eof
110131 )
111132 )
112133 ) else (
113- rem The file exists and IS older than 3 days, so ERRORLEVEL 0 was returned.
114- call :LOG " Checking for updates (last check older than 3 days)..."
134+ rem The file exists and IS older than %UPDATE_PERIOD% days, so ERRORLEVEL 0 was returned.
135+ call :LOG " Checking for updates (last check older than %UPDATE_PERIOD% days)..."
115136 set " UPDATE_NEEDED = true"
116137 )
117138
@@ -133,8 +154,8 @@ rem --- Core Logic Functions ---
133154 set " NEW_SIZE = 0"
134155 if exist " %ARCHIVE_FILE% " for %%F in (" %ARCHIVE_FILE% " ) do set NEW_SIZE = %%~zF
135156
136- if not " % OLD_SIZE% " == " % NEW_SIZE% " (
137- call :LOG " New release downloaded (size changed: Old=% OLD_SIZE% , New=% NEW_SIZE% ). Unpacking update."
157+ if not " ! OLD_SIZE! " == " ! NEW_SIZE! " (
158+ call :LOG " New release downloaded (size changed: Old=! OLD_SIZE! , New=! NEW_SIZE! ). Unpacking update."
138159 rem 4. Unpack the new release
139160 call :PERFORM_FULL_INSTALL
140161 ) else (
@@ -181,9 +202,9 @@ if /I NOT "%SCRIPT_DIR_CLEAN%" EQU "%BIN_DIR%" (
181202rem If we are running from %BIN_DIR%, the script continues below this line.
182203call :LOG " Running inside the application's environment (%BIN_DIR% )."
183204rem This is the "whatever might be there" section.
184- echo --- Application %NAME % is running ---
205+ echo --- Application %APP_NAME % is running ---
185206echo Arguments received: %*
186207echo ------------------------------------
187208
188209endlocal
189- exit /b 0
210+ exit /b 0
0 commit comments