#!/bin/bash

#
# Automated Plesk installer, see the documentation reference at
# http://www.parallels.com/au/products/plesk/documentation/ for more.
# Originally created sometime in early 2004, Last updated by Glenn Enright
#

echo "$0 (v10.0.1 $Id: installplesk.sh 204 2012-02-22 22:28:22Z glenn $)
  Copyright RimuHosting.com
  Please only run this on a cleanly installed system, Plesk will likely 
  not play nice with any modified or added applications.
"
echo -n "Press Ctrl-C to quit or enter to continue... "
read -s
echo

# script basics
WGET_OPTS="--tries=2 --timeout=10 --quiet"
supportemail="support@rimuhosting.com"
arch=$(uname -m) && [ "${arch}" == "i686" ] && arch="i386"

# Change installerfile and release as necessary
release="PLESK_10_4_4"
installerversion="v3.11.0_build111102.17"
installersite="ftp://download1.swsoft.com/Plesk/PP10/10.4.4"
installerfile= # placeholder, constructed later
installerurl= # placeholder, constructed later
component_list="base
psa-autoinstaller
pmm
backup
billing
health-monitor
php
asp
mod_python
ruby
miva
psa-firewall
psa-vpn
psa-fileserver
sshterm
postgresql
postfix
mailman
drweb
kav
spamassassin
horde
atmail"

echo "Runnnig system checks"
if [[ $UID != "0" ]]; then
    echo "!! Please run this script as the root user only."
    exit 1
fi

if [ $(hostname) = "localhost" ]; then
  echo "Hostname = localhost. Set the real hostname and update /etc/hosts to match."
  exit 1
fi

if [ -e /etc/psa ] ; then
  echo "Skipping plesk install, since it looks like it is already done"
  exit 1
fi

if [ -e /etc/redhat-release ]; then
  if [ $(grep -ic "CentOS release 5" /etc/redhat-release) -gt 0 ]; then
    dist="CentOS5"
    installerfile=parallels_installer_${installerversion}_os_CentOS_5_${arch}
  fi
  if [ $(grep -ic "CentOS release 6" /etc/redhat-release) -gt 0 ]; then
    dist="CentOS6"
    installerfile=parallels_installer_${installerversion}_os_CentOS_6_${arch}
  fi

elif [ -e /etc/debian_version ]; then
  if [ $(grep -ic "ubuntu 8.04" /etc/issue.net) -gt 0 ]; then
    dist="Ubuntu8"
    installerfile=parallels_installer_${installerversion}_os_Ubuntu_8.04_${arch}
  fi
  if [ $(grep -ic "ubuntu 10.04" /etc/issue.net) -gt 0 ]; then
    dist="Ubuntu10"
    installerfile=parallels_installer_${installerversion}_os_Ubuntu_10.04_${arch}
  fi
  if [ $(grep -c "5.0" /etc/debian_version) -gt 0 ]; then
    dist="Debian5"
    installerfile=parallels_installer_${installerversion}_os_Debian_5.0_${arch}
  fi
  if [ $(grep -c "6.0" /etc/debian_version) -gt 0 ]; then
    dist="Debian6"
    installerfile=parallels_installer_${installerversion}_os_Debian_6.0_${arch}
  fi

  export DEBIAN_PRIORITY=critical
  export DEBIAN_FRONTEND=noninteractive
fi

if [ -z "${dist}" ]; then
  echo "Distribution unknown. I only support CentOS5 and 6, Debian 5.0 and 6.0, Ubuntu 8.04 and 10.04. Exiting."
  exit 1
fi

if [ -z "${rootpasswd}" ]; then
  echo "Need administrative user password for Plesk"
  echo -n "Enter 'admin' password now: " && read rootpasswd
  if [ $? -ne 0 ]; then echo "Bailing, no password known/provided"; exit 1; fi
fi

if [ -e /etc/init/mysql.conf.disabled ]; then
  mv /etc/init/mysql.conf{.disabled,}
fi

echo "Replacing /etc/hosts"
# TODO add a test with hostname -s? to reduce the number of time we need to do 
# this? Not sure if that is needed since we only run this on new setups
cp /etc/hosts{,.orig}
echo -e "127.0.0.1 localhost\n$(ifconfig eth0 | grep "inet addr" | cut -d: -f2 | awk '{print $1}') $(hostname) $(echo $(hostname) | cut -d. -f1)" > /etc/hosts

if [ -e /etc/init.d/webmin ]; then
  echo "Disabling webmin to avoid conflicts"
  [ -e /etc/redhat-release ] && chkconfig --del webmin || update-rc.d -f webmin remove
  /etc/init.d/webmin stop
fi

# some packages below are installed and then removed to get dependancies
# installed due to the installer's brokeness
echo "Installing supporting packages"
commonpackages="expect"
if [ "$dist" = "CentOS5" ]; then
  packages="ImageMagick php-pgsql selinux-policy-targeted"

elif [ "${dist}" = "Debian" -o "${dist}" = "Ubuntu" ]; then
  packages="postgresql postgresql-client php5-pgsql"
fi
packages="${commonpackages} ${packages}"

apt-get -y -qq update
for package in ${packages}; do
  apt-get -y -qq install "${package}"
  if [ $? -ne 0 ]; then echo "apt-get install failed for ${package}"; exit 1; fi
done

apt-get -y -qq upgrade
if [ $? -ne 0 ]; then echo "apt-get upgrade failed"; exit 1; fi

# pull so it doesnt get in the way
apt-get -y -qq remove dovecot

echo "Grabbing the control panel installer"
installerurl="${installersite}/${dist}/${installerfile}"
wget ${WGET_OPTS} ${installerurl} -O ${installerfile}
if [ $? -ne 0 ]; then echo "* Unable to wget '${installerurl}'. Exiting."; exit 1; fi
chmod +x $installerfile

cmd_line="./${installerfile} --override-os-arch $(uname -m) --select-release-id ${release} ${cmd_line}"
for component in ${component_list}; do
  cmd_line="${cmd_line} --install-component ${component}"
done
cmd_line="${cmd_line} --no-daemon --notify-email ${supportemail} --enable-xml-output --truncate-log"

echo "* Running: $cmd_line"
$cmd_line
if [ $? -ne 0 ]; then echo "Bailing, Plesk installer did not run ok"; exit 1; fi

echo; echo;

if [ ! -e /etc/psa/.psa.shadow ] ; then
  echo "no /etc/psa/.psa.shadow as there should be.  Did the plesk install fail?"
  exit 1
fi

# no longer use .psa.shadow, pass is now crypted internally
if [ -n "$rootpasswd" -a -e /etc/psa/.psa.shadow ]; then
  mysql -u admin -p$(cat /etc/psa/.psa.shadow) mysql -e "update user set password = password(\"$rootpasswd\") where user='admin';flush privileges;"
  if [ $? -ne 0 ]; then echo "mysql password reset failed failed"; exit 1; fi
  /usr/local/psa/bin/init_conf -u -passwd $rootpasswd
#  echo "$rootpasswd" > /etc/psa/.psa.shadow
fi

echo "Improving PCI compliance by disabling weak ciphers, additional compliance can be acheived per http://download1.parallels.com/Plesk/PP10/10.0.1/Doc/en-US/online/plesk-pci-compliance-guide/index.htm?fileName=65871.htm"
/usr/local/psa/admin/bin/pci_compliance_resolver --enable all

# FIXME this seem old and no longer useful?
#echo "Setup the backup dumps dir"
#while true; do
#  if ! df -h | grep -qai backups ; then
#    echo "no /backup mount point"
#    break
#  fi
#  mkdir -p /backups/dumps
#  rm -rf /var/lib/psa/dumps
#  ln -sf /backups/dumps /var/lib/psa/dumps
#  chown -R psaadm:psaadm /var/lib/psa/dumps /backups/dumps
#  break
#done

echo "Tuning Plesk's custom Apache"
if [ -e /usr/local/psa/admin/conf/httpsd.conf ]; then
  sed -i "s/MinSpareServers 5/MinSpareServers 1/" /usr/local/psa/admin/conf/httpsd.conf
  sed -i "s/MaxSpareServers 10/MaxSpareServers 2/"  /usr/local/psa/admin/conf/httpsd.conf
  sed -i "s/StartServers 5/StartServers 2/" /usr/local/psa/admin/conf/httpsd.conf
fi

echo "Tuning MySQL"
MYSQLCNF="/etc/my.cnf"
if [ -e /etc/mysql/my.cnf ]; then
  MYSQLCNF="/etc/mysql/my.cnf"
fi
sed -i "s/innodb_buffer_pool_size=16M/innodb_buffer_pool_size=3M/" "${MYSQLCNF}"
sed -i "s/innodb_log_buffer_size=8M/innodb_log_buffer_size=2M/" "${MYSQLCNF}"

echo "Tuning mail checkers"
[ -e /etc/init.d/spamassassin ]  && sed -i 's/-m5/-m1/' /etc/init.d/spamassassin
[ -e /etc/sysconfig/spamassassin ] && sed -i 's/-m5/-m1/' /etc/sysconfig/spamassassin
[ -e /etc/drweb/drweb32.ini ] && sed -i 's/MaxChildren = 16/MaxChildren = 2/' /etc/drweb/drweb32.ini
echo "SPAMASSASSIN_MAX_CHILDREN 1" >> /etc/psa/psa.conf

echo "Running domain name redirect fixup"
ipaddr="$(ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -d: -f2)"
/usr/share/plesk-billing/update-hostname --new-hostname=${ipaddr}

echo "Cleaning up files after the install"
rm -f $installerfile
apt-get clean
for dir in psa parallels; do
  if [ -d "${dir}" ]; then rm -rf ${dir}; fi
done

echo "Install done"


