@echo off
echo.
if "%OS%"=="Windows_NT" goto :DIRECT
echo This batch works only on Windows 7 or later.
goto STOP

:DIRECT
SETLOCAL ENABLEEXTENSIONS

set MAPSET=Canada_OSM_Topo
set MAPDEL=Canada_OSM_Topo
if NOT exist "%MAPSET%.gmap" (
	echo. == Unknown mapset, nothing installed.
	goto STOP
)

if DEFINED ProgramData (
 set ALL_DIR=%ProgramData%
 goto WORK
)

set APP_DIR1=%APPDATA%
:LOOP
set APP_DIR2=%APP_DIR1%
for /F "tokens=1,* delims=\" %%i in ("%APP_DIR1%") do set APP_DIR1=%%j
if NOT "%APP_DIR1%" == "" goto LOOP
set ALL_DIR=%ALLUSERSPROFILE%\%APP_DIR2%

:WORK
if exist "%ALL_DIR%\Garmin\Maps" (
	for /D %%I in ("%ALL_DIR%\Garmin\Maps\%MAPDEL%*.gmap") do (
		rd "%%I"
		if not exist "%%I" (
			echo. == %%~nI uninstalled from %ALL_DIR%\Garmin\Maps
			echo.
		)
	)
)
if exist "%APPDATA%\Garmin\Maps" (
	for /D %%I in ("%APPDATA%\Garmin\Maps\%MAPDEL%*.gmap") do (
		rd "%%I"
		if not exist "%%I" (
			echo. == %%~nI uninstalled from %APPDATA%\Garmin\Maps
			echo.
		)
	)
)
if exist "%ALL_DIR%\Garmin\Maps" (
	if exist "%ALL_DIR%\Garmin\Maps\%MAPSET%.gmap" rd "%ALL_DIR%\Garmin\Maps\%MAPSET%.gmap"
	if NOT exist "%ALL_DIR%\Garmin\Maps\%MAPSET%.gmap" (
		mklink /J "%ALL_DIR%\Garmin\Maps\%MAPSET%.gmap" "%~dp0%MAPSET%.gmap"
		if exist "%ALL_DIR%\Garmin\Maps\%MAPSET%.gmap" (
			echo. == %MAPSET% installed in %ALL_DIR%\Garmin\Maps
			echo.
			goto STOP
		)
	)
)

if not exist "%APPDATA%\Garmin\Maps" md "%APPDATA%\Garmin\Maps"
if exist "%APPDATA%\Garmin\Maps" (
	if exist "%APPDATA%\Garmin\Maps\%MAPSET%.gmap" rd "%APPDATA%\Garmin\Maps\%MAPSET%.gmap"
	if NOT exist "%APPDATA%\Garmin\Maps\%MAPSET%.gmap" (
		mklink /J "%APPDATA%\Garmin\Maps\%MAPSET%.gmap" "%~dp0%MAPSET%.gmap"
		if exist "%APPDATA%\Garmin\Maps\%MAPSET%.gmap" (
			echo. == %MAPSET% installed in %APPDATA%\Garmin\Maps
			echo.
			goto STOP
		)
	)
)

echo. == No maps installed.

:STOP
echo.
ENDLOCAL
pause
