### Install ServiceDesk on Centos 7.0 / PHP7.0 ###

## Preparing the OS

# Disabling SELinux
sudo sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux /etc/selinux/config

# Adding PHP7 repos
sudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# Adding repo for mailparse #
curl http://rpms.remirepo.net/enterprise/remi.repo | sudo tee /etc/yum.repos.d/remi.repo

# Adding repo for the drivers for MSSQL
curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo

## Installing MariaDB
sudo yum -y install mariadb mariadb-server
sudo service mariadb start

echo
echo "We will create a database in MySql called servicedesk and a user called servicedesk with password servicedesk, so you can use it later"
echo "PRESS ENTER WITHOUT ENTERING PASSWORD"
echo
sudo mysql -uroot -p -e "CREATE USER 'servicedesk'@'localhost' IDENTIFIED BY 'servicedesk'; CREATE DATABASE servicedesk; GRANT ALL ON servicedesk.* TO 'servicedesk'@'localhost'";

# PRESS ENTER WITHOUT ENTERING PASSWORD

## Install iptables-services
# Add iptables rule for port 80 (if it were necessary)
sudo yum -y install net-tools iptables-services
sudo service iptables start
interface=$(ifconfig -a | head -n1 | awk '{print $1}' | sed 's/:$//') && sudo iptables -I INPUT -i $interface -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

# Adding Invgate ServiceDesk repo
echo -e "[invgate]\nname=InvGate Packages\nbaseurl=https://download.invgate.net/servicedesk/packages/centos/LTS/stable/\nenabled=1\ngpgcheck=0\n" | sudo tee /etc/yum.repos.d/invgate.repo

## Installing ServiceDesk
sudo yum -y install redis
sudo service redis start
sudo yum install yum-utils
sudo yum-config-manager --enable remi-php81
sudo yum -y install invgate-service-desk
sudo cp -f /usr/share/invgate/servicedesk/application/defaultconfig/resources/cache_redis.ini /usr/share/invgate/servicedesk/config/cache.ini
sudo usermod invgate -G apache

# Restart Apache
sudo service httpd restart

## Configure services at boot
sudo chkconfig invgate-queue on
sudo chkconfig httpd on
sudo chkconfig mariadb on
sudo chkconfig redis on
sudo chkconfig iptables on
sudo sed -i "s/;date.timezone =/date.timezone = 'UTC'/g" /etc/php.ini
sudo iptables-save | sudo tee /etc/sysconfig/iptables
sudo systemctl stop firewalld
sudo systemctl disable firewalld

## Install wget
sudo yum -y install wget
sudo wget https://download.invgate.net/servicedesk/licenses/demo/invgate.ato --output-document=/usr/share/invgate/servicedesk/invgate.ato
echo -e "[production]\nresources.db.adapter         = \"pdo_mysql\"\nresources.db.params.host     = \"localhost\"\nresources.db.params.dbname   = \"servicedesk\"\nresources.db.params.username = \"servicedesk\"\nresources.db.params.password = \"servicedesk\"\n" | sudo tee /usr/share/invgate/servicedesk/config/db.ini
php /usr/share/invgate/servicedesk/tasks/app.php db:initialize -n
php /usr/share/invgate/servicedesk/tasks/app.php migrations:migrate -n
php /usr/share/invgate/servicedesk/tasks/app.php system:resetpassword 1 servicedesk
php /usr/share/invgate/servicedesk/tasks/app.php system:updateconfig -n
sudo chmod 777 /usr/share/invgate/servicedesk/logs/*.log
php /usr/share/invgate/servicedesk/tasks/app.php system:maintenance disable
echo "Please, select the password for the user suerpadmin"
read superpass
php /usr/share/invgate/servicedesk/tasks/app.php system:resetpassword 1 $superpass


## RESTART THE SERVER
let "loop=0"
while test $loop == 0
do
clear
echo
echo "INVGATE SERVICE DESK MUST BE RESTARTED TO APPLY ALL CONFIGURATION CHANGES"
echo "After the reboot, you have to open a browser and go to:"
echo "http://IP_OF_THIS_SERVER/"
echo "You can log as superadmin with password $superpass" 
echo -n "RESTART NOW (Y/N)?  "
read choice
case $choice in q)
let "loop=1"
esac
############################################
case $choice in [yY])
        echo
        echo
        echo "SYSTEM IS RESTARTING TO COMPLETE INSTALLATION"
        echo
        sudo shutdown -r now
        break
esac
case $choice in [nN])
        echo
        echo
        echo "PLEASE RESTART THE SERVER TO BEGIN USING INVGATE SERVICE DESK"
        echo
        break
esac
done
