@echo off
title FS9 "UHWW_pht" scenery installer
color 0A
call :nlm

:find_fs
for /f "tokens=3*" %%a In ('Reg query "HKLM\SOFTWARE\Microsoft\Microsoft Games\flight simulator\9.0" ^| Find /i "EXE Path"') do (
set fs="%%b"
goto ch_fs
)
goto fs_error

:set_fs
echo %\n%^ Please, input FS9 path like Disk:\path\FS2004 and press enter:%\n%^
 You can copy path, right click mouse and paste here:%\n%^ 
set /p fs= 
set fs="%fs%"

:ch_fs
if not exist %fs%\FS9.exe call :fs_error
set fs=%fs:\"="%

:ch_cfg
if not exist %fs%\Scenery.CFG goto cfg_error

:add_cfg
set scn=%~dp0
setlocal enabledelayedexpansion
set scn=!scn:%fs:~1,-1%\=!
setlocal disabledelayedexpansion
set scn=%scn:~,-1%

for /f "skip=2 tokens=*" %%a in ('find /i "Local=%scn%" %fs%\Scenery.CFG') do if "%%a"=="Local=%scn%" goto end
copy %fs%\Scenery.CFG %fs%\NEWScenery.CFG

set maxcount=38
for /f "skip=2 tokens=*" %%a in ('find /i "Area." %fs%\NewScenery.CFG') do (
set count=%%a
call :count
)
set /a area=%maxcount%+1

set maxcount=38
for /f "skip=2 tokens=*" %%a in ('find /i "Layer=" %fs%\NewScenery.CFG') do (
set count=%%a
call :count
)
set /a layer=%maxcount%+1

if %area% LEQ 99 set area=0%area%

echo.>>%fs%\NewScenery.CFG
echo [Area.%area%]>>%fs%\NewScenery.CFG
echo Title=UHWW_pht>>%fs%\NewScenery.CFG
echo Local=%scn%>>%fs%\NewScenery.CFG
echo Active=TRUE>>%fs%\NewScenery.CFG
echo Required=FALSE>>%fs%\NewScenery.CFG
echo Layer=%layer%>>%fs%\NewScenery.CFG

:end
exit

:count
set count=%count:~6%
set count=%count:]=%
set count=%count:-=%
:killzero
if %count:~,1% EQU 0 (
set count=%count:~1%
goto killzero
)
if %count% GTR 2147483646 goto cfg_error
if %count% GTR %maxcount% (
set maxcount=%count%
)
exit /b

:nlm
set nlm=^


set \n=^^^%nlm%%nlm%^%nlm%%nlm%
exit /b

:yes_no
echo.
set /p ans= Input "Y" if yes or "N" if no: 
if "%ans%"=="y" set ans=Y
if "%ans%"=="Y" exit /b
if "%ans%"=="n" set ans=N
if "%ans%"=="N" exit /b
goto yes_no

:pause
echo %\n%^ Press any key to continue . . .
pause >nul
exit /b

:fs_error
echo %\n%^ FS9 not found!%\n%^
 Do you want to set FS9 path?
call :yes_no
if "%ans%"=="Y" goto set_fs
if "%ans%"=="N" goto end

:cfg_error
echo %\n%^ Can't add scenery to FS9!%\n%^
 You will need to do this manually in FS9 Scenery Library.%\n%^
 Press any key for exit . . .
pause >nul
goto end