#!/bin/bash
set -euo pipefail

cdx_mode="${1-}"
cdx_marker=CDXGOODS4362E6B4E8
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_add="$cdx_root/work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-goods-write5-20261007/add_edit.disabled.php"
cdx_fixture_sha=1e162fce62d03f38cc2119b8dfe51837df238d533fbbef3c38e5e6642a9723b2
cdx_add_sha=4ac3a03c70cc39ca9ef062f1e8c822f9cce107b036af5b1ed69c3e203d40ced2
cdx_fixture_gate=''
cdx_add_gate=''
cdx_recovery_gate=''
case "$cdx_mode" in
    probe) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--probe ;;
    base-create) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--create; cdx_fixture_gate=OWNED_GOODS_FIXTURE_V1 ;;
    base-cleanup) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--cleanup; cdx_fixture_gate=OWNED_GOODS_FIXTURE_V1 ;;
    base-audit) cdx_source="$cdx_fixture"; cdx_sha="$cdx_fixture_sha"; cdx_php_mode=--audit; cdx_fixture_gate=OWNED_GOODS_FIXTURE_V1 ;;
    add-preflight) cdx_source="$cdx_add"; cdx_sha="$cdx_add_sha"; cdx_php_mode=--preflight ;;
    add-run) cdx_source="$cdx_add"; cdx_sha="$cdx_add_sha"; cdx_php_mode=--execute; cdx_add_gate=OWNED_ADD_EDIT_SINGLE_V1 ;;
    token-audit) cdx_source="$cdx_add"; cdx_sha="$cdx_add_sha"; cdx_php_mode=--audit-token; cdx_add_gate=OWNED_ADD_EDIT_SINGLE_V1 ;;
    api-cleanup) cdx_source="$cdx_add"; cdx_sha="$cdx_add_sha"; cdx_php_mode=--cleanup; cdx_add_gate=OWNED_ADD_EDIT_SINGLE_V1 ;;
    api-audit) cdx_source="$cdx_add"; cdx_sha="$cdx_add_sha"; cdx_php_mode=--audit-cleanup; cdx_add_gate=OWNED_ADD_EDIT_SINGLE_V1 ;;
    partial-recover) cdx_source="$cdx_add"; cdx_sha="$cdx_add_sha"; cdx_php_mode=--recover-partial; cdx_add_gate=OWNED_ADD_EDIT_SINGLE_V1; cdx_recovery_gate=OWNED_ADD_EDIT_PARTIAL_RECOVERY_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" == add-preflight ]]; then
    if [[ $# -ne 1 ]]; then exit 78; fi
else
    cdx_expected="APPROVE_C_GOODS_ADD_${cdx_mode}_4362E6B4E8_V1"
    if [[ $# -ne 2 || "${2-}" != "$cdx_expected" ]]; then exit 78; fi
    if [[ "${CDX_GOODS_C_ADD_V1_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-add.XXXXXX')"
if [[ ! "$cdx_dir" =~ ^/tmp/cdx-goods-c-add\.[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_AE_SOURCE_CHECKED=1 CDX_GOODS_AE_REVIEWED_SHA256="$2" \
    CDX_GOODS_AE_APPROVAL="$6" CDX_GOODS_AE_RECOVER_APPROVAL="$7" \
    /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_add_gate_quoted '%q' "$cdx_add_gate"
printf -v cdx_recovery_gate_quoted '%q' "$cdx_recovery_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_add_gate_quoted $cdx_recovery_gate_quoted"
