@ECHO off
cls
  cd "%~dp0\install_0"
  if EXIST F0001 goto 1_newinstall
  if EXIST F0002 goto 2_update
  if EXIST F0003 goto 2_update   
  
  echo "unexpected"
  pause > NUL
  goto end

:1_newinstall

	cd "%~dp0\.."
	if EXIST $b$t goto 12_print_error_overwrite
	
	::Trying to install to a default user folder
	if EXIST Bt0_000.xlsm goto 12_print_error_overwrite

    cd "%~dp0\install_0"
	type 0099.1

	cd "%~dp0\install_0"
	type 0001.1
	robocopy $b$t ..\..\$b$t /E /NFL /NDL /NJH /NJS /NC /NS /NP
	robocopy $bak ..\..\$b$t /E /NFL /NDL /NJH /NJS /NC /NS /NP

	copy Bt0_000.bt ..\Bt0_000.bt
	
	cd "%~dp0
	copy Bt0_000.bt ..\Bt0_000.bt
	Del Bt0_000.bt
	
	cd "%~dp0\..
	rename Bt0_000.bt Bt0_000.xlsm

	cd "%~dp0\..
	copy Bt0_000.xlsm $b$t\$000\$000_0\Bt0_000.xlsm

	cd "%~dp0\..
	md A002
	copy Bt0_000.xlsm A002\Bt0_000.xlsm


    cd "%~dp0\install_0"
	type 0099.2
	pause > NUL	
	goto end
    
	:12_print_error_overwrite
		cd "%~dp0\install_0"
		type 0001.2
		pause > NUL	
		goto end

:2_update

	cd "%~dp0\.."
	if not EXIST $b$t goto 22_print_wrongdirectory

    cd "%~dp0\install_0"
	type 0099.1

	cd "%~dp0\install_0"
	type 0002.1

	::backup log
		cd "%~dp0\..\$b$t\$000"
		copy Bt.log Btbk.log

	cd "%~dp0\install_0"
	robocopy $b$t ..\..\$b$t /E /NFL /NDL /NJH /NJS /NC /NS /NP

	copy Bt0_000.bt ..\Bt0_000.bt
	
	cd "%~dp0
	copy Bt0_000.bt ..\Bt0_000.bt
	Del Bt0_000.bt
	
	cd "%~dp0\..
	
		Del Bt0_000.xlsm
	
	rename Bt0_000.bt Bt0_000.xlsm
	
	cd "%~dp0\..
	copy Bt0_000.xlsm $b$t\$000\$000_0\Bt0_000.xlsm
	
    cd "%~dp0\install_0"
	type 0099.2
	pause > NUL	
	goto end

	goto end

	:22_print_wrongdirectory
	
    cd "%~dp0\install_0"
    type 0002.2
    pause > NUL	
    goto end
	
	
:end