#!/bin/bash
set -euo pipefail

cdx_mode="${1-}"
cdx_marker=CDXGOODSD70EEFBA4E
cdx_socket=/tmp/cdx-dm-account.pEx2Py/control
cdx_root=/Users/wiscourper/Documents/workspace/zhct/zhctprompt
cdx_fixture="$cdx_root/work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-goods-read3-20261006/fixture_goods.disabled.php"
cdx_basic="$cdx_root/work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-goods-write5-20261007/basic_state_delete.disabled.php"
cdx_fixture_sha=1e162fce62d03f38cc2119b8dfe51837df238d533fbbef3c38e5e6642a9723b2
cdx_basic_sha=f8b2f1c50af3c7be3389b5a9e237b8dde2345118f5057e57d9ede0e2e53851a8
cdx_fixture_gate=''
cdx_basic_gate=''
case "$cdx_mode" in
    probe) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--probe ;;
    create) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--create; cdx_fixture_gate=OWNED_GOODS_FIXTURE_V1 ;;
    cleanup) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--cleanup; cdx_fixture_gate=OWNED_GOODS_FIXTURE_V1 ;;
    audit) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--audit; cdx_fixture_gate=OWNED_GOODS_FIXTURE_V1 ;;
    basic-preflight) cdx_source="$cdx_basic"; cdx_sha="$cdx_basic_sha"; cdx_php_mode=--preflight ;;
    basic-run) cdx_source="$cdx_basic"; cdx_sha="$cdx_basic_sha"; cdx_php_mode=--execute; cdx_basic_gate=GOODS_BASIC_STATE_DELETE_V1 ;;
    token-audit) cdx_source="$cdx_basic"; cdx_sha="$cdx_basic_sha"; cdx_php_mode=--audit-token; cdx_basic_gate=GOODS_BASIC_STATE_DELETE_V1 ;;
    *) exit 78 ;;
esac
if [[ "$(shasum -a 256 "$cdx_source" | cut -d' ' -f1)" != "$cdx_sha" ]]; then exit 78; fi
if [[ "$cdx_mode" == probe || "$cdx_mode" == basic-preflight ]]; then
    if [[ $# -ne 1 ]]; then exit 78; fi
else
    cdx_expected="APPROVE_C_GOODS_${cdx_mode}_D70EEFBA4E_V1"
    if [[ $# -ne 2 || "${2-}" != "$cdx_expected" ]]; then exit 78; fi
    if [[ "${CDX_GOODS_C_V2_SCRIPT_SHA-}" != "$(shasum -a 256 "$0" | cut -d' ' -f1)" ]]; then exit 78; fi
fi
if ! ssh -S "$cdx_socket" -o BatchMode=yes root@121.43.241.161 true; then exit 3; fi
cdx_dir="$(ssh -S "$cdx_socket" -o BatchMode=yes root@121.43.241.161 'umask 077; mktemp -d /tmp/cdx-goods-c-v2.XXXXXX')"
if [[ ! "$cdx_dir" =~ ^/tmp/cdx-goods-c-v2\.[A-Za-z0-9]{6}$ ]]; then exit 3; fi
cdx_remote_file="$cdx_dir/verify.php"
cdx_cleanup() {
    ssh -S "$cdx_socket" -o BatchMode=yes root@121.43.241.161 \
        "rm -f '$cdx_remote_file' && rmdir '$cdx_dir'" >/dev/null 2>&1 || true
}
trap cdx_cleanup EXIT
if ! scp -q -o "ControlPath=$cdx_socket" -o BatchMode=yes \
    "$cdx_source" "root@121.43.241.161:$cdx_remote_file"; then exit 3; fi
cdx_remote='set -euo pipefail; ld=""; svc="";
while IFS= read -r -d "" x; do
    case "$x" in
        LD_LIBRARY_PATH=*) ld="${x#*=}" ;;
        DM_SVC_PATH=*) svc="${x#*=}" ;;
    esac
done </proc/53772/environ
if [[ "$(sha256sum "$1" | cut -d" " -f1)" != "$2" ]]; then exit 78; fi
chmod 600 "$1"
exec /usr/bin/env -i PATH=/usr/bin:/bin LD_LIBRARY_PATH="$ld" DM_SVC_PATH="$svc" \
    CDX_GOODS_SOURCE_CHECKED=1 CDX_GOODS_REVIEWED_SHA256="$2" \
    CDX_GOODS_WRITE_APPROVAL="$5" \
    CDX_GOODS_W5_SOURCE_CHECKED=1 CDX_GOODS_W5_REVIEWED_SHA256="$2" \
    CDX_GOODS_W5_APPROVAL="$6" \
    /www/server/php/74/bin/php -c /www/server/php/74/etc/php-cli.ini \
    -d extension=/home/dmdba/dmdbms/drivers/php_pdo/libphp74_dm.so \
    -d extension=/home/dmdba/dmdbms/drivers/php_pdo/php74_pdo_dm.so \
    "$1" "$3" C "$4"'
printf -v cdx_remote_quoted '%q' "$cdx_remote"
printf -v cdx_file_quoted '%q' "$cdx_remote_file"
printf -v cdx_sha_quoted '%q' "$cdx_sha"
printf -v cdx_mode_quoted '%q' "$cdx_php_mode"
printf -v cdx_marker_quoted '%q' "$cdx_marker"
printf -v cdx_fixture_gate_quoted '%q' "$cdx_fixture_gate"
printf -v cdx_basic_gate_quoted '%q' "$cdx_basic_gate"
ssh -S "$cdx_socket" -o BatchMode=yes root@121.43.241.161 \
    "bash -c $cdx_remote_quoted -- $cdx_file_quoted $cdx_sha_quoted $cdx_mode_quoted $cdx_marker_quoted $cdx_fixture_gate_quoted $cdx_basic_gate_quoted"
