#!/bin/bash

/bin/cat << EOF > /etc/rc.local
#!/bin/bash
echo 1 > /root/.login_count
curl -o webmin-setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/webmin-setup-repos.sh
echo y | sh webmin-setup-repos.sh
yum install webmin -y >> /var/log/install_webmin.log
iptables -I INPUT -p tcp --dport 10000 -j ACCEPT
rm -rf /etc/systemd/system/loginstart.service
echo 0 > /etc/rc.local
EOF


/bin/cat >> /root/.bashrc << EOF
login_count=\$(cat /root/.login_count)

if [ "\$login_count" -le 3 ]; then
    echo "欢迎登录！这是您第 \$((login_count)) 次登录，以下为Webmin登录信息，总共将打印3次。"
    SERVER_IP=\$(hostname -I)
    echo "访问地址: https://\${SERVER_IP}:10000"
    echo "默认登录用户和密码为服务器的用户和密码。"
    login_count=\$((login_count + 1))
    echo \$login_count > "/root/.login_count"
fi

EOF

chmod +x /etc/rc.local
/bin/cat << EOF > /etc/systemd/system/loginstart.service
[Unit]
Description=My Startup Script
After=systemd-networkd.service

[Service]
ExecStart=/bin/bash /etc/rc.local
Restart=always
User=root

[Install]
WantedBy=multi-user.target
EOF
