#!/bin/bash


install_package()
{
    apt-get update -y
    apt-get remove -y unattended-upgrades needrestart
    apt-get install -y golang dialog nano vim git unzip wget ntpdate dos2unix net-tools tree lshw xinetd rsync rsyslog dstat dnsutils
    apt-get install -y htop ncdu nload sysstat psmisc bash-completion bind9-utils ca-certificates curl gnupg lsb-release
    apt-get install -y fail2ban gcc g++ make jq nfs-common rpcbind libpam-cracklib cron chrony parted
    apt-get install -y curl lrzsz iotop bzip2 htop iftop supervisor lsof autojump libjsoncpp-dev jq redis-tools mariadb-client apache2-utils glusterfs-client
}

kernel_setting()
{
    echo ${_date}
    for file in /etc/systemd/system.conf /etc/systemd/user.conf /etc/security/limits.conf
    do
	cp ${file} ${file}_${_date}
    done
    echo "DefaultLimitNOFILE=500000:500000" | tee -a /etc/systemd/system.conf /etc/systemd/user.conf
    echo "DefaultLimitNPROC=500000:500000" | tee -a /etc/systemd/system.conf /etc/systemd/user.conf
    echo "root soft nproc 500000" | tee -a /etc/security/limits.conf
    echo "root hard nproc 500000" | tee -a /etc/security/limits.conf
    echo "root soft nofile 500000" | tee -a /etc/security/limits.conf
    echo "root hard nofile 500000" | tee -a /etc/security/limits.conf
    echo "* soft nproc 500000" | tee -a /etc/security/limits.conf
    echo "* hard nproc 500000" | tee -a /etc/security/limits.conf
    echo "* soft nofile 500000" | tee -a /etc/security/limits.conf
    echo "* hard nofile 500000" | tee -a /etc/security/limits.conf
}

system_setting()
{
    # configure hostname
    ip_format=`echo "${ip}" | sed 's/\./\-/g'`
    host_name="${datacenter}-${ip_format}"
    hostnamectl set-hostname $host_name
    sed -i "1i\\127.0.0.1 ${host_name}" /etc/hosts

    # configure ssh service settings
    mv /etc/ssh/sshd_config /etc/ssh/sshd_config_${_date}
    wget http://download.nct.vn/init/ubuntu22/config/sshd_config -P /etc/ssh
    sed -i "s/0.0.0.0/${ip}/g" /etc/ssh/sshd_config

    # set timezone
    timedatectl set-timezone Asia/Ho_Chi_Minh

    # ntpdate
    echo '*/10 * * * * /usr/sbin/ntpdate 192.168.100.19 >/dev/null 2>&1' >> /var/spool/cron/crontabs/root

    # create account
    groupadd -g 2000 web && useradd -s /bin/bash -g web -u 2000 -m web
}

install_docker()
{
    mkdir -p /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    apt-get update -y
    apt-get remove -y docker docker-engine docker.io containerd runc
    apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-compose
    mkdir -p /data/docker/data
    echo '{' >> /etc/docker/daemon.json
    echo '        "log-driver":"json-file",' >> /etc/docker/daemon.json
    echo '        "log-opts": {' >> /etc/docker/daemon.json
    echo '                "max-size":"1024m",' >> /etc/docker/daemon.json
    echo '                "max-file":"7"' >> /etc/docker/daemon.json
    echo '        },' >> /etc/docker/daemon.json
    echo '        "data-root": "/data/docker/data"' >> /etc/docker/daemon.json
    echo '}' >> /etc/docker/daemon.json
    usermod -aG docker web
    systemctl restart docker && systemctl enable docker docker.socket
}

create_user()
{
    declare -A user_dict
    user_dict['luger']='ssh-rsa [payload removed: 372 characters] fenghui.liu@proxy118'
    user_dict['zhao']='ssh-rsa [payload removed: 372 characters] zhaojialei'
    user_dict['anhvt']='ssh-rsa [payload removed: 716 characters] nct@NCTs-MacBook-Pro.local'
    for user in "${!user_dict[@]}"
    do
	id $user ||  useradd -m -s /bin/bash $user && \
	su - $user -c "mkdir ~/.ssh && chmod 0700 ~/.ssh" && \
	su - $user -c "echo ${user_dict[$user]} >> ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys" && \
	echo "$user ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers.d/$user
    done
}

main()
{
    download_dir="/tmp/download"
    _date=`date +%F-%H%M%S`
    ip=`ip ad| grep 'inet 192.168.1' | awk -F '[ /]+' '{print $3}' | head -1`
    mkdir -p ${download_dir}
    id=`grep '^ID=' /etc/os-release | awk -F '[="]+' '{print $2}'`
    version_id=`grep '^VERSION_ID=' /etc/os-release | awk -F '[".]+' '{print $2}'`
    os_version=${id}${version_id}
    datacenter="VNPT"
    create_user

    while true
    do
        read -p ' Make Sure You Had Already Format And Mount All Hard Disk And Modify /etc/fstab Before Initialize !!!!!! (y/n): ' initialized_disk
        if [[ "${initialized_disk}" == "y" || "${initialized_disk}" == "Y" ]];then
	    break
	elif [[ "${initialized_disk}" == "n" || "${initialized_disk}" == "N" ]];then
	    exit
	fi
    done

    while true
    do
        echo -e "  make a choice:\n    0 install all\n    1 install_package\n    2 kernel_setting\n    3 system_setting\n    4 install_docker\n    e exit"
        read choice
        case $choice in
        "0")
            install_package;kernel_setting;system_setting;install_docker;exit
        ;;
        "1")
            install_package
        ;;
        "2")
            kernel_setting
        ;;
        "3")
            system_setting
        ;;
        "4")
            install_docker
        ;;
        "e")
            exit 0
        ;;
        *)
            echo "wrong type"
        esac
    done
    echo 'end'
}

main
