1@echo OFF
2setlocal EnableDelayedExpansion
3echo Obtaining Git commit
4git rev-parse HEAD 2>NUL > temp.txt
5set /p commit=<temp.txt
6if not defined commit (
7	echo Git command not present, trying folder approach
8	if exist "..\.git" (
9		echo Git folder found, using HEAD file
10		type "..\.git\HEAD" 2>NUL > temp.txt
11		for /f "delims=" %%a in (temp.txt) do set head=%%a&goto next
12		:next
13		set start=%head:~0,5%
14		set rest=%head:~5%
15		if "%start%"=="ref: " (
16			echo HEAD file contains a ref, following the ref
17			echo ..\.git\%rest% > temp.txt
18			for /f "delims=" %%a in (temp.txt) do set ref_path=%%a&goto nexttt
19			:nexttt
20			set ref_path=%ref_path:/=\%
21			type "%ref_path%" 2>NUL > temp.txt
22			for /f "delims=" %%a in (temp.txt) do set commit=%%a&goto nextt
23			:nextt
24			echo Extracted commit: %commit%
25			goto final
26		) else (
27			echo HEAD contains a commit, using it (%head%)
28			set commit = %head%
29			goto final
30		)
31	)
32)
33:final
34if not defined commit set commit=Unknown
35for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
36set builddate=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
37echo storing variables in file
38echo commit: %commit%
39echo date: %builddate%
40echo #ifndef CCX_CCEXTRACTOR_COMPILE_REAL_H > ../src/lib_ccx/compile_info_real.h
41echo #define CCX_CCEXTRACTOR_COMPILE_REAL_H >> ../src/lib_ccx/compile_info_real.h
42echo #define GIT_COMMIT "%commit%" >> ../src/lib_ccx/compile_info_real.h
43echo #define COMPILE_DATE "%builddate%" >> ../src/lib_ccx/compile_info_real.h
44echo #endif >> ../src/lib_ccx/compile_info_real.h
45echo stored all in compile_info_real.h
46del temp.txt
47echo done