#!/bin/bash
if ! grep -q "bullseye-backports" /etc/apt/sources.list; then
    echo 'deb http://deb.debian.org/debian bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free' >> /etc/apt/sources.list
fi
apt update
apt-get install -y tar vim curl wget git tig telnet net-tools whois dnsutils mtr iftop iotop htop vnstat ntpdate locales-all nfs-kernel-server nfs-common xfsprogs screen rsync sudo btop
git config --global credential.helper store
useradd nginx
ntpdate -u  pool.ntp.org
cat <(crontab -l) <(echo "*/20 * * * * /usr/sbin/ntpdate pool.ntp.org > /var/log/ntpdate.log 2>&1") | crontab -
service cron reload

# echo 'kernel.msgmni = 16384' >>  /etc/sysctl.conf  
# echo 'kernel.sem = 250 32000 100 1024' >>  /etc/sysctl.conf  
# echo 'kernel.pid_max = 4194303' >>  /etc/sysctl.conf  
# echo 'vm.overcommit_memory = 1' >>  /etc/sysctl.conf  

grep -qF '* soft nofile 102400' /etc/security/limits.conf  || echo '* soft nofile 102400' >>  /etc/security/limits.conf  
grep -qF '* hard nofile 102400' /etc/security/limits.conf  || echo '* hard nofile 102400' >>  /etc/security/limits.conf   

if [ ! -f /etc/sysctl.conf.bak ]; then
    mv /etc/sysctl.conf /etc/sysctl.conf.bak 
    wget http://list.downsoft.cn/Linux/sysinit/sysctl.conf  -O  /etc/sysctl.conf  
fi
modprobe nf_conntrack 
/sbin/sysctl -p

grep -qF 'ulimit -HSn 102400' /etc/profile  || echo 'ulimit -HSn 102400' >>  /etc/profile 
echo 1024 >/proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled


if [[ ! -f "/etc/rc.local" ]]
then
cat <<EOF >/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
EOF
chmod +x /etc/rc.local
fi

sed -i '/exit 0/d' /etc/rc.local && echo 'exit 0' >>  /etc/rc.local
grep -qF 'modprobe nf_conntrack ' /etc/rc.local || sed -i 's/exit 0/modprobe nf_conntrack\nexit 0/g' /etc/rc.local
grep -qF 'ulimit -HSn 102400' /etc/rc.local || sed -i 's/exit 0/ulimit -HSn 102400\nexit 0/g' /etc/rc.local
grep -qF '/sys/kernel/mm/transparent_hugepage/enabled' /etc/rc.local || sed -i 's/exit 0/echo never > \/sys\/kernel\/mm\/transparent_hugepage\/enabled\nexit 0/g' /etc/rc.local
grep -qF '/proc/sys/net/core/somaxconn' /etc/rc.local || sed -i 's/exit 0/echo 1024 >\/proc\/sys\/net\/core\/somaxconn\nexit 0/g' /etc/rc.local

source /etc/profile
ulimit -n 102400

sed -i 's/Port 22/#Port 22\n/g' /etc/ssh/sshd_config
sed -i 's/#Port 22/Port 22\nPort 17385\n/g' /etc/ssh/sshd_config
sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
sed -i 's/GSSAPICleanupCredentials yes/GSSAPICleanupCredentials no/g' /etc/ssh/sshd_config
sed -i 's/UsePAM no/UsePAM yes\n/g' /etc/ssh/sshd_config
sed -i 's/#UseDNS no/UseDNS no\n/g' /etc/ssh/sshd_config
sed -i 's/#TCPKeepAlive yes/TCPKeepAlive yes/g' /etc/ssh/sshd_config
sed -i '/#ClientAliveInterval/cClientAliveInterval 60' /etc/ssh/sshd_config
sed -i '/#ClientAliveCountMax/cClientAliveCountMax 60' /etc/ssh/sshd_config

day=$(date +%d)
if [ $day -lt 29 ]; then
    sed -i '/^;MonthRotate/c\MonthRotate 1' /etc/vnstat.conf
    sed -i "/^MonthRotate /c\MonthRotate $day" /etc/vnstat.conf
fi

systemctl restart sshd
systemctl enable vnstat
systemctl start vnstat

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
localedef -c -f UTF-8 -i en_US en_US.UTF8
ulimit -Sa | grep "open files"

if [ ! -f /usr/local/bin/besttrace ]; then
    wget http://list.downsoft.cn/Linux/sysinit/besttrace  -O  /usr/local/bin/besttrace  
    chmod +x /usr/local/bin/besttrace  
fi
bash -c "$(curl nxtrace.org/nt)"

mkdir /root/.ssh
apt-get install -y zsh
wget https://list.downsoft.cn/Linux/zsh/welcome -O /root/.welcome
sh -c "$(curl -fsSL https://list.downsoft.cn/Linux/zsh/oh-my-zsh-init.sh)"

mkdir -p /root/.oh-my-zsh/custom/plugins/
wget https://list.downsoft.cn/Linux/zsh/zsh-autosuggestions.tar.gz -O /root/zsh-autosuggestions.tar.gz
tar zxvf /root/zsh-autosuggestions.tar.gz  -C /root/.oh-my-zsh/custom/plugins/
chmod -R 644 /root/.oh-my-zsh/custom/plugins/zsh-autosuggestions
chown -R root:root /root/.oh-my-zsh/

mv /root/.zshrc /root/.zshrc.bak
wget https://list.downsoft.cn/Linux/zsh/zshrc -O /root/.zshrc
wget https://list.downsoft.cn/Linux/sysinit/vimrc -O /root/.vimrc
mv /root/.oh-my-zsh/lib/misc.zsh /root/.oh-my-zsh/lib/misc.zsh.bak
wget http://list.downsoft.cn/Linux/zsh/misc.zsh -O /root/.oh-my-zsh/lib/misc.zsh
#env zsh -l
#source /root/.zshrc
curl https://get.acme.sh | sh
mkdir /etc/cert 
rm -rf /root/zsh-autosuggestions.tar.gz

echo "#############"
#echo "wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh && chmod +x bbr.sh && ./bbr.sh"
echo "wget --no-check-certificate https://list.downsoft.cn/Linux/sysinit/bbr.sh && chmod +x bbr.sh && ./bbr.sh"
echo "wget https://list.downsoft.cn/Linux/sysinit/docker-debian.sh && chmod +x docker-debian.sh && ./docker-debian.sh"
echo "wget https://list.downsoft.cn/Linux/sysinit/ufw.sh && chmod +x ufw.sh && ./ufw.sh"
echo "#############"
#bash <(curl -s -L https://list.downsoft.cn/Linux/sysinit/docker-debian.sh)
#bash <(curl -s -L https://list.downsoft.cn/Linux/sysinit/ufw.sh)
#bash <(curl -s -L https://list.downsoft.cn/Linux/sysinit/bbr.sh)
#bash -c "$(curl http://nexttrace-io-leomoe-api-a0.shop/nt_install.sh)"

exit 0
