Index: update.cmd
===================================================================
--- update.cmd	(revision 25128)
+++ update.cmd	(working copy)
@@ -10,24 +10,28 @@
 Title Freenet Update Over HTTP Script
 echo -----
 echo - Freenet Windows update script 1.6 by Zero3Cool (zero3cool@zerosplayground.dk)
-echo - Freenet Windows update script 1.7 to 2.4 by Juiceman (juiceman69@gmail.com)
+echo - Freenet Windows update script 1.7-2.4,2.6-2.7 by Juiceman (juiceman69@gmail.com)
 echo - Thanks to search4answers, Michael Schierl and toad for help and feedback.
-echo - This script will automatically update your Freenet installation.
+echo -----
+echo - This script will automatically update your Freenet installation
+echo - from our web server freenetproject.org and/or mirrors.
 echo - In case of an unrecoverable error, this script will pause.
 echo -----
 echo -----------------------------------------------------------
 echo - Please try to use the update over Freenet feature of your
 echo - node to reduce traffic on our servers, thanks!!!
-echo - FYI, updating over Freenet is more secure and better for
-echo - your anonymity.
+echo - FYI, updating over Freenet is easy, more secure and
+echo - is better for your anonymity.
 echo -----------------------------------------------------------
 echo -----
 
-
 ::CHANGELOG:
+:: 2.7 - Better error handling
+:: 2.6 - Prepare for new binary start and stop.exe's
+:: ---   Many various changes
 :: 2.4 - Test downloaded jar after making sure it is not empty.  Copy over freenet.jar after testing for integrity.
 :: 2.3 - Reduce retries to 5.  Turn on file resuming.  Clarify text.
-:: 2.2 - Reduce retry delay and time between retries. 
+:: 2.2 - Reduce retry delay and time between retries.
 :: 2.1 - Title, comments, hide "Please ignore, it is a side effect of a work-around" echo unless its needed.
 :: 2.0 - Warn user not to abort script.
 :: 1.9 - Cosmetic fixes (Spacing, typos)
@@ -38,9 +42,12 @@
 set MAGICSTRING=INDO
 set CAFILE=startssl.pem
 set RESTART=0
+set MAINUPDATED=0
 set PATH=%SYSTEMROOT%\System32\;%PATH%
 set RELEASE=stable
 if "%1"=="testing" set RELEASE=testing
+if "%1"=="-testing" set RELEASE=testing
+if "%1"=="/testing" set RELEASE=testing
 
 ::Go to our location
 for %%I in (%0) do set LOCATION=%%~dpI
@@ -50,7 +57,7 @@
 if not exist freenet.ini goto error2
 if not exist bin\wget.exe goto error2
 
-if not exist wrapper.conf.bak copy wrapper.conf wrapper.conf.bak > NUL
+::if not exist wrapper.conf.bak copy wrapper.conf wrapper.conf.bak > NUL
 
 ::Get the filename and skip straight to the Freenet update if this is a new updater
 for %%I in (%0) do set FILENAME=%%~nxI
@@ -58,7 +65,7 @@
 
 ::Download latest updater and verify it
 if exist update.new.cmd del update.new.cmd
-echo - Checking for newer version of update script...
+echo - Checking for newer version of this update script...
 bin\wget.exe -o NUL -c --timeout=5 --tries=5 --waitretry=10 http://downloads.freenetproject.org/alpha/update/update.cmd -O update.new.cmd
 Title Freenet Update Over HTTP Script
 
@@ -74,9 +81,12 @@
 
 :: fix #1527
 find "freenet-ext.jar.new" wrapper.conf > NUL
-if not errorlevel 1 goto skipit
+if errorlevel 1 goto skipit
 copy freenet-ext.jar.new freenet-ext.jar > NUL
-del /F freenet-ext.jar.new
+:: Try to delete the file.  If the node is running, it will likely fail.
+if exist freenet-ext.jar.new del /F freenet-ext.jar.new > NUL
+:: Fix the wrapper.conf
+goto error5
 :skipit
 
 ::Check if updater has been updated
@@ -84,23 +94,16 @@
 if not errorlevel 1 goto updaterok
 
 ::It has! Run new version and end self
-echo - Update script updated, restarting update script...
+echo - New update script found, restarting update script...
 echo -----
 start update.new.cmd
 goto veryend
 
-
 ::Updater is up to date, check Freenet
 :updaterok
-echo - Update script is current.
+echo    - Update script is current.
 echo -----
 
-::Handle older installations where start and stop are not in the bin\ directory
-if not exist bin\stop.cmd copy stop.cmd bin\stop.cmd > NUL
-if not exist bin\start.cmd copy start.cmd bin\start.cmd > NUL
-if not exist bin\stop.cmd goto error2
-if not exist bin\start.cmd goto error2
-
 echo - Freenet installation found at %LOCATION%
 echo -----
 echo - Checking for Freenet JAR updates...
@@ -114,81 +117,85 @@
 bin\wget.exe -o NUL -c --timeout=5 --tries=5 --waitretry=10 http://downloads.freenetproject.org/alpha/freenet-%RELEASE%-latest.jar.url -O freenet-%RELEASE%-latest.jar.new.url
 Title Freenet Update Over HTTP Script
 
-echo Fetched new main jar
-
 if not exist freenet-%RELEASE%-latest.jar.new.url goto error3
 FOR %%I IN ("%LOCATION%freenet-%RELEASE%-latest.jar.url") DO if %%~zI==0 goto error3
 
 ::Do we have something old to compare with? If not, update right away
-if not exist freenet-%RELEASE%-latest.jar.url goto update1
+if not exist freenet-%RELEASE%-latest.jar.url goto mainyes
 
 ::Compare with current copy
 fc freenet-%RELEASE%-latest.jar.url freenet-%RELEASE%-latest.jar.new.url > NUL
-if not errorlevel 1 goto checkext
-goto update0
+if errorlevel 1 goto mainyes
+echo    - Main jar is current.
+goto checkext
 
+:mainyes
+:: Handle loop if there is no old URL to compare to.
+if not exist freenet-%RELEASE%-latest.jar.url copy freenet-%RELEASE%-latest.jar.new.url freenet-%RELEASE%-latest.jar.url > NUL
+echo    - New main jar found!
+set MAINUPDATED=1
+
 :checkext
-::Main jar not updated.
-echo - Main jar not updated
 echo - Checking ext jar
 ::Check for a new freenet-ext.jar.
 ::Unfortunately there is no .url file for it, so we have to download the whole thing.
+:: FIXME can we use the .sha1 file to check for changes?  I think it would work.
 if exist freenet-ext.jar.copy del freenet-ext.jar.copy
 bin\wget.exe -o NUL -c --timeout=5 --tries=5 --waitretry=10 http://downloads.freenetproject.org/alpha/freenet-ext.jar -O freenet-ext.jar.copy
 if not exist freenet-ext.jar.copy goto error3
 FOR %%I IN ("%LOCATION%freenet-ext.jar.copy") DO if %%~zI==0 goto error3
 ::Update anyway if doesn't exist...
-if not exist freenet-ext.jar goto update1
+if not exist freenet-ext.jar goto extyes
+::Compare with current copy
 fc freenet-ext.jar freenet-ext.jar.copy > NUL
-if errorlevel 1 goto update1
-del freenet-ext.jar.copy
+if errorlevel 1 goto extyes
+echo    - ext jar is current.
+if exist freenet-ext.jar.copy del freenet-ext.jar.copy
+::Check if we had flagged the main jar as updated and if so we still need to update
+if %MAINUPDATED%==1 goto update1
 goto noupdate
 
-:update0
-echo - Main jar updated
-echo - Checking ext jar as well
-::Check for a new freenet-ext.jar.
-::Unfortunately there is no .url file for it, so we have to download the whole thing.
-if exist freenet-ext.jar.copy del freenet-ext.jar.copy
-bin\wget.exe -o NUL -c --timeout=5 --tries=5 --waitretry=10 http://downloads.freenetproject.org/alpha/freenet-ext.jar -O freenet-ext.jar.copy
-if not exist freenet-ext.jar.copy goto error3
-FOR %%I IN ("%LOCATION%freenet-ext.jar.copy") DO if %%~zI==0 goto error3
-if not exist freenet-ext.jar goto update1
-fc freenet-ext.jar freenet-ext.jar.copy > NUL
-if errorlevel 1 goto update1
-del freenet-ext.jar.copy
+:extyes
+echo    - New ext jar found!
 
 ::New version found, check if the node is currently running
 :update1
-echo - New version found!
 echo -----
-
+echo - New Freenet version found!  Installing now...
+echo -----
+:: FIXME  we still need to pull dynamic service name from file the new installer is going to create.
 net start | find "Freenet 0.7 darknet" > NUL
 if errorlevel 1 goto update2 > NUL
 set RESTART=1
+
+::See if we are using the new binary stop.exe
+if not exist bin\stop.exe goto oldstopper
+call bin\stop.exe > NUL
+::  FIXME   do we need a new error handling section for the new .exe?  Will it handle errors itself?
+goto update2
+
+:oldstopper
 ::Tell the user not to abort script, it gets very messy.
 echo - Shutting down Freenet...   (This may take a moment, please don't abort)
 call bin\stop.cmd > NUL
 net start | find "Freenet 0.7 darknet" > NUL
-if errorlevel 1 goto update2 > NUL
+if errorlevel 1 goto update2
 :: Uh oh, this may take a few tries.  Better tell the user not to panic.
 echo -
-echo - If you see an error message about: 
+echo - If you see an error message about:
 echo - "The service could not be controlled in its present state."
-echo - Please ignore, it is a side effect of a work-around 
+echo - Please ignore, it is a side effect of a work-around
 echo - to make sure the node is stopped before we copy files.
 echo -
 ::Keep trying until service is stopped for sure.
 :safetycheck
 net start | find "Freenet 0.7 darknet" > NUL
-if errorlevel 1 goto update2 > NUL
+if errorlevel 1 goto update2
 call bin\stop.cmd > NUL
 goto safetycheck
 
-
 ::Ok Freenet is stopped, it is safe to copy files.
 :update2
-echo -----
 echo - Downloading new version and updating local installation...
 
 ::Backup last version of Freenet-%RELEASE%-latest.jar file, we will need it if update fails.
@@ -219,25 +226,21 @@
 if exist freenet-ext.jar ren freenet-ext.jar freenet-ext.jar.bak
 ren freenet-ext.jar.copy freenet-ext.jar
 java -cp lib\sha1test.jar Sha1Test freenet-ext.jar . %CAFILE% > NUL
-if not errorlevel 0 goto errore4
-ren freenet-ext.jar.copy freenet-ext.jar
+if not errorlevel 0 goto errorext4
 echo Copied updated freenet-ext.jar
-
 goto end
 
-
 ::No update needed
 :noupdate
+echo -----
 echo - Freenet is up to date.
 goto end
 
-
 ::Server gave us a damaged version of the update script, tell user to try again later.
 :error1
 echo - Error! Downloaded update script is invalid. Try again later.
 goto end
 
-
 ::Can't find Freenet installation
 :error2
 echo - Error! Please run this script from a working Freenet installation.
@@ -247,7 +250,7 @@
 
 ::Server may be down.
 :error3
-@del /F bin\netuser.exe
+@del /F bin\netuser.exe > NUL
 echo - Error! Could not download latest Freenet update information. Try again later.
 goto end
 
@@ -259,8 +262,8 @@
 if exist freenet-%RELEASE%-latest.jar.url del freenet-%RELEASE%-latest.jar.url
 goto end
 
-:Corrupt ext jar was downloaded, restore from backup
-:errore4
+::Corrupt ext jar was downloaded, restore from backup
+:errorext4
 echo Error! freenet-ext.jar update failed, trying to restore backup...
 if exist freenet-ext.jar del freenet-ext.jar
 if exist freenet-ext.jar.bak ren freenet-ext.jar.bak freenet-ext.jar
@@ -269,13 +272,26 @@
 ::Wrapper.conf is old, downloading new version and restarting update script
 :error5
 echo - Your wrapper.conf needs to be updated .... updating it ; please restart the script when done.
-if exist wrapper.conf.bak del wrapper.conf.bak
+:: Let's try falling back to the old version of the wrapper so we can keep our memory settings.  If it doesn't work we'll get a new one next time around.
+if not exist wrapper.conf.bak goto newwrapper
+if exist wrapper.conf del wrapper.conf
+ren wrapper.conf.bak wrapper.conf
+start update.new.cmd
+goto veryend
+
+:newwrapper
 if exist wrapper.conf ren wrapper.conf wrapper.conf.bak
+:: This will set the memory settings back to default, but it can't be helped.
 bin\wget.exe -o NUL -c --timeout=5 --tries=5 --waitretry=10 http://downloads.freenetproject.org/alpha/update/wrapper.conf -O wrapper.conf
+if not exist wrapper.conf goto wrappererror
 if exist wrapper.password type wrapper.password >> wrapper.conf
 start update.new.cmd
 goto veryend
 
+:wrappererror
+if exist wrapper.conf.bak ren wrapper.conf.bak wrapper.conf
+goto error3
+
 ::Cleanup and restart if needed.
 :end
 echo -----
@@ -284,27 +300,30 @@
 if exist freenet-%RELEASE%-latest.jar.bak del freenet-%RELEASE%-latest.jar.bak
 
 :: Maybe fix bug #2556
-echo   - Changing file permissions
+echo    - Changing file permissions
 echo Y| cacls . /E /T /C /G freenet:f 2> NUL > NUL
 
 if %RESTART%==0 goto cleanup2
 echo - Restarting Freenet...
+::See if we are using the new binary start.exe
+if not exist bin\start.exe goto oldstarter
+call bin\start.exe > NUL
+goto cleanup2
+
+:oldstarter
 call bin\start.cmd > NUL
 
-
 :cleanup2
 if %FILENAME%==update.new.cmd goto newend
 if exist update.new.cmd del update.new.cmd
 echo -----
 goto veryend
 
-
 ::If this session was launched by an old updater, replace it now (and force exit, or we will leave a command prompt open)
 :newend
 copy /Y update.new.cmd update.cmd > NUL
 echo -----
 exit
 
-
 :veryend
 ::FREENET WINDOWS UPDATE SCRIPT
