@echo off & setlocal enableextensions & echo.

:: webgenm.bat for caes. Revised 21/7/2019. M.J. Dallwitz.
:: Prepares files for uploading main folder.

if "%~1" equ "/?" (call :usage & exit /b)

:: Set folder and file names.
::    'dataname' = name of dataset.
::    'webdir' = URL of Web folder (used by webink_.bat).
::    'updir' = local folder from which the dataset will be uploaded.
if not exist webnam.bat goto no_webnam
call webnam || goto abort

call chkdiq_ %updir%
if errorlevel 2 goto abort
if errorlevel 1 md %updir%
call chkdir_ %updir% || goto abort

set "img=images"
set "zpd=%dataname%data.zip"
set "zpw=%cd%\%dataname%.zip"

call lcase

:: Set variable 'webdataexc' containing the name of the exclusion file
:: for generating the zipfile for the DELTA-format data files.
call webdataexc_ || goto abort

:: Generate image-links file.
set "img=images"
call chkdir_ %img% || goto abort
if "%1" neq "" goto checkdata
call webimg_ || goto abort

:: Check and zip files in main folder.
:checkdata
if exist addfil_.tmp del addfil_.tmp > nul
call webaddm data || goto abort
if exist %zpd% del %zpd% > nul
call zip -Doq %zpd% * -i@addfil_.tmp -x@%webdataexc%

:: Check and zip files in language folders.
if exist addfil_.tmp del addfil_.tmp > nul
set lan=en
call webadds data || goto abort
call zip -Doq %zpd% %lan%\* %lan%\info\* %lan%\www\* -i@addfil_.tmp -x@%webdataexc%

if exist addfil_.tmp del addfil_.tmp > nul
set lan=fr
call webadds data || goto abort
call zip -Doq %zpd% %lan%\* %lan%\info\* %lan%\www\* -i@addfil_.tmp -x@%webdataexc%

echo    Data files processed.

:: Check presence of Web files.
if exist addfil_.tmp del addfil_.tmp > nul
call webaddm general || goto abort
if exist %zpd% call addfil_ %zpd% || goto abort

:: Zip Web files.
if exist %zpw% del %zpw% > nul
call zip -Doq %zpw% * -i@addfil_.tmp
echo    Web files processed.

goto finish

:no_webnam
echo    'webgenm.bat' - 'webnam.bat' not found.

:abort
echo.
echo    ***** 'webgenm.bat' failed.
exit /b 1

:finish
echo.
call showfilepathsize_ "%zpw%"
if exist addfil_.tmp del addfil_.tmp > nul
if exist %zpd% del %zpd% > nul

cd /d %updir% || exit /b 1
call unzip -ouq "%zpw%"
sleep 1 /quiet
del "%zpw%"

exit /b 0

:: -------------------------------------------------------------------
:usage :: Subroutine
echo.
echo Prepares files for uploading the main folder
echo The dataset name and folder names are set in webnam.bat.
echo The names of files to be uploaded are set in webaddm.bat.
echo If there is a parameter, generating of images.htm is skipped.
echo.
exit /b