@echo off
REM ===================================================================
REM  Replication runner for "What does Retraction Watch's
REM  'computer-generated content' reason capture?"
REM
REM    reproduce.cmd              run everything
REM    reproduce.cmd --smoke      check inputs and imports only, run nothing
REM    reproduce.cmd C:\py\python.exe   use a specific interpreter
REM
REM  Exits nonzero if any step fails or any expected output is missing.
REM ===================================================================
setlocal EnableDelayedExpansion

set "PKG=%~dp0"
if "%PKG:~-1%"=="\" set "PKG=%PKG:~0,-1%"
if "%REPRO_ROOT%"=="" set "REPRO_ROOT=%PKG%"

set "SMOKE="
set "PY="
for %%A in (%*) do (
  if /I "%%~A"=="--smoke" (set "SMOKE=1") else (set "PY=%%~A")
)
if "%PY%"=="" set "PY=python"

set "TBL=%REPRO_ROOT%\runs\ai-retractions-v3\tables"
set "SNAP=%REPRO_ROOT%\data\retraction_watch_gitlab.csv"

echo Package root : %REPRO_ROOT%
echo Python       : %PY%
echo Tables dir   : %TBL%
echo.

REM ---- dependencies -------------------------------------------------
"%PY%" -c "import numpy, scipy; print('numpy', numpy.__version__, '| scipy', scipy.__version__)"
if errorlevel 1 (
  echo ERROR: numpy and scipy are required.
  echo     "%PY%" -m pip install "numpy==2.5.1" "scipy==1.18.0"
  exit /b 1
)

REM ---- create the directory layout the scripts expect ---------------
if not exist "%TBL%" mkdir "%TBL%"
if not exist "%REPRO_ROOT%\papers\ai-retractions\submission-package" mkdir "%REPRO_ROOT%\papers\ai-retractions\submission-package"
if not exist "%REPRO_ROOT%\papers\ai-retractions\refs" mkdir "%REPRO_ROOT%\papers\ai-retractions\refs"
if not exist "%REPRO_ROOT%\figures" mkdir "%REPRO_ROOT%\figures"

REM ---- stage the frozen tables shipped at package root --------------
REM The archive stores frozen inputs in .\tables for easy inspection; the scripts read them
REM from .\runs\ai-retractions-v3\tables. Copy rather than move so the archive stays intact.
if exist "%REPRO_ROOT%\tables\*.csv" (
  echo Staging frozen tables into the analysis directory...
  copy /Y "%REPRO_ROOT%\tables\*.csv" "%TBL%\" >nul
  if errorlevel 1 (echo ERROR: could not stage frozen tables & exit /b 1)
)

REM ---- required frozen inputs ---------------------------------------
set "MISSING="
for %%F in (t19_citations_full.csv t39_control_candidates_openalex.csv t42_openalex_topics.csv) do (
  if not exist "%TBL%\%%F" set "MISSING=!MISSING! %%F"
)
if not "!MISSING!"=="" (
  echo ERROR: required frozen input^(s^) missing from %TBL%:!MISSING!
  exit /b 1
)

REM ---- the snapshot is not redistributed ----------------------------
if not exist "%SNAP%" (
  echo.
  echo ERROR: the Retraction Watch snapshot is not present at
  echo     %SNAP%
  echo.
  echo It is not redistributed in this package. Obtain it with:
  echo     git clone https://gitlab.com/crossref/retraction-watch-data
  echo     git -C retraction-watch-data checkout dda8c8749c6ac35923f69162936c53fc9b9f5e3c
  echo     copy retraction-watch-data\retraction_watch.csv "%SNAP%"
  echo.
  echo Then verify ^(must match exactly^):
  echo     certutil -hashfile "%SNAP%" SHA256
  echo     1B61BABC73AD7F9861175ECB5DECC2B5C3BE33E0464C40489355A5BB96B9EF2F
  exit /b 1
)

if defined SMOKE (
  echo Smoke test: compiling all scripts...
  "%PY%" -m py_compile "%REPRO_ROOT%\scripts\matched_dispersion.py" "%REPRO_ROOT%\scripts\referee_reanalysis.py" "%REPRO_ROOT%\scripts\regen_esm1.py" "%REPRO_ROOT%\scripts\mh_cluster_bootstrap.py" "%REPRO_ROOT%\scripts\validate_stats.py" "%REPRO_ROOT%\scripts\verify_references.py"
  if errorlevel 1 (echo SMOKE TEST FAILED: a script does not compile & exit /b 1)
  echo Smoke test passed: inputs present, dependencies available, all scripts compile.
  exit /b 0
)

echo [1/6] matched dispersion reanalysis  ^(Section 4.4, Table 7, Fig. 3^)
"%PY%" -u "%REPRO_ROOT%\scripts\matched_dispersion.py" || exit /b 1

echo.
echo [2/6] citation rematch, terminology audit, 10k bootstrap  ^(Tables 6, 14, 16^)
"%PY%" -u "%REPRO_ROOT%\scripts\referee_reanalysis.py" all || exit /b 1

echo.
echo [3/6] notice-cluster bootstrap for Mantel-Haenszel  ^(Table 10^)
"%PY%" -u "%REPRO_ROOT%\scripts\mh_cluster_bootstrap.py" || exit /b 1

echo.
echo [4/6] regenerate Online Resource 1 and 1b
"%PY%" -u "%REPRO_ROOT%\scripts\regen_esm1.py" || exit /b 1

echo.
echo [5/6] independent numpy/scipy validation  ^(Online Resource 2^)
"%PY%" -u "%REPRO_ROOT%\scripts\validate_stats.py" || exit /b 1

echo.
echo [6/6] verify references against Crossref and arXiv  ^(needs network^)
"%PY%" -u "%REPRO_ROOT%\scripts\verify_references.py"
if errorlevel 1 echo   WARNING: reference verification reported a failure

REM ---- confirm every expected output exists -------------------------
echo.
echo Verifying generated outputs...
set "GONE="
for %%F in (t45_stratum_dispersion.csv t46_matched_dispersion_summary.csv t47_citation_rematched.csv t48_terminology_broadened.csv t49_terminology_specific_matches.csv t50_bootstrap_10k.csv t51_mh_cluster_bootstrap.csv) do (
  if not exist "%TBL%\%%F" set "GONE=!GONE! %%F"
)
if not "!GONE!"=="" (
  echo ERROR: expected output^(s^) were not produced:!GONE!
  exit /b 1
)

echo   all expected tables present.
echo.
echo Recording SHA-256 of generated outputs to outputs.sha256 ...
if exist "%REPRO_ROOT%\outputs.sha256" del "%REPRO_ROOT%\outputs.sha256"
for %%F in ("%TBL%\t4*.csv" "%TBL%\t5*.csv") do (
  for /f "skip=1 tokens=1" %%H in ('certutil -hashfile "%%~F" SHA256 ^| findstr /v ":"') do (
    if not "%%H"=="" echo %%H  %%~nxF>>"%REPRO_ROOT%\outputs.sha256"
  )
)
echo   wrote %REPRO_ROOT%\outputs.sha256

echo.
echo Done. Figures require Windows PowerShell with System.Drawing:
echo   powershell -File "%REPRO_ROOT%\scripts\New-FiguresTiff.ps1" -RunDir "%REPRO_ROOT%\runs\ai-retractions-v3" -OutDir "%REPRO_ROOT%\figures"
echo   powershell -File "%REPRO_ROOT%\scripts\New-Fig3Forest.ps1"  -RunDir "%REPRO_ROOT%\runs\ai-retractions-v3" -OutDir "%REPRO_ROOT%\figures"

endlocal
exit /b 0
