@Echo off
@setlocal enableDelayedExpansion
@set "endParen=)"
@set "saveType=Excel Workbook (*.xlsx)"
REM @set "saveType=Text (Tab Delimited%endParen% (*.txt)"
for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value') do set datetime=%%G
set yyyy=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set MAPDIR=%CD%
set jarfile=%MAPDIR%\mulchmaps.jar
set infile=%MAPDIR%\%yyyy% mulch.xlsx
set outdirName=maps%yyyy%
set outdir=%CD%\%outdirName%

if exist "%infile%" (
    echo Creating maps from %infile%
) else (
    echo ERROR: %infile% does not exist.
    echo "%infile% must be:"
    rem echo "  - a SORTED by Map - Stop - Customer"
    rem echo "  --- AND --- "
    echo "  - a %saveType% save of the Mulch orders "%yyyy% mulch.xlsx" excel spreadsheet"
    echo "  - the first sheet defined in the workbook will be used for map creation, regardless of the name"
    rem echo "Use Excel Save As function and choose %saveType% as the "Save as type" format to"
    rem echo "create %infile%"
    goto exitLabel
)

if exist "%outdir%" (
    echo "%outdir" exists - attempting to rename to "%outdirName%.bu"
    if exist "%outdir%.bu" (
        echo rmdir /S /Q "%outdir%.bu"
        rmdir /S /Q "%outdir%.bu"
    )
    echo ren "%outdir%" "%outdirName%.bu"
    ren "%outdir%" "%outdirName%.bu"
)
if not exist "%outdir%" (
   echo mkdir "%outdir%"
   mkdir "%outdir%"
   if exist "%MAPDIR%\mapOutputBase" (
       robocopy /E /NFL /NJS  "%MAPDIR%\mapOutputBase" "%outdir%"
   )
) else (
   echo ERROR: "%outdir%" still exists after attempted rename - ABORTING RUN
    goto exitLabel
)

set mainClass=MapitWithBlack
echo dir "%jarfile%"
dir "%jarfile%"
echo dir "%infile%"
dir "%infile%"
echo java -cp "%MAPDIR%\mulchmaps.jar" %mainClass% "%infile%" "%outdir%"
java -cp "%MAPDIR%\mulchmaps.jar" %mainClass% "%infile%" "%outdir%"

cd "%MAPDIR%
if exist "%outdir%.zip" (
    echo "%outdir.zip" exists - attempting to rename to "%outdirName%.zip.bu"
    if exist "%outdir%.zip.bu" (
        echo del /S /F /Q "%outdir%.zip.bu"
        del /S /F /Q "%outdir%.zip.bu"
    )
    echo ren "%outdir%.zip" "%outdirName%.zip.bu"
    ren "%outdir%.zip" "%outdirName%.zip.bu"
)

7z a -r %outdir%.zip %outdirName%

echo after checking the maps at %outdir%\index.html
echo You can upload %outdir%.zip to parkviewscouting.org/public_html
echo Extract it
echo Rename existing maps directory to maps{lastYear}
echo rename %outdirName% maps
echo Once complete, http://parkviewsouting.org/maps should bring up these maps.

:exitLabel
REM Done