#!/bin/bash
#--File variables--
#
scriptrepowebsite=webstorage.exactasystems.com
scriptfolder=/mnt/remotefs
logfile=/etc/ExactaUbuntuConfigurator-Log
lastranfile=/etc/ExactaUbuntuConfigurator-LastRan
toiletfile=/usr/bin/toilet
#--Command-based variables--
#
currentday=$(date "+%d")
currentdate=$(date "+%c")
osversion=$(lsb_release -a | grep Release:)
lastrantouch=$(touch $lastranfile)
ver="v1.9.1"
#--Text Color variables--
#
txtwhite='\033[97m'
whiteboldblink='\033[1;5m\033[97m'
warnblink='\033[5m\033[38;5;15;48;5;118m'
orangebold='\033[1m\033[38;5;208m'
orangeboldblink='\033[1;5m\033[38;5;208m'
bluebold='\033[1m\033[38;5;75m'
blueboldblink='\033[1;5m\033[38;5;75m'
redstopblink='\033[5;1m\033[38;5;9m'
STOP='\e[0m'
#--"Last Ran" Check--
#
#This section makes sure the /etc/ExactaUbuntuConfigurator-LastRan file exists then checks it for a
#numerical value (would be pulled from the last day of the month the script was run). If that number matches
#the current day of the month, it skips all of the early installs and updates and goes straight to the menu
#
mkdir /etc/scripts
mkdir /etc/scripts/ExactaSetup
$lastrantouch
chmod 777 $lastranfile
if grep "$currentday" $lastranfile
	then
		printf "${orangeboldblink}"
		read -p "Script ran today already, skipping the updates. Press ENTER to resume..."
		printf "${STOP}"
	else
		#--Fancy Text Software section--`
		#
		apt-get -qq update -y       # Checks for updated packages
		echo -e "Installing \033[38;5;154mFIGlet \e[0mfor fancy fonts..."
		apt-get -qq install figlet -y
		git clone https://github.com/xero/figlet-fonts.git /usr/share/figlet/fonts/
		echo -e "Installing \033[38;5;152mTOIlet \e[0mfor even fancier fonts..."
		apt-get -qq install toilet -y
		if [ ! -f "$toiletfile" ]; then
    		echo -e "\033[5;1m\033[38;5;9mFont software did not install properly!\e[0m Waiting for 45 seconds while startup file locks clear..."
    		sleep 45s
    		echo -e "Installing \033[38;5;154mFIGlet \e[0mfor fancy fonts..."
			apt-get -qq install figlet -y
			git clone https://github.com/xero/figlet-fonts.git /usr/share/figlet/fonts/
			echo -e "Installing \033[38;5;152mTOIlet \e[0mfor even fancier fonts..."
			apt-get -qq install toilet -y
		else
			printf "${blueboldblink}"
			echo -e "Font software installed successfully!"
			printf "${STOP}"
		fi
		if [ ! -f "$toiletfile" ]; then
    		echo -e "\033[5;1m\033[38;5;9mFont software STILL did not install properly!\e[0m You may have an internet connection issue. Please resolve and try running script again..."
    		exit
    	fi
   
		#--Welcome Banner--
		#
		printf "${orangebold}"
		echo "================================================================================================="
		figlet -f '/usr/share/figlet/fonts/Big Money-nw.flf' Exacta
		printf "${STOP}"
		printf "${bluebold}"
		figlet -f '/usr/share/figlet/fonts/Big Money-nw.flf' Systems
		printf "${STOP}"
		echo ""
		printf "${whiteboldblink}"
		echo  "            -----------------Ubuntu Configurator Script $ver-----------------"
		printf "${STOP}"
		printf "${orangebold}"
		toilet -f term --filter border -w 160 'Welcome to the Exacta Systems Ubuntu configurator script! Press CTRL+C to quit at any time'
		echo -e '                                     \033[5mPLEASE READ\e[0m'                       
		echo -e 'The script will now perform some automated installs including any OS and software package updates'
		printf "${whiteboldblink}"
		printf "${STOP}"
		read -p "Press ENTER to resume..."
		sleep 2s
		#--OS Update Section--
		#
		echo -e "Installing \033[38;5;125mnmon \e[0mfor fancy monitoring..."
		apt-get -qq install nmon -y
		echo -e "Installing \033[38;5;228mnet-tools \e[0mbecause why not?..."
		apt-get -qq install net-tools -y
		printf "${bluebold}"
		toilet -f term --filter border -w 160 "Updating Ubuntu software repos..."
		printf "${STOP}"
		apt-get -qq update -y       # Checks for updated packages
		printf "${bluebold}"
		toilet -f term --filter border -w 160 "Upgrading Ubuntu OS & software packages. This might take a bit..."
		printf "${STOP}"
		apt-get -qq upgrade -y      # Installs updated packages
		printf "${bluebold}"
		toilet -f term --filter border -w 160 "Cleaning up old OS & software packages..."
		printf "${STOP}"
		apt-get -qq autoremove -y       # Removes deprecated dependencies
		#--Python Check--
		#You need Python's program PIP to install Gdown as well as some other tasks in other sub-scripts
		#Ubuntu 18.0.4 doesn't support Python3 so I created this check to ask what version your OS is running and give the one the OS needs
		#
		printf "${blueboldblink}"
		echo $osversion
		printf "${STOP}"
		printf "${orangebold}"
		read -p "Is this server's Ubuntu version 18 or 20 (see above)? [type: 18/20]?" versionprompt
			if [[ $versionprompt =~ ^[18]+$ ]]; then
			    apt-get install debhelper -t bionic-backports -y
				printf "${bluebold}"
				toilet -f term --filter border -w 160 "Installing PIP for Python libraries to run. This one takes a while..."
				printf "${STOP}"
				apt-get -qq install python-pip -y
				printf "${bluebold}"
		        toilet -f term --filter border -w 160 "Installing gdown for better Google Drive downloads..."
		        printf "${STOP}"
					sudo pip install --upgrade --no-cache-dir gdown --quiet
		            #Check for log file and download if it isn't there
		        if [ ! -f "$logfile" ]; then
    		        gdown 1lNDl9Ejiavtcqy0RqTDyqMcxxQPic4sA
			        mv ExactaUbuntuConfigurator-Log $logfile
			        chmod 777 $logfile
			        fi
			elif [[ $versionprompt =~ ^[20]+$ ]]; then
				printf "${bluebold}"
				toilet -f term --filter border -w 160 "Installing PIP3 for Python libraries to run. This one takes a while..."
				printf "${STOP}"
				apt-get -qq install python3-pip -y
				printf "${bluebold}"
		        toilet -f term --filter border -w 160 "Installing gdown for better Google Drive downloads..."
		        printf "${STOP}"
					sudo pip3 install --upgrade --no-cache-dir gdown --quiet
		            #Check for log file and download if it isn't there
		        if [ ! -f "$logfile" ]; then
    		        gdown 1lNDl9Ejiavtcqy0RqTDyqMcxxQPic4sA
			        mv ExactaUbuntuConfigurator-Log $logfile
			        chmod 777 $logfile
			        fi
		fi
			#elif [[ $versionprompt =~ ^[22]+$ ]]; then
			#	printf "${bluebold}"
			#	toilet -f term --filter border -w 160 "Installing PIP3 for Python libraries to run. This one takes a while..."
			#	printf "${STOP}"
			#	apt-get -qq install python3-pip -y
			#		sudo pip3 install --upgrade --no-cache-dir gdown --quiet
		            #Check for log file and download if it isn't there
		    #    if [ ! -f "$logfile" ]; then
    		#       gdown 1lNDl9Ejiavtcqy0RqTDyqMcxxQPic4sA
			#        mv ExactaUbuntuConfigurator-Log $logfile
			#        chmod 777 $logfile
			#else
			#	echo "That is not a valid option. Exiting script..."
			#exit 1
			#fi
		#--Gdown Install--
		#

	
		#Remove unwanted MotD information and download logo
		mv /etc/update-motd.d/00-header /etc/update-motd.d/01-header
		gdown 1CswkUvjeEoIYd3SZaA75EtDGfz2Ut7B0
		mv 00-exactalogo /etc/update-motd.d/
		chmod +x /etc/update-motd.d/00-exactalogo
		rm /etc/update-motd.d/10-help-text
		rm /etc/update-motd.d/50-motd-news
		rm /etc/update-motd.d/90-updates-available
		rm /etc/update-motd.d/91-release-upgrade
		echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
		echo -e "\033[1m\033[38;5;208mNext prompt will configure the \033[1m\033[38;5;15mROOT user \033[1m\033[38;5;208mpassword. Please enter ROOT user password from the \033[38;5;15m'Site Linux/Ubuntu Server Root User' \033[1m\033[38;5;208mnote in LastPass\e[0m"
		read -p "Press ENTER to resume..."
		passwd root
		echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
		printf "${orangebold}"
		read -p "Is this an EC2 instance hosted AWS? [y/n]?" cont
		printf "${STOP}"
			if [[ $cont =~ ^[Yy]$ ]]; then
			  useradd -m -G sudo,adm -s /bin/bash user;
			  echo -e "\033[1m\033[38;5;208mNext prompt will configure the \033[1m\033[38;5;15mUSER user account \033[1m\033[38;5;208mpassword. Please enter USER user password from the \033[38;5;15m'Site Linux/Ubuntu Server Root User' \033[1m\033[38;5;208mnote in LastPass\e[0m"
			  passwd user
			  sed -i "41i PubkeyAuthentication no" /etc/ssh/sshd_config
			  sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/" /etc/ssh/sshd_config
			else
			  echo "OK, moving on!";
			fi
		sleep 1s
		echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
		printf "${orangebold}"
		read -p "Is this a VM hosted on a Synology NAS? [y/n]?" cont
		printf "${STOP}"
			if [[ $cont =~ ^[Yy]$ ]]; then
			  printf "${bluebold}"
			  toilet -f term --filter border -w 160 "Installing qemu-guest-agent for Synology VM management..."
			  printf "${STOP}"
			  apt-get -qq install qemu-guest-agent -y;
			else
			  echo "OK, moving on!";
			fi
		sleep 2s
		echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
		printf "${orangebold}"
		read -p "Is this a VM hosted on a Hyper-V hypervisor? [y/n]?" hyperv
		printf "${STOP}"
			if [[ $hyperv =~ ^[Yy]$ ]]; then
				  printf "${bluebold}"
				  toilet -f term --filter border -w 160 "Installing Hyper-V Linux Integration Services (LIS). This might take a bit..."
				  printf "${STOP}"
				  sed -i "20i hv_vmbus" /etc/initramfs-tools/modules;
				  sed -i "21i hv_storvsc" /etc/initramfs-tools/modules;
				  sed -i "22i hv_blkvsc" /etc/initramfs-tools/modules;
				  sed -i "23i hv_netvsc" /etc/initramfs-tools/modules;
				  apt-get -qq install linux-virtual linux-cloud-tools-virtual linux-tools-virtual -y;
				  update-initramfs -u
				  printf "${orangebold}"
				  echo -e 'Hyper-V LIS will start running after the next reboot...'
				  sleep 2s
				  printf "${STOP}"
				else
				echo "OK, moving on!"
				sleep 2
				fi
	echo "$currentday" > $lastranfile
fi
#apt-get install sshfs -y
#mkdir /etc/scripts
#mkdir /etc/scripts/ExactaSetup
#mkdir /mnt/remotefs
#sudo chmod 777 /mnt/remotefs
#If you ever have a problem interacting with a file in /mnt/remotefs/etc/scripts/ExactaSetup you may need to
#chown the file on the webstorage server for user zggwahrlfttlcgeqmwbsdebfcaoho
#ssh-keyscan -t ecdsa -p 55512 $scriptrepowebsite >> ~/.ssh/known_hosts
#sshfs zggwahrlfttlcgeqmwbsdebfcaoho@$scriptrepowebsite:/ /mnt/remotefs -p 55512
#
#
#----Menu Section. Script skips to here if it's been ran today already
#
#
printf "${orangebold}"
echo "================================================================================================="
figlet -f '/usr/share/figlet/fonts/Big Money-nw.flf' Exacta
printf "${STOP}"
printf "${bluebold}"
figlet -f '/usr/share/figlet/fonts/Big Money-nw.flf' Systems
printf "${STOP}"
echo ""
printf "${whiteboldblink}"
echo  "-----------------Ubuntu Configurator Script $ver-----------------"
printf "${STOP}"
printf "${orangebold}"
echo "================================================================================================="
printf "${STOP}"
PS3='Please enter your choice: '
#add options for multi-option
options=("FIRST TIME SETUP: Run all REQUIRED scripts" "REQUIRED: Install/Update RADIUS Deployment and update RADIUS users" "REQUIRED: Install/Update NTP Deployment" "REQUIRED: Install/Update SNMP Deployment" "REQUIRED: Install/Update RSYSLOG Deployment" "REQUIRED: Install/Update Forticlient Deployment" "OPTIONAL: Install/Update Uptime Kuma" "OPTIONAL: Install/Update Syslog-NG" "Quit")
select opt in "${options[@]}"
do
	case $opt in
		"FIRST TIME SETUP: Run all REQUIRED scripts")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 1: First Time Setup aka RUN ALL REQUIRED SCRIPTS"
			printf "${STOP}"
			echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
			printf "\033[1m\033[38;5;197m"
			read -p "This will run all Ubuntu setup scripts without pausing. Do you want to continue? y/n : " runall
			printf "\e[0m"
			if [[ $runall =~ ^[Yy]$ ]]
				then
					gdown 1dledzPMwLSbQWdg8Ui4j1TD1BdB9k4WN #download RADIUS install script
					gdown 1Y67fP2DeMK2jYJV7jfy3QICRbMTJL0WZ #download NTP install script
					gdown 1K5j-Zkc0CeO7d-OINjaxJjy3o7FrUzJH #download SNMP install script
					gdown 1Z7rayDCcMvaSO8fgmyw2jLRv7R6l4bVI #download RSYSLOG FIRST TIME install script
					gdown 101jrnvQVx6pn_XGBYFIoYnpCJ3vv_Meb #download Forticlient install script
					bash radius-install.sh 
					sed -i -e '2s/.*/RADIUS - $ver/' $logfile
					bash ntp-install.sh 
					sed -i -e '3s/.*/NTP - $ver/' $logfile
					bash snmp-install.sh 
					sed -i -e '4s/.*/SNMP - $ver/' $logfile
					bash rsyslog-first-install.sh
					sed -i -e '5s/.*/RSYSLOG - $ver/' $logfile
					#printf "${orangeboldblink}"
					#read -p "Is this server going to be used in production [y/n]?" forticlientask
					#printf "${STOP}"
					#    if [[ $forticlientask =~ ^[Yy]$ ]]; then
					bash forticlientems-install.sh
					sed -i -e '6s/.*/Forticlient - $ver/' $logfile
					sed -i -e '1s/.*/ALL REQUIRED SCRIPTS OPTION LAST RAN - $currentdate/' $logfile	
					echo -e "\e[5m\033[38;5;0;48;5;118m These settings will apply on next reboot! \e[0m"
					printf "${orangeboldblink}"
					read -p "These settings will apply on next reboot!"
					printf "${STOP}"
					#echo -e "\e[5m\033[38;5;0;48;5;118m STOP! \e[0m"
					#printf "${orangeboldblink}"
					#read -p "Need to reboot to apply new hostname. Ready to reboot? [y/n]?" allreboot
					#printf "${STOP}"
					#	if [[ $allreboot =~ ^[Yy]$ ]]
					#	then
					#		echo "Rebooting in 15 seconds. Thank you, come again!"
					#		sleep 15s
					#		reboot
					#	else
					#		echo "OK, remember to reboot when you're ready"
					#	fi     
			fi
			;;
		"REQUIRED: Install/Update RADIUS Deployment and update RADIUS users")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 2: Update RADIUS Deployment"
			printf "${STOP}"
			read -p "This will update your deployed RADIUS configuration to the latest version. Do you want to continue? y/n : " option2
			if [[ $option2 =~ ^[Yy]$ ]]; then
			gdown 1dledzPMwLSbQWdg8Ui4j1TD1BdB9k4WN; #download RADIUS install script
			sed -i -e '2s/.*/RADIUS - $ver/' $logfile
			bash radius-install.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"REQUIRED: Install/Update NTP Deployment")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 3: Update NTP Deployment"
			printf "${STOP}"
			read -p "This will update your deployed NTP configuration to the latest version. Do you want to continue? y/n : " option3
			if [[ $option3 =~ ^[Yy]$ ]]; then
			gdown 1Y67fP2DeMK2jYJV7jfy3QICRbMTJL0WZ; #download NTP install script
			sed -i -e '3s/.*/NTP - $ver/' $logfile
			bash ntp-install.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"REQUIRED: Install/Update SNMP Deployment")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 4: Update SNMP Deployment"
			printf "${STOP}"
			read -p "This will update your deployed SNMP configuration to the latest version. Do you want to continue? y/n : " option4
			if [[ $option4 =~ ^[Yy]$ ]]; then
			gdown 1K5j-Zkc0CeO7d-OINjaxJjy3o7FrUzJH; #download SNMP install script
			sed -i -e '4s/.*/SNMP - $ver/' $logfile
			bash snmp-install.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"REQUIRED: Install/Update RSYSLOG Deployment")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 5: Update RSYSLOG Deployment"
			printf "${STOP}"
			read -p "This will update your deployed RSYSLOG configuration to the latest version. Do you want to continue? y/n : " option5
			if [[ $option5 =~ ^[Yy]$ ]]; then
			gdown 1HfAxcPlwZfNoUthwAlM3QIAFDZJcPmsc; #download RSYSLOG install script
			sed -i -e '5s/.*/RSYSLOG - $ver/' $logfile
			bash rsyslog-install.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"REQUIRED: Install/Update Forticlient Deployment")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 6: Update Forticlient Deployment"
			printf "${STOP}"
			read -p "This will update your deployed FORTICLIENT configuration to the latest version. Do you want to continue? y/n : " option6
			if [[ $option6 =~ ^[Yy]$ ]]; then
			gdown 101jrnvQVx6pn_XGBYFIoYnpCJ3vv_Meb; #download Forticlient install script
			sed -i -e '6s/.*/FORTICLIENT - $ver/' $logfile
			bash forticlientems-install.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;    
		"OPTIONAL: Install/Update Uptime Kuma")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose OPTIONAL choice 7: Install/Update Uptime Kuma"
			printf "${STOP}"
			read -p "This will install the latest version of Uptime Kuma or update your deployed Uptime Kuma deployment to the latest version. Do you want to continue? y/n : " option7
			if [[ $option7 =~ ^[Yy]$ ]]; then
			gdown 1efmbcQEUgPjs2iwTPgKdMivTfaUmdW31; #download UK install script
			sed -i -e '7s/.*/UPTIME-KUMA - $ver/' $logfile
			bash Uptime-Kuma-Guided.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"OPTIONAL: Install/Update Syslog-NG")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose OPTIONAL choice 8: Install/Update Syslog-NG"
			printf "${STOP}"
			read -p "This will update your deployed Syslog-NG configuration to the latest version. Do you want to continue? y/n : " option8
			if [[ $option8 =~ ^[Yy]$ ]]; then
			gdown 1oRh7bmDuQKh-4pKSxn3yZDV2qx6dz595; #download Syslog-NG install script
			sed -i -e '8s/.*/SYSLOG-NG - $ver/' $logfile
			bash syslog-ng-install.sh
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"Reboot Ubuntu")
			printf "${orangebold}"
			toilet -f term --filter border -w 160 "You chose choice 9: Reboot Ubuntu"
			printf "${STOP}"
			read -p "This will restart the entire OS of the VM. Do you want to continue? y/n : " option9
			if [[ $option8 =~ ^[Yy]$ ]]; then
			reboot
			else
				echo "OK, back to the main menu";
				sleep 2s
			fi
			;;
		"Quit")
			break
			;;
		*) echo -e "\033[38;5;9mInvalid option\e[0m $REPLY";;
  	esac
done
#read -p "!! STOP !! Do you want to run any other scripts? y/n : " menuend
#if [[ $menuend =~ ^[Yy]$ ]]; then
#    continue
#else
#    break

umount /mnt/remotefs