#! /bin/bash
txtwhite='\033[97m'
warnblink='\033[5m\033[38;5;15;48;5;118m'
orangebold='\033[1m\033[38;5;208m'
bluebold='\033[1m\033[38;5;75m'
STOP="\e[0m"
printf "${orangebold}"
echo "================================================================================================="
printf "${STOP}"
#Script for installing NTP service and configuring
PS3='Please select your time zone: '
#add options for multi-option
options=("EASTERN: America/New_York" "CENTRAL: America/Chicago" "MOUNTAIN: America/Denver" "PACIFIC: America/Los_Angeles" "SOUTH AFRICA: Africa/Johannesburg" "CENTRAL EUROPE: Malta")
select opt in "${options[@]}"
do
    case $opt in
        "EASTERN: America/New_York")
            echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
            printf "${orangebold}"
            read -p "This will set the America/New_York timezone and restart the NTP service. Do you want to continue? [y/n] : " newyork
            printf "${STOP}"
            if [[ $newyork =~ ^[Yy]$ ]]; then
                timedatectl set-timezone America/New_York
                break
            else
                echo "OK, back to the main menu";
            fi
            ;;
        "CENTRAL: America/Chicago")
            echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
            printf "${orangebold}"
            read -p "This will set the America/Chicago timezone and restart the NTP service. Do you want to continue? [y/n] : " chicago
            printf "${STOP}"
            if [[ $chicago =~ ^[Yy]$ ]]; then
                timedatectl set-timezone America/Chicago
                break
            else
              echo "OK, back to the main menu";
            fi
            ;;
        "MOUNTAIN: America/Denver")
            echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
            printf "${orangebold}"
            read -p "This will set the America/Denver timezone and restart the NTP service. Do you want to continue? [y/n] : " denver
            printf "${STOP}"
            if [[ $denver =~ ^[Yy]$ ]]; then
                timedatectl set-timezone America/Denver
                break
            else
              echo "OK, back to the main menu";
            fi
            ;;
        "PACIFIC: America/Los_Angeles")
            echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
            printf "${orangebold}"
            read -p "This will set the America/Los_Angeles timezone and restart the NTP service. Do you want to continue? [y/n] : " losangeles
            printf "${STOP}"
            if [[ $losangeles =~ ^[Yy]$ ]]; then
                timedatectl set-timezone America/Los_Angeles
                break
            else
              echo "OK, back to the main menu";
            fi
            ;;
        "SOUTH AFRICA: Africa/Johannesburg")
            echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
            printf "${orangebold}"
            read -p "This will set the Africa/Johannesburg timezone and restart the NTP service. Do you want to continue? [y/n] : " johannesburg
            printf "${STOP}"
            if [[ $johannesburg =~ ^[Yy]$ ]]; then
                timedatectl set-timezone Africa/Johannesburg
                break
            else
              echo "OK, back to the main menu";
            fi
            ;;
        "CENTRAL EUROPE: Malta")
            echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
            printf "${orangebold}"
            read -p "This will set the Central Europe timezone and restart the NTP service. Do you want to continue? [y/n] : " centraleurope
            printf "${STOP}"
            if [[ $centraleurope =~ ^[Yy]$ ]]; then
                timedatectl set-timezone Europe/Paris
                break
            else
              echo "OK, back to the main menu";
            fi
            ;;
        *) echo "invalid option $REPLY";;
    esac
done
printf "${bluebold}"
toilet -f term --filter border -w 160 "Installing ntpd..."
printf "${STOP}"
apt-get -qq install ntp -y
apt -qq install systemd-timesyncd -y
sed -i 's/pool/#pool/g' /etc/ntp.conf
printf "${orangebold}"
read -p 'Enter first NTP server IP or hostname : ' ntpserver1
read -p 'Enter second NTP server IP or hostname : ' ntpserver2
printf "${STOP}"
sed -i "25i server $ntpserver1" /etc/ntp.conf
sed -i "26i server $ntpserver2" /etc/ntp.conf
systemctl restart ntp
mkdir /etc/scripts
touch /etc/scripts/ntp-startup.sh
echo -e '#!/bin/bash \nsudo systemctl start ntp' >> /etc/scripts/ntp-startup.sh
chmod +x /etc/scripts/ntp-startup.sh
crontab -l | { cat; echo "@reboot /etc/scripts/ntp-startup.sh"; } | crontab -
#printf "${orangebold}"
#echo -e "NTP Install Complete! Back to the main menu..."
#printf "${STOP}"
#exit