#!/bin/bash
set -euo pipefail

# C-only control-process launcher. No DB/HTTP in --probe; --start is separately
# gated and must never be used without a fresh, explicit approval.
cdx_phase="${1-}"
if [[ $# -ne 1 || ( "$cdx_phase" != "--probe" && "$cdx_phase" != "--start" ) ]]; then
    printf 'DENIED: exact C control phase required\n' >&2
    exit 78
fi
if [[ "$cdx_phase" == "--probe" ]]; then
    if [[ "${CDX_SCORE_C_CONTROL_APPROVED-}" != "MODULE_PROBE_ONLY_V1" ]]; then
        printf 'DENIED: C module probe approval required\n' >&2
        exit 78
    fi
else
    if [[ "${CDX_SCORE_C_CONTROL_APPROVED-}" != "START_C_SINGLE_V1" ]]; then
        printf 'DENIED: separate C start approval required\n' >&2
        exit 78
    fi
fi
if [[ "${CDX_SCORE_C_CONTROL_SHA-}" != "$(/usr/bin/sha256sum "$0" | /usr/bin/cut -d' ' -f1)" ]]; then
    printf 'DENIED: reviewed C control SHA required\n' >&2
    exit 78
fi

cdx_dir=/root/cdx-score-rank-plan-20261004
cdx_self="$cdx_dir/score_rank_c_control.disabled.sh"
cdx_lifecycle="$cdx_dir/score_rank_lifecycle.disabled.php"
cdx_php=/www/server/php/74/bin/php
cdx_ini=/www/server/php/74/etc/php-cli.ini
cdx_dm_base=/home/dmdba/dmdbms/drivers/php_pdo/libphp74_dm.so
cdx_dm_pdo=/home/dmdba/dmdbms/drivers/php_pdo/php74_pdo_dm.so
cdx_old_pid=53772
cdx_lifecycle_sha=827e6beb2adba56ba18f892ba8dbfe043c70baa648b9b8f4d6b54d4e23d5a074

if [[ "$0" != "$cdx_self" || ! -d "$cdx_dir" || -L "$cdx_dir" \
    || "$(/usr/bin/stat -c %U "$cdx_dir")" != root \
    || ! -f "$cdx_self" || -L "$cdx_self" \
    || "$(/usr/bin/stat -c %U "$cdx_self")" != root \
    || "$(/usr/bin/stat -c %a "$cdx_self")" != 600 \
    || "$(/usr/bin/stat -c %a "$cdx_dir")" != 700 \
    || ! -f "$cdx_lifecycle" || -L "$cdx_lifecycle" \
    || "$(/usr/bin/stat -c '%U|%a' "$cdx_lifecycle")" != 'root|600' \
    || -L "$cdx_php" || -L "$cdx_ini" \
    || -L "$cdx_dm_base" || -L "$cdx_dm_pdo" \
    || "$(/usr/bin/sha256sum "$cdx_lifecycle" | /usr/bin/cut -d' ' -f1)" != "$cdx_lifecycle_sha" \
    || "$(/usr/bin/sha256sum "$cdx_php" | /usr/bin/cut -d' ' -f1)" != 7de65d98a793410b40f59da8fde6fcc0c2d0d40ba7788b0d966a319a29b9817e \
    || "$(/usr/bin/sha256sum "$cdx_ini" | /usr/bin/cut -d' ' -f1)" != fa0576b7d002e872cb148dc1813b93fb30c09cd8bdfd12ad48fdbf8b0a0d75a0 \
    || "$(/usr/bin/sha256sum "$cdx_dm_base" | /usr/bin/cut -d' ' -f1)" != 342fe4e9b28a2c8066e9b940c96d403ed93f281b8a80325ba2ac4a01af7b1991 \
    || "$(/usr/bin/sha256sum "$cdx_dm_pdo" | /usr/bin/cut -d' ' -f1)" != 5f44eb2b197faf701c483e7e7ae995add204a55e684b6f9dfc61ee64bf1652bc ]]; then
    printf 'HALT: private C binary, extension or source drift\n' >&2
    exit 3
fi
if [[ "$(/usr/bin/stat -c '%U|%a' "$cdx_php")" != 'root|755' \
    || "$(/usr/bin/stat -c '%U|%a' "$cdx_ini")" != 'root|644' \
    || "$(/usr/bin/stat -c '%U|%a' "$cdx_dm_base")" != 'dmdba|755' \
    || "$(/usr/bin/stat -c '%U|%a' "$cdx_dm_pdo")" != 'dmdba|755' \
    || "$(/usr/bin/readlink /proc/$cdx_old_pid/fd/3)" != 'socket:[441267]' \
    || "$(/usr/bin/readlink -f /proc/$cdx_old_pid/exe)" != "$cdx_php" \
    || "$(/usr/bin/printf '%s|%s' "$(/usr/bin/hostname)" "$(/usr/bin/cat /etc/machine-id)" | /usr/bin/sha256sum | /usr/bin/cut -d' ' -f1)" != d56d81988bb0f106946cce91c608fdd0ef1116e5150b23376ea87e086eace4b9 ]]; then
    printf 'HALT: C host or fixed original PHP process drift\n' >&2
    exit 3
fi

# Read only two non-secret runtime-path variables from the fixed old process.
# Never echo either value, any POC password, or the raw environment.
cdx_ld_path=''
cdx_svc_path=''
while IFS= read -r -d '' cdx_entry; do
    case "$cdx_entry" in
        LD_LIBRARY_PATH=*) cdx_ld_path="${cdx_entry#LD_LIBRARY_PATH=}" ;;
        DM_SVC_PATH=*) cdx_svc_path="${cdx_entry#DM_SVC_PATH=}" ;;
    esac
done < "/proc/$cdx_old_pid/environ"
if [[ -z "$cdx_ld_path" || -z "$cdx_svc_path" \
    || "$(/usr/bin/printf '%s\n' "$cdx_ld_path" | /usr/bin/sha256sum | /usr/bin/cut -d' ' -f1)" != 33fd646cff0a28903729f81864adfa6236ab191ffd577a9f678164b502bf48a2 \
    || "$(/usr/bin/printf '%s\n' "$cdx_svc_path" | /usr/bin/sha256sum | /usr/bin/cut -d' ' -f1)" != 3455a35006ae1284223711af80100064acb1458da5b1a1094a9caf7d3991dde2 ]]; then
    printf 'HALT: fixed C DM library environment drift\n' >&2
    exit 3
fi

cdx_runtime_env=(/usr/bin/env -i
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    "LD_LIBRARY_PATH=$cdx_ld_path" "DM_SVC_PATH=$cdx_svc_path")
cdx_php_args=("$cdx_php" -c "$cdx_ini"
    -d "extension=$cdx_dm_base" -d "extension=$cdx_dm_pdo")
if ! "${cdx_runtime_env[@]}" "${cdx_php_args[@]}" -r \
    'if (PHP_VERSION_ID < 70400 || PHP_VERSION_ID >= 70500 || !in_array("dm", PDO::getAvailableDrivers(), true)) exit(79);' \
    >/dev/null 2>&1; then
    printf 'HALT: C PHP7.4 PDO dm driver probe failed before any database query\n' >&2
    exit 3
fi
if [[ "$cdx_phase" == "--probe" ]]; then
    printf 'C_MODULE_PROBE_OK php74_abi=true pdo_dm=true database_queries=0 business_http=0\n'
    exit 0
fi

# Only a separately approved --start reaches this exec. The lifecycle still
# performs its own SHA, lane, source, database-identity and no-retry gates.
exec "${cdx_runtime_env[@]}" \
    CDX_SCORE_START_APPROVED=START_NEW_ISOLATED_RUNTIME_V1 \
    "CDX_SCORE_LIFECYCLE_SHA=$cdx_lifecycle_sha" CDX_SCORE_LIFECYCLE_LANE=C \
    "${cdx_php_args[@]}" "$cdx_lifecycle" --start C
