@echo off
REM - assumes that the parent directory is also the project name, extracts it
REM - and copies the following files to its root directory:
REM - * _BuildProject.cmd
REM - * _OpenInIDE.cmd
REM - * AutoBuildCheck
REM - also it copies the file template.manifest.in
REM - to its src subdir and names it <project>.manifest.in

setlocal
pushd ..\..

rem Is there a __SetProjectName.cmd which supposedly sets %project%?
if exist __SetProjectName.cmd call __SetProjectName.cmd
if not "%project%"=="" goto projgiven
rem Still no project, use the parent directory name to get it
call :GetLastDir3 %0
set project=%result%

:projgiven

@echo on
copy buildtools\templates\_BuildProject.cmd .
copy buildtools\templates\_OpenInIde.cmd
copy buildtools\templates\_AutoBuildCheck.cmd .
copy .\buildtools\templates\template.manifest.in .\src\%project%.manifest.in
@echo off

if exist .\src\%project%_icon.rc goto icoexist
@echo on
copy .\buildtools\templates\template_icon.rc .\src\%project%_icon.rc
@echo off
:icoexist

if exist .\src\%project%_version.ini goto verexist
@echo on
copy .\buildtools\templates\template_version.ini .\src\%project%_version.ini
@echo off
:verexist

pause
start notepad .\src\%project%_version.ini
popd
endlocal

goto :eof

:GetLastDir3
set directory=%1%

call :ExtractDir %directory%
set directory=%result%

call :ExtractDir %directory%
set directory=%result%

call :ExtractDir %directory%
set directory=%result%

rem extract "filename" (= last directory of path)
call :LastItem %directory%
goto :eof

:ExtractDir
rem extract path
set result=%~p1%
rem remove backslash (=last character)
set result=%result:~0,-1%
goto :eof

:LastItem
set result=%~n1%
goto :eof
