@echo off
:start0
if exist change_layout_contourlines_gmapsupp.cmd goto :begin
if exist "%~dp0change_layout_contourlines_gmapsupp.cmd" goto :find_drive


:begin
:precheck
set KEY=HKLM\SOFTWARE\Garmin\MapSource
reg ADD %KEY%\Families\FAMILY_openmtbmap /f 2>NUL >NUL
reg QUERY %KEY%\Families\FAMILY_openmtbmap 2>NUL >NUL
if %errorlevel% EQU 1 echo Error Adding keys to Windows Registry
if %errorlevel% EQU 1 echo This batch needs to be run with Admin rights, because it adds/removes registry entries.
if %errorlevel% EQU 1 echo Under Windows 7 / Vista or newer you will have to right-click the batch and select "Run as administrator"
if %errorlevel% EQU 1 echo or switch of Windows UAC (User Activation Controll) which is not recommended
if %errorlevel% EQU 1 echo.
if %errorlevel% EQU 1 echo Installation not possible because of missing Admin rights.
if %errorlevel% EQU 1 echo Press any key to exit
if %errorlevel% EQU 1 pause >Nul
if %errorlevel% EQU 1 exit
if %errorlevel% EQU 0 reg DELETE HKLM\SOFTWARE\Garmin\MapSource\Families\FAMILY_openmtbmap /f 2>NUL >NUL
reg QUERY HKLM\SOFTWARE\Garmin\MapSource\Families\FAMILY_openmtbmap 2>NUL >NUL
if %errorlevel% EQU 0 echo Error Deleting keys from Windows Registry
if %errorlevel% EQU 0 echo This batch needs to be run with Admin rights, because it adds/removes registry entries.
if %errorlevel% EQU 0 echo Under Windows 7 / Vista  or newer you will have to right-click the batch and select "Run as administrator"
if %errorlevel% EQU 0 echo or switch of Windows UAC (User Activation Controll) which is not recommended.
if %errorlevel% EQU 0 echo.
if %errorlevel% EQU 0 echo Installation not possible because of missing Admin rights.
if %errorlevel% EQU 0 echo Press any key to exit
if %errorlevel% EQU 0 pause >Nul
if %errorlevel% EQU 0 exit
echo -
echo -
echo -  This batch will install map for Mapsource and Basecamp
echo -
echo -  Map:  "openmtbmap_contourlinesonly_austria"
echo -  FID:  9365
echo -  PID:  1 
echo -
echo -  Press any key to continue or Ctrl-C to abort.
echo -
echo -  Note after installing you have to run the uninstall_openmbmtbamp_contourlinesonly_austria.bat
echo -  from this folder first before you can move or delete the mapfiles/folder.
echo -
@echo.
pause >NUL 
@echo.
@echo.


:Select_Typfile
:: List of .TYP files
echo Please Select which .Typfile/Layout you want to Use.
echo 1: Thin     - Contourlines are Thin (1pixel)
echo 2: Medium   - Major Contourlines (1000 or 500m) are a bit wider (2pixel)
echo 3: Thick    - All contourlines are 2 pixel wide
echo 4: Extra Thick - All contourlines are 3 pixel wide
echo 5: Default  - Your GPS device decides how to display the contourlines.
echo.

set input=5
set /p input=Enter your choice (1-5):
set input=%input:~0,1%

if /i "%input%"=="1" set TYPfile=thin.TYP
if /i "%input%"=="2" set TYPfile=mid.TYP
if /i "%input%"=="3" set TYPfile=bold.TYP
if /i "%input%"=="4" set TYPfile=xbol.TYP
if /i "%input%"=="5" set TYPfile=def.TYP
if "%TYPfile%"=="" goto :start

:: check if the chosen .TYP file exists
if not exist %~dp0\%TYPfile% goto :error2



:register1
goto :dec2hex
:register2
reg QUERY HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365 2>NUL >NUL
if %errorlevel% EQU 0 reg DELETE HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365 /f 
reg ADD HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365 /v ID /t REG_BINARY /d %hex2%%hex1% /f 2>NUL >NUL 
reg ADD HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365 /v TYP /t REG_SZ /d "%~dp0%TYPfile%" /f 2>NUL >NUL 
reg ADD HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365\1 /v Loc /t REG_SZ /d "%~dp0\" /f 2>NUL >NUL 
reg ADD HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365\1 /v Bmap /t REG_SZ /d "%~dp0mapsetz.img" /f 2>NUL >NUL 
reg ADD HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365\1 /v Tdb /t REG_SZ /d "%~dp0mapsetz.tdb" /f 2>NUL >NUL 
reg QUERY HKLM\SOFTWARE\Wow6432Node\Garmin\MapSource\Families\FAMILY_9365\1 /v Tdb 2>NUL >NUL 
if 0 EQU 0 echo. 
if 0 EQU 0 echo. 
if 0 EQU 0 echo -------------------------------------------------------- 
if 0 EQU 0 echo. 
if 0 EQU 0 echo ""openmtbmap_contourlines_only_austria"" successfully registered 
if 0 EQU 0 echo. 
if 0 EQU 0 echo Press any key to exit 
if 0 EQU 0 echo. 
if 0 EQU 0 echo -------------------------------------------------------- 
if 0 EQU 1 echo. 
if 0 EQU 1 echo. 
if 0 EQU 1 echo -------------------------------------------------------- 
if 0 EQU 1 echo. 
if 0 EQU 1 echo New installation method failed. Unkown error. Please contact me (extremecarver) for debugging 
if 0 EQU 1 echo. 
if 0 EQU 1 echo Press any key to exit 
if 0 EQU 1 echo. 
if 0 EQU 1 echo -------------------------------------------------------- 
pause >NUL 
exit


:find_drive
cd "%~dp0"
a: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
b: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
c: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
d: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
e: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
f: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
g: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
h: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
i: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
j: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
k: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
l: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
m: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
n: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
o: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
p: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
q: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
r: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
s: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
t: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
u: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
v: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
w: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
x: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
y: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
z: >NUL 2>NUL
if exist change_layout_contourlines_gmapsupp.cmd goto :precheck
@echo You are running this batch with admin rights. 
@echo However not possible to determine from which drive you are running this batch
@echo Please move the whole folder and this batch to drive C:\ D:\ E:\ or F:\ (the subfolder does not matter)
@echo.
@echo.
pause
goto :start0


:dec2hex
@ECHO OFF
:: Enable delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION
SET Hex=
SET Cvt=0123456789ABCDEF
SET /A Tmp0 = 9365

:Loop
:: Get the last (least significant) digit
SET /A Tmp1 = %Tmp0% / 16
SET /A Tmp2 = %Tmp0% - 16 * %Tmp1%
:: Convert that last digit (still decimal) to hexadecimal
SET Tmp2=!Cvt:~%Tmp2%,1!
:: Prepend it to the intermediate hexadecimal result
SET Hex=%Tmp2%%Hex%
:: Remove the last digit from the original number
SET Tmp0=%Tmp1%
:: If there are any digits left, loop once more
IF %Tmp0% GTR 0 GOTO Loop

if 15 GEQ %FID% set Hex=0%Hex%
if 255 GEQ %FID% set Hex=0%Hex%
if 4095 GEQ %FID% set Hex=0%Hex%
set Hex1=%Hex:~0,2%
set Hex2=%Hex:~2,4%
goto :register2

:Loop
:: Get the last (least significant) digit
SET /A Tmp1 = %Tmp0% / 16
SET /A Tmp2 = %Tmp0% - 16 * %Tmp1%
:: Convert that last digit (still decimal) to hexadecimal
SET Tmp2=!Cvt:~%Tmp2%,1!
:: Prepend it to the intermediate hexadecimal result
SET Hex=%Tmp2%%Hex%
:: Remove the last digit from the original number
SET Tmp0=%Tmp1%
:: If there are any digits left, loop once more
IF %Tmp0% GTR 0 GOTO Loop

if 15 GEQ %FID% set Hex=0%Hex%
if 255 GEQ %FID% set Hex=0%Hex%
if 4095 GEQ %FID% set Hex=0%Hex%
set Hex1=%Hex:~0,2%
set Hex2=%Hex:~2,4%
goto :register2


:error2
echo You have removed the typfiles from the contourlines download
echo Cannot install map without typfiles
echo Please use "Mapset Toolkit" or another tool to install the maps on your PC.
echo Sorry I cannot provide anymore support.
echo Press any key to exit 
echo. 
echo Press any key to exit 
echo. 
pause >Nul
exit


