@echo off & setlocal enableextensions & echo.

:: webgens.bat for caes. Revised 12/10/2018. M.J. Dallwitz.
:: Prepares files for uploading a language subfolder.

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

:: Set name of the language subfolder.
if "%~1" equ "" (call :usage & goto abort)

for %%p in (en EN) do if %%p==%1 goto en
for %%p in (fr FR) do if %%p==%1 goto fr
goto usage

:en
set lan=en
goto start

:fr
set lan=fr
goto start

:start
call chkdir_ %lan% || goto abort
cd %lan%

:: Set the URL of the Web folder.
set "webdir=%webdir%%lan%/"
:: Set the local folder from which the dataset will be uploaded.
set "updir=%updir%\%lan%"

call chkdiq_ %updir% || md %updir%
call chkdir_ %updir% || goto abort

set "inf=info"
set "www=www"
call chkdir_ %inf% || goto abort
call chkdir_ %www% || goto abort

set "zpi=intkeyw.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

:: Check presence of Intkey files.
if exist addfil_.tmp del addfil_.tmp > nul
call ..\webadds intkey || goto abort

:: Modify intkey.ink for Web.
call webink_ || goto abort

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

:: Check presence of Web files.
if exist addfil_.tmp del addfil_.tmp > nul
call ..\webadds general || goto abort

call getiny_ "Add descriptions [y]?"
if errorlevel 1 goto no_desc
call addfil_ %inf%\* || goto abort
call addfil_ %www%\* || goto abort

:: Zip Web files.
if exist %zpw% del %zpw% > nul
call zip -Doq %zpw% * %inf%\* %www%\* -i@addfil_.tmp
echo.
echo    Web files (including descriptions) processed.
goto finish

:no_desc
if exist %zpw% del %zpw% > nul
call zip -Doq %zpw% * -i@addfil_.tmp
echo.
echo    Web files (without descriptions) processed.
goto finish

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

:abort
echo.
echo    ***** 'webgens.bat %lan%' failed.
exit /b 1

:finish
echo.
call showfilepathsize_ "%zpw%"
if exist addfil_.tmp del addfil_.tmp > nul
if exist %zpi% del %zpi% > nul
cd /d %updir% || exit /b 1
call unzip -ouq "%zpw%"
del "%zpw%"

exit /b 0

:: -------------------------------------------------------------------
:usage :: Subroutine
echo.
echo Prepares files for uploading a language subfolder.
echo    Usage: WEBGENS language
echo where 'language' is en or fr.
echo.
echo Before running webgens.bat, run Confor and other DELTA programs as
echo necessary to update the relevant files - see runall.bat.
echo.
echo The dataset name and folder names are set in webnam.bat.
echo.
echo The names of files to be uploaded are set in webadds.bat.
echo.
exit /b