; IDL batch script: compile local gximagecomputing routines with local precedence.
; Run from IDL command line with:
;   @/path/to/gximagecomputing/examples/idl/compile_local_idl
;
; This uses explicit .compile commands so the local repository checkout wins over
; stale gx_simulator/rendergrff routines already present on the SSW path.

; Resolve repository root in a portable way:
; 1) GXIMAGECOMPUTING_ROOT environment variable (recommended)
; 2) Current working directory when it is the repo root
; 3) Parent of current working directory

repo_root = ''
env_root = GETENV('GXIMAGECOMPUTING_ROOT')

IF env_root NE '' THEN BEGIN
	IF FILE_TEST(FILEPATH('idlcode/ConvertToGX.pro', ROOT_DIR=env_root)) THEN repo_root = env_root
ENDIF

IF repo_root EQ '' THEN BEGIN
	cwd = FILEPATH('', /CURRENT_DIRECTORY)
	IF FILE_TEST(FILEPATH('idlcode/ConvertToGX.pro', ROOT_DIR=cwd)) THEN repo_root = cwd
ENDIF

IF repo_root EQ '' THEN BEGIN
	parent = FILEPATH('..', ROOT_DIR=FILEPATH('', /CURRENT_DIRECTORY))
	IF FILE_TEST(FILEPATH('idlcode/ConvertToGX.pro', ROOT_DIR=parent)) THEN repo_root = parent
ENDIF

IF repo_root EQ '' THEN BEGIN
	PRINT, 'Could not locate gximagecomputing repository root.'
	PRINT, 'Set GXIMAGECOMPUTING_ROOT or run from the repo root (or its examples/idl folder).'
ENDIF ELSE BEGIN

	.compile FILEPATH('idlcode/ConvertToGX.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/LoadGXmodel__load_box.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/LoadGXmodel.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/GXObserverGeometry.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/GXDumpDLLInputs.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/LoadEBTEL.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/LoadEUVresponse.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/MakeSimulationBox.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/MakeSimulationBoxEUV.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/DefineCoronaParams.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/ReserveOutputSpace.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/ReserveOutputSpaceEUV.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/ConvertToMaps.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('idlcode/ConvertToMapsEUV.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('examples/idl/RenderExampleMW.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('examples/idl/RenderExampleEUV.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('scripts/idl/renderexampleeuv_test.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('scripts/idl/renderexamplemw_test.pro', ROOT_DIR=repo_root)
	.compile FILEPATH('scripts/idl/DumpIDLLoadGXmodelParity.pro', ROOT_DIR=repo_root)
	PRINT, 'Compiled local gximagecomputing IDL routines (MW + EUV) from: ' + repo_root
ENDELSE
