@echo off & setlocal enableextensions

:: webgens.bat for citeswood language subfolder. Revised 17/2/2019. M.J. Dallwitz.

if "%1" equ "/?" (
   echo.
   echo Generates files for uploading the DELTA Web package.
   echo Set the dataset name and folder names in this file.
   echo The names of files to be uploaded should be in webadds.bat.
   exit /b 0
   )

:: Set an abbreviation for name of the dataset. Must not include blanks.
set "dataname=citeswood"

:: Set name of the language subfolder.
if '%1' neq '' goto getpar

:usage
echo Generates files for uploading the DELTA Web package.
echo Usage: WEBGENS language
echo where 'language' is en, de, fr, or es.
exit /b 1

:getpar
for %%p in (de DE) do if %%p==%1 goto de
for %%p in (en EN) do if %%p==%1 goto en
for %%p in (es ES) do if %%p==%1 goto es
for %%p in (fr FR) do if %%p==%1 goto fr
goto usage

:de
set lan=de
goto start

:en
set lan=en
goto start

:es
set lan=es
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=https://www.delta-intkey.com/%dataname%/%lan%/"
:: Set the local folder from which the dataset will be uploaded.
call chkdir_ %up% || goto abort
set "updir=%up%\%dataname%\%lan%"

echo.
call chkdiq_ %updir% || md %updir%
call chkdir_ %updir% || goto abort
set "w=www"
set "i=info"
call chkdir_ %w% || goto abort
call chkdir_ %i% || goto abort

set "zpd=%dataname%data.zip"
set "zpi=intkeyw.zip"
set "zpw=%cd%\%dataname%.zip"

call lcase

:: 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
zip -Doq %zpi% * -i@addfil_.tmp
if exist *.bak del *.bak > nul
echo Intkey files processed.

:: (Data zipfile is generated by webgenm.bat.)

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

echo.
call getiny_ "Add descriptions [y]?"
if errorlevel 1 goto no_desc
call addfil_ %w%\* || goto abort
call addfil_ %i%\* || goto abort

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

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

goto finish

: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 %zpd% del %zpd% > nul
if exist %zpi% del %zpi% > nul

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

exit /b 0
