#!/bin/bash
#
#    otrs_setup.sh is a helper script to automate most of the steps for setting up OTRS on CentOS7.
#
#    Copyright (C) 2019 Yordan Manolov
#
#    This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
#

OTRS_VER="otrs-6.0.19-02"
OTRS_RPM="$OTRS_VER.noarch.rpm"
OTRS_URL="http://ftp.otrs.org/pub/otrs/RPMS/rhel/7/$OTRS_RPM"

if [ "$EUID" -ne 0 ]
  then echo "Insufficient privileges: non-root user detected. Aborting ... "
  exit
fi

sed -ie 's@^ONBOOT="no@ONBOOT="yes@' $(ls /etc/sysconfig/network-scripts/ | grep ens)
dhclient -v

yum install -y epel-release mariadb-server wget


if [ $(grep -c "SELINUX=enforcing" /etc/selinux/config) -gt 0 ]; then
	echo "SELINUX is preventing OTRS installation. Disabling SELINUX rules... "
        sed -ie 's@SELINUX=enforcing@SELINUX=permissive@' /etc/selinux/config
	reboot 
fi 
 
touch /etc/my.cnf.d/zotrs.cnf

echo "
[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8

max_allowed_packet   = 64M
query_cache_size     = 32M
innodb_log_file_size = 256M
" > /etc/my.cnf.d/zotrs.cnf

if [ $? -ne 0 ]; then
	echo "Could not write MariaDB config for OTRS. Aborting..."
	exit 1
fi

systemctl enable mariadb
systemctl restart mariadb

wget $OTRS_URL
yum install -y $OTRS_RPM

yum install -y "perl(YAML::XS)" "perl(Archive::Tar)" "perl(Archive::Zip)" "perl(Date::Format)" "perl(DateTime)" "perl(Digest::SHA)" "perl(Net::DNS)" "perl(Template)"  "perl(Crypt::Eksblowfish::Bcrypt)" "perl(JSON::XS)" "perl(Text::CSV_XS)" "perl(DBD::ODBC)" "perl(DBD::Pg)" "perl(Mail::IMAPClient)"  "perl(Authen::NTLM)"  "perl(ModPerl::Util)"

systemctl enable httpd
systemctl restart httpd

echo "This system uses the following network interfaces: "
ip -br -f inet addr

ip route add default via $(ip -br addr | tail -n 1 | awk '{print $3}' | cut -c 1-14)
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
systemctl restart firewalld
