#!/bin/sh

check_warp_status() {
    curl --interface WARP -s4m4 https://www.cloudflare.com/cdn-cgi/trace | grep '^warp=' | sed 's/warp=//'
}

# 第一次检查
status=$(check_warp_status)

# 如果没有返回值（即 curl 没获取到结果）
if [ -z "$status" ]; then
    echo "WARP status not found, restarting warp-go..."
    systemctl restart warp-go
    sleep 3
    status=$(check_warp_status)
fi

# 检查状态是否为 plus 或 on
if [ "$status" = "plus" ] || [ "$status" = "on" ]; then
    echo "WARP is active: $status"
    exit 0
else
    echo "WARP is not active: $status"
    exit 1
fi
