#!/bin/sh
# Read-only JSR preflight. No application bootstrap, DB, printer, writes or service control.
set -eu
root=/workspace/wwwroot/zhct_jsr/project
printf 'JSR_PREFLIGHT_BEGIN\n'
hostname
date -u '+%Y-%m-%dT%H:%M:%SZ'
test -d "$root"
test ! -L "$root"
readlink -f "$root"
for relative in application/p/logic/MealOrder.php application/common/enum/ReceiptPrinterTemplate.php application/api/model/meal/Order.php application/api/controller/MealOrder.php composer.json composer.lock thinkphp/library/think/Model.php; do
    if test -f "$root/$relative"; then
        sha256sum "$root/$relative"
    fi
done
printf 'ENV_MARKERS\n'
grep -E "define\('TP_ENV'|define\('BUSINESS'" "$root/env.php"
printf 'RECEIPT_FEATURES\n'
grep -nE 'function orderPrint|function buildAdvancedReceiptContent|function buildCompactReceiptContent|function formatReceiptLabeledLines|function formatReceiptPickupTime' "$root/application/p/logic/MealOrder.php"
printf 'PACKAGE_LIST\n'
find /workspace/wwwroot/zhct_jsr -maxdepth 1 -type f -name 'zhct_*.tgz' -printf '%f %s %TY-%Tm-%TdT%TH:%TM\n'
printf 'CODE_STAT\n'
stat -c '%n %s %a %U:%G %y' "$root/application/p/logic/MealOrder.php"

printf 'POSTDEPLOY_HEALTH\n'
for path in /dist/ /p/mealOrder/detail; do
  curl --max-time 8 --silent --output /dev/null --write-out "$path http=%{http_code}\n" -H 'Host: jsr.yyangpt.cn' "http://127.0.0.1$path"
done
printf 'TODAY_ERROR_LOG_COUNTS\n'
log=/workspace/wwwroot/log/zhct_jsr/$(date +%Y%m%d)_error.log
if test -f "$log"; then
  printf 'error_log_bytes='; wc -c < "$log"
  printf 'error_log_lines='; wc -l < "$log"
  printf 'php_fatal_or_parse_count='; grep -Ec 'Fatal error|Parse error|syntax error' "$log" || true
else
  printf 'daily_error_log_not_present\n'
fi
printf 'BACKUP_HASH\n'
sha256sum /workspace/wwwroot/zhct_jsr/hotfix-backups/VWCG-1357-receipt-date/MealOrder.php.before
printf 'ACTUAL_LOG_PATHS\n'
for logroot in /workspace/wwwroot/log/zhct_jsr /workspace/wwwroot/zhct_jsr/project/runtime/log; do
  if test -d "$logroot"; then find "$logroot" -maxdepth 3 -type f -mmin -60 -printf '%p %s\n' | head -n 20; fi
done
printf 'NGINX_ERROR_LOG_STAT\n'
for file in /workspace/wwwlogs/zhctjsr.yyangpt.cn.error.log /workspace/wwwlogs/jsr.yyangpt.cn.error.log; do
 if test -f "$file"; then stat -c '%n %s %y' "$file"; fi
done
printf 'BOUNDED_LOG_COUNTS\n'
for file in /workspace/wwwroot/log/zhct_jsr/20260912_info.log /workspace/wwwroot/log/zhct_jsr/cron/meal_order_print_retry_20260912.log; do
 if test -f "$file"; then
   printf '%s matched_recent_error_count=' "$file"
   tail -c 4194304 "$file" | grep -Ec 'PHP Fatal|PHP Parse|Fatal error|Parse error|syntax error|\[MealOrder\]\[orderPrint\] exception' || true
 fi
done
printf 'NGINX_WINDOW\n'
file=/workspace/wwwlogs/zhctjsr.yyangpt.cn.error.log
if test -f "$file"; then
 tail -c 4194304 "$file" | awk -v since='2026/09/12 14:29:41' '$1 ~ /^2026\/09\/12$/ { ts=$1 " " $2; if(first=="")first=ts; last=ts; if(ts>=since && $3 ~ /^\[(error|crit|alert|emerg)\]/)errors++ } END {print "sample_first=" first; print "sample_last=" last; print "nginx_error_since_primary=" errors+0}'
fi
printf 'JSR_PREFLIGHT_END\n'
