#!/bin/sh
set -eu

export LC_ALL=C

expiry_epoch=$(date -d "2026-08-19 13:15:00 +0800" +%s)
[ "$(date +%s)" -le "$expiry_epoch" ] || {
    echo PLAN_EXPIRED
    exit 42
}

backup_dir=/workspace/zhct-backups/ECS-CRON-CHOWN-FIX-20260819-01
archive=$backup_dir/original-five-scripts.tar.gz
file_1=/workspace/wwwroot/datacenter/project/application/cron/shell/prod/sync_breezingpro.sh
file_2=/workspace/wwwroot/tnxlahtks/project/application/cron/shell/prod/sync_eliga_data_v1.sh
file_3=/workspace/wwwroot/tnxlahtks/project/application/cron/shell/prod/sync_eliga_data.sh
file_4=/workspace/wwwroot/tjkx/project/application/cron/shell/prod/sync_tj_medical.sh
file_5=/workspace/wwwroot/sxkx/project/application/cron/shell/prod/deal_wattbike_data.sh

sha256sum -c "$backup_dir/archive.sha256"

check_original() {
    file_path=$1
    expected=$2
    actual=$(sha256sum "$file_path" | cut -d " " -f1)
    [ "$actual" = "$expected" ]
    archived=$(tar -xOf "$archive" "${file_path#/}" | sha256sum | cut -d " " -f1)
    [ "$archived" = "$expected" ]
}

check_original "$file_1" 45709431529317e9bb5b3441645f4bcc54f9170e78ad4287c153da7f55743e68
check_original "$file_2" 42228f6eb54846a64900d0139ee96906ff056be4df3e899a261376fc7c665737
check_original "$file_3" 9bac452d7775d3455157e015013096ec0638c91aef53225341ac5cf4b0b935aa
check_original "$file_4" 9ac0392b4fdace0a64ad084b7a11f486966cd6a65fef24d725036d2c360abc50
check_original "$file_5" 33c5eecb5fbde3d45c3a076ab51357360e2bb0f8680668f3790d98e89b880b9a

prepare() {
    file_path=$1
    output_path=$2
    awk '
        /^chown -R www:www \/workspace\/wwwroot\/log\/?$/ {
            print "if [ \"$(id -u)\" -eq 0 ]; then"
            print "    chown -R www:www /workspace/wwwroot/log/"
            print "fi"
            replaced++
            next
        }
        { print }
        END { if (replaced != 1) exit 46 }
    ' "$file_path" > "$output_path"
    sh -n "$output_path"
    [ "$(grep -F -c 'if [ "$(id -u)" -eq 0 ]; then' "$output_path")" -eq 1 ]
}

prepare "$file_1" "$backup_dir/prepared/0.sh"
prepare "$file_2" "$backup_dir/prepared/1.sh"
prepare "$file_3" "$backup_dir/prepared/2.sh"
prepare "$file_4" "$backup_dir/prepared/3.sh"
prepare "$file_5" "$backup_dir/prepared/4.sh"

if install -o www -g www -m 755 "$backup_dir/prepared/0.sh" "$file_1" \
    && install -o www -g www -m 755 "$backup_dir/prepared/1.sh" "$file_2" \
    && install -o www -g www -m 755 "$backup_dir/prepared/2.sh" "$file_3" \
    && install -o www -g www -m 755 "$backup_dir/prepared/3.sh" "$file_4" \
    && install -o www -g www -m 755 "$backup_dir/prepared/4.sh" "$file_5"; then
    :
else
    tar -xzpf "$archive" -C /
    echo INSTALL_FAILED_ROLLED_BACK
    exit 47
fi

verify_file() {
    file_path=$1
    sh -n "$file_path"
    [ "$(grep -F -c 'if [ "$(id -u)" -eq 0 ]; then' "$file_path")" -eq 1 ]
    [ "$(grep -E -c '^chown -R www:www /workspace/wwwroot/log/?$' "$file_path")" -eq 0 ]
    [ "$(stat -c %U:%G "$file_path")" = www:www ]
    [ "$(stat -c %a "$file_path")" = 755 ]
}

if verify_file "$file_1" \
    && verify_file "$file_2" \
    && verify_file "$file_3" \
    && verify_file "$file_4" \
    && verify_file "$file_5"; then
    :
else
    tar -xzpf "$archive" -C /
    echo VERIFY_FAILED_ROLLED_BACK
    exit 48
fi

: > "$backup_dir/patched.sha256"
for file_path in "$file_1" "$file_2" "$file_3" "$file_4" "$file_5"; do
    stat -c "PATCHED %U:%G %a %n" "$file_path" >> "$backup_dir/patched.sha256"
    sha256sum "$file_path" >> "$backup_dir/patched.sha256"
done

printf "plan_id=ECS-CRON-CHOWN-FIX-20260819-01\nexecuted_at=%s\nexpiry=2026-08-19T13:15:00+08:00\n" \
    "$(date -Is)" > "$backup_dir/execution.txt"

echo MUTATION_SUCCESS
cat "$backup_dir/patched.sha256"
