#!/bin/bash
set -euo pipefail

# 可調參數
WAIT_INTERVAL_SEC=30
WAIT_TIMEOUT_SEC=300   # 最多等候秒數 (例如 5 分鐘)

usage() {
    exec echo "Usage: $0 <ACTION: open|close> <MACHINE1> [MACHINE2 ...]"
}

# 參數檢查
[[ $# -lt 2 ]] && usage
ACTION="$1"; shift

case "${ACTION}" in
    open)
        param="open"
        return_code="1"
        ;;
    close)
        param="close"
        return_code="0"
        ;;
    *)
        echo "ERROR: ACTION must be 'open' or 'close'"; usage
        ;;
esac

MACHINES=("$@")

# 單台檢查函式：成功回傳 0，逾時/錯誤回傳 1
check_one() {
    local host="$1"
    local start now elapsed result

    echo "==> [$host] start ACTION: $ACTION (timeout ${WAIT_TIMEOUT_SEC}s, interval ${WAIT_INTERVAL_SEC}s)"
    start=$(date +%s)

    while true; do
        # 你可視環境加入更多 SSH 選項
        # 例如：-o BatchMode=yes -o ConnectTimeout=5
        if ! result=$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$host" "running_server_tools ${param}" 2>/dev/null); then
            echo "[$host] ssh failed (will retry until timeout)"
            result=""
        fi

        if [[ "$result" == "${return_code}" ]]; then
            echo "[$host] ${param} Finished"
            return 0
        fi

        now=$(date +%s)
        elapsed=$(( now - start ))
        if (( elapsed >= WAIT_TIMEOUT_SEC )); then
            echo "[$host] ${param} Timeout after ${WAIT_TIMEOUT_SEC}s (last result: '${result}')"
            return 1
        fi

        echo "[$host] ${param} Not finished yet (elapsed ${elapsed}s). Sleep ${WAIT_INTERVAL_SEC}s..."
        sleep "$WAIT_INTERVAL_SEC"
    done
}

# 逐台執行
fail=0
for host in "${MACHINES[@]}"; do
    if ! check_one "$host"; then
        fail=1
    fi
done

if (( fail == 0 )); then
    echo "All machines (${MACHINES[@]}) finished."
else
    echo "Some machines failed or timed out."
fi

exit "$fail"
