#!/bin/bash

# ==================== Ubuntu 24.04 初始化脚本 ====================
# 对应原 centos9_init.sh 的功能

set -e  # 遇到错误立即退出，便于排查

echo -e "\033[32m=== 开始 Ubuntu 24.04 系统初始化 ===\033[0m"

# ---------- 1. 防火墙处理 ----------
# Ubuntu 默认使用 ufw，一般未启用，直接禁用并停止
if systemctl status ufw >/dev/null 2>&1; then
    echo "关闭 ufw 防火墙..."
    sudo ufw disable
    sudo systemctl stop ufw
    sudo systemctl disable ufw
else
    echo "ufw 未安装或未启用，跳过"
fi

# ---------- 2. 安装基础软件包 ----------
# 更新软件源
sudo apt update -y

# 安装基础工具（替换 yum 包名，Ubuntu 中大部分名称一致）
echo "安装基础工具包..."
sudo apt install -y \
    wget \
    lrzsz \
    gcc \
    g++ \
    perl \
    unzip \
    make \
    automake \
    autoconf \
    libtool \
    net-tools \
    # 以下按需安装，若有需要可取消注释
    # htop \
    # dnsutils (nslookup) \
    # psmisc (killall) \
    # axel \
    # netcat-openbsd (nc) \
    # lsof \
    # nmap \
    # logrotate

# ---------- 3. 调整文件打开数限制 ----------
ulimit_status=$(grep -c "ulimit" /etc/profile || true)
if [ $ulimit_status -eq 0 ]; then
    echo "ulimit -SHn 51200" | sudo tee -a /etc/profile > /dev/null
    source /etc/profile
else
    echo "ulimit 配置已存在，跳过"
fi

# 写入 limits.conf
sudo tee /etc/security/limits.conf > /dev/null <<EOF
* soft nproc 1024
* hard nproc 1024
* soft nofile 51200
* hard nofile 51200
EOF

# ---------- 4. 设置中国时区 ----------
sudo timedatectl set-timezone Asia/Shanghai
# 硬件时钟使用本地时间（可选，原脚本未设置，但建议）
# sudo timedatectl set-local-rtc 1

# ---------- 5. 时间同步（使用 ntpdate + cron，与原行为一致） ----------
# 安装 ntpdate（Ubuntu 仓库中有）
sudo apt install -y ntpdate

# 添加 crontab 任务（每分钟/每天同步，原脚本为每天 5:10）
cron_job="10 5 * * * root /usr/sbin/ntpdate timekeeper.isi.edu && /sbin/hwclock -w"
if ! grep -q "ntpdate" /etc/crontab; then
    echo "$cron_job" | sudo tee -a /etc/crontab > /dev/null
    echo "已添加 cron 任务"
else
    echo "cron 任务已存在，跳过"
fi

# 立即执行一次同步
sudo /usr/sbin/ntpdate timekeeper.isi.edu && sudo /sbin/hwclock -w

# 注意：Ubuntu 默认使用 systemd-timesyncd，也可以启用它（推荐）
# 如果希望使用 systemd-timesyncd 替代 ntpdate，可取消下面注释
# sudo timedatectl set-ntp true

# ---------- 6. 设置内核参数 ----------
sudo tee /etc/sysctl.conf > /dev/null <<EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65535
EOF

sudo sysctl -p
#####关闭自动更新和禁用更新提示重启
# 1) 关闭自动更新
sudo cp -a /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/20auto-upgrades.bak.$(date +%Y%m%d%H%M%S)
printf 'APT::Periodic::Update-Package-Lists "0";\nAPT::Periodic::Unattended-Upgrade "0";\n' | sudo tee /etc/apt/apt.conf.d/20auto-upgrades >/dev/null

# 2) 禁用自动升级 timer
sudo systemctl disable --now unattended-upgrades.service
sudo systemctl disable --now apt-daily.timer apt-daily-upgrade.timer
sudo systemctl mask apt-daily.timer apt-daily-upgrade.timer

# 3) 安装包后不再提示重启服务
sudo tee /etc/needrestart/conf.d/99-local.conf >/dev/null <<'EOF'
$nrconf{restart} = 'l';
EOF


# ---------- 8. 附加说明 ----------
echo -e "\033[31m### 时区已设置为 Asia/Shanghai (CST) ###\033[0m"
echo -e "\033[31m### 使用 timedatectl 查看时间：timedatectl ###\033[0m"
echo -e "\033[31m### 可使用 chrony 或 systemd-timesyncd 替代 ntpdate ###\033[0m"
echo -e "\033[31m### 参考文档: https://www.cnblogs.com/lei0213/p/8723106.html ###\033[0m"

echo -e "\033[32m=== 初始化完成 ===\033[0m"
