#!/bin/bash
# $Id: dsminstall 1979 2012-06-11 19:43:32Z joel $ 
yum -y install wget
NEEDED="/bin/rpm /bin/egrep /bin/grep /usr/bin/wget /bin/sed /bin/awk /usr/bin/yum"
RUNID=`date +'%s'`
DEFAULTMYSQL="NO"
function add_excludes {
echo "Checking for needed excludes"
   if /sbin/ifconfig | grep venet0 >/dev/null ; then
      if [ -d /etc/yum.repos.d ]; then
         for f in `find /etc/yum.repos.d/*`
             do
                  if /bin/grep "exclude=glibc glibc-common udev" $f  >/dev/null
		  then  
	 	  echo "Exclude already added to $f"
                  else
                  echo "Adding excludes to $f"
                  echo "exclude=glibc glibc-common udev" >> $f
                  fi
          done
	rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY
         fi
       else
        echo "Nothing to do"
fi
}

function prerun_check
{
echo "===== Checking for basic tools ====="
for i in $NEEDED
do
   echo -n "Checking for $i: "
   if [ ! -x $i ]
   then
      echo MISSING
      echo "FATAL: $i not present, cannot install"
      exit 1
   else
      echo FOUND
   fi
done
if [ -f /usr/bin/mysql ]
then
   MYSQLVERSION=`mysql -V | perl -pe 's|^.*Distrib ([0-9\.]+).*|\1|g;'`
   echo "MySQL Version detected as $MYSQLVERSION"
else
   echo "MySQL isn't installed (thats ok)."
fi
echo
}

function disable_selinux {
if [ -e "/etc/sysconfig/selinux" ]
then
grep -q SELINUX=enforcing /etc/sysconfig/selinux
if [ "x$?" == "x0" ]
then
echo "Disabling SELinux"
cp /etc/sysconfig/selinux /etc/sysconfig/selinux-predsm
sed 's/SELINUX=enforcing/SELINUX=disabled/g' < /etc/sysconfig/selinux-predsm > /etc/sysconfig/selinux
if [ -e "/usr/sbin/setenforce" ]
then
/usr/sbin/setenforce 0
fi
fi
fi
}


function detect_os
{
echo "===== Detecting Operating System ====="
# Find my OS
OS=UNKNOWN
NEWYUM=NO
USEUP2DATE=NO
SYSTEMD=NO
rpm -q redhat-release >/dev/null 2>&1
if [ "x$?" == "x0" ]
then
OS=`rpm -q redhat-release | awk -F- '{print $1 "/" $3}'`
BASEOS=redhat
fi
rpm -q centos-release >/dev/null 2>&1
if [ "x$?" == "x0" ]
then
OS=`rpm -q centos-release | awk -F- '{print $1 "/" $3}'`
BASEOS=centos
fi
rpm -q fedora-release >/dev/null 2>&1
if [ "x$?" == "x0" ]
then
OS=`rpm -q fedora-release | awk -F- '{print $1 "/" $3}'`
BASEOS=fedora
fi
if [ "$OS" == "UNKNOWN" ]
then
echo "Error: Can't detect OS"
exit 1
fi

if [ "$OS" == "fedora/3" ]
then
NEWYUM=YES
fi

if [ "$OS" == "fedora/4" ]
then
NEWYUM=YES
fi

if [ "$OS" == "fedora/5" ]
then
NEWYUM=YES
fi
  
if [ "$OS" == "fedora/6" ]
then
NEWYUM=YES
DEFAULTMYSQL=YES
fi
 
if [ "$OS" == "centos/4" ]
then
NEWYUM=YES
fi

if [ "$OS" == "centos/5" ]
then
NEWYUM=YES
fi

if [ "$OS" == "centos/6" ]
then
NEWYUM=YES
fi

if [ "$OS" == "centos/7" ]
then
NEWYUM=YES
DEFAULTMYSQL=YES
SYSTEMD=YES
fi


echo "Detected Operating System as $OS ($BASEOS)"

INSTALLOK=no

for i in redhat/7.3 redhat/9 fedora/1
do
if [ "$OS" == "$i" ]
then
echo 
echo "***************************************************"
echo "*** You are installing on an operating system   ***"
echo "*** that is no longer supported by RedHat       ***"
echo "***************************************************"
echo "*** By default, we will enable updates from the ***"
echo "*** Fedora Legacy project. For more information ***"
echo "*** See www.fedoralegacy.org                    ***"
echo "***************************************************"
INSTALLOK=yes
beep 2>/dev/null
sleep 5
fi
done

if [ "$OS" == "redhat/8.0" ]
then
   echo 
   echo "***************************************************"
   echo "*** You are installing on an operating system   ***"
   echo "*** that is no longer supported by RedHat       ***"
   echo "***************************************************"
   echo "*** WARNING WARNING WARNING WARNING WARNING     ***"
   echo "***************************************************"
   echo "RedHat 8.0 is not currently maintained by RedHat or"
   echo "FedoraLegacy. We do not recomend utilizing this    "
   echo "operating system in a server enviroment, because   "
   echo "there is no reliable source for security updates   "
   echo "at this time                                       "
   echo "***************************************************"
   echo "*** WARNING WARNING WARNING WARNING WARNING     ***"
   echo "***************************************************"
   INSTALLOK=yes
   beep 2>/dev/null
   beep 2>/dev/null
   beep 2>/dev/null
   sleep 30
fi

for i in fedora/2 fedora/3 fedora/4 fedora/5 fedora/6
do
   if [ "$OS" == "$i" ]
   then
      echo 
      echo "*****************************************************"
      echo "*****************************************************"
      echo "You are installing on a recent version of Fedora Core"
      echo "Fedora Core is on a quarterly release schedule and   "
      echo "previous revisions are only maintained until three   "
      echo "months after a new revision is released              "
      echo "Please see further details regarding Fedora at       "
      echo "http://fedora.redhat.com/                            "
      echo "*****************************************************"
      echo "*****************************************************"
      INSTALLOK=yes
      sleep 5
   fi
done

for i in centos/3 centos/4 centos/5 centos/6 centos/7
do
if [ "$OS" == "$i" ]
then
   echo
   echo "***************************************************"
   echo "***************************************************"
   echo "You are installed on a recent version of CentOS    "
   echo "You can obtain update and release information at   "
   echo "http://www.centos.org/                             "
   echo "***************************************************"
   echo "***************************************************"
   INSTALLOK=yes
   sleep 5
fi
done

for i in redhat/4ES redhat/4AS redhat/3ES redhat/3AS
do
   if [ "$OS" == "$i" ]
   then
   echo
   echo "***************************************************"
   echo "***************************************************"
   echo "You are installing a recent version of RHEL        "
   echo "You can obtain update and release information at   "
   echo "http://www.redhat.com/                             "
   echo "***************************************************"
   echo "***************************************************"
   INSTALLOK=yes
   NEWYUM=yes
   USEUP2DATE=YES
   sleep 5
   fi
done    

# One of the above should have applied
if [ "$INSTALLOK" != "yes" ]
then
   echo "You are not running a supported operating system."
   exit 1
fi
echo
echo
}

function create_tmpdir
{
echo "===== Creating temporary install directory ====="
MYHOME=/home/.dsminstall/dsm3-$RUNID
echo -n "Creating $MYHOME: "
mkdir -p $MYHOME
chmod 700 $MYHOME
if [ "x$?" == "x0" ]
then
   echo Success
else
   echo Failed
   exit 1
fi
cd $MYHOME
date > runtime
rpm -qa > rpm-list-before
echo
}

function import_gpgkeys
{
echo "===== Attempting to import gpg keys ====="
if [ -d /usr/share/rhn ]
then
   for i in /usr/share/rhn/*RPM*KEY*
   do
      echo "Importing $i..."
      rpm --import $i
   done
   if [ "$OS" == "centos/3" ]
   then
   rpm --import http://dist.zervex.com/install/keys/GPG-KEY-centos3
   fi
   if [ "$OS" == "centos/4" ]
   then
   rpm --import http://dist.zervex.com/install/keys/GPG-KEY-centos4
   fi
   if [ "$OS" == "centos/5" ]
   then
   rpm --import http://dist.zervex.com/install/keys/GPG-KEY-centos4
   fi
   if [ "$OS" == "centos/6" ]
   then
   rpm --import http://dist.zervex.com/install/keys/GPG-KEY-centos4
   fi

fi

for i in `wget -q -O - http://dist.zervex.com/install/ZERVEX-KEYS`
do
   echo "Importing http://dist.zervex.com/install/keys/$i..."
   wget http://dist.zervex.com/install/keys/$i
   rpm --import $i
   if [ "x$?" != "x0" ]
   then
      echo "Install of $i failed (may be fatal)"
   fi
   if [ "$USEUP2DATE" == "YES" ]
   then
    gpg `up2date --gpg-flags` --import $i
   fi
done
}

function configure_yum
{
echo "===== Configuring Update Software ====="
if [ "$NEWYUM" == "NO" ]
then
   echo -n "Backing up yum.conf: "
   cp -f /etc/yum.conf /etc/yum.conf-dsmbak-$RUNID
   echo Complete
   for i in `wget -q -O - http://dist.zervex.com/install/$OS/REPOS`
   do
      MYI=`echo $i | sed -e 's#\.repo$##'`
      echo -n "Checking for existing entry ($MYI): "
      egrep "^\[$MYI\]" /etc/yum.conf >/dev/null 2>&1
      if [ "x$?" == "x0" ]
      then
         echo FOUND
      else
         echo "NOT FOUND"
         echo -n "Appending $MYI to /etc/yum.conf: "
         rm -rf /tmp/yum.conf-$i >/dev/null 2>&1
         wget -q -O - http://dist.zervex.com/install/$OS/$i > /etc/yum.conf-$i 2>/dev/null
         if [ "x$?" == "x0" ]
         then
            echo COMPLETE
         else
            echo FAILED
         fi
         cat /etc/yum.conf-$i >> /etc/yum.conf
      fi
   done
else
# NEWYUM=yes
   rpm -q dsm-yumconf >chkrpm-dsmyumconf.log 2>&1
   if [ "x$?" == "x0" ]
   then
      echo "dsm-yumconf already present"
   else
      rm -f dsm-yumconf.rpm >/dev/null 2>&1
      wget http://dist.zervex.com/install/$OS/dsm-yumconf.rpm >wget-dsmyumconf.log 2>&1
      rpm -Uvh dsm-yumconf.rpm >rpminstall-dsmyumconf.log 2>&1
      if [ "x$?" != "x0" ]
      then
         echo "FATAL: Configuration RPM install failed"
         exit 1
      fi
      rm -f dsm-yumconf.rpm
   fi
   if [ "$OS" == "centos/6" ]
      then
         wget http://dist.zervex.com/install/$OS/epel-release-6-5.noarch.rpm > wget-dsmyumconf.log 2>&1
         rpm -Uvh epel-release-6-5.noarch.rpm >rpminstall-dsmyumconf.log 2>&1
         rm -f epel-release-6-5.noarch.rpm
         cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo-preservercp
         sed 's/https/http/g' < /etc/yum.repos.d/epel.repo-preservercp > /etc/yum.repos.d/epel.repo
      fi
   if [ "$OS" == "centos/5" ]
      then
         wget http://dist.zervex.com/install/$OS/epel-release-5-4.noarch.rpm > wget-dsmyumconf.log 2>&1
         rpm -Uvh epel-release-5-4.noarch.rpm >rpminstall-dsmyumconf.log 2>&1
         rm -f epel-release-5-4.noarch.rpm 
      fi
   if [ "$OS" == "centos/7" ]
      then
	yum -y install epel-release
      fi
fi
echo
echo "===== Making sure that yum is the latest available version ====="
yum -y update yum 
if [ "x$?" == "x0" ]
then
   echo "Update Complete!"
else
   echo "Update of yum failed!"
fi
}

function configure_up2date
{
   echo "===== Configuring Update Software ====="
   echo -n "Backing up rhn/sources: "
   cp -f /etc/sysconfig/rhn/sources /etc/sysconfig/rhn/sources-$RUNID
   echo Complete
   egrep '^yum dsm' /etc/sysconfig/rhn/sources >/dev/null 2>&1
   if [ "x$?" == "x1" ]
   then
    echo "Added dsm to rhn/sources"
    echo "yum dsm http://dist.zervex.com/release/redhat/el4/zvx" >> /etc/sysconfig/rhn/sources
   else
    echo "dsm already in rhn/sources"
   fi
} 

function system_update
{
echo "===== Performing Blanket System Upgrade ====="
if [ "$USEUP2DATE" != "YES" ]
then
    CMD="yum -y update"
else
    CMD="up2date -u"
fi
$CMD
if [ "x$?" == "x0" ]
then
   echo "Update Complete!"
else
   echo "Update failed! (Press enter to continue, Ctrl-C to exit)"
   read BS
fi
}

function php_yum_config
{
   if [ "$OS" == "centos/5" ]
      then
         phpoutfile="/etc/yum.repos.d/dsmphp5.repo"
         echo "# For Centos releases" > $phpoutfile
         echo "[dsmphp5]" >> $phpoutfile
         echo "name=DSM PHP5 for CentOS \$releasever" >> $phpoutfile
         echo "baseurl=http://dist.zervex.com/release/centos/\$releasever/dsmphp5" >> $phpoutfile
         echo "enabled=1" >> $phpoutfile
         # MySQL 5 install cludge
         phpoutfile="/etc/yum.repos.d/dsmmysql5.repo"
         echo "# For Centos releases" > $phpoutfile
         echo "[dsmmysql5]" >> $phpoutfile
         echo "name=DSM MySQL5 for CentOS \$releasever" >> $phpoutfile
         echo "baseurl=http://dist.zervex.com/release/centos/\$releasever/mysql5" >> $phpoutfile
         echo "enabled=1" >> $phpoutfile
     else
         echo "Not CentOS skipping"
     fi
}


function upgrade_mysql
{
   if [ "$OS" == "centos/5" ]
   then
      echo "Upgrading MySQL"
      /etc/rc.d/init.d/mysql stop
      echo "Removing Legacy dirs.."
      mserial=`date +%m%d%H%M%Y.%S`
      mv /var/lib/mysql /var/lib/mysql-$mserial
      echo "Removing Legacy RPMS..."
      rpm -qa | grep -i mysql | xargs rpm -e --nodeps
      echo "Downloading latest binaries..."
      for i in `wget -q -O - http://dist.zervex.com/release/centos/5/MySQL5/PACKAGES`
         do
         mysqlarr=( "${mysqlarr[@]}" "$i" )
         echo "Fetching $i..."
         wget -q --progress=dot http://dist.zervex.com/release/centos/5/MySQL5/$i
      done
      rpm -Uvh ${mysqlarr[*]}
      echo "cleaning up.."
      rm -f ${mysqlarr[*]}
      echo "MySQL Upgrade complete."
  else
     echo "No Upgrade needed."
  fi

   if [ "$OS" == "centos/6" ]
   then
      echo "Upgrading MySQL"
      /etc/rc.d/init.d/mysql stop
      echo "Removing Legacy dirs.."
      mserial=`date +%m%d%H%M%Y.%S`
      mv /var/lib/mysql /var/lib/mysql-$mserial
      echo "Removing Legacy RPMS..."
      rpm -qa | grep -i mysql | xargs rpm -e --nodeps
      echo "Downloading latest binaries..."
      for i in `wget -q -O - http://dist.zervex.com/release/centos/6/mysql5/PACKAGES`
         do
         mysqlarr=( "${mysqlarr[@]}" "$i" )
         echo "Fetching $i..."
         wget -q --progress=dot http://dist.zervex.com/release/centos/6/MySQL5/$i
      done
      rpm -Uvh ${mysqlarr[*]}
      echo "cleaning up.."
      rm -f ${mysqlarr[*]}
      echo "MySQL Upgrade complete."
  else
     echo "No Upgrade needed."
  fi

}

function initialize_mysql
{
if [ "$DEFAULTMYSQL" == "YES" ]
then
    echo "Performing MySQL initialization"
    if [ "$OS" != "centos/7" ];then
        /etc/rc.d/init.d/mysqld start
    fi
fi
}
function remove_conflicts
{
echo "===== Removing Conflicting Software Packages ====="
if [ "$OS" != "centos/7" ]
then
for i in `wget -q -O - http://dist.zervex.com/install/$OS/CONFLICTING`
do
   echo -n "Checking for $i: "
   rpm -q $i >chkrpm-$i.log 2>&1
   if [ "x$?" == "x0" ]
   then
      echo FOUND
      echo -n "Removing $i: "
      rpm -e $i >rpmremove-$i.log 2>&1
      if [ "x$?" == "x0" ]
      then
         echo Success
      else
         echo Failed
         echo -n "Removing $i (nodeps): "
         rpm -e --nodeps $i >rpmremove-$i-nodeps.log 2>&1
         if [ "x$?" == "x0" ]
         then
            echo Success
         else
            echo Failed
            echo "FATAL: Cannot remove conflicting package $i"
            exit 1
         fi
      fi
   else
      echo "NOT PRESENT"
   fi
done
echo
fi
}

function prep_config
{
   echo "===== Preparing system configuration for upgrade ====="
   if [ "$OS" == "centos/7" ];then
	ln -s /etc/httpd /usr/local/apache
   fi
   for i in /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/webmail.conf
   do
      if [ -f $i ]
      then
         if [ -f $i.pre3 ]
         then 
            echo "Backing up existing $i.pre3"
            mv $i.pre3 $i.pre3-bak-$RUNID
         fi
         echo "Backing up existing $i to $i.pre3"
         mv $i $i.pre3
      fi
   done
   echo
   cp -raf /var/lib/rpm /var/lib/rpm-pre3-bak-$RUNID >/dev/null 2>&1
   for i in redhat/7.3 redhat/8.0
   do
      if [ "$OS" == "$i" ]
      then
         if [ -d /etc/courier ]
         then
            echo "===== Preparing courier configuration for upgrade ====="
            tar zcf /etc/courier-bak-$RUNID.tar.gz /etc/courier
            if [ -d /etc/courier-pre3 ]
            then
               echo "Backing up existing /etc/courier-pre3 to /etc/courier-pre3-bak-$RUNID"
               mv /etc/courier-pre3 /etc/courier-pre3-bak-$RUNID
            fi
            cp -raf /etc/courier /etc/courier-pre3
            echo "Removing all old courier rpms....."
            rpm -e --nodeps --noscripts `rpm -qa | egrep '^courier-'` >courier-remove.log 2>&1
         fi
      fi
   done      
   echo
}

function install_packages
{
echo "===== Installing Packages ====="
for i in `wget -q -O - http://dist.zervex.com/install/$OS/REQUIRED`
do
   echo "== Installing $i =="
   if [ "$USEUP2DATE" != "YES" ]
   then
    IOPT=install
    rpm -q $i >/dev/null 2>&1
    if [ "x$?" == "x0" ]
    then
      IOPT=update
      echo "$i is already installed - upgrading instead"
    fi
    CMD="yum -y $IOPT $i"
   else
    IOPT="-i"
    rpm -q $i >/dev/null 2>&1
    if [ "x$?" == "x0" ]
    then
      IOPT="-u"
      echo "$i is already installed - upgrading instead"
    fi
    CMD="up2date $IOPT $i"
   fi
   $CMD
   if [ "x$?" == "x0" ]
   then
      echo "== Installed $i successfully =="
      echo
   else
      echo "FATAL: Install of $i failed"
      exit 1
   fi
done
}

function enable_quotas
{
   echo "===== Enabling Quotas ====="
   cp -raf /etc/fstab /etc/fstab-old
   if [ ! -f /etc/fstab-stock ]
   then
      echo "Creating /etc/fstab-stock...."
      cp -raf /etc/fstab /etc/fstab-stock
   fi
   echo "Updating fstab...."
   cp -f /etc/fstab /etc/fstab-dsmbak-$RUNID >/dev/null 2>&1
   grep "quota" /etc/fstab >/dev/null 2>&1
   if [ "x$?" != "x0" ]
   then
      NOREMOUNT=1
      echo "============================================================================================="
      echo "We have detected an existing quota system enabled on this server, we recomend you restart the"
      echo "server after the install completes so that our changes to the fstab can take effect"
      echo "============================================================================================="
   else
      NOREMOUNT=0
   fi
   perl -pi.orig -e 's|,?usrquota||g; s|,?grpquota||g; s|(ext[23])([ \t]+)defaults([^ \t]*)([ \t])|\1\2defaults\3,usrquota,grpquota\4|g' /etc/fstab > fstab-update.log 2>&1
   if [ "$NOREMOUNT" != "1" ]
   then 
      for i in `mount | egrep "ext[23]" | awk -F\  '{print $3}'`
      do
         if [ -d $i ]
         then
  	         echo "Running quotaoff on $i...."
	         /sbin/quotaoff -vug $i >> quotaoff.log 2>&1 
            echo "Remounting $i...."
            echo "=== $i ===" >> remount.log
            mount -o remount $i >> remount.log 2>&1
         fi
      done
      echo "Running quotacheck...."
      /sbin/quotacheck -vmaug >quotacheck.log 2>&1
      echo "Running quotaon...."
      /sbin/quotaon -vaug >quotaon.log 2>&1 
   fi
}

function convert_courier
{
touch /etc/courier/locallowercase
echo "36700160" > /etc/courier/sizelimit
if [ -d /etc/courier-pre3 ]
then
   echo "===== Restoring /etc/courier configuration from pre3 ====="
   if [ -d /etc/courier-pre3/userdb ]
   then
      echo "Restoing userdb...."
      cp -raf /etc/courier-pre3/userdb /etc/courier
   fi
   if [ -d /etc/courier-pre3/aliases ]
   then
      echo "Restoing aliases...."
      cp -raf /etc/courier-pre3/aliases /etc/courier
   fi
   if [ -f /etc/courier-pre3/hosteddomains ]
   then
      echo "Restoing hosteddomains...."
      cp -raf /etc/courier-pre3/hosteddomains /etc/courier/hosteddomains
   fi
   if [ -d /etc/courier-pre3/esmtpacceptmailfor.dir ]
   then
      echo "Restoring esmtpacceptmailfor.dir...."
      cp -raf /etc/courier-pre3/esmtpacceptmailfor.dir /etc/courier
   fi
   if [ -d /etc/courier-pre3/smtpaccess ]
   then
      echo "Restoring smtpaccess...."
      cp -raf /etc/courier-pre3/smtpaccess /etc/courier
   fi
   echo
fi
echo "===== Converting Courier ====="
if [ -d /etc/courier/userdb ]
then
   cp -raf /etc/courier/userdb /etc/courier/userdb-bak
   if [ ! -d /etc/authlib/userdb ]
   then
      mkdir /etc/authlib/userdb
      chmod 700 /etc/authlib/userdb
   fi
   for i in /etc/courier/userdb/*
   do
      echo "Copying $i...."
      cp $i /etc/authlib/userdb 
   done
   mv /etc/courier/userdb /etc/courier/userdb-old >courier-mv.log 2>&1
   echo "Creating link to /etc/authlib/userdb...."
   ln -s /etc/authlib/userdb /etc/courier/userdb >courier-link.log 2>&1
   echo "Running makeuserdb....."
   /usr/sbin/makeuserdb
   echo "Running makealiases....."
   /usr/lib/courier/sbin/makealiases
   echo "Restarting courier-authlib...."
   /sbin/service courier-authlib restart
   echo "Running makesmtpaccess....."
   /usr/lib/courier/sbin/makesmtpaccess
   echo "Running makesmtpaccess....."
   /usr/lib/courier/sbin/makesmtpaccess
   echo "Running makeacceptmailfor....."
   /usr/lib/courier/sbin/makeacceptmailfor
   echo "Restarting courier...."
   /sbin/service courier restart
fi
}

function run_setup
{
   echo "===== Running Setup ====="
   mkdir -p /usr/local/dsm/int/templates_c
   rm -f setup
   wget http://dist.zervex.com/install/setup >setup-wget.log 2>&1
   if [ ! -f setup ]
   then
      echo "FAILED: Setup download failed!"
      exit 1
   fi
   /usr/local/dsm/bin/php setup
   if [ "x$?" != "x0" ]
   then
      echo "FATAL: Setup failed!"
      exit 1
   fi
}

function verify_config
{
    mkdir -p /usr/local/dsm/int/templates_c
    for i in /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/webmail.conf
    do
        if [ ! -f $i ]
        then
            if [ -f $i.rpmnew ]
            then
                cp -af $i.rpmnew $i
                echo "Restored $i from $i.rpmnew"
            else
                cp -af $i.pre3 $i
                echo "Restored $i from $i.pre3 (this may not be right)"
            fi
        fi
    done
}

function change_services
{
   # All these changes are made on a "Best Effort" basis" - if they fail - oh well
   for svc in apmd autofs lpd nfslock nfs portmap gpm cups iptables auditd
   do
      echo -n "Stopping $svc...."
      if [ "$SYSTEMD" != "YES" ]
      then
           /sbin/chkconfig $svc off >$svc-chkconfig-off.log 2>&1
           /sbin/service $svc stop >$svc-service-stop.log 2>&1
      else
     	   /bin/systemctl disable $svc >$svc-chkconfig-off.log 2>&1 
     	   /bin/systemctl stop $svc >$svc-service-stop.log 2>&1 
      fi
      echo Done
   done
   
   for svc in courier-authlib courier dsm proftpd pop-before-smtp httpd named aliases mailman postgresql
   do
      echo -n "Starting $svc...."
      if [ "$SYSTEMD" != "YES" ]
      then
          /sbin/chkconfig $svc on >$svc-chkconfig-on.log 2>&1
          /sbin/service $svc start >$svc-service-start.log 2>&1
      else
     	  /bin/systemctl enable $svc >$svc-chkconfig-on.log 2>&1 
     	  /bin/systemctl restart $svc >$svc-service-start.log 2>&1
      fi
      echo Done
   done
}     
function complete_message
{
echo " 
ZERVEX ServerCP INSTALLATION COMPLETE!

You can now login at http://<this server ip>:3000 to administrate this server.

If you determine that ServerCP is not running follow the steps listed below to determine the problem.

* Be sure that the time is correct on your machine.
* Be sure that you do not have iptables rules blocking port 3000.

If the above are fine run the below four commands as root.

dsm --refreshlisc
/etc/rc.d/init.d/dsm restart
/etc/rc.d/init.d/dsm restart

Please report any problems http://support.zervex.com. 

The latest information about Zervex ServerCP is available on the web at
http://www.zervex.com.
"
}      

function get_install_confirm {
   echo "#####################################################"
   echo "# Zervex ServerCP Installer (www.zervex.com)        #"
   echo "# Thanks for Choosing the ServerCP online           #"
   echo "# control panel.                                    #"
   echo "#                                                   #"
   echo "# This installer is used to install ServerCP on     #"
   echo "# the operating systems listed on the zervex.com    #"
   echo "# website.                                          #"
   echo "#                                                   #"
   echo "# This installer is to be ran on a clean machine    #"
   echo "# running one of the supported operating systems.   #"
   echo "#                                                   #"
   echo "# This installer will remove all websites and may   #"
   echo "# delete data for configured websites to include    #"
   echo "# MySQL and Postgresql data.                        #"
   echo "#                                                   #"
   echo "# Please type yes or no below if you would like to  #"
   echo "# continue knowing that all files and data to       #"
   echo "# include any existing websites etc will be removed.#"
   echo "#                                                   #"
   echo "# Typing yes will continue through the installation #"
   echo "# process and typing no will abort the installation.#"
   echo "#####################################################"

   echo -n "Type yes or no: "
   read answer
   if [ "$answer" == "" ]; then
      echo "Yes or no was not typed!"
      echo "Installation aborted"
      exit
      fi
   if [ "$answer" == "no" ]; then
      echo "Zervex ServerCP installation aborted!"
      exit
      fi
   if [ "$answer" != "yes" ]; then
      echo "DSM installation aborted!"
      exit
      fi
}

function remove_mycnf {
if [ "$DEFAULTMYSQL" == "NO" ]
then
   echo "Removing /etc/my.cnf file"
   if [ -e /etc/my.cnf ]; then
         mv /etc/my.cnf /etc/my.cnf-dsmorig
         echo "Removing /etc/my.cnf file"
         echo "The origianl can be found at /etc/my.cnf-dsmorig"
         sleep 1
         fi
fi
}

function setup_named {
   namedConf="/etc/named.conf"
   if [ ! -f $namedConf ];
      then
      echo "Generating Named configuration file"
      echo "options {" > $namedConf
      echo "directory \"/var/named\";" >> $namedConf
      echo "dump-file \"/var/named/data/cache_dump.db\";" >> $namedConf
      echo "statistics-file \"/var/named/data/named_stats.txt\";" >> $namedConf
      echo "};" >> $namedConf
      echo "include \"/etc/rndc.key\";" >> $namedConf
      ln /etc/named.conf /var/named/chroot/etc/named.conf
  fi
}



get_install_confirm
remove_mycnf
prerun_check
disable_selinux
detect_os
create_tmpdir
php_yum_config
import_gpgkeys
if [ "$USEUP2DATE" != "YES" ]
then
    configure_yum
else
    configure_up2date
fi
add_excludes
remove_conflicts
prep_config
install_packages
system_update
initialize_mysql
enable_quotas
convert_courier
run_setup
verify_config
setup_named
change_services
complete_message
