#!/bin/sh
# Pass -i parameter for interactive script
# check for -i and create prompts for interactive script
# copy to testbuild using scp root@dev2.rconfig.com:/var/www/html/installer.sh /home

# Setup logging
LOGFILE=install.log

## Set some Colors
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
magenta=`tput setaf 5`
cyan=`tput setaf 6`
reset=`tput sgr0`

function pauseForinput {
        if [ "$1" = "-i" ]; then
                read -p "Continue (y/n)?" CONT
                if [ "$CONT" == "y" ]; then
                  echo "${green}yaaa";
                else
                  exit 1
                fi
        fi
}
function pauseForinputReboot {
        if [ "$1" = "-i" ]; then
                echo "The install has completed...";
                read -p "Reboot (y/n)?" CONT
                if [ "$CONT" == "y" ]; then
                  echo "${green}yaaa";
                else
                  exit 1
                fi
        fi
}
echo "${blue}Starting rConfig installation...${reset}";
sleep 1

#test internet connectivity
INTERNETMSG="Testing internet connectivity..."
echo $INTERNETMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $INTERNETMSG >>>>"  >> $LOGFILE 2>&1
	curl http://google.com  >> $LOGFILE 2>&1
	if [ $? -eq 0 ]; then
		echo -ne '##########################(100%)\n'
		echo -e "${green}You are Online!${reset}\n"
	else
		echo -e  "${red}Status: You are offline...\n";
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $INTERNETMSG >>>>"  >> $LOGFILE 2>&1


# install wget
WGETINSTALLMSG="Installing wget..."
echo $WGETINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $WGETINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	yum -y install wget >> $LOGFILE 2>&1
	if which wget > /dev/null; then
		echo -ne '##########################(100%)\n'
		echo -e "${green}Status: wget is installed!${reset}\n"
	else
		echo -e "${red}Status: wget was not installed - the script has failed\n";
		echo -e  "${red}The script will now terminate!!!${reset}\n";
	fi
echo "<<<< End - $WGETINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1


# download and install initial login script for console
LOGININSTALLMSG="Now installing the login script..."
echo $LOGININSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '##########################(100%)\n'
	echo "<<<< Start - $LOGININSTALLMSG >>>>"  >> $LOGFILE 2>&1
	wget https://www.rconfig.com/downloads/scripts/login.sh -O /etc/profile.d/login.sh >> $LOGFILE 2>&1
	chmod +x /etc/profile.d/login.sh
	FILE='/etc/profile.d/login.sh'
	if [ -f $FILE ];
	then
		echo -e "${green}Status: Login Script installed${reset}\n"
	fi
echo "<<<< End - $LOGININSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1


#SELINUX Update
SELINUXINSTALLMSG="Updating SELINUX File..."
echo $SELINUXINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '##########################(100%)\n'
	#Create temporary file with new line in place
	echo "<<<< Start - $SELINUXINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	cat /etc/selinux/config | sed -e "7s/SELINUX=enforcing/SELINUX=disabled/" > /tmp/seLinuxConfigTmp
	#Copy the new file over the original file
	echo y | mv /tmp/seLinuxConfigTmp /etc/selinux/config
	echo '<<<< Display full SELINUX file in install.log >>>>'  >> $LOGFILE 2>&1
	cat /etc/selinux/config >> $LOGFILE 2>&1

	# check if SELinux was updated correctly
	if cat /etc/selinux/config | grep -q 'SELINUX=disabled';
	then
		echo -e  "${green}Status: SELINUX Updated${reset}\n";
	else
		echo -e  "${red}Status: SELINUX was not updated - the script has failed\n";
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $SELINUXINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1

#SUDOERs Update
SUDOINSTALLMSG="Updating Sudoers File..."
echo $SUDOINSTALLMSG;
echo -ne '#####                     (33%\r)'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '##########################(100%)\n'
echo "<<<< Start - $SUDOINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	# update sudoers for rconfig specific tasks
	echo '### rConfig specific Apahce configuration' >> /etc/sudoers
	echo 'apache ALL = (ALL) NOPASSWD: /usr/bin/crontab, /usr/bin/zip, /bin/chmod, /bin/chown, /usr/bin/whoami, /usr/bin/wc, /usr/bin/tail, /bin/rm' >> /etc/sudoers
	echo 'Defaults:apache !requiretty' >> /etc/sudoers
	cat /etc/sudoers >> $LOGFILE 2>&1
	echo -e  "${green}Status: Sudoers Updated${reset}\n";
echo "<<<< End - $SUDOINSTALLMSG >>>>"  >> $LOGFILE 2>&1


# Stop Firewall if enabled
FWSERVICE=iptables;
FWINSTALLMSG="Disabling Firewall..."
echo $FWINSTALLMSG;
echo -ne '#####                     (33%\r)'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '##########################(100%)\n'
	echo "<<<< Start - $FWINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	if ps ax | grep $FWSERVICE > /dev/null
	then
		chkconfig iptables off >> $LOGFILE 2>&1
		service iptables stop >> $LOGFILE 2>&1
		echo -e  "${green}Status: Firewall (iptables) Disabled ${reset}\n";
		# check again to make sure it is disabled
	fi
	if ps ax | grep -v grep | grep $FWSERVICE >> $LOGFILE 2>&1
	then
		service iptables stop >> $LOGFILE 2>&1
		echo -e  "${red}Status: Firewall (iptables) was not Updated - the script has failed\n";
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $FWINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1

# install repos and RPMs
REPOINSTALLMSG="Installing Repos..."
echo $REPOINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '########                  (50%)\r'
sleep 1
	echo "<<<< Start - $REPOINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	rpm --import /etc/pki\rpm-gpg\rPM-GPG-KEY*  >> $LOGFILE 2>&1
	rpm --import http://dag.wieers.com\rpm/packages\rPM-GPG-KEY.dag.txt  >> $LOGFILE 2>&1
	rpm -ivh http://pkgs.repoforge.org\rpmforge-release\rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm  >> $LOGFILE 2>&1
	echo -ne '#####################    (90%)\r'
	rpm -ivh http://mirror.cogentco.com/pub/linux/epel/6/i386/epel-release-6-8.noarch.rpm  >> $LOGFILE 2>&1
	echo -ne '##########################(100%)\n'
	echo -e  "${green}Status: Repos installed installed...${reset}\n"

echo "<<<< End - $REPOINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1


# install required packages
PKGINSTALLMSG="Installing required packages... This will take a few moments.... "
echo $PKGINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo "<<<< Start - $PKGINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	yum -y groupinstall 'Development Tools'  >> $LOGFILE 2>&1
	echo -ne '#######                   (40%)\r'
	yum -y install ntp sudo telnet bind-utils tree traceroute unzip vim >> $LOGFILE 2>&1
	echo -ne '#########                 (45%)\r'
	yum -y install vixie-cron crontabs  >> $LOGFILE 2>&1
	echo -ne '############              (50%)\r'
	yum -y install httpd openssl-devel openssl mod_ssl vsftpd  >> $LOGFILE 2>&1
	echo -ne '################          (60%)\r'
	yum -y install mysql mod_auth_mysql mod_ssl mysql-devel mysql-server  >> $LOGFILE 2>&1
	echo -ne '###################       (67%)\r'
	yum -y install php-devel php php-domxml php-gd php-mbstring php-mysql php-ncurses php-pear php-cli php-common php-pdo php-ldap >> $LOGFILE 2>&1
	echo -ne '######################    (80%)\r'
	sudo curl -sS https://getcomposer.org/installer | php
	mv composer.phar /usr/local/bin/composer
	export PATH=~/.config/composer/vendor/bin:$PATH
	composer global require laravel/envoy

	yum -y update  >> $LOGFILE 2>&1
	echo -ne '##########################(100%)\n'
	echo -e "${green}Status: Required packages installed...${reset}\n"
echo "<<<< End - $PKGINSTALLMSG >>>>"  >> $LOGFILE 2>&1



SERVICESSTARTMSG="Starting required services..."
echo $SERVICESSTARTMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '########                  (50%)\r'
sleep 1
echo "<<<< Start - $SERVICESSTARTMSG >>>>"  >> $LOGFILE 2>&1
	chkconfig ntpd on >> $LOGFILE 2>&1
	service ntpd start >> $LOGFILE 2>&1
	chkconfig httpd on >> $LOGFILE 2>&1
	service httpd start >> $LOGFILE 2>&1
	chkconfig mysqld on >> $LOGFILE 2>&1
	service mysqld start >> $LOGFILE 2>&1
	chkconfig vsftpd on >> $LOGFILE 2>&1
	service vsftpd start >> $LOGFILE 2>&1
	chkconfig crond on >> $LOGFILE 2>&1
	service crond start >> $LOGFILE 2>&1
	echo -ne '##########################(100%)\n'
	echo -e "${green}Status: Required services started...${reset}\n"
echo "<<<< End - $SERVICESSTARTMSG >>>>"  >> $LOGFILE 2>&1

# Check PHP Version
PHPINSTALLMSG="Checking for PHP..."
echo $PHPINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $PHPINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	if which php > /dev/null; then
	PHPVER=$(rpm -q php)
		echo -ne '##########################(100%)\n'
		echo -e "${green}Status: PHP $PHPVER is installed!${reset}\n"
		echo -e "${green}Status: PHP $PHPVER is installed!${reset}\n" >> $LOGFILE 2>&1
	else
		echo -e "${red}Status: PHP was not installed - the script has failed\n";
		echo -e "${red}Status: PHP was not installed - the script has failed\n"; >> $LOGFILE 2>&1
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $PHPINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1


# Check HTTPD Version
HTTPDINSTALLMSG="Checking for Apache..."
echo $HTTPDINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $HTTPDINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	if which httpd > /dev/null; then
		APACHEVER=$(httpd -v | grep version)
		echo -ne '##########################(100%)\n'
		echo -e "${green}Status: Apache $APACHEVER is installed!${reset}\n"
		echo -e "${green}Status: Apache $APACHEVER is installed!${reset}\n" >> $LOGFILE 2>&1
	else
		echo -e "${red}Status: APACHE was not installed - the script has failed\n";
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $HTTPDINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1


# Check MySQL Version
MYSQLINSTALLMSG="Checking for MYSQL..."
echo $MYSQLINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $MYSQLINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	if which mysql > /dev/null; then
		MYSQL=$(mysql --version|awk '{ print $5 }'|awk -F\, '{ print $1 }')
		echo -ne '##########################(100%)\n'
		echo -e "${green}Status: MySQL $MYSQL is installed!${reset}\n"
		echo -e "${green}Status: MySQL $MYSQL is installed!${reset}\n" >> $LOGFILE 2>&1
	else
		echo -e "${red}Status: MySQL was not installed - the script has failed\n";
		echo -e "${red}Status: MySQL was not installed - the script has failed\n"; >> $LOGFILE 2>&1
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $MYSQLINSTALLMSG >>>>"  >> $LOGFILE 2>&1

pauseForinput $1

# Install VSFTPD
VSFTPDINSTALLMSG="Setting up VSFTPD..."
echo $VSFTPDINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $VSFTPDINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	FILE="/etc/vsftpd/vsftpd.conf"

	if [ -f $FILE ];
	then
		mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.original
		echo "#Custom rConfig VSFTPD conf file" > /etc/vsftpd/vsftpd.conf;
		echo "anonymous_enable=NO" >> /etc/vsftpd/vsftpd.conf;
		echo "listen=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "local_enable=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "write_enable=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "local_umask=022" >> /etc/vsftpd/vsftpd.conf;
		echo "dirmessage_enable=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "xferlog_enable=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "connect_from_port_20=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "xferlog_std_format=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "pam_service_name=vsftpd" >> /etc/vsftpd/vsftpd.conf;
		echo "userlist_enable=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "tcp_wrappers=YES" >> /etc/vsftpd/vsftpd.conf;
		echo "idle_session_timeout=600" >> /etc/vsftpd/vsftpd.conf;
		echo "data_connection_timeout=120" >> /etc/vsftpd/vsftpd.conf;
		echo "ftpd_banner=Welcome to the FTP Service" >> /etc/vsftpd/vsftpd.conf;
		echo "ls_recurse_enable=YES" >> /etc/vsftpd/vsftpd.conf;
		echo -ne '##########################(100%)\n'

		read -p "Do you want to allow Root access to FTP... (y/n)?" CONT
		if [ "$CONT" == "y" ]; then
				# update FTP users files for root access
				#Create temporary file with new line in place
				cat /etc/vsftpd/ftpusers | sed '\root/d' > /tmp/ftpusers   >> $LOGFILE 2>&1
				#Copy the new file over the original file
				echo y | mv /tmp/ftpusers /etc/vsftpd/ftpusers   >> $LOGFILE 2>&1
				cat /etc/vsftpd/ftpusers   >> $LOGFILE 2>&1

				#Create temporary file with new line in place
				cat /etc/vsftpd/user_list | sed '\root/d' > /tmp/user_list   >> $LOGFILE 2>&1
				#Copy the new file over the original file
				echo y | mv /tmp/user_list /etc/vsftpd/user_list  >> $LOGFILE 2>&1
				cat /etc/vsftpd/user_list  >> $LOGFILE 2>&1
		else
			continue;
		fi
			echo -e "${green}Status: VSFTPD is configured!${reset}\n"
			echo -e "${green}Status: VSFTPD is configured!${reset}\n" >> $LOGFILE 2>&1
			service vsftpd restart >> $LOGFILE 2>&1
	else
		echo -e "${red}Status: VSFTPD configuration file does not exist - the script has failed\n";
		echo -e  "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $VSFTPDINSTALLMSG >>>>"  >> $LOGFILE 2>&1
pauseForinput $1


# Setup NTP
NTPINSTALLMSG="Configure NTP..."
echo $NTPINSTALLMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $NTPINSTALLMSG >>>>"  >> $LOGFILE 2>&1
	read -p "Do you want to enter your own NTP Server (default is time.nist.gov)?  (y/n)?" choice
	case "$choice" in
	  y|Y|"" ) read -p "Please enter NTP Server Name or IP:" NTPSRV;;
	  n|N ) NTPSRV="time.nist.gov";;
	  * ) echo "Invalid response, please setup NTP manually"; exit;;
	esac
	echo $NTPSRV; >> $LOGFILE 2>&1
	service ntpd stop  >> $LOGFILE 2>&1
	ntpdate $NTPSRV >> $LOGFILE 2>&1
	service ntpd start >> $LOGFILE 2>&1
	ntpstat >> $LOGFILE 2>&1
	echo -ne '##########################(100%)\n'
	echo -e "${green}Status: NTP Service is configured!${reset}\n"
	echo -e "${green}Status: NTP Service is configured!${reset}\n" >> $LOGFILE 2>&1
echo "<<<< End - $NTPINSTALLMSG >>>>"  >> $LOGFILE 2>&1

# Getting post-RebootScript
POSTREBOOTSCRIPTMSG="Getting Post-Reboot Script..."
echo $POSTREBOOTSCRIPTMSG;
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
	echo "<<<< Start - $POSTREBOOTSCRIPTMSG >>>>"  >> $LOGFILE 2>&1
	curl -O https://www.rconfig.com/downloads/scripts/centos6_postReboot.sh  -A "Mozilla"  >> $LOGFILE 2>&1
	FILE="centos6_postReboot.sh"
	if [ -f $FILE ];
	then
		chmod +x $FILE;
		echo -ne '##########################(100%)\n'
		echo -e "${green}Status: Post-Reboot file retrieved!${reset}\n"
		echo -e "${green}Status: Post-Reboot file retrieved!${reset}\n" >> $LOGFILE 2>&1
		sleep 5
	else
		echo -e "${red}Status: Post-Reboot file not retrieved - the script has failed\n";
		echo -e "${red}The script will now terminate!!!${reset}\n";
		exit
	fi
echo "<<<< End - $POSTREBOOTSCRIPTMSG >>>>"  >> $LOGFILE 2>&1

# Reboot Requirement
# REBOOTMSG="You are now required to reboot your OS..."
echo -e "${red}rConfig system installation is almost complete...\r"
echo -e "Your final task will be to setup MySQL.\r"
echo -e "Once the MySQL Setup wizard is complete, Please reboot your server and then run the post installation script /home/$FILE ${reset}";
sleep 5

# mysql_secure_installation
MYSQLSETUPMSG="mysql_secure_installation wizard"
echo -e "The MySQL setup wizard will now launch\r"
echo -ne '....'
sleep 1
echo -ne '.........'
sleep 1
echo -ne '..............'
sleep 1
echo -ne '...................'
sleep 1
# echo -e "You can set a password for root accounts.\r"
# echo -e "You can remove root accounts that are accessible from outside the local host.\r"
# echo -e "You can remove anonymous-user accounts.\r"
# echo -e "You can remove the test database (which by default can be accessed by all users, \r"
# echo -e "even anonymous users), and privileges that permit anyone to access databases with names that start with test_.\r"
# echo -e "Make sure to remember the root password you enter in the wizard\r"
sleep 3
echo "<<<< Start - $MYSQLSETUPMSG >>>>"  >> $LOGFILE 2>&1
mysql_secure_installation
#end script

