#!/bin/sh

clear
echo ""
echo -e "\033[1;36m======================================\033[0m"
echo -e "\033[1;33m    海纳思独立监控面板 - 一键安装    \033[0m"
echo -e "\033[1;35m           作者：Sebastian           \033[0m"
echo -e "\033[1;36m======================================\033[0m"
echo ""

read -p "  确定要安装监控面板吗？(y/n): " ans
if [ "$ans" != "y" ] && [ "$ans" != "Y" ]; then
    echo ""
    echo -e "\033[1;31m  安装已取消！\033[0m"
    echo ""
    exit 0
fi

echo ""
echo -n "  正在安装: ["

# 10秒进度条，一共20格，每格0.5秒
total=20
for i in $(seq 1 $total); do
    echo -n "="
    percent=$((i * 100 / total))
    echo -ne "\033[1;32m$percent%\033[0m\r  正在安装: ["
    sleep 0.5
done

echo "] 100%"
echo -e "\033[1;32m  安装完成！\033[0m"
echo ""

cat > /status/www/info.php <<'EOF'
<?php
if (isset($_GET['ajax'])) {
    $temp = round(file_get_contents('/sys/class/thermal/thermal_zone0/temp') / 1000, 1);

    $disks = [];
    $lines = explode("\n", shell_exec('df -h 2>/dev/null'));
    foreach ($lines as $line) {
        $line = trim($line);
        if (!$line) continue;
        $parts = preg_split('/\s+/', $line);
        if (count($parts) < 6) continue;

        $mount = $parts[5];
        $skip = ['/proc','/sys','/dev','/run','/boot','/tmp'];
        $skipIt = false;
        foreach ($skip as $s) if (strpos($mount, $s) === 0) { $skipIt = true; break; }
        if ($skipIt) continue;

        $disks[] = [
            'mount'   => $mount,
            'total'   => $parts[1],
            'used'    => $parts[2],
            'avail'   => $parts[3],
            'percent' => (int)trim($parts[4], '%')
        ];
    }

    $net = shell_exec('cat /sys/class/net/eth0/statistics/tx_bytes /sys/class/net/eth0/statistics/rx_bytes 2>/dev/null');
    list($tx1, $rx1) = array_map('trim', explode("\n", $net));
    sleep(1);
    $net2 = shell_exec('cat /sys/class/net/eth0/statistics/tx_bytes /sys/class/net/eth0/statistics/rx_bytes 2>/dev/null');
    list($tx2, $rx2) = array_map('trim', explode("\n", $net2));

    $up = ($tx2 - $tx1) * 8 / 1024;
    $down = ($rx2 - $rx1) * 8 / 1024;

    echo json_encode([
        'temp'  => $temp,
        'disks' => $disks,
        'up'    => round($up, 1),
        'down'  => round($down, 1)
    ]);
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>系统监控面板</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:linear-gradient(135deg,#1E293B,#242E3D);color:#fff;font-family:"Microsoft YaHei",sans-serif;min-height:100vh;padding:20px}
.container{max-width:500px;margin:0 auto}
.author{text-align:center;font-size:24px;font-weight:bold;color:#FFD700;text-shadow:0 0 12px rgba(255,215,0,0.6);margin-bottom:20px;letter-spacing:2px}
.card{background:#324052;border-radius:16px;padding:22px;margin-bottom:16px}
.title{font-size:18px;margin-bottom:18px;display:flex;align-items:center;gap:8px}
.circle-box{position:relative;width:110px;height:110px;margin:0 auto}
.circle-bg{fill:none;stroke:#445264;stroke-width:8;cx:55;cy:55;r:47}
.circle-progress{fill:none;stroke-width:8;cx:55;cy:55;r:47;stroke-dasharray:295.3;stroke-dashoffset:295.3;transition:all 1s ease}
.circle-text{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);font-size:18px;font-weight:bold}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:16px}
.item{text-align:center}
.label{font-size:13px;color:#9ab;margin-top:8px}
.disk-info{font-size:12px;color:#ccc;margin-top:4px}
.time{text-align:center;font-size:12px;color:#9ab;margin-top:20px}
</style>
</head>
<body>
<div class="container">
    <div class="author">作者：Sebastian</div>
    <div class="card"><div class="title">🚀 CPU 温度</div>
        <div class="circle-box"><svg width="110" height="110"><circle class="circle-bg"></circle><circle id="temp-circle" class="circle-progress"></circle></svg><div id="temp-text" class="circle-text">--°C</div></div>
    </div>
    <div class="card"><div class="title">🌐 网速监控</div>
        <div class="grid">
            <div class="item"><div class="circle-box"><svg width="110" height="110"><circle class="circle-bg"></circle><circle id="up-circle" class="circle-progress" stroke="#2196F3"></circle></svg><div id="up-text" class="circle-text">0 KB</div></div><div class="label">上行</div></div>
            <div class="item"><div class="circle-box"><svg width="110" height="110"><circle class="circle-bg"></circle><circle id="down-circle" class="circle-progress" stroke="#4CAF50"></circle></svg><div id="down-text" class="circle-text">0 KB</div></div><div class="label">下行</div></div>
        </div>
    </div>
    <div class="card"><div class="title">💿 磁盘容量</div><div class="grid" id="disk-list">加载中...</div></div>
    <div class="time" id="update-time">更新时间：--:--:--</div>
</div>
<script>
function getTempColor(t){if(t<=50)return'#4CAF50';if(t<=65)return'#FFC107';if(t<=80)return'#FF9800';return'#F44336'}
function getDiskColor(p){if(p<=60)return'#4CAF50';if(p<=80)return'#FFC107';if(p<=90)return'#FF9800';return'#F44336'}
function setCircle(id,val,max,color){const circ=295.3;const ratio=Math.min(val/max,1);const offset=circ*(1-ratio);const e=document.getElementById(id);e.style.stroke=color;e.style.strokeDashoffset=offset}
async function update(){const res=await fetch('?ajax=1');const data=await res.json();
setCircle('temp-circle',data.temp,100,getTempColor(data.temp));document.getElementById('temp-text').innerText=data.temp+'°C';
document.getElementById('up-text').innerText=data.up+' KB';document.getElementById('down-text').innerText=data.down+' KB';
setCircle('up-circle',data.up,10000,'#2196F3');setCircle('down-circle',data.down,10000,'#4CAF50');
let html='';data.disks.forEach((d,i)=>{const c=getDiskColor(d.percent);html+=`<div class="item"><div class="circle-box"><svg width="110" height="110"><circle class="circle-bg"></circle><circle id="d-${i}" class="circle-progress"></circle></svg><div class="circle-text">${d.percent}%</div></div><div class="label">${d.mount}</div><div class="disk-info">${d.used}/${d.total}</div></div>`});
document.getElementById('disk-list').innerHTML=html;data.disks.forEach((d,i)=>{setCircle('d-'+i,d.percent,100,getDiskColor(d.percent))});
document.getElementById('update-time').innerText='更新时间：'+new Date().toLocaleTimeString()}
setInterval(update,2000);update();
</script>
</body>
</html>
EOF

# 自动获取IP并带上8088端口
IP=$(hostname -I | awk '{print $1}')

echo ""
echo -e "\033[1;36m  访问地址：http://$IP:8088/info.php\033[0m"
echo ""
echo -e "\033[1;33m  面板与海纳思系统完全独立，不影响原有功能\033[0m"
echo -e "\033[1;32m  支持远程云更新，后期直接后台更新即可\033[0m"
echo -e "\033[1;35m  作者：Sebastian\033[0m"
echo ""
