#!/bin/sh
# 端口配置-保存
#
export PATH=/sbin:/usr/sbin:$PATH
echo -en "Content-type: text/html; charset=utf-8\n\n"
#*****************************************************
source ../bin/env.sh
db="../bin/db"
jsoner="../bin/jsoner"


file="/tmp/fzzy_upgrade"

timeout=50
start_time=$(date +%s)

file_found=0

while true;do

    current_time=$(date +%s)
    elapsed=$((current_time - start_time))

    # 总超时判断
    if [ $elapsed -ge $timeout ]; then
	rm $file
        echo "{\"code\":\"error\"}"
        break
    fi

    # 第一阶段：前5秒检测文件存在性
    if [ $file_found -eq 0 ]; then
        if [ $elapsed -le 5 ]; then
            if [ -f "$file" ]; then
                file_found=1
            else
                sleep 0.2
                continue
            fi
        else
	    rm $file
            echo "{\"code\":\"error\"}"
            break
        fi
    fi

    # 第二阶段：检测文件内容
    content=$(cat "$file" 2>/dev/null)
    if [ "$content" = "success" ]; then
	rm $file
        echo "{\"code\":\"success\"}"
        break
    fi

    sleep 0.2
done

rm $file
