@echo off
setlocal

REM Adding Wix to the path.
set PATH=%PATH;%@EXECSTR[cd]\wix
set OLD_PATH=%PATH
set OLD_EIFFEL_SRC=%EIFFEL_SRC
set OLD_ISE_LIBRARY=%ISE_LIBRARY
set OLD_ISE_SRC=%ISE_SRC
set INIT_DIR=%@EXECSTR[cd]
set TMP_ISE_EIFFEL=%ISE_EIFFEL

call %~dp0inc\set_aliases.btm

REM Create parent dir of %INSTALL_LOG if it does not exist.
set_parent_dir_of PDIR %INSTALL_LOG%
iff not isdir %PDIR then
	md /S %PDIR
	remtrace Create directory %PDIR
endiff

remtrace Set up the environment

on break goto broken
TIMER ON /1 >> NUL
TIMER ON /2 >> NUL
iff %@INDEX[%@LOWER[%$],h] ge 0 .or. %@INDEX[%@LOWER[%$],help] ge 0 then
	echo Installation for ISE Eiffel .
	echo Parameters are 
	echo   - no_compile: to discard any compilation
	echo   - no_new_ec: to avoid compiling a new version of EiffelStudio
	echo   - enterprise: to build only the enterprise version of EiffelStudio
	echo   - no_wizards: to discard wizards building
	echo   - no_install: to generare only the delivery (but not the installation executable) 
	echo   (or nothing to fully regenerate the installation).
	CANCEL
endiff
iff %@INDEX[%@LOWER[%$],no_compil] ge 0 then
	set NO_COMPILE="True"
else
	unset /Q NO_COMPILE
endiff
iff %@INDEX[%@LOWER[%$],no_install] ge 0 then
	set NO_INSTALL="True"
else
	unset /Q NO_INSTALL
endiff
iff %@INDEX[%@LOWER[%$],no_new_ec] ge 0 then
	set NO_NEW_EC="True"
else
	unset /Q NO_NEW_EC
endiff
iff %@INDEX[%@LOWER[%$],enterprise] ge 0 then
	set BUILD_ENTERPRISE_ONLY="True"
else
	unset /Q BUILD_ENTERPRISE_ONLY
endiff
iff %@INDEX[%@LOWER[%$],no_wizard] ge 0 then
	set NO_WIZARDS="True"
else
	unset /Q NO_WIZARDS
endiff

set EIFFEL_SRC=%TMP_EIFFEL_SRC
set ISE_LIBRARY=%TMP_EIFFEL_SRC
set ISE_SRC=%TMP_EIFFEL_SRC

cdd %INIT_DIR

REM This is not a comment, it should create a 0-sized log file.
REM > %INSTALL_LOG

remtrace Approximative starting time:
remtrace %_TIME

call inc\check_environment.btm
call inc\display_environment.btm
call inc\get_environment_confirmation.btm
if %ERRORLEVEL% == 0 CANCEL

rem remtrace --------------------------------
rem remtrace Installation parameters are:
rem remtrace  - ISE_EIFFEL=%ISE_EIFFEL
rem remtrace  - ISE_PLATFORM=%ISE_PLATFORM
rem remtrace  - INSTALL_DIR=%INSTALL_DIR 
rem remtrace  - STUDIO_DIR=%STUDIO_DIR
rem remtrace --------------------------------

cdd %INIT_DIR

give_time_to warm up
remtrace Retrieve and organize the delivery
remtrace Remove %INSTALL_DIR
if isdir %INSTALL_DIR nolog_fullrd %INSTALL_DIR 
if exist %INSTALL_DIR nolog_fullrf %INSTALL_DIR 
iff exist %INSTALL_DIR then
	remtrace Cannot delete %INSTALL_DIR!
	CANCEL
endiff
md %INSTALL_DIR
md %STUDIO_DIR
cdd %STUDIO_DIR
remtrace studio
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/studio studio
fullrd studio\help\defaults\unix
fullrd studio\spec\unix
cdd %STUDIO_DIR\studio\spec
move windows\compile_library.bat %ISE_PLATFORM\bin\
move windows\mkdir.bat %ISE_PLATFORM\bin\
iff "windows"=="%ISE_PLATFORM" then
	fullrd win64
else
	fullrd windows
endiff
cdd %STUDIO_DIR\studio\config
if not "windows"=="%ISE_PLATFORM" ren windows %ISE_PLATFORM
cdd %STUDIO_DIR\studio\config\%ISE_PLATFORM%\
xcopy msc msc_vc140 /E /I

safe_md %STUDIO_DIR\tools
cdd %STUDIO_DIR\tools
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/tools/iron/delivery/iron iron
cdd %STUDIO_DIR\tools\iron\spec
if not "windows"=="%ISE_PLATFORM" ren windows %ISE_PLATFORM
fullrd unix

cdd %STUDIO_DIR\studio\lang
fullrd po_files
fullrd script
cdd %STUDIO_DIR
remtrace esbuilder
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/esbuilder esbuilder
remtrace vision2 tour
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/vision2_demo vision2_demo
remtrace examples
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/examples examples
cd examples
fullrd obsolete
fullrd eiffeltest
fullrd iphone
fullrd libevent
fullrd math
fullrd matisse
fullrd store\matisse
fullrd old_com
fullrd old_matisse
fullrd vision
fullrd build
cd dotnet
fullrd envision
fullrd winforms\data\simple_binding
cd ..
cd vision2
fullrd Boxes
fullrd drawing_area
fullrd ev_list
fullrd fake_event
fullrd figure
fullrd hello_world
fullrd menu
fullrd multicolumn_list
fullrd pixmap
fullrd split_area
fullrd test_all_widgets
fullrd test_events
fullrd tests
fullrd tutorial
fullrd widget_test
cd ..
cd ..
remtrace precomp
safe_md %STUDIO_DIR\experimental
safe_md %STUDIO_DIR\experimental\precomp
safe_md %STUDIO_DIR\experimental\precomp\spec
cd %STUDIO_DIR\experimental\precomp\spec
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/precomp/spec/platform %ISE_PLATFORM
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/precomp/spec/dotnet %ISE_PLATFORM-dotnet
cdd %ISE_PLATFORM
fullrf *-safe.ecf
cdd ..\%ISE_PLATFORM-dotnet
fullrf *-safe.ecf
safe_md %STUDIO_DIR\precomp
safe_md %STUDIO_DIR\precomp\spec
cd %STUDIO_DIR\precomp\spec
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/precomp/spec/platform %ISE_PLATFORM
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/precomp/spec/dotnet %ISE_PLATFORM-dotnet
cdd %STUDIO_DIR
remtrace wizards
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery/wizards wizards
fullrf %STUDIO_DIR\studio\wizards\new_projects\wizard.dsc
fullrd %STUDIO_DIR\studio\wizards\new_projects\wizard
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/help/wizards/ewf/rootdir %STUDIO_DIR\studio\wizards\new_projects\ewf
move %STUDIO_DIR\studio\wizards\new_projects\ewf\ewf.dsc %STUDIO_DIR\studio\wizards\new_projects\ewf.dsc

remtrace C_library
safe_md %STUDIO_DIR\C_library
cdd %STUDIO_DIR\C_library
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/C_library/libpng libpng
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/C_library/zlib zlib
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/C_library/curl curl
cdd %STUDIO_DIR
remtrace root
exprt -N -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src/Delivery Delivery
move Delivery\*.* . >>& %INSTALL_LOG
fullrd Delivery >>& %INSTALL_LOG
fullrf INSTALL README make_install compile_libraries
remtrace eweasel (just the necessary)
exprt -N -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/eweasel eweasel
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/eweasel/bin eweasel/bin
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/eweasel/control eweasel/control
fullrf %STUDIO_DIR\eweasel\INSTALL

remtrace MinGW files
cdd %STUDIO_DIR
safe_md gcc
cd gcc
exprt -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/free_add_ons/gcc/%ISE_PLATFORM% %ISE_PLATFORM%

remtrace Create directories
cdd %STUDIO_DIR
safe_md %STUDIO_DIR\studio\spec
safe_md %STUDIO_DIR\studio\spec\%ISE_PLATFORM
safe_md %STUDIO_DIR\esbuilder\spec
safe_md %STUDIO_DIR\esbuilder\spec\%ISE_PLATFORM
safe_md %STUDIO_DIR\esbuilder\spec\%ISE_PLATFORM\bin
safe_md %STUDIO_DIR\vision2_demo\spec
safe_md %STUDIO_DIR\vision2_demo\spec\%ISE_PLATFORM
safe_md %STUDIO_DIR\vision2_demo\spec\%ISE_PLATFORM\bin
safe_md %STUDIO_DIR\wizards
safe_md %STUDIO_DIR\wizards\com
safe_md %STUDIO_DIR\wizards\com\config
safe_md %STUDIO_DIR\studio\spec\%ISE_PLATFORM\include
safe_md %STUDIO_DIR\studio\spec\%ISE_PLATFORM\lib
safe_md %STUDIO_DIR\studio\spec\%ISE_PLATFORM\lib\msc
safe_md %STUDIO_DIR\studio\spec\%ISE_PLATFORM\lib\msc_vc140
safe_md %STUDIO_DIR\studio\spec\%ISE_PLATFORM\lib\mingw
safe_md %STUDIO_DIR\rb

give_time_to organize the delivery and to start filling it

remtrace Checkout all the libraries
cdd %INIT_DIR
call inc\make_libraries.btm %STUDIO_DIR library
cdd %INIT_DIR
call inc\make_libraries.btm %STUDIO_DIR\experimental experimental/library
cdd %INIT_DIR
call inc\make_other_libraries.btm %STUDIO_DIR

remtrace Compile C code of Eiffel Software C libraries
cdd %INIT_DIR

call %INIT_DIR\inc\set_has_c_compiler HAS_VC140 VC140 
iff defined ISE_C_COMPILER_VER .and. %ISE_C_COMPILER_VER% != "VC140" then
	call %INIT_DIR\inc\set_has_c_compiler HAS_C_COMP_VER %ISE_C_COMPILER_VER 
else
	set "HAS_C_COMP_VER=False"
endiff

iff %HAS_VC140% == "True" then
	call espawn -u:VC140 "%COMSPEC% /c inc\compile_c_libraries.btm %STUDIO_DIR msc_vc140"
endiff
iff %HAS_C_COMP_VER% == "True" then
	cdd %INIT_DIR
	call espawn -u:%ISE_C_COMPILER_VER% "%COMSPEC% /c inc\compile_c_libraries.btm %STUDIO_DIR msc"
endiff
cdd %INIT_DIR
call inc\compile_c_libraries.btm %STUDIO_DIR mingw

cdd %INIT_DIR
iff %HAS_VC140% == "True" then
	call espawn -u:VC140 "%COMSPEC% /c inc\compile_c_libraries.btm %STUDIO_DIR\experimental msc_vc140"
endiff
iff %HAS_C_COMP_VER% == "True" then
	cdd %INIT_DIR
	espawn -u:%ISE_C_COMPILER_VER% "%COMSPEC% /c inc\compile_c_libraries.btm %STUDIO_DIR\experimental msc"
endiff
cdd %INIT_DIR
call inc\compile_c_libraries.btm %STUDIO_DIR\experimental mingw


remtrace Build the C libraries of non-Eiffel Software C libraries
cdd %INIT_DIR
iff %HAS_VC140% == "True" then
	espawn -u:VC140 "%COMSPEC% /c inc\compile_other_c_libraries.btm %STUDIO_DIR msc_vc140"
endiff
iff %HAS_C_COMP_VER% == "True" then
	cdd %INIT_DIR
	espawn -u:%ISE_C_COMPILER_VER% "%COMSPEC% /c inc\compile_other_c_libraries.btm %STUDIO_DIR msc"
endiff
cdd %INIT_DIR
call inc\compile_other_c_libraries.btm %STUDIO_DIR mingw

cdd %INIT_DIR

REM Fill directories
iff not defined NO_COMPILE then
	remtrace Update all libraries
	iff isdir %EIFFEL_SRC then
		remtrace Delete %EIFFEL_SRC
		fullrd %EIFFEL_SRC
	endiff
	safe_md %EIFFEL_SRC

	REM Clean up the files
	remtrace Update all source files.
	cdd %EIFFEL_SRC\..
	co -r %PUBLIC_SVN_REVISION %DEFAULT_PUBLIC_SVN/Src %EIFFEL_SRC

	remtrace ISE specific files
	cdd %EIFFEL_SRC
	co %DEFAULT_ISE_SVN/Src/library/keygen library/keygen
	cd tools
	co %DEFAULT_ISE_SVN/Src/tools/activation activation
	co %DEFAULT_ISE_SVN/Src/tools/estudio_enterprise estudio_enterprise

	remtrace Build the normal libraries
	cdd %INIT_DIR
	call inc\update_libraries.btm %EIFFEL_SRC
	remtrace Build the experimental libraries
	cdd %INIT_DIR
	call inc\update_libraries.btm %EIFFEL_SRC\experimental

	cdd %INIT_DIR
	give_time_to update libraries

	remtrace Generate needed executables of the bin directory
	call inc\make_exes.btm
	cdd %INIT_DIR
	give_time_to compile exes

	iff not defined NO_WIZARDS then
		remtrace Generate the precompilation and the "new project" wizards
		call inc\make_wizards.btm
		cdd %INIT_DIR
		give_time_to compile basic wizards

		remtrace Generate the COM wizard
		call inc\make_com_wizard.btm
		cdd %INIT_DIR
		give_time_to compile the COM wizard
	else
		quick_move /s studio\wizards
		quick_move /s wizards
	endiff

	cdd %INIT_DIR

	remtrace Generate Resource Bench
	call inc\make_rb.btm
	cdd %INIT_DIR
	give_time_to compile Resource Bench
else
	remtrace Copy executable files from the old delivery
	quick_move /s studio\spec
	quick_move /s studio\wizards
	quick_move /s rb
	quick_move /s wizards
	cdd %STUDIO_DIR
	give_time_to copy executables from the old delivery
endiff

cdd %INIT_DIR

iff not defined NO_INSTALL then
	iff defined ISE_C_COMPILER_VER then
		call %INIT_DIR\inc\set_has_c_compiler HAS_C_COMP_VER %ISE_C_COMPILER_VER 
	else
		set "HAS_C_COMP_VER=False"
	endiff
	iff defined ISE_C_COMPILER_VER .and. %HAS_C_COMP_VER% == "True" then
		espawn -u:%ISE_C_COMPILER_VER% "%COMSPEC% /c make_installations.btm"
	else
		espawn "%COMSPEC% /c make_installations.btm"
	endiff
	cdd %INIT_DIR
endiff

REM Finish
remtrace final time:
remtrace %_TIME
remtrace total used time:
remtrace %@TIMER[2]

REM Error handling and normal ending
:broken
CANCEL

