File:  [mozdev] / booksync / www / booksync4RC.bat
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Thu Feb 26 17:45:31 2004 UTC (13 years, 9 months ago) by simon
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

@echo off
rem  
rem  S2's bookmark sync, BAT version 4RC
rem  For more information see http://booksync.mozdev.org/
rem 
rem 
rem  --- purpose & differences from alternatives ---
rem  This is a very small and easy bat file for windows (it does 
rem  not require a running scripting host like the JS version).
rem  
rem  It automatically downloads your bookmarks from an ftp
rem  server when you start Mozilla (or any other browser for
rem  that matter), and reups them on the server when you
rem  close your Mozilla session.
rem  
rem  Thus you can use most current bookmarks in different places (e.g. 
rem  at university, work and home) without the need for disks or the like.
rem  
rem  Most other tools for that purpose require some scripts (perl, php,...) 
rem  running on a server - this script only requires an ftp server which you
rem  can easily get for free from ISPs etc. Others aim at merging different 
rem  versions rather than avoiding different versions, e.g. 
rem  http://BookmarkBridge.SourceForge.net
rem 
rem  At some point in the future, Mozilla itself will support roaming profiles
rem  like Netscape4 did (http://bugzilla.mozilla.org/show_bug.cgi?id=124029). 
rem  But this requires a special server as well.
rem 
rem 
rem  --- how to use ---
rem  All you have to do is 
rem  1) edit the variables below this remarks-text
rem 
rem  choose variant a: uses two icons, one for up- and one for downloading
rem  a2) create two shortcuts to the bat file on your desktop, call 
rem      the first "upload", and the second "download".
rem  a3) right click the "upload" shortcut, select "Properties", and 
rem      put "up" at the end of the destination textbox.
rem      ex. if you downloaded booksync.bat to 
rem          C:\Program Files\mozilla.org\Mozilla\boksync.bat, 
rem          the text in the Destination textbox should be 
rem          "C:\Program Files\mozilla.org\Mozilla\boksync.bat" up
rem 
rem  or choose variant b: uses one single icon and stores status (up or down?) to 
rem                       a temporary file so you may close the CMD-window
rem  b2) create one shortcut to the bat file on your desktop, call it "Mozilla"
rem  b3) nothing ;-)
rem 
rem  4) you are done!
rem
rem
rem --- troubleshooting---
rem
rem * If the command-window only closes within milliseconds and you're unable 
rem   to read anything, remove the "rem" of last line of this file
rem * If this BATch is in version b and down- and uploads the bookmarks without
rem   waiting until you closed Moz, Moz was running before (Composer, Mail,...)
rem * Check whether all paths are valid and encpsulated "within quotes"
rem
rem
rem --- changes ---
rem BAT v2 to BAT v4RC by Georg (booksync4@SchoSchi.de)
rem * added variant b which allows to close the command window with requiring 
rem   only one icon
rem * added some hopefully helpful error messages for runtime
rem * added some basic documentation into the BAT itself
rem * includes the enhancements of BAT v3 "20031115 - Update - New booksync 
rem   version by Daniel Yuan-Chih Lin"
rem * includes the proposal of Comment "[8] Submitted by:  Brodie Tuesday May 
rem   20th 2003" concerning cygwin
rem * changed some var names so they won't interfere so easily with other app's 
rem   vars
rem * removed angles brackets for default ftp infos thus avoiding a "syntax 
rem   error" message if user forgot to change the delimerters as well (has been 
rem   confusing for some)
rem * works not only under WinXP like BAT v3
rem * comments [1] until [41] at http://booksync.mozdev.org/ were considered
rem
rem BAT v2 to BAT v3 by Daniel (daniel@twpda.com)
rem * won't copy zero size bookmarks.html when forget upload bookmarks to ftp at
rem   first
rem * auto upload bookmarks to ftp after first run the booksync3
rem * clear unused variable at the batch file end
rem * reduce environment size
rem * BUT requires windows xp ( which has the && syntax and findstr command )
rem
rem BAT v1 to BAT v2 by S2
rem * now the BATch can up/download AND call mozilla in one step 
rem 
rem 
rem --- notes to developers ---
rem * "set locErrorlevel=%errorlevel%" is very fragile. 
rem   * It may not be followed by any charcter, elsewise "%locErrorlevel%"=="0" 
rem     won't work
rem   * Use it _directly_ after the command whose errorlevel you want!
rem   * If the previos command and the set are inside a bracket 
rem     (IF..ELSE..-construct) then the errorlevel will always be zero. 
rem     Reason: The stuff inside the bracket will be converted to commands 
rem     (so vars will be replaced by its values etc) and later be executed - at
rem     this time, locErrorlevel will already be zero, so it gets no information
rem     about the runtime but only "make-cmd-time" of its successing command. 
rem     Simply remove "@echo off" from first line and you'll see what I mean...
rem     Well, how to handle? Just break the bracket in 2 parts like this:
rem       IF "%a%"=="b" (...command) 
rem       set locErrorlevel=%errorlevel%
rem       IF "%a%"=="b" ( continue here, now %locErrorlevel% makes sense ...)
rem * remember, there are a lot of configurations and languages, so some paths 
rem   may contain special characters and/or spaces
rem * avoid normal bracktes () as they may interfere with IfThenElse. Use [ ].


rem ##########################################################
rem  --- editable section [please edit the following vars] ---


rem  -+- ftp info -+-
set ftpserver=your_ftp_server_here
set ftpuser=your_username_here
set ftppass=your_password_here
rem if you want to copy to a subdir put it here, without tailing slash (eg /moz)
rem Elsewise enter only a point . for "local dir"
set ftpbookdir=.
set ftpfile=bookmarks.html


rem  -+- local file info (in which directory sits bookmarks.html?)-+-
set localbookdir=C:\Dokumente und Einstellungen\georg\Anwendungsdaten\Mozilla\Profiles\Default\n9ov4z4k.slt


rem  -+- path to mozilla.exe (including the .exe) -+-
set batmozExecutable=C:\Programme\mozilla.org\Mozilla\mozilla.exe
rem set batmozExecutable=c:\Program Files\Mozilla.org\Firebird0.7\MozillaFirebird.exe


rem  -+- use variant a [two icons] or b [one icon, closable due to temp file]?  
rem      Please use lower case. -+-
set batmozvariant=b


rem  -+- Under WinXP the BAT can catch some errors. Set true if you're running 
rem      Win XP (NOT Win 2000!), elsewise false. -+-
set isWinXPorHigher=false


rem  -+- Behind a firewall, one may need passive ftp. ncftpget/ncftpput from 
rem      cygwin may help. Set true if you want to use Cygwin-tools, else false.
rem      SYNATX UNTESTED AS I DO NOT HAVE THESE TOOLS! -+-
set UseCygwin=false


rem  --- end editable section [please edit the above vars] ---
rem ##########################################################



rem start error checking
if not exist "%localbookdir%\bookmarks.html" (
    rem Given path is somehow bad!
    set message=Please check your setting of "localbookdir" as I cannot find "%localbookdir%\bookmarks.html". Please search "bookmarks.html" and set it's path as "localbookdir". Maybe you need quotes around the string.
    goto error
)
if not exist "%batmozExecutable%" (
    rem Given path is somehow bad!
    set message=Please check your setting of "batmozExecutable" as I cannot find "%batmozExecutable%" so the mozilla executable! Maybe you need quotes around the string.
    goto error
)


if "%batmozvariant%"=="a" (
    goto variant_a
) else (
    if "%batmozvariant%"=="b" (
        goto variant_b
    ) else (
        rem if neither a nor b there was an error, do not start moz, so user 
	rem may have a closer look
        set message=Please check your setting of "batmozvariant" as it is neither a nor b!
        goto error
    )
)
rem end error checking




:variant_a
set batupordown=down
if "%1"=="up" set batupordown=up
goto build_ftp_file


:variant_b
if EXIST ""%temp%\boksynctmp_LastStatusWasup"" (    
    echo Last time I started, I uploaded, so I guess I shall switch to download again...
    set batupordown=down
    del %temp%\boksynctmp_LastStatusWasup    
) ELSE (
    if EXIST ""%temp%\boksynctmp_LastStatusWasdown"" (   
        echo Last time I started, I downloaded, so I guess I shall switch to upload again...
        set batupordown=up
        del %temp%\boksynctmp_LastStatusWasdown
    ) ELSE (
        echo There is no status file, so I have no information. :-(
        echo I, the BATch, guess that I'm started the first time, meaning: make status file, download bookmarks, start moz
        set batupordown=down    
    )
)
rem next 3 lines create the status information file
echo Just the name is important, not the content... > "%temp%\boksynctmp_LastStatusWas%batupordown%"
echo I save information whether Mozilla shall start and down- or upload bookmarks >> "%temp%\boksynctmp_LastStatusWas%batupordown%"
echo I was created by %0 >> "%temp%\boksynctmp_LastStatusWas%batupordown%"

echo.
goto build_ftp_file



:build_ftp_file
echo Creating backup copy of old local file in %localbookdir%
rem Removed tailing "> nul" so user can see whether copying was [not] successful
copy /y "%localbookdir%\bookmarks.html" "%localbookdir%\bookmarks.bak_of_last_local_version_before_%batupordown%.html" 


if "%UseCygwin%"=="true" (
    goto FTPUseCygwin
) else (
    goto FTPUseNormalFTP
)
    

:FTPUseCygwin
rem process only for cygwin
echo.
echo Transferring...

if "%batupordown%"=="down" ncftpget -a -v -F -u %ftpuser% -p %ftppass% %ftpserver% "%temp%\bookmarks.html" "%ftpbookdir%/%ftpfile%"
if "%batupordown%"=="down" set locErrorlevel=%errorlevel%
rem We need this seldom if-construct b/c errorlevel was always be zero if it would be in the same bracket as ftp

if "%batupordown%"=="up"   ncftpput -a -v -F -u %ftpuser% -p %ftppass% %ftpserver% "%ftpbookdir%/%ftpfile%" "%localbookdir%\bookmarks.html"
if "%batupordown%"=="up"   set locErrorlevel=%errorlevel%
rem We need this seldom if-construct b/c errorlevel was always be zero if it would be in the same bracket as ftp
    
goto FTPreunion    


:FTPUseNormalFTP
rem process only for normal ftp-app shipped along with windows

rem echo Creating file containing the ftp commands [%temp%\boksynctmp]
rem Do not add spaces before the greater-then as this produces errors on some servers, especcially for user & pass
echo open %ftpserver%> %temp%\boksynctmp
echo %ftpuser%>> %temp%\boksynctmp
echo %ftppass%>> %temp%\boksynctmp
rem seems as international versions may have troubles with [faster] 7bit only so
rem use binary instead
rem echo type ascii >> %temp%\boksynctmp
echo type binary>> %temp%\boksynctmp
echo cd %ftpbookdir%>> %temp%\boksynctmp
if "%batupordown%"=="down" echo get %ftpfile% "%temp%\bookmarks.html">> %temp%\boksynctmp
if "%batupordown%"=="up" echo send "%localbookdir%\bookmarks.html" %ftpfile%>> %temp%\boksynctmp
echo quit>> %temp%\boksynctmp

echo.
echo Transferring...
ftp -s:%temp%\boksynctmp >nul
set locErrorlevel=%errorlevel%

rem Overwrite (with nonesense so undelete does not reveal ftp settings) + delete
rem file containing the ftp commands (and password!)
echo This file shall be empty now! > %temp%\boksynctmp
del /f /q %temp%\boksynctmp > nul

goto FTPreunion


:FTPreunion
rem This part will be executed by both, cygwin and normal ftp, again

if NOT "%locErrorlevel%"=="0" (
    rem something went wrong
    echo FTP-ERRORLEVEL=%locErrorlevel%!!!
    set message=The ftp-errorlevel was not zero, so something was bad... The FTP-script-file may have been refused.
    goto error
) else (
    echo FTP-errorlevel=%locErrorlevel% - FTP accepted the script :-]
)
rem At the end of the next line has tzo stand "=" and nothing behind it!
set locErrorlevel=


if "%batupordown%"=="up" (
    rem As we do not download, we have no idocation that some error happend. At
    rem least try to ping the server, so we know a connection was possibible.
    ping -n 1 %ftpserver% >nul
)
if "%batupordown%"=="up" set locErrorlevel=%errorlevel%
rem We need this seldom if-construct b/c errorlevel was always be zero if it would be in the same bracket as ping
if "%batupordown%"=="up" (
    if NOT "%locErrorlevel%"=="0" (
        rem something went wrong
        echo PING-ERRORLEVEL=%locErrorlevel%!!!
        set message=I wanted to make sure the server is available but ping didn't work. Was the FTP-script-file accepted? Did FTP-transfer suceed? Please control timestamp on ftp-server, not only the messages above!
        goto error
    ) else (
        echo PING-errorlevel=%locErrorlevel% - PING was ok :-]
    )
)


if "%batupordown%"=="down" (
    if exist "%temp%\bookmarks.html" (    
        rem do this stuff only if we downloaded with success
        if "%isWinXPorHigher%"=="true" (
            rem The following line only works in Win XP, not Win200,as "findstr"
	    rem and "&&" are not yet known It searchs for "bookmark" in the 
            rem bookmarks-file - this way it ensures that the file at least 
            rem _may have_ valid content as the first header is always 
	    rem "bookmarks" (if this doesn't exist the file is invalid for sure)
            echo Moving temporary file to final dir if file is not empty.
            findstr bookmark "%temp%\bookmarks.html" && move /y "%temp%\bookmarks.html" "%localbookdir%\bookmarks.html"
        ) else (
            rem for other OS types use this one
            echo Moving temporary file to final dir.
            move /y "%temp%\bookmarks.html" "%localbookdir%\bookmarks.html"
        )
    ) else (
        rem something went wrong
        set message=I was in download-mode but I can't the file "%temp%\bookmarks.html". Please check the ftp-errorlevel!
        goto error
    )    
)


if "%batmozvariant%"=="b" (
    rem The version a shall not start itself for upload automatically! [even 
    rem though it works perfectly good as long as the BATch isn't closed in the 
    rem meanwhile]
    echo.    
    if "%batupordown%"=="down" (
        echo Startin' Mozilla - you may close this window if you love a clearly arragend desktop :-]
        %batmozExecutable% %2 %3 %4 %5 %6 %7 %8 %9
        echo.
        echo -----------------------------------------------------------------------
        echo.
        echo You closed Mozilla [if not, it was already running], so I [the BAT] calls itself and uploads the bookmarks again
        if "%batupordown%"=="down" call %0 up
    )
    goto endOfBAT
) else (
    goto endOfBAT
)


:error
echo -----------------------------------------------------------------------
echo.
echo There happend an error :-o
echo.
echo %message%
echo.
echo You can change your settings and have a look at the code in BAT-file %0
echo.
echo.
echo Please press any key to continue...
pause >nul
goto endOfBAT

:endOfBAT
rem clear vars
set ftpserver=
set ftpuser=
set ftppass=
set ftpbookdir=
set ftpfile=
set batmozvariant=
set localbookdir=
set batmozExecutable=
set message=
set batupordown=
set isWinXPorHigher=
set UseCygwin=
set locErrorlevel=

rem Remove the next rem [in front of "pause"] if the command-window only closes 
rem within milliseconds and you're unable to read anything
rem pause

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>