#!/bin/bash


###----------------------------------------###
###
###  Octopus Aegir Installer
###
###  Copyright (C) 2010-2014 Omega8.cc
###  noc@omega8.cc www.omega8.cc
###
###  This program is free software. You can
###  redistribute it and/or modify it under
###  the terms of the GNU GPL as published by
###  the Free Software Foundation, version 2
###  or later.
###
###  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 GPL
###  for more details.
###
###  You should have received a copy of the
###  GNU GPL along with this program.
###  If not, see http://www.gnu.org/licenses/
###
###  Code: https://code.aegir.cc/aegir
###
###----------------------------------------###


###----------------------------------------###
### DON'T EDIT ANYTHING BELOW THIS LINE    ###
###----------------------------------------###

_INSTALLER_VERSION=EDIT_INSTALLER_VERSION


###---### Distro config.
#
_AGOV=EDIT_AGOV
_CIVICRM_M6=EDIT_CIVICRM_M6
_CIVICRM_M7=EDIT_CIVICRM_M7
_COD=EDIT_COD
_COMMERCE1=EDIT_COMMERCE1
_COMMERCE2=EDIT_COMMERCE2
_COMMONS2=EDIT_COMMONS2
_COMMONS3=EDIT_COMMONS3
_DRUPAL_EDGE_8=EDIT_DRUPAL_EDGE_8
_DRUPAL7=EDIT_DRUPAL7
_DRUPAL7D="EDIT_DRUPAL7-dev"
_DRUPAL7P="EDIT_DRUPAL7-prod"
_DRUPAL7S="EDIT_DRUPAL7-stage"
_DRUPAL8=EDIT_DRUPAL8
_DRUSH_CC_REQ=EDIT_DRUSH_CC_REQ
_ERPAL=EDIT_ERPAL
_GUARDR=EDIT_GUARDR
_MANAGINGNEWS=EDIT_MANAGINGNEWS
_NODESTREAM7=EDIT_NODESTREAM7
_OPEN_DEALS=EDIT_OPEN_DEALS
_OPENACADEMY=EDIT_OPENACADEMY
_OPENATRIUM6=EDIT_OPENATRIUM6
_OPENATRIUM7=EDIT_OPENATRIUM7
_OPENBLOG=EDIT_OPENBLOG
_OPENCHURCH=EDIT_OPENCHURCH
_OPENDEALS=EDIT_OPENDEALS
_OPENOUTREACH=EDIT_OPENOUTREACH
_OPENPUBLISH=EDIT_OPENPUBLISH
_OPENSCHOLAR=EDIT_OPENSCHOLAR
_PANOPOLY=EDIT_PANOPOLY
_PROSEPOINT=EDIT_PROSEPOINT
_RECRUITER=EDIT_RECRUITER
_RESTAURANT=EDIT_RESTAURANT
_SMALLCORE6=EDIT_SMALLCORE6
_SMALLCORE7=EDIT_SMALLCORE7
_SMALLCORE8=EDIT_SMALLCORE8
_SPARK=EDIT_SPARK
_TOTEM=EDIT_TOTEM
_UBERCART6=EDIT_UBERCART6
_UBERCART7=EDIT_UBERCART7


###---### Main config.
#
_ADM_EMAIL="EDIT_MY_EMAIL"
_AEGIR_VERSION=EDIT_AEGIR_VERSION
_ALL_DISTRO=EDIT_ALL_DISTRO
_AUTOPILOT=EDIT_AUTOPILOT
_BOA_REPO_GIT_URL=EDIT_BOA_REPO_GIT_URL
_BOA_REPO_NAME=EDIT_BOA_REPO_NAME
_CLIENT_CORES=EDIT_CLIENT_CORES
_CLIENT_EMAIL="EDIT_CLIENT_EMAIL"
_CLIENT_OPTION=EDIT_CLIENT_OPTION
_DEBUG_MODE=EDIT_DEBUG_MODE
_DIST_INSTALL=NO
_DISTRO=EDIT_DISTRO
_DOMAIN=EDIT_DOMAIN
_DRUSH_VERSION=EDIT_DRUSH_VERSION
_HM_DISTRO=EDIT_HM_DISTRO
_HM_ONLY=EDIT_HM_ONLY
_HOT_SAUCE=EDIT_HOT_SAUCE
_LAST_ALL=EDIT_LAST_ALL
_LAST_HMR=EDIT_LAST_HMR
_LASTNUM=EDIT_LASTNUM
_NOW=EDIT_NOW
_O_CONTRIB_UP=EDIT_O_CONTRIB_UP
_PHP_CLI_VERSION=EDIT_PHP_CLI_VERSION
_PHP_FPM_VERSION=EDIT_PHP_FPM_VERSION
_PLATFORMS_LIST="EDIT_PLATFORMS_LIST"
_PLATFORMS_ONLY=EDIT_PLATFORMS_ONLY
_PURGE_FOR_SEVEN=EDIT_PURGE_FOR_SEVEN
_PURGE_MODE=EDIT_PURGE_MODE
_REL_VERSION=`lsb_release -sc`
_SPINNER=EDIT_SPINNER
_STATUS=INIT
_STRONG_PASSWORDS=EDIT_STRONG_PASSWORDS
_THIS_DB_HOST=EDIT_THIS_DB_HOST
_THIS_OS=`lsb_release -si`
_THISHTIP=EDIT_THISHTIP
_USE_CURRENT=EDIT_USE_CURRENT
_USE_STOCK=EDIT_USE_STOCK
_USRG=EDIT_USRG
_WEBG=EDIT_WEBG
#
_USER=EDIT_USER
_ROOT="/data/disk/$_USER"
_HOSTMASTER_ROOT="$_ROOT/aegir/distro/$_HM_DISTRO"
_DISTRO_ROOT="$_ROOT/distro/$_DISTRO"
#
if [ "$_PHP_CLI_VERSION" = "5.5" ] && [ -x "/opt/php55/bin/php" ] ; then
  _L_PHP_CLI=/opt/php55/bin
elif [ "$_PHP_CLI_VERSION" = "5.4" ] && [ -x "/opt/php54/bin/php" ] ; then
  _L_PHP_CLI=/opt/php54/bin
elif [ "$_PHP_CLI_VERSION" = "5.3" ] && [ -x "/opt/php53/bin/php" ] ; then
  _L_PHP_CLI=/opt/php53/bin
elif [ "$_PHP_CLI_VERSION" = "5.2" ] && [ -x "/opt/php52/bin/php" ] ; then
  _L_PHP_CLI=/opt/php52/bin
fi
_DRUSHCMD="$_L_PHP_CLI/php $_ROOT/tools/drush/drush.php"
#
SHELL=/bin/bash
PATH=$_L_PHP_CLI:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/usr/bin:/usr/sbin:/bin:/sbin


###---### Functions.
#
# Noticeable messages.
msg () {
  echo "Octopus [`date`] ==> $*"
}
#
# Silent runner.
st_runner () {
  CMD="$1"
  touch busy
  if [ "$_SPINNER" = "YES" ] ; then
    bash $_SRCDIR/spinner busy &
  fi
  if $CMD >> $_LOG; then
    rm busy
    sleep 1
    return 0
  else
    rm busy
    sleep 1
    echo "$CMD failed.  Error (if any): $?"
    echo " "
    echo "Displaying the last 15 lines of $_LOG to help troubleshoot this problem"
    echo " "
    tail -15 $_LOG
    return 1
  fi
}
#
# Small spinner.
mrun () {
  CMD="$1"
  touch busy
  if [ "$_SPINNER" = "YES" ] ; then
    bash $_SRCDIR/spinner busy &
  fi
  if $CMD >> $_LOG; then
    rm busy
    sleep 1
    return 0
  fi
}
#
# Simple prompt.
prompt_yes_no () {
if [ "$_AUTOPILOT" = "YES" ] ; then
  return 0
else
  while true ; do
    printf "$* [Y/n] "
    read answer
    if [ -z "$answer" ] ; then
      return 0
    fi
    case $answer in
      [Yy]|[Yy][Ee][Ss])
        return 0
        ;;
      [Nn]|[Nn][Oo])
        return 1
        ;;
      *)
        echo "Please answer yes or no"
        ;;
    esac
 done
fi
}
#
# Remove dangerous stuff from the string.
sanitize_string () {
  echo "$1" | sed 's/[\\\/\^\?\>\`\#\"\{\(\$\@\&\|\*]//g; s/\(['"'"'\]\)//g'
}
#
# Extract archive.
extract_archive () {
  if [ ! -z $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1    ;;
      *.tar.gz)    tar xzf $1    ;;
      *.bz2)       bunzip2 $1    ;;
      *.rar)       unrar x $1    ;;
      *.gz)        gunzip -q $1  ;;
      *.tar)       tar xf $1     ;;
      *.tbz2)      tar xjf $1    ;;
      *.tgz)       tar xzf $1    ;;
      *.zip)       unzip -qq $1  ;;
      *.Z)         uncompress $1 ;;
      *.7z)        7z x $1       ;;
      *)           echo "'$1' cannot be extracted via >extract<" ;;
    esac
    rm -f $1
  fi
}
#
# Download and extract from dev/contrib mirror.
get_dev_contrib () {
  if [ ! -z $1 ] ; then
    curl -L --max-redirs 10 -k -s -O --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/contrib/$1"
    extract_archive "$1"
  fi
}
#
# Download and extract from dev.
get_dev_ext () {
  if [ ! -z $1 ] ; then
    curl -L --max-redirs 10 -k -s -O --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/dev/$_AEGIR_VERSION/$1"
    extract_archive "$1"
  fi
}
#
_SRCDIR=/opt/tmp/files
_LOG=/var/backups/octopus-$_USER-$_NOW.log
#
# Create shared dirs.
create_shared_dirs () {
  msg "$_STATUS A: Creating shared directories..."
  mkdir -p /data/all/000/{core,modules}
  if [ ! -d "$_CORE" ] ; then
    mkdir -p $_CORE
    mkdir -p /data/src
  fi
  chown $_USER:$_USRG /data/all &> /dev/null
  chown $_USER:$_USRG $_CORE &> /dev/null
  if [ ! -e "$_CORE/o_contrib/ctrl-$_INSTALLER_VERSION" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
    chown -R $_USER:$_USRG $_CORE/o_contrib &> /dev/null
  fi
  if [ ! -e "$_CORE/o_contrib_seven/ctrl-$_INSTALLER_VERSION" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
    chown -R $_USER:$_USRG $_CORE/o_contrib_seven &> /dev/null
  fi
  if [ ! -e "/data/all/$_LAST_ALL/o_contrib/ctrl-$_INSTALLER_VERSION" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
    chown -R $_USER:$_USRG /data/all/$_LAST_ALL/o_contrib &> /dev/null
  fi
  if [ ! -e "/data/all/$_LAST_ALL/o_contrib_seven/ctrl-$_INSTALLER_VERSION" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
    chown -R $_USER:$_USRG /data/all/$_LAST_ALL/o_contrib_seven &> /dev/null
  fi
  chown -R $_USER:$_USRG /data/src &> /dev/null
  chown -R $_USER:$_USRG /opt/tmp &> /dev/null
  chown -R $_USER:$_USRG /data/conf &> /dev/null
  chown -R $_USER:$_USRG /data/all/000 &> /dev/null
  chmod 777 $_CORE /data/all /data/src /data /data/disk /data/conf &> /dev/null
}
#
# Fix for FServer broken due to required now strongarm.
#
feature_server_fix_002 () {
  if [ ! -e "/var/xdrago/log/feature-server-fixed-002.log" ] ; then
    msg "$_STATUS A: Adding missing strongarm module to old FServer platforms"
    REVISIONS="001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050"
    for i in $REVISIONS; do
      if [ -d "/data/all/$i/fserver" ] ; then
        if [ ! -d "/data/all/$i/fserver/profiles/feature_server/modules/strongarm" ] ; then
          cd /data/all/$i/fserver/profiles/feature_server/modules/
          get_dev_contrib "strongarm-6.x-2.2.tar.gz"
          cd /data/all/$i/fserver/profiles/feature_server
          rm -f feature_server.make
          rm -f feature_server.profile
          cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/makefiles/feature_server.make ./
          cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/makefiles/feature_server.profile ./
          if [ -e "$_ROOT/.drush/hostmaster.alias.drushrc.php" ] ; then
            _THIS_HOSTMASTER=`cat $_ROOT/.drush/hostmaster.alias.drushrc.php | grep 'site_path' | cut -d: -f2 | awk '{ print $3}' | sed "s/[\,']//g"`
            cd _THIS_HOSTMASTER &> /dev/null
            su -s /bin/bash - $_USER -c "$_DRUSHCMD --root=$_ROOT/distro/$i/fserver provision-save @platform_FeatureServerPF$i &> /dev/null"
          fi
          msg "$_STATUS A: Missing strongarm added to /data/all/$i/fserver"
        fi
      fi
    done
    touch /var/xdrago/log/feature-server-fixed-002.log
    cd
  fi
}
#
# Fix for FServer broken due to incorrect context version downloaded in fserver-6.28.3.
#
feature_server_fix_004 () {
  if [ ! -e "/var/xdrago/log/feature-server-fixed-004.log" ] ; then
    msg "$_STATUS A: Fixing context version in FServer platforms"
    REVISIONS="001 003 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050"
    for i in $REVISIONS; do
      if [ -d "/data/all/$i/fserver-6.28.3" ] ; then
        _CONTEXT_TEST=`grep "7.x" /data/all/$i/fserver-6.28.3/profiles/feature_server/modules/context/context.info 2>&1`
        if [[ "$_CONTEXT_TEST" =~ "7.x" ]] ; then
          cd /data/all/$i/fserver-6.28.3/profiles/feature_server/modules/
          rm -f -r context
          get_dev_contrib "context-6.x-3.3.tar.gz"
          msg "$_STATUS A: Correct context version added to /data/all/$i/fserver-6.28.3"
        fi
      elif [ -d "/data/all/$i/fserver-6.28.2" ] ; then
        _CONTEXT_TEST=`grep "7.x" /data/all/$i/fserver-6.28.2/profiles/feature_server/modules/context/context.info 2>&1`
        if [[ "$_CONTEXT_TEST" =~ "7.x" ]] ; then
          cd /data/all/$i/fserver-6.28.2/profiles/feature_server/modules/
          rm -f -r context
          get_dev_contrib "context-6.x-3.3.tar.gz"
          msg "$_STATUS A: Correct context version added to /data/all/$i/fserver-6.28.2"
        fi
      fi
    done
    touch /var/xdrago/log/feature-server-fixed-004.log
    cd
  fi
}
#
# Update o_contrib.
#
o_contrib_update_global () {
REVISIONS="001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050"
RMMODULES="drupal-nginx-fast-x-accel-redirect varnish bakery session443 cookie_cache_bypass_adv module_supports purge expire imageinfo_cache httprl boost phpass advagg force_password_change fpa views_cache_bully views_content_cache panels_content_cache cdn js session_expire"
for i in $REVISIONS; do
  if [ -e "/data/all/$i/o_contrib" ] && [ ! -e "/data/all/$i/o_contrib/update-044.info" ] ; then
    _FORCE_UP_SIX=YES
  elif [ -e "/data/all/$i/o_contrib" ] && [ ! -L "/data/all/$i/o_contrib/redis_edge" ] ; then
    _FORCE_UP_SIX=YES
  elif [ -e "/data/all/$i/o_contrib" ] && [ ! -L "/data/all/$i/o_contrib/redis" ] ; then
    _FORCE_UP_SIX=YES
  else
    _FORCE_UP_SIX=NO
  fi
  if [ "$_FORCE_UP_SIX" = "YES" ] || [ ! -e "/data/all/000/modules/redis_edge" ]; then
    for m in $RMMODULES; do
      if [ -d "/data/all/$i/o_contrib/$m" ] ; then
        rm -f -r /data/all/$i/o_contrib/$m
        msg "$_STATUS A: Replaced /data/all/$i/o_contrib/$m with latest release"
      fi
    done
    cd /data/all/$i/o_contrib
    get_dev_contrib "advagg-6.x-1.x-dev.tar.gz"
    get_dev_contrib "boost-6.x-1.x.tar.gz"
    get_dev_contrib "cdn-6.x-2.x-dev.tar.gz"
    get_dev_contrib "force_password_change-6.x-3.4.tar.gz"
    get_dev_contrib "fpa-6.x-2.5.tar.gz"
    get_dev_contrib "httprl-6.x-1.14.tar.gz"
    get_dev_contrib "js-6.x-1.3.tar.gz"
    get_dev_contrib "panels_content_cache-6.x-1.0.tar.gz"
    get_dev_contrib "phpass-6.x-2.0.tar.gz"
    get_dev_contrib "session_expire-6.x-1.x-dev.tar.gz"
    get_dev_contrib "views_cache_bully-6.x-3.1.tar.gz"
    get_dev_contrib "views_content_cache-6.x-2.x-dev.tar.gz"
    if [ ! -d "/data/all/$i/o_contrib/print" ] ; then
      get_dev_contrib "print-6.x-1.19.tar.gz"
    fi
    if [ "$_PURGE_MODE" = "ON" ] ; then
      get_dev_contrib "purge-6.x-1.6.tar.gz"
      get_dev_contrib "expire-6.x-1.x-dev.tar.gz"
    fi
    if [ -d "/data/all/$i/o_contrib/cache_backport" ] ; then
      rm -f -r /data/all/$i/o_contrib/cache_backport
    fi
    if [ -d "/data/all/$i/o_contrib/redis" ] ; then
      rm -f -r /data/all/$i/o_contrib/redis
    fi
    if [ -d "/data/all/$i/o_contrib/redis_edge" ] ; then
      rm -f -r /data/all/$i/o_contrib/redis_edge
    fi
    if [ -e "/data/all/000/modules/cache_backport" ] && [ ! -L "/data/all/$i/o_contrib/cache_backport" ] ; then
      ln -sf /data/all/000/modules/cache_backport /data/all/$i/o_contrib/cache_backport
    fi
    if [ -e "/data/all/000/modules/redis" ] && [ ! -L "/data/all/$i/o_contrib/redis" ] ; then
      ln -sf /data/all/000/modules/redis /data/all/$i/o_contrib/redis
    fi
    if [ -e "/data/all/000/modules/redis_edge" ] && [ ! -L "/data/all/$i/o_contrib/redis_edge" ] ; then
      ln -sf /data/all/000/modules/redis_edge /data/all/$i/o_contrib/redis_edge
    fi
    touch /data/all/$i/o_contrib/update-044.info
    find /data/all/$i/o_contrib -type d -exec chmod 0755 {} \; &> /dev/null
    find /data/all/$i/o_contrib -type f -exec chmod 0644 {} \; &> /dev/null
  fi
  if [ -d "/data/all/$i" ] ; then
    for p in `find /data/all/$i/ -maxdepth 1 -mindepth 1 -type d | sort`; do
      if [ -d "$p/modules/cookie_cache_bypass" ] ; then
        rm -f -r $p/modules/cookie_cache_bypass
      fi
    done
  fi
done
cd
}
#
# Update o_contrib_seven.
#
o_contrib_seven_update_global () {
REVISIONS="001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050"
RMMODULES="session443 cookie_cache_bypass_adv purge expire agrcache httprl filefield_nginx_progress boost speedy entitycache taxonomy_edge advagg force_password_change fpa views_cache_bully views_content_cache panels_content_cache display_cache cdn js session_expire"
for i in $REVISIONS; do
  if [ -e "/data/all/$i/o_contrib_seven" ] && [ ! -e "/data/all/$i/o_contrib_seven/update-044.info" ] ; then
    _FORCE_UP_SEVEN=YES
  elif [ -e "/data/all/$i/o_contrib_seven" ] && [ ! -L "/data/all/$i/o_contrib_seven/redis_edge" ] ; then
    _FORCE_UP_SEVEN=YES
  elif [ -e "/data/all/$i/o_contrib_seven" ] && [ ! -L "/data/all/$i/o_contrib_seven/redis" ] ; then
    _FORCE_UP_SEVEN=YES
  else
    _FORCE_UP_SEVEN=NO
  fi
  if [ "$_FORCE_UP_SEVEN" = "YES" ] || [ ! -e "/data/all/000/modules/redis_edge" ]; then
    for m in $RMMODULES; do
      if [ -d "/data/all/$i/o_contrib_seven/$m" ] ; then
        rm -f -r /data/all/$i/o_contrib_seven/$m
        msg "$_STATUS A: Replaced /data/all/$i/o_contrib_seven/$m with latest release"
      fi
    done
    if [ "$_PURGE_MODE" = "ON" ] && [ "$_PURGE_FOR_SEVEN" = "YES" ] ; then
      cd /data/all/$i/o_contrib_seven
      get_dev_contrib "purge-7.x-1.6.tar.gz"
      get_dev_contrib "expire-7.x-2.0-rc3.tar.gz"
    fi
    cd /data/all/$i/o_contrib_seven
    get_dev_contrib "advagg-7.x-2.6.tar.gz"
    get_dev_contrib "boost-7.x-1.0.tar.gz"
    get_dev_contrib "cdn-7.x-2.x-dev.tar.gz"
    get_dev_contrib "display_cache-7.x-1.0.tar.gz"
    get_dev_contrib "entitycache-7.x-1.2.tar.gz"
    get_dev_contrib "filefield_nginx_progress-7.x-2.x-dev.tar.gz"
    get_dev_contrib "force_password_change-7.x-1.0-rc2.tar.gz"
    get_dev_contrib "fpa-7.x-2.5.tar.gz"
    get_dev_contrib "httprl-7.x-1.14.tar.gz"
    get_dev_contrib "js-7.x-1.x-dev.tar.gz"
    get_dev_contrib "panels_content_cache-7.x-1.0.tar.gz"
    get_dev_contrib "session_expire-7.x-1.x-dev.tar.gz"
    get_dev_contrib "speedy-7.x-1.8.tar.gz"
    get_dev_contrib "taxonomy_edge-7.x-1.x-dev.tar.gz"
    get_dev_contrib "views_cache_bully-7.x-3.1.tar.gz"
    get_dev_contrib "views_content_cache-7.x-3.0-alpha3.tar.gz"
    if [ ! -d "/data/all/$i/o_contrib_seven/print" ] ; then
      get_dev_contrib "print-7.x-2.0.tar.gz"
    fi
    if [ -d "/data/all/$i/o_contrib_seven/redis" ] ; then
      rm -f -r /data/all/$i/o_contrib_seven/redis
    fi
    if [ -d "/data/all/$i/o_contrib_seven/redis_edge" ] ; then
      rm -f -r /data/all/$i/o_contrib_seven/redis_edge
    fi
    if [ -e "/data/all/000/modules/redis" ] && [ ! -L "/data/all/$i/o_contrib_seven/redis" ] ; then
      ln -sf /data/all/000/modules/redis /data/all/$i/o_contrib_seven/redis
    fi
    if [ -e "/data/all/000/modules/redis_edge" ] && [ ! -L "/data/all/$i/o_contrib_seven/redis_edge" ] ; then
      ln -sf /data/all/000/modules/redis_edge /data/all/$i/o_contrib_seven/redis_edge
    fi
    find /data/all/$i/o_contrib_seven -type d -exec chmod 0755 {} \; &> /dev/null
    find /data/all/$i/o_contrib_seven -type f -exec chmod 0644 {} \; &> /dev/null
    touch /data/all/$i/o_contrib_seven/update-044.info
  fi
done
cd
}
#
# Download Textile.
download_textile () {
  cd textile
  svn export http://textpattern.googlecode.com/svn/development/4.x/textpattern/lib/classTextile.php &> /dev/null
  cp -af classTextile.php include/ &> /dev/null
  cd ../
  patch -p1 < /opt/tmp/$_BOA_REPO_NAME/aegir/patches/textile.patch &> /dev/null
}
#
# Download o_contrib_seven.
download_o_contrib_seven () {
  touch update-044.info
  #msg "$_STATUS A: Downloading o_contrib_seven modules, please wait..."
  get_dev_contrib "admin-7.x-2.0-beta3.tar.gz"
  get_dev_contrib "advagg-7.x-2.6.tar.gz"
  get_dev_contrib "blockcache_alter-7.x-1.x-dev.tar.gz"
  get_dev_contrib "boost-7.x-1.0.tar.gz"
  get_dev_contrib "cdn-7.x-2.x-dev.tar.gz"
  get_dev_contrib "config_perms-7.x-2.x-dev.tar.gz"
  get_dev_contrib "css_emimage-7.x-1.x-dev.tar.gz"
  get_dev_contrib "display_cache-7.x-1.0.tar.gz"
  get_dev_contrib "entitycache-7.x-1.2.tar.gz"
  get_dev_contrib "esi-7.x-3.x-dev.tar.gz"
  get_dev_contrib "filefield_nginx_progress-7.x-2.x-dev.tar.gz"
  get_dev_contrib "flood_control-7.x-1.x-dev.tar.gz"
  get_dev_contrib "force_password_change-7.x-1.0-rc2.tar.gz"
  get_dev_contrib "fpa-7.x-2.5.tar.gz"
  get_dev_contrib "httprl-7.x-1.14.tar.gz"
  get_dev_contrib "js-7.x-1.x-dev.tar.gz"
  get_dev_contrib "login_security-7.x-1.9.tar.gz"
  get_dev_contrib "nocurrent_pass-7.x-1.0.tar.gz"
  get_dev_contrib "panels_content_cache-7.x-1.0.tar.gz"
  get_dev_contrib "print-7.x-2.0.tar.gz"
  get_dev_contrib "readonlymode-7.x-1.1.tar.gz"
  get_dev_contrib "reroute_email-7.x-1.x-dev.tar.gz"
  get_dev_contrib "robotstxt-7.x-1.1.tar.gz"
  get_dev_contrib "securesite-7.x-2.x-dev.tar.gz"
  get_dev_contrib "session_expire-7.x-1.x-dev.tar.gz"
  get_dev_contrib "site_verify-7.x-1.1.tar.gz"
  get_dev_contrib "speedy-7.x-1.8.tar.gz"
  get_dev_contrib "taxonomy_edge-7.x-1.x-dev.tar.gz"
  get_dev_contrib "textile-7.x-2.0-rc11.tar.gz"
  get_dev_contrib "variable_clean-7.x-1.x-dev.tar.gz"
  get_dev_contrib "vars-7.x-2.0-alpha10.tar.gz"
  get_dev_contrib "views_cache_bully-7.x-3.1.tar.gz"
  get_dev_contrib "views_content_cache-7.x-3.0-alpha3.tar.gz"
  get_dev_contrib "views404-7.x-1.x-dev.tar.gz"
  if [ "$_PURGE_MODE" = "ON" ] && [ "$_PURGE_FOR_SEVEN" = "YES" ] ; then
    rm -f -r expire*
    rm -f -r purge*
    get_dev_contrib "expire-7.x-2.0-rc3.tar.gz"
    get_dev_contrib "purge-7.x-1.6.tar.gz"
  else
    rm -f -r nginx_accel_redirect*
    rm -f -r purge*
    rm -f -r expire*
  fi
  find ./ -type d -exec chmod 0755 {} \; &> /dev/null
  find ./ -type f -exec chmod 0644 {} \; &> /dev/null
  touch ctrl-$_INSTALLER_VERSION
  if [ ! -L "./redis" ] ; then
    ln -sf /data/all/000/modules/redis redis
  fi
  if [ ! -L "./redis_edge" ] ; then
    ln -sf /data/all/000/modules/redis_edge redis_edge
  fi
  download_textile
  if [ ! -e "/data/all/000/modules/redis/update-044.info" ] ; then
    mkdir -p /data/all/000/modules
    cd /data/all/000/modules
    rm -f -r /data/all/000/modules/redis
    get_dev_contrib "redis-7.x-2.x-o8-revert-A.tar.gz"
    rm -f redis/redis.info
    echo update > redis/update-044.info
  fi
  if [ ! -e "/data/all/000/modules/redis_edge/update-044.info" ] ; then
    mkdir -p /data/all/000/modules
    cd /data/all/000/modules
    rm -f -r /data/all/000/modules/redis_edge
    get_dev_contrib "redis_edge-7.x-2.x-o8-2.6-B.tar.gz"
    rm -f redis_edge/redis.info
    echo update > redis_edge/update-044.info
  fi
  find /data/all/000/modules -type d -exec chmod 0755 {} \; &> /dev/null
  find /data/all/000/modules -type f -exec chmod 0644 {} \; &> /dev/null
}
#
# Download o_contrib_six.
download_o_contrib_six () {
  touch update-044.info
  #msg "$_STATUS A: Downloading o_contrib modules, please wait..."
  get_dev_contrib "admin-6.x-2.0.tar.gz"
  get_dev_contrib "advagg-6.x-1.x-dev.tar.gz"
  get_dev_contrib "blockcache_alter-6.x-1.5.tar.gz"
  get_dev_contrib "boost-6.x-1.x.tar.gz"
  get_dev_contrib "cdn-6.x-2.x-dev.tar.gz"
  get_dev_contrib "config_perms-6.x-2.x-dev.tar.gz"
  get_dev_contrib "css_emimage-6.x-2.x-dev.tar.gz"
  get_dev_contrib "dbtuner-6.x-1.x-dev.tar.gz"
  get_dev_contrib "esi-6.x-2.x-dev.tar.gz"
  get_dev_contrib "filefield_nginx_progress-6.x-1.x-dev.tar.gz"
  get_dev_contrib "force_password_change-6.x-3.4.tar.gz"
  get_dev_contrib "fpa-6.x-2.5.tar.gz"
  get_dev_contrib "httprl-6.x-1.14.tar.gz"
  get_dev_contrib "image-6.x-1.2.tar.gz"
  get_dev_contrib "js-6.x-1.3.tar.gz"
  get_dev_contrib "login_security-6.x-1.4.tar.gz"
  get_dev_contrib "panels_content_cache-6.x-1.0.tar.gz"
  get_dev_contrib "phpass-6.x-2.0.tar.gz"
  get_dev_contrib "print-6.x-1.19.tar.gz"
  get_dev_contrib "private_upload-6.x-1.x-dev.tar.gz"
  get_dev_contrib "readonlymode-6.x-1.2.tar.gz"
  get_dev_contrib "reroute_email-6.x-1.2.tar.gz"
  get_dev_contrib "robotstxt-6.x-1.3.tar.gz"
  get_dev_contrib "securesite-6.x-2.4.tar.gz"
  get_dev_contrib "session_expire-6.x-1.x-dev.tar.gz"
  get_dev_contrib "site_verify-6.x-1.0.tar.gz"
  get_dev_contrib "taxonomy_edge-6.x-1.7.tar.gz"
  get_dev_contrib "textile-6.x-2.4.tar.gz"
  get_dev_contrib "variable_clean-6.x-1.x-dev.tar.gz"
  get_dev_contrib "views_cache_bully-6.x-3.1.tar.gz"
  get_dev_contrib "views_content_cache-6.x-2.x-dev.tar.gz"
  get_dev_contrib "views404-6.x-1.x-dev.tar.gz"
  if [ "$_PURGE_MODE" = "ON" ] ; then
    rm -f -r purge*
    rm -f -r expire*
    get_dev_contrib "purge-6.x-1.6.tar.gz"
    get_dev_contrib "expire-6.x-1.x-dev.tar.gz"
  else
    rm -f -r nginx_accel_redirect*
    rm -f -r purge*
    rm -f -r expire*
  fi
  find ./ -type d -exec chmod 0755 {} \; &> /dev/null
  find ./ -type f -exec chmod 0644 {} \; &> /dev/null
  touch ctrl-$_INSTALLER_VERSION
  if [ ! -L "./redis" ] ; then
    ln -sf /data/all/000/modules/redis redis
  fi
  if [ ! -L "./redis_edge" ] ; then
    ln -sf /data/all/000/modules/redis_edge redis_edge
  fi
  if [ ! -L "./cache_backport" ] ; then
    ln -sf /data/all/000/modules/cache_backport cache_backport
  fi
  download_textile
  if [ ! -e "/data/all/000/modules/cache_backport/update-044.info" ] ; then
    mkdir -p /data/all/000/modules
    cd /data/all/000/modules
    rm -f -r /data/all/000/modules/cache_backport
    get_dev_contrib "cache_backport-6.x-1.0-rc1.tar.gz"
    sed -i "s/Cache Backand/D7 Cache Backport for D6/g" cache_backport/cache_backport.info &> /dev/null
    echo update > cache_backport/update-044.info
  fi
  if [ ! -e "/data/all/000/modules/redis/update-044.info" ] ; then
    mkdir -p /data/all/000/modules
    cd /data/all/000/modules
    rm -f -r /data/all/000/modules/redis
    get_dev_contrib "redis-7.x-2.x-o8-revert-A.tar.gz"
    rm -f redis/redis.info
    echo update > redis/update-044.info
  fi
  if [ ! -e "/data/all/000/modules/redis_edge/update-044.info" ] ; then
    mkdir -p /data/all/000/modules
    cd /data/all/000/modules
    rm -f -r /data/all/000/modules/redis_edge
    get_dev_contrib "redis_edge-7.x-2.x-o8-2.6-B.tar.gz"
    rm -f redis_edge/redis.info
    echo update > redis_edge/update-044.info
  fi
  find /data/all/000/modules -type d -exec chmod 0755 {} \; &> /dev/null
  find /data/all/000/modules -type f -exec chmod 0644 {} \; &> /dev/null
}
#
# Upgrade o_contrib.
upgrade_o_contrib () {
if [ -e "$_CORE/o_contrib/ctrl-$_INSTALLER_VERSION" ] && [ -e "$_CORE/o_contrib_seven/ctrl-$_INSTALLER_VERSION" ] ; then
  _O_CONTRIB_UP=NO
fi
if [ -e "/data/all/$_LAST_ALL/o_contrib/ctrl-$_INSTALLER_VERSION" ] && [ -e "/data/all/$_LAST_ALL/o_contrib_seven/ctrl-$_INSTALLER_VERSION" ] ; then
  _O_CONTRIB_UP=NO
fi
if [ -e "$_CORE/o_contrib" ] ; then
  if [ ! -e "$_CORE/o_contrib/textile/include/classTextile.php" ] ; then
    _O_CONTRIB_UP=YES
  fi
fi
if [ -e "$_CORE/o_contrib_seven" ] ; then
  if [ ! -e "$_CORE/o_contrib_seven/textile/include/classTextile.php" ] ; then
    _O_CONTRIB_UP=YES
  fi
fi
if [ -e "/data/all/$_LAST_ALL/o_contrib" ] ; then
  if [ ! -e "/data/all/$_LAST_ALL/o_contrib/textile/include/classTextile.php" ] ; then
    _O_CONTRIB_UP=YES
  fi
  if [ ! -e "/data/all/$_LAST_ALL/o_contrib/redis" ] || [ ! -e "/data/all/$_LAST_ALL/o_contrib/redis_edge" ] ; then
    _O_CONTRIB_UP=YES
  fi
fi
if [ -e "/data/all/$_LAST_ALL/o_contrib_seven" ] ; then
  if [ ! -e "/data/all/$_LAST_ALL/o_contrib_seven/textile/include/classTextile.php" ] ; then
    _O_CONTRIB_UP=YES
  fi
  if [ ! -e "/data/all/$_LAST_ALL/o_contrib_seven/redis" ] || [ ! -e "/data/all/$_LAST_ALL/o_contrib_seven/redis_edge" ] ; then
    _O_CONTRIB_UP=YES
  fi
fi
if [ ! -e "$_THIS_CTRL" ] ; then
  _O_CONTRIB_UP=YES
fi
if [ ! -e "/data/all/000/modules/redis/update-044.info" ] || [ ! -e "/data/all/000/modules/redis_edge/update-044.info" ] ; then
  _O_CONTRIB_UP=YES
fi
if [ "$_PURGE_MODE" = "ON" ] ; then
  _TEST_MODULE=purge
else
  _TEST_MODULE=textile
fi
if [ -e "/data/all/$_LAST_ALL/o_contrib" ] ; then
 if [ ! -d "/data/all/$_LAST_ALL/o_contrib/$_TEST_MODULE" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
  #msg "$_STATUS A: Running o_contrib upgrade #1, please wait..."
  cd /data/all/$_LAST_ALL/o_contrib
  rm -f -r *
  download_o_contrib_six
  cd /data/all/$_LAST_ALL/o_contrib
 fi
fi
if [ -e "$_CORE/o_contrib" ] ; then
 if [ ! -d "$_CORE/o_contrib/$_TEST_MODULE" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
  #msg "$_STATUS A: Running o_contrib upgrade #2, please wait..."
  cd $_CORE/o_contrib
  rm -f -r *
  download_o_contrib_six
  cd $_CORE/o_contrib
 fi
fi
if [ -e "/data/all/$_LAST_ALL/o_contrib_seven" ] ; then
 if [ ! -d "/data/all/$_LAST_ALL/o_contrib_seven/robotstxt" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
  #msg "$_STATUS A: Running o_contrib_seven upgrade #1, please wait..."
  cd /data/all/$_LAST_ALL/o_contrib_seven
  rm -f -r *
  download_o_contrib_seven
  cd /data/all/$_LAST_ALL/o_contrib_seven
 fi
fi
if [ -e "$_CORE/o_contrib_seven" ] ; then
 if [ ! -d "$_CORE/o_contrib_seven/robotstxt" ] || [ "$_O_CONTRIB_UP" = "YES" ] ; then
  #msg "$_STATUS A: Running o_contrib_seven upgrade #2, please wait..."
  cd $_CORE/o_contrib_seven
  rm -f -r *
  download_o_contrib_seven
  cd $_CORE/o_contrib_seven
 fi
fi
}
#
# Manage o_contrib.
manage_o_contrib () {
  if [ ! -e "$_CORE/o_contrib_seven" ] ; then
    mkdir -p $_CORE/o_contrib_seven
    cd $_CORE/o_contrib_seven
    download_o_contrib_seven
    cd $_CORE/o_contrib_seven
  fi
  if [ ! -e "$_CORE/o_contrib" ] ; then
    mkdir -p $_CORE/o_contrib
    cd $_CORE/o_contrib
    download_o_contrib_six
    cd $_CORE/o_contrib
  fi
  mkdir -p /data/all/000/modules
  rm -f /data/all/000/modules/o_contrib_seven
  ln -s $_CORE/o_contrib_seven /data/all/000/modules/o_contrib_seven
  rm -f /data/all/000/modules/o_contrib
  ln -s $_CORE/o_contrib /data/all/000/modules/o_contrib
  if [ $_STATUS != "INIT" ] ; then
    msg "$_STATUS A: Running o_contrib modules check and upgrade..."
    upgrade_o_contrib
  fi
}


###---### Checking status.
#
if [ -d "$_ROOT" ] ; then
  _STATUS=UPGRADE
  cd $_ROOT
  rm -f $_ROOT/AegirSetupC.sh.txt
  rm -f $_ROOT/AegirSetupB.sh.txt
fi


###---### User check.
#
if [ `whoami` = "root" ] ; then
  msg "$_STATUS A: Aegir automated install script part A"
else
  msg "$_STATUS A: FATAL ERROR: This script should be ran as a root user"
  msg "$_STATUS A: FATAL ERROR: Aborting AegirSetupA installer NOW!"
  touch /opt/tmp/status-AegirSetupA-FAIL
  exit 1
fi


###---### Checking version.
#
msg "$_STATUS A: Checking OCTOPUS version..."
if [ "$_AEGIR_VERSION" = "HEAD" ] ; then
  _FILE_VERSION=octopus-release.txt
else
  _FILE_VERSION=octopus-version.txt
fi
if [ -e "/opt/tmp/$_BOA_REPO_NAME/aegir/conf/$_FILE_VERSION" ] ; then
  _VERSIONS_TEST=`cat /opt/tmp/$_BOA_REPO_NAME/aegir/conf/$_FILE_VERSION`
  if [[ "$_VERSIONS_TEST" =~ "-dev" ]] ; then
    if [[ "$_VERSIONS_TEST" =~ "$_INSTALLER_VERSION" ]] && [ "$_AEGIR_VERSION" = "HEAD" ] ; then
      _VERSIONS_TEST_RESULT=OK
      msg "$_STATUS A: OCTOPUS version test: OK"
    else
      _VERSIONS_TEST_RESULT=FAIL
    fi
  else
    if [[ "$_VERSIONS_TEST" =~ "$_INSTALLER_VERSION" ]] && [[ "$_AEGIR_VERSION" =~ "BOA" ]] ; then
      _VERSIONS_TEST_RESULT=OK
      msg "$_STATUS A: OCTOPUS version test: OK"
    else
      _VERSIONS_TEST_RESULT=FAIL
    fi
  fi
  if [ "$_VERSIONS_TEST_RESULT" = "FAIL" ] ; then
    msg "ERROR: This OCTOPUS version is outdated and will not work correctly"
    msg "Bye"
    touch /opt/tmp/status-AegirSetupA-FAIL
    exit 1
  fi
fi


###---### Hot Sauce check.
#
if [ "$_HOT_SAUCE" = "NO" ] ; then
  _CORE="/data/all/$_LAST_ALL"
  _THIS_CORE="$_LAST_ALL"
  if [ "$_USE_CURRENT" = "YES" ] && [ -e "/data/all/000/core-v-$_SMALLCORE6.txt" ] && [ -e "/data/all/000/core-v-$_SMALLCORE7.txt" ] ; then
    msg "$_STATUS A: Shared platforms code v.$_LAST_ALL (latest available) will be used for this install"
  elif [ "$_USE_CURRENT" = "NO" ] || [ ! -e "/data/all/000/core-v-$_SMALLCORE6.txt" ] || [ ! -e "/data/all/000/core-v-$_SMALLCORE7.txt" ] ; then
    _CORE="/data/all/$_ALL_DISTRO"
    _THIS_CORE="$_ALL_DISTRO"
    msg "$_STATUS A: Shared platforms code v.$_ALL_DISTRO (hot new) will be created"
    sed -i "s/^_USE_CURRENT=.*/_USE_CURRENT=NO/g" /opt/tmp/$_BOA_REPO_NAME/aegir/scripts/AegirSetupC.sh.txt &> /dev/null
  else
    msg "$_STATUS A: Shared platforms code v.$_LAST_ALL (latest available) will be used for this install"
  fi
else
  _CORE="/data/all/$_ALL_DISTRO"
  _THIS_CORE="$_ALL_DISTRO"
  msg "$_STATUS A: Shared platforms code v.$_ALL_DISTRO (hot new) will be created"
fi
_THIS_CTRL="$_CORE/o_contrib/ctrl-$_INSTALLER_VERSION"


###---### Add required users and create directories.
#
msg "$_STATUS A: Creating directories with correct permissions..."
mkdir -p /data/u
mkdir -p /data/disk
mkdir -p /data/conf
chown root:root /data &> /dev/null
chown root:root /data/disk &> /dev/null
if [ ! -d "$_ROOT" ] ; then
  msg "$_STATUS A: Adding user..."
  find /etc/[a-z]*\.lock -maxdepth 1 -type f -exec rm -rf {} \; &> /dev/null
  adduser --system --home $_ROOT --ingroup $_USRG $_USER &> /dev/null
  adduser $_USER $_WEBG &> /dev/null
fi
chown -R $_USER:$_USRG /opt/tmp &> /dev/null
chown -R $_USER:$_USRG /data/conf &> /dev/null


###---### Prepare scripts.
#
chmod 0711 $_ROOT
cd $_ROOT
cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/scripts/AegirSetupB.sh.txt ./
cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/scripts/AegirSetupC.sh.txt ./
chown $_USER:$_USRG $_ROOT/AegirSetupB.sh.txt &> /dev/null
chown $_USER:$_USRG $_ROOT/AegirSetupC.sh.txt &> /dev/null


###---### Run standard pre-install.
#
# Generate provision backend db_passwd.
provision_backend_dbpass_generate () {
  touch $_ROOT/.$_USER.pass.txt
  chmod 0600 $_ROOT/.$_USER.pass.txt
  _ESC_PASS=""
  _LEN_PASS=0
  if [ "$_STRONG_PASSWORDS" = "YES" ] ; then
    _ESC_PASS=$(randpass 32 esc)
    _ESC_PASS=`echo -n $_ESC_PASS | tr -d "\n"`
    _ENC_PASS=$(python -c "import urllib; print urllib.quote('''$_ESC_PASS''')")
    _LEN_PASS=$(echo ${#_ESC_PASS})
  fi
  if [ -z "$_ESC_PASS" ] || [ $_LEN_PASS -lt 19 ] ; then
    _ESC_PASS=`pwgen -v -s -1`
    _ESC_PASS=`echo -n $_ESC_PASS | tr -d "\n"`
    _ESC_PASS=`sanitize_string "$_ESC_PASS"`
    _ENC_PASS="$_ESC_PASS"
  fi
  echo "$_ESC_PASS" > $_ROOT/.$_USER.pass.txt
  chown $_USER:$_USRG $_ROOT/.$_USER.pass.txt &> /dev/null
  _AEGIR_HOST=`uname -n`
  _RESOLVEIP=`echo $(getent ahostsv4 $_AEGIR_HOST) | cut -d: -f2 | awk '{ print $1}' 2> /dev/null`
  if [ -z "$_RESOLVEIP" ] ; then
    msg "$_STATUS A: FATAL ERROR: This server does not have a hostname that resolves to an IP address"
    msg "$_STATUS A: FATAL ERROR: Aborting AegirSetupA installer NOW!"
    touch /opt/tmp/status-AegirSetupA-FAIL
    exit 1
  fi
_ESC="\`%\`.*"
mysqladmin flush-privileges &> /dev/null
if [ "$_STATUS" = "INIT" ] ; then
  mysql -uroot mysql<<EOFMYSQL
GRANT ALL PRIVILEGES ON $_ESC TO '$_USER'@'$_AEGIR_HOST' IDENTIFIED BY '$_ESC_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON $_ESC TO '$_USER'@'$_RESOLVEIP' IDENTIFIED BY '$_ESC_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON $_ESC TO '$_USER'@'localhost' IDENTIFIED BY '$_ESC_PASS' WITH GRANT OPTION;
EOFMYSQL
else
  mysql -uroot mysql<<EOFMYSQL
DROP USER '$_USER'@'$_AEGIR_HOST';
DROP USER '$_USER'@'$_RESOLVEIP';
DROP USER '$_USER'@'localhost';
EOFMYSQL
  mysql -uroot mysql<<EOFMYSQL
GRANT ALL PRIVILEGES ON $_ESC TO '$_USER'@'$_AEGIR_HOST' IDENTIFIED BY '$_ESC_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON $_ESC TO '$_USER'@'$_RESOLVEIP' IDENTIFIED BY '$_ESC_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON $_ESC TO '$_USER'@'localhost' IDENTIFIED BY '$_ESC_PASS' WITH GRANT OPTION;
EOFMYSQL
fi
mysqladmin flush-privileges &> /dev/null
}
#
# Sync provision backend db_passwd.
provision_backend_dbpass_sync () {
  msg "$_STATUS A: Syncing provision backend db_passwd..."
  _L_SYS="$_ROOT/.$_USER.pass.txt"
  mv -f $_L_SYS $_L_SYS-pre-$_INSTALLER_VERSION-$_NOW &> /dev/null
  provision_backend_dbpass_generate
  if [ ! -z "$_ESC_PASS" ] && [ ! -z "$_ENC_PASS" ] ; then
    su -s /bin/bash - $_USER -c "$_DRUSHCMD @hostmaster sqlq \"UPDATE hosting_db_server SET db_passwd='$_ESC_PASS' WHERE db_user='$_USER'\" &> /dev/null"
    mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('$_ESC_PASS') WHERE User='$_USER';" &> /dev/null
    sed -i "s/mysql:\/\/$_USER:.*/mysql:\/\/$_USER:$_ENC_PASS@localhost',/g" $_ROOT/.drush/server_localhost.alias.drushrc.php &> /dev/null
    sed -i "s/mysql:\/\/$_USER:.*/mysql:\/\/$_USER:$_ENC_PASS@localhost',/g" $_ROOT/.drush/server_master.alias.drushrc.php &> /dev/null
  fi
  mysqladmin flush-privileges &> /dev/null
  if [ "$_USE_STOCK" = "YES" ] || [ "$_DRUSH_CC_REQ" = "YES" ] ; then
    su -s /bin/bash $_USER -c "$_DRUSHCMD cache-clear drush &> /dev/null"
  fi
  if [ -e "$_ROOT/.drush/server_localhost.alias.drushrc.php" ] ; then
    su -s /bin/bash $_USER -c "$_DRUSHCMD @hostmaster hosting-task @server_localhost verify --force &> /dev/null"
  else
    su -s /bin/bash $_USER -c "$_DRUSHCMD @hostmaster hosting-task @server_master verify --force &> /dev/null"
  fi
  msg "$_STATUS A: Running hosting-dispatch (1/3)..."
  su -s /bin/bash $_USER -c "$_DRUSHCMD '@hostmaster' hosting-dispatch &> /dev/null"
  mrun "sleep 5"
  msg "$_STATUS A: Running hosting-dispatch (2/3)..."
  su -s /bin/bash $_USER -c "$_DRUSHCMD '@hostmaster' hosting-dispatch &> /dev/null"
  mrun "sleep 5"
  msg "$_STATUS A: Running hosting-dispatch (3/3)..."
  su -s /bin/bash $_USER -c "$_DRUSHCMD '@hostmaster' hosting-dispatch &> /dev/null"
}
#
# Sync hostmaster frontend db_passwd.
hostmaster_frontend_dbpass_sync () {
  msg "$_STATUS A: Syncing hostmaster frontend db_passwd..."
  _THIS_HM_SPTH=`cat $_ROOT/.drush/hostmaster.alias.drushrc.php | grep "site_path'" | cut -d: -f2 | awk '{ print $3}' | sed "s/[\,']//g"`
  _THIS_HM_DBUR=`cat $_THIS_HM_SPTH/drushrc.php | grep "options\['db_user'\] = " | cut -d: -f2 | awk '{ print $3}' | sed "s/[\,';]//g"`
  _THIS_HM_DBPD=`cat $_THIS_HM_SPTH/drushrc.php | grep "options\['db_passwd'\] = " | cut -d: -f2 | awk '{ print $3}' | sed "s/[\,';]//g"`
  if [ -e "$_THIS_HM_SPTH" ] && [ ! -z "$_THIS_HM_DBUR" ] && [ ! -z "$_THIS_HM_DBPD" ] ; then
    mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('$_THIS_HM_DBPD') WHERE User='$_THIS_HM_DBUR';" &> /dev/null
  fi
  mysqladmin flush-privileges &> /dev/null
}
if [ "$_STATUS" = "INIT" ] ; then
  mysqladmin flush-hosts &> /dev/null
  provision_backend_dbpass_generate
  echo "$_USER ALL=NOPASSWD: /etc/init.d/nginx" >> /etc/sudoers
else
  mysqladmin flush-hosts &> /dev/null
  provision_backend_dbpass_sync
fi
cd $_ROOT


###---### Install or upgrade Aegir Satellite Instance [Y/n]
#
_LOCAL_STATUS="$_STATUS"
if [ -e "/var/run/aegir_upgrade.pid" ] ; then
  _LOCAL_STATUS=INIT
fi
if [ "$_LOCAL_STATUS" = "INIT" ] ; then
  msg "$_STATUS A: Switching user and running AegirSetupB..."
  rm -f /opt/tmp/testecho*
  chown root:$_USRG /data/u &> /dev/null
  chmod 0771 /data/u &> /dev/null
  su -s /bin/bash - $_USER -c "/bin/bash AegirSetupB.sh.txt"
  if [ -e "/opt/tmp/status-AegirSetupB-FAIL" ] ; then
    msg "$_STATUS A: FATAL ERROR: AegirSetupB installer failed"
    msg "$_STATUS A: FATAL ERROR: Aborting AegirSetupA installer NOW!"
    touch /opt/tmp/status-AegirSetupA-FAIL
    exit 1
  fi
  _U_HD="$_ROOT/.drush"
  chattr +i $_U_HD/php.ini &> /dev/null
  chmod 0700 /data/u &> /dev/null
  chown root:root /data/u &> /dev/null
  msg "$_STATUS A: Aegir Satellite Instance installation completed"
else
  echo " "
  if prompt_yes_no "Do you want to upgrade this Aegir Satellite Instance?" ; then
    true
    if [ "$_PLATFORMS_ONLY" = "YES" ] ; then
      msg "$_STATUS A: Aegir Satellite Instance upgrade skipped - platforms only"
    else
      if [ ! -d "$_ROOT/.drush/sys/provision/http" ] ; then
        mkdir -p $_ROOT/.drush/{sys,xts,usr}
        cd $_ROOT/.drush/sys
        rm -f -r $_ROOT/.drush/{provision,drush_make}
        rm -f -r $_ROOT/.drush/sys/{provision,drush_make}
        curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 15 -A iCab "http://files.aegir.cc/versions/stable/tar/provision-$_AEGIR_VERSION.tar.gz" | tar -xzf -
        get_dev_ext "drush_make.tar.gz"
        cp -af /var/aegir/drush/includes/table.inc /data/disk/o1/tools/drush/includes/table.inc
      fi
      hostmaster_frontend_dbpass_sync
      msg "$_STATUS A: Switching user and running AegirSetupB..."
      rm -f /opt/tmp/testecho*
      if [ ! -e "/root/.upstart.cnf" ] ; then
        service cron stop &> /dev/null
      fi
      _THIS_HM_ROOT=`cat $_ROOT/.drush/hostmaster.alias.drushrc.php | grep "root'" | cut -d: -f2 | awk '{ print $3}' | sed "s/[\,']//g"`
      _THIS_HM_SITE=`cat $_ROOT/.drush/hostmaster.alias.drushrc.php | grep "site_path'" | cut -d: -f2 | awk '{ print $3}' | sed "s/[\,']//g"`
      _U_HD="$_ROOT/.drush"
      chattr -i $_U_HD/php.ini &> /dev/null
      chown -R $_USER:$_USRG $_ROOT/.drush
      chown -R $_USER:$_USRG $_ROOT/backups
      chown -R $_USER:$_USRG $_ROOT/clients
      chown -R $_USER:$_USRG $_ROOT/config
      chown -R $_USER:$_USRG $_ROOT/tools
      chown -R $_USER $_THIS_HM_ROOT
      chown -R $_USER:$_WEBG $_THIS_HM_SITE/files
      chmod -R 02775 $_THIS_HM_SITE/files
      chown root:$_USRG /data/u &> /dev/null
      chmod 0771 /data/u &> /dev/null
      su -s /bin/bash - $_USER -c "/bin/bash AegirSetupB.sh.txt"
      if [ -e "/opt/tmp/status-AegirSetupB-FAIL" ] ; then
        msg "$_STATUS A: FATAL ERROR: AegirSetupB installer failed"
        msg "$_STATUS A: FATAL ERROR: Aborting AegirSetupA installer NOW!"
        touch /opt/tmp/status-AegirSetupA-FAIL
        exit 1
      else
        chattr +i $_U_HD/php.ini &> /dev/null
        mkdir -p $_ROOT/backups/system/old_hostmaster
        chmod 700 $_ROOT/backups/system/old_hostmaster
        chmod 700 $_ROOT/backups/system
        mv -f $_ROOT/backups/*host8* $_ROOT/backups/system/old_hostmaster/ &> /dev/null
        mv -f $_ROOT/backups/*o8.io* $_ROOT/backups/system/old_hostmaster/ &> /dev/null
        chmod 600 $_ROOT/backups/system/old_hostmaster/* &> /dev/null
        hostmaster_frontend_dbpass_sync
      fi
      chmod 0700 /data/u &> /dev/null
      chown root:root /data/u &> /dev/null
      msg "$_STATUS A: Aegir Satellite Instance upgrade completed"
    fi
  else
    msg "$_STATUS A: Aegir Satellite Instance not upgraded this time"
  fi
fi


###---### Create ~/bin dir if not exists.
#
_L_BIN="$_ROOT/bin"
if [ ! -d "$_L_BIN" ] ; then
  mkdir -p $_L_BIN
  chown $_USER:$_USRG $_L_BIN
  chmod 700 $_L_BIN
fi
if [ ! -L "$_L_BIN/drush" ] ; then
  ln -sf $_ROOT/tools/drush/drush $_L_BIN/drush
fi


###---### Run standard post-install.
#
_LOCAL_STATUS="$_STATUS"
if [ -e "/var/run/aegir_upgrade.pid" ] ; then
  _LOCAL_STATUS=INIT
fi
if [ "$_LOCAL_STATUS" = "INIT" ] ; then
  if [ ! -e "$_ROOT/config/$_USER.nginx.conf" ] ; then
    rm -f /var/aegir/config/server_master/nginx/platform.d/$_USER.conf
    echo "include $_ROOT/config/server_master/nginx/vhost.d/*;" > $_ROOT/config/$_USER.nginx.conf
    ln -sf $_ROOT/config/$_USER.nginx.conf /var/aegir/config/server_master/nginx/platform.d/$_USER.conf
  fi
  chgrp -R $_WEBG $_HOSTMASTER_ROOT/sites/$_DOMAIN/files
  chgrp $_WEBG $_HOSTMASTER_ROOT/sites/$_DOMAIN/settings.php
  rm -f -r $_HOSTMASTER_ROOT/profiles/default
  rm -f -r $_HOSTMASTER_ROOT/themes/bluemarine
  rm -f -r $_HOSTMASTER_ROOT/themes/chameleon
  rm -f -r $_HOSTMASTER_ROOT/themes/pushbutton
  rm -f -r $_HOSTMASTER_ROOT/scripts
  rm -f $_HOSTMASTER_ROOT/themes/README.txt
  rm -f $_HOSTMASTER_ROOT/*.txt
  mrun "service nginx reload" &> /dev/null
  cd $_HOSTMASTER_ROOT
  cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/robots.txt ./
  cd $_ROOT
fi


###---### Set permissions for all.
#
chmod 0755 $_HOSTMASTER_ROOT &> /dev/null
find $_ROOT/config/server_master -type d -exec chmod 0700 {} \; &> /dev/null
find $_ROOT/config/server_master -type f -exec chmod 0600 {} \; &> /dev/null
chmod 0711 $_ROOT/config &> /dev/null
chmod 0711 $_ROOT/config/includes &> /dev/null
chmod 0750 $_ROOT/backups &> /dev/null
chmod 0750 $_ROOT/clients &> /dev/null
find $_ROOT/aegir/distro/*/profiles/* -type d -exec chmod 0755 {} \; &> /dev/null
find $_ROOT/aegir/distro/*/profiles/* -type f -exec chmod 0644 {} \; &> /dev/null
find $_ROOT/aegir/distro/*/sites/all/* -type d -exec chmod 0755 {} \; &> /dev/null
find $_ROOT/aegir/distro/*/sites/all/* -type f -exec chmod 0644 {} \; &> /dev/null
chown -R $_USER:$_USRG $_ROOT/.drush &> /dev/null
find $_ROOT/.drush -type d -exec chmod 0710 {} \; &> /dev/null
find $_ROOT/.drush/usr/registry_rebuild -type d -exec chmod 0750 {} \; &> /dev/null
find $_ROOT/.drush/usr/drush_ecl -type d -exec chmod 0750 {} \; &> /dev/null
find $_ROOT/.drush/usr/clean_missing_modules -type d -exec chmod 0750 {} \; &> /dev/null
find $_ROOT/.drush -type f -exec chmod 0640 {} \; &> /dev/null
chmod 0440 $_ROOT/.drush/*.php &> /dev/null
chmod 0400 $_ROOT/.drush/server_*.php &> /dev/null
chmod 0400 $_ROOT/.drush/platform_*.php &> /dev/null
chmod 0400 $_ROOT/.drush/hostmaster*.php &> /dev/null
chmod 0710 $_ROOT/.drush &> /dev/null


###---### Platforms [Y/n]
#
_LOCAL_STATUS="$_STATUS"
if [ -e "/var/run/aegir_upgrade.pid" ] ; then
  _LOCAL_STATUS=INIT
fi
if [ "$_LOCAL_STATUS" = "INIT" ] ; then
  _DIST_INSTALL=YES
  rm -f /opt/tmp/testecho*
  create_shared_dirs
  manage_o_contrib
  msg "$_STATUS A: Switching user and running Platforms build"
  cd $_ROOT
  su -s /bin/bash - $_USER -c "/bin/bash $_ROOT/AegirSetupC.sh.txt"
  if [ -e "/opt/tmp/status-AegirSetupC-FAIL" ] ; then
    msg "$_STATUS A: FATAL ERROR: AegirSetupC installer failed"
    msg "$_STATUS A: FATAL ERROR: Aborting AegirSetupA installer NOW!"
    touch /opt/tmp/status-AegirSetupA-FAIL
    exit 1
  fi
  msg "$_STATUS A: Platforms installation completed"
else
  if [ "$_HM_ONLY" = "YES" ] ; then
    _DIST_INSTALL=NO
  else
    echo " "
    if prompt_yes_no "Do you want to install some latest, ready to use platforms?" ; then
      true
      _DIST_INSTALL=YES
      if [ ! -e "/root/.upstart.cnf" ] ; then
        service cron stop &> /dev/null
      fi
      rm -f /opt/tmp/testecho*
      create_shared_dirs
      manage_o_contrib
      msg "$_STATUS A: Switching user and running Platforms build"
      cd $_ROOT
      su -s /bin/bash - $_USER -c "/bin/bash $_ROOT/AegirSetupC.sh.txt"
      if [ -e "/opt/tmp/status-AegirSetupC-FAIL" ] ; then
        msg "$_STATUS A: FATAL ERROR: AegirSetupC installer failed"
        msg "$_STATUS A: FATAL ERROR: Aborting AegirSetupA installer NOW!"
        touch /opt/tmp/status-AegirSetupA-FAIL
        exit 1
      fi
      msg "$_STATUS A: Platforms installation completed"
    else
      msg "$_STATUS A: No new platforms added this time"
    fi
  fi
fi
if [ ! -e "$_CORE/dot-files-ctrl-$_INSTALLER_VERSION" ] ; then
  msg "$_STATUS A: Cleaning up various dot files..."
  cd $_CORE
  st_runner "find . -name .svn -exec rm -rf {} \;" &> /dev/null
  st_runner "find . -name .bzr -exec rm -rf {} \;" &> /dev/null
  st_runner "find . -name .git -exec rm -rf {} \;" &> /dev/null
  st_runner "find . -name .DS_Store -exec rm -rf {} \;" &> /dev/null
  st_runner "find . -name "._*" -type f | xargs rm -rf" &> /dev/null
  touch $_CORE/dot-files-ctrl-$_INSTALLER_VERSION
fi


###---### Fix files permissions.
#
if [ -e "/var/run/aegir_upgrade.pid" ] ; then
  _PLATF_IMP=$(ls -la /data/u | grep $_USER | cut -d'>' -f2 | sort | uniq | awk '{ print $1}')
  for _PLATF in $_PLATF_IMP ; do
    if [[ "$_PLATF" =~ "aegir" ]] ; then
	  _FOUND_HM=YES
    else
      chown -R $_USER:www-data $_PLATF/sites/*/files &> /dev/null
      chmod -R 02775 $_PLATF/sites/*/files &> /dev/null
    fi
  done
fi


###---### Clean up old and install files.
#
rm -f $_ROOT/AegirSetupC.sh.txt
rm -f $_ROOT/AegirSetupB.sh.txt
rm -f $_ROOT/*.sh.txt


###---### Add cron task.
#
_LOCAL_STATUS="$_STATUS"
if [ -e "/var/run/aegir_upgrade.pid" ] ; then
  _LOCAL_STATUS=INIT
fi
rm -f /var/spool/cron/crontabs/$_USER
if [ ! -e "/var/xdrago/log/cron_aegir_off.pid" ] ; then
  sed -i "s/\*\/1 \* \* \* \* bash \/var\/xdrago\/run/#*\/1 * * * * bash \/var\/xdrago\/run/g" /var/spool/cron/crontabs/root &> /dev/null
  echo -e "* * * * * bash /var/xdrago/runner.sh >/dev/null 2>&1" | tee -a /var/spool/cron/crontabs/root >/dev/null 2>&1
  touch /var/xdrago/log/cron_aegir_off.pid
fi


###---### Add ftps/lshell access.
#
_LOCAL_STATUS="$_STATUS"
if [ -e "/var/run/aegir_upgrade.pid" ] ; then
  _LOCAL_STATUS=INIT
fi
if [ "$_LOCAL_STATUS" = "INIT" ] ; then
  _USERFTP="${_USER}.ftp"
  _USERFTP_ROOT="/home/$_USERFTP"
  if [ -e "/usr/bin/MySecureShell" ] && [ -e "/etc/ssh/sftp_config" ] ; then
    _PATH_LSHELL="/usr/bin/MySecureShell"
  else
    _PATH_LSHELL="/usr/bin/lshell"
  fi
  if [ ! -d "$_USERFTP_ROOT" ] ; then
    msg "$_STATUS A: Adding ftps/lshell user"
    # add user
    find /etc/[a-z]*\.lock -maxdepth 1 -type f -exec rm -rf {} \; &> /dev/null
    useradd -d /home/$_USERFTP -s $_PATH_LSHELL -m -N -r $_USERFTP &> /dev/null
    adduser $_USERFTP $_WEBG &> /dev/null
    # Make sure new file which contains password is private
    cd $_ROOT/log
    touch $_ROOT/log/pass.txt
    chmod 0600 $_ROOT/log/pass.txt
    # generate a nice secure password and put it in a file
    _ESC_LUPASS=""
    _LEN_LUPASS=0
    if [ "$_STRONG_PASSWORDS" = "YES" ] ; then
      _ESC_LUPASS=$(randpass 32 alnum)
      _ESC_LUPASS=`echo -n $_ESC_LUPASS | tr -d "\n"`
      _LEN_LUPASS=$(echo ${#_ESC_LUPASS})
    fi
    if [ -z "$_ESC_LUPASS" ] || [ $_LEN_LUPASS -lt 19 ] ; then
      _ESC_LUPASS=`pwgen -v -s -1`
      _ESC_LUPASS=`echo -n $_ESC_LUPASS | tr -d "\n"`
      _ESC_LUPASS=`sanitize_string "$_ESC_LUPASS"`
    fi
    echo "$_ESC_LUPASS" > $_ROOT/log/pass.txt
    # get the password hash
    ph=$(mkpasswd -m sha-512 $_ESC_LUPASS $(openssl rand -base64 16 | tr -d '+=' | head -c 16))
    # Set the password
    usermod -p $ph $_USERFTP &> /dev/null
    passwd -w 7 -x 90 $_USERFTP &> /dev/null
  fi
  usermod -aG lshellg $_USERFTP &> /dev/null
  chsh -s $_PATH_LSHELL $_USERFTP &> /dev/null
  echo >> /etc/lshell.conf
  echo "[$_USERFTP]" >> /etc/lshell.conf
  echo "path : ['$_ROOT/distro', '$_ROOT/static', '$_ROOT/backups', '$_ROOT/clients']" >> /etc/lshell.conf
fi


###---### Add symlink to the sites backups.
#
_USERFTP="${_USER}.ftp"
_USER_HD="/home/$_USERFTP"
_USER_DS="$_USER_HD/.drush"
if [ ! -L "$_USER_HD/backups" ] ; then
  msg "$_STATUS A: Adding symlink to the sites backups"
  ln -sf $_ROOT/backups $_USER_HD/backups
fi


###---### Remove legacy symlinks.
#
if [ -e "$_USER_DS/drush_make" ] ; then
  rm -f $_USER_DS/drush_make
fi
if [ -e "$_USER_DS/registry_rebuild" ] ; then
  rm -f $_USER_DS/registry_rebuild
fi
if [ -e "$_USER_DS/clean_missing_modules" ] ; then
  rm -f $_USER_DS/clean_missing_modules
fi
if [ -e "$_USER_DS/drush_ecl" ] ; then
  rm -f $_USER_DS/drush_ecl
fi


###---### Add symlink to the system registry_rebuild.
#
if [ ! -L "$_USER_DS/usr/registry_rebuild" ] ; then
  msg "$_STATUS A: Adding symlink to the system registry_rebuild"
  mkdir -p $_USER_DS/usr
  chown $_USERFTP:$_USRG $_USER_DS
  chown $_USERFTP:$_USRG $_USER_DS/usr
  chmod 700 $_USER_DS
  ln -sf /data/disk/${_USER}/.drush/usr/registry_rebuild $_USER_DS/usr/registry_rebuild
fi


###---### Add symlink to the system clean_missing_modules.
#
if [ ! -L "$_USER_DS/usr/clean_missing_modules" ] ; then
  msg "$_STATUS A: Adding symlink to the system clean_missing_modules"
  mkdir -p $_USER_DS/usr
  chown $_USERFTP:$_USRG $_USER_DS
  chown $_USERFTP:$_USRG $_USER_DS/usr
  chmod 700 $_USER_DS
  ln -sf /data/disk/${_USER}/.drush/usr/clean_missing_modules $_USER_DS/usr/clean_missing_modules
fi


###---### Add symlink to the system drush_ecl.
#
if [ ! -L "$_USER_DS/usr/drush_ecl" ] ; then
  msg "$_STATUS A: Adding symlink to the system drush_ecl"
  mkdir -p $_USER_DS/usr
  chown $_USERFTP:$_USRG $_USER_DS
  chown $_USERFTP:$_USRG $_USER_DS/usr
  chmod 700 $_USER_DS
  ln -sf /data/disk/${_USER}/.drush/usr/drush_ecl $_USER_DS/usr/drush_ecl
fi


###---### Create .ssh dir if not exists.
#
_USER_SSH="$_USER_HD/.ssh"
if [ ! -d "$_USER_SSH" ] ; then
  mkdir -p $_USER_SSH
  chown -R $_USERFTP:$_USRG $_USER_SSH
  chmod 700 $_USER_SSH
fi
chmod 600 $_USER_SSH/id_{r,d}sa &> /dev/null


###---### Create .bazaar dir and conf file if not exist.
#
_USER_BZR="$_USER_HD/.bazaar"
if [ -x "/usr/local/bin/bzr" ] ; then
  if [ ! -e "$_USER_BZR/bazaar.conf" ] ; then
    mkdir -p $_USER_BZR
    echo ignore_missing_extensions=True > $_USER_BZR/bazaar.conf
    chown -R $_USERFTP:$_USRG $_USER_BZR
    chmod 700 $_USER_BZR
  fi
else
  rm -f -r $_USER_BZR
fi


###---### Add symlink to the clients directory.
#
if [ ! -L "$_USER_HD/clients" ] ; then
  msg "$_STATUS A: Adding symlink to the clients directory"
  ln -sf $_ROOT/clients $_USER_HD/clients
fi
rm -f -r $_ROOT/clients/admin &> /dev/null
rm -f -r $_ROOT/clients/omega8ccgmailcom &> /dev/null
rm -f -r $_ROOT/clients/nocomega8cc &> /dev/null
rm -f -r $_ROOT/clients/*/backups &> /dev/null
symlinks -dr $_ROOT/clients &> /dev/null


###---### Create .tmp dir if not exists.
#
_USER_TMP="$_USER_HD/.tmp"
if [ ! -d "$_USER_TMP" ] ; then
  rm -f $_USER_TMP
  mkdir -p $_USER_TMP
  chown -R $_USERFTP:$_USRG $_USER_TMP
  chmod 700 $_USER_TMP
fi


###---### Remove not used dot files.
#
rm -f $_USER_HD/{.profile,.bash_logout,.bashrc}


###---### Reading or creating pass.txt.
#
if [ "$_HM_ONLY" = "YES" ] ; then
  _DO_NOTHING=YES
else
  if [ -e "$_ROOT/pass.txt" ] ; then
    _PASWD=`cat $_ROOT/pass.txt`
    _PASWD=`echo -n $_PASWD | tr -d "\n"`
    mv -f $_ROOT/pass.txt $_ROOT/log/pass.txt &> /dev/null
  elif [ -e "$_ROOT/log/pass.txt" ] ; then
    _PASWD=`cat $_ROOT/log/pass.txt`
    _PASWD=`echo -n $_PASWD | tr -d "\n"`
    rm -f $_ROOT/pass.txt
  else
    cd $_ROOT/log
    touch $_ROOT/log/pass.txt
    chmod 0600 $_ROOT/log/pass.txt
    _ESC_LUPASS=""
    _LEN_LUPASS=0
    if [ "$_STRONG_PASSWORDS" = "YES" ] ; then
      _ESC_LUPASS=$(randpass 32 alnum)
      _ESC_LUPASS=`echo -n $_ESC_LUPASS | tr -d "\n"`
      _LEN_LUPASS=$(echo ${#_ESC_LUPASS})
    fi
    if [ -z "$_ESC_LUPASS" ] || [ $_LEN_LUPASS -lt 19 ] ; then
      _ESC_LUPASS=`pwgen -v -s -1`
      _ESC_LUPASS=`echo -n $_ESC_LUPASS | tr -d "\n"`
      _ESC_LUPASS=`sanitize_string "$_ESC_LUPASS"`
    fi
    echo "$_ESC_LUPASS" > $_ROOT/log/pass.txt
    ph=$(mkpasswd -m sha-512 $_ESC_LUPASS $(openssl rand -base64 16 | tr -d '+=' | head -c 16))
    usermod -p $ph $_USERFTP &> /dev/null
    _PASWD=`cat $_ROOT/log/pass.txt`
    _PASWD=`echo -n $_PASWD | tr -d "\n"`
  fi
fi


###---### Creating ftp symlinks.
#
if [ "$_DIST_INSTALL" = "YES" ] ; then
msg "$_STATUS A: Creating ftp symlinks"
  if [ ! -d "/home/$_USERFTP/platforms/$_THIS_CORE" ] ; then
    mkdir -p /home/$_USERFTP/platforms/$_THIS_CORE
  fi
  chown -R $_USER:$_USRG /home/$_USERFTP/platforms &> /dev/null
  chmod 700 /home/$_USERFTP &> /dev/null
  rm -f /home/$_USERFTP/platforms/$_THIS_CORE/{cod,commerce,commons,managingnews,openatrium,openscholar,prosepoint,drupal6,drupal7}
  if [ -d "$_ROOT/distro/$_THIS_CORE/acquia-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/acquia-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/acquia-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/acquia-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/agov-$_AGOV-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/agov-$_AGOV-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/agov-$_AGOV-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/agov-$_AGOV-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/civicrm-$_CIVICRM_M6-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/civicrm-$_CIVICRM_M6-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/civicrm-$_CIVICRM_M6-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/civicrm-$_CIVICRM_M6-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/civicrm-$_CIVICRM_M7-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/civicrm-$_CIVICRM_M7-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/civicrm-$_CIVICRM_M7-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/civicrm-$_CIVICRM_M7-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/cod-$_COD-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/cod-$_COD-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/cod-$_COD-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/cod-$_COD-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/commerce-$_COMMERCE1-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/commerce-$_COMMERCE1-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/commerce-$_COMMERCE1-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/commerce-$_COMMERCE1-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/commerce-$_COMMERCE2-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/commerce-$_COMMERCE2-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/commerce-$_COMMERCE2-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/commerce-$_COMMERCE2-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/commons-$_COMMONS2-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/commons-$_COMMONS2-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/commons-$_COMMONS2-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/commons-$_COMMONS2-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/commons-$_COMMONS3-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/commons-$_COMMONS3-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/commons-$_COMMONS3-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/commons-$_COMMONS3-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-dev/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-dev" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-dev/sites /home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-dev
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-stage/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-stage" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-stage/sites /home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-stage
    fi
  fi
  if [ ! -d "$_ROOT/distro/$_THIS_CORE/pressflow" ] ; then
    if [ -d "$_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-prod/sites" ] ; then
      if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-prod" ] ; then
        ln -sf $_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-prod/sites /home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-prod
      fi
    fi
  elif [ ! -d "$_ROOT/distro/$_THIS_CORE/pressflow-$_SMALLCORE6-prod" ] ; then
    if [ -d "$_ROOT/distro/$_THIS_CORE/pressflow/sites" ] ; then
      if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-prod" ] ; then
        ln -sf $_ROOT/distro/$_THIS_CORE/pressflow/sites /home/$_USERFTP/platforms/$_THIS_CORE/pressflow-$_SMALLCORE6-prod
      fi
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/$_DRUPAL7D/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-dev" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/$_DRUPAL7D/sites /home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-dev
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/$_DRUPAL7S/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-stage" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/$_DRUPAL7S/sites /home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-stage
    fi
  fi
  if [ ! -d "$_ROOT/distro/$_THIS_CORE/$_DRUPAL7" ] ; then
    if [ -d "$_ROOT/distro/$_THIS_CORE/$_DRUPAL7P/sites" ] ; then
      if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-prod" ] ; then
        ln -sf $_ROOT/distro/$_THIS_CORE/$_DRUPAL7P/sites /home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-prod
      fi
    fi
  elif [ ! -d "$_ROOT/distro/$_THIS_CORE/$_DRUPAL7P" ] ; then
    if [ -d "$_ROOT/distro/$_THIS_CORE/$_DRUPAL7/sites" ] ; then
      if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-prod" ] ; then
        ln -sf $_ROOT/distro/$_THIS_CORE/$_DRUPAL7/sites /home/$_USERFTP/platforms/$_THIS_CORE/$_DRUPAL7-prod
      fi
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/drupal-$_DRUPAL8/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/drupal-$_DRUPAL8" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/drupal-$_DRUPAL8/sites /home/$_USERFTP/platforms/$_THIS_CORE/drupal-$_DRUPAL8
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/drupal-$_DRUPAL_EDGE_8/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/drupal-$_DRUPAL_EDGE_8" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/drupal-$_DRUPAL_EDGE_8/sites /home/$_USERFTP/platforms/$_THIS_CORE/drupal-$_DRUPAL_EDGE_8
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/erpal-$_ERPAL-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/erpal-$_ERPAL-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/erpal-$_ERPAL-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/erpal-$_ERPAL-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/fserver-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/fserver-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/fserver-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/fserver-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/guardr-$_GUARDR-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/guardr-$_GUARDR-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/guardr-$_GUARDR-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/guardr-$_GUARDR-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/managingnews-$_MANAGINGNEWS-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/managingnews-$_MANAGINGNEWS-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/managingnews-$_MANAGINGNEWS-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/managingnews-$_MANAGINGNEWS-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/nodestream-$_NODESTREAM7-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/nodestream-$_NODESTREAM7-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/nodestream-$_NODESTREAM7-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/nodestream-$_NODESTREAM7-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openacademy-$_OPENACADEMY-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openacademy-$_OPENACADEMY-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openacademy-$_OPENACADEMY-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openacademy-$_OPENACADEMY-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openatrium-$_OPENATRIUM6-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openatrium-$_OPENATRIUM6-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openatrium-$_OPENATRIUM6-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/openatrium-$_OPENATRIUM6-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openatrium-$_OPENATRIUM7-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openatrium-$_OPENATRIUM7-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openatrium-$_OPENATRIUM7-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openatrium-$_OPENATRIUM7-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openblog-$_OPENBLOG-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openblog-$_OPENBLOG-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openblog-$_OPENBLOG-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openblog-$_OPENBLOG-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openchurch-$_OPENCHURCH-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openchurch-$_OPENCHURCH-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openchurch-$_OPENCHURCH-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openchurch-$_OPENCHURCH-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/opendeals-$_OPEN_DEALS-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/opendeals-$_OPEN_DEALS-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/opendeals-$_OPEN_DEALS-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/opendeals-$_OPEN_DEALS-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openoutreach-$_OPENOUTREACH-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openoutreach-$_OPENOUTREACH-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openoutreach-$_OPENOUTREACH-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openoutreach-$_OPENOUTREACH-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openpublish-$_OPENPUBLISH-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openpublish-$_OPENPUBLISH-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openpublish-$_OPENPUBLISH-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openpublish-$_OPENPUBLISH-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/openscholar-$_OPENSCHOLAR-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/openscholar-$_OPENSCHOLAR-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/openscholar-$_OPENSCHOLAR-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/openscholar-$_OPENSCHOLAR-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/panopoly-$_PANOPOLY-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/panopoly-$_PANOPOLY-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/panopoly-$_PANOPOLY-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/panopoly-$_PANOPOLY-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/prosepoint-$_PROSEPOINT-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/prosepoint-$_PROSEPOINT-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/prosepoint-$_PROSEPOINT-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/prosepoint-$_PROSEPOINT-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/recruiter-$_RECRUITER-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/recruiter-$_RECRUITER-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/recruiter-$_RECRUITER-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/recruiter-$_RECRUITER-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/restaurant-$_RESTAURANT-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/restaurant-$_RESTAURANT-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/restaurant-$_RESTAURANT-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/restaurant-$_RESTAURANT-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/spark-$_SPARK-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/spark-$_SPARK-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/spark-$_SPARK-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/spark-$_SPARK-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/totem-$_TOTEM-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/totem-$_TOTEM-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/totem-$_TOTEM-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/totem-$_TOTEM-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/ubercart-$_UBERCART6-$_SMALLCORE6/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/ubercart-$_UBERCART6-$_SMALLCORE6" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/ubercart-$_UBERCART6-$_SMALLCORE6/sites /home/$_USERFTP/platforms/$_THIS_CORE/ubercart-$_UBERCART6-$_SMALLCORE6
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/ubercart-$_UBERCART7-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/ubercart-$_UBERCART7-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/ubercart-$_UBERCART7-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/ubercart-$_UBERCART7-$_SMALLCORE7
    fi
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/ubercart-testing-$_UBERCART7-$_SMALLCORE7/sites" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/ubercart-testing-$_UBERCART7-$_SMALLCORE7" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/ubercart-testing-$_UBERCART7-$_SMALLCORE7/sites /home/$_USERFTP/platforms/$_THIS_CORE/ubercart-testing-$_UBERCART7-$_SMALLCORE7
    fi
  fi
fi


###---### Create ~/static dir if not exists.
#
if [ ! -d "$_ROOT/static" ] ; then
  mkdir -p $_ROOT/static
  ln -sf $_ROOT/static /home/$_USERFTP/static
fi
chown $_USER:$_USRG $_ROOT/static &> /dev/null
chmod 02775 $_ROOT/static &> /dev/null
echo empty > $_ROOT/static/EMPTY.txt


###---### Create ~/.tmp dir if not exists.
#
if [ ! -d "$_ROOT/.tmp" ] ; then
  rm -f -r $_ROOT/.tmp
  mkdir -p $_ROOT/.tmp
fi
chown $_USERFTP:www-data $_ROOT/.tmp
chmod 02775 $_ROOT/.tmp


###---### Create .ssh dir and keys if not exist, plus some known_hosts for system user.
#
if [ ! -e "$_ROOT/.ssh/id_rsa.pub" ] ; then
  su -s /bin/bash - $_USER -c "ssh-keygen -b 4096 -t rsa -N \"\" -f ~/.ssh/id_rsa" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H $_USER.beanstalkapp.com >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H $_USER.unfuddle.com >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H beanstalkapp.com >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H bitbucket.org >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H codebasehq.com >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H drupal.org >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H git.drupal.org >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H github.com >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H gitorious.org >> ~/.ssh/known_hosts" &> /dev/null
  su -s /bin/bash - $_USER -c "ssh-keyscan -t rsa -H unfuddle.com >> ~/.ssh/known_hosts" &> /dev/null
  cp -af $_ROOT/.ssh/id_rsa.pub $_ROOT/static/$_USER.id_rsa.pub
  chmod 644 $_ROOT/static/$_USER.id_rsa.pub
fi


###---### Create .bazaar dir and conf file if not exist for system user.
#
_SYSTEM_USER_BZR="$_ROOT/.bazaar"
if [ -x "/usr/local/bin/bzr" ] ; then
  if [ ! -e "$_SYSTEM_USER_BZR/bazaar.conf" ] ; then
    mkdir -p $_SYSTEM_USER_BZR
    echo ignore_missing_extensions=True > $_SYSTEM_USER_BZR/bazaar.conf
    chown -R $_USER:$_USRG $_SYSTEM_USER_BZR
    chmod 700 $_SYSTEM_USER_BZR
  fi
else
  rm -f -r $_SYSTEM_USER_BZR
fi


###---### Create other dirs and symlinks if not exist.
#
if [ "$_HM_ONLY" = "YES" ] ; then
  _DO_NOTHING=YES
else
  if [ ! -d "$_ROOT/distro/$_THIS_CORE/keys" ] ; then
    mkdir -p $_ROOT/distro/$_THIS_CORE/keys
    chown $_WEBG:$_WEBG $_ROOT/distro/$_THIS_CORE/keys &> /dev/null
    chmod 02775 $_ROOT/distro/$_THIS_CORE/keys
  fi
  if [ -d "$_ROOT/distro/$_THIS_CORE/keys" ] ; then
    if [ ! -L "/home/$_USERFTP/platforms/$_THIS_CORE/keys" ] ; then
      ln -sf $_ROOT/distro/$_THIS_CORE/keys /home/$_USERFTP/platforms/$_THIS_CORE/keys
    fi
  fi
  rm -f $_ROOT/distro/$_THIS_CORE/*/robots.txt &> /dev/null
  if [ ! -e "/data/all/$_LAST_ALL/javascript_aggregator.out.txt" ] ; then
    sed -i "s/, 'javascript_aggregator'//g" /data/all/$_LAST_ALL/*/profiles/*/*.profile &> /dev/null
    touch /data/all/$_LAST_ALL/javascript_aggregator.out.txt
  fi
  if [ ! -e "$_CORE/javascript_aggregator.out.txt" ] ; then
    sed -i "s/, 'javascript_aggregator'//g" $_CORE/*/profiles/*/*.profile &> /dev/null
    touch $_CORE/javascript_aggregator.out.txt
  fi
fi


###---### Remove not used cache module.
#
if [ ! -e "/data/all/000/old_cache.out2.txt" ] && [ -e "/data/all/000" ] ; then
  sed -i "s/, 'cache'//g" /data/all/*/*/profiles/*/*.profile &> /dev/null
  sed -i "s/'cache', //g" /data/all/*/*/profiles/*/*.profile &> /dev/null
  rm -f -r /data/all/*/o_contrib/cache
  touch /data/all/000/old_cache.out2.txt
fi


###---### Preparing setupmail.txt.
#
if [ "$_HM_ONLY" = "YES" ] ; then
  _DO_NOTHING=YES
else
msg "$_STATUS A: Preparing setupmail.txt"
_AT_CLIENT_EMAIL=${_CLIENT_EMAIL//\\\@/\@}
_A_DM_EMAIL=${_ADM_EMAIL//\\\@/\@}
# rm -f /opt/tmp/.admemail.txt
if [ -e "$_ROOT/log/setupmail.txt" ] ; then
  if [ "$_DIST_INSTALL" = "YES" ] ; then
    cd $_ROOT/log
    if [ -e "$_ROOT/log/upgrademail.txt" ] ; then
      mv -f $_ROOT/log/upgrademail.txt $_ROOT/log/upgrademail-pre-$_THIS_CORE.txt &> /dev/null
    fi
    cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/upgrademail.txt ./
    sed -i "s/aegir.dragon.ny.host8.biz/$_DOMAIN/g" $_ROOT/log/upgrademail.txt &> /dev/null
    sed -i "s/dragon/$_USER/g" $_ROOT/log/upgrademail.txt &> /dev/null
    sed -i "s/FN8rXcQn/$_PASWD/g" $_ROOT/log/upgrademail.txt &> /dev/null
    sed -i "s/166.84.6.231/$_THISHTIP/g" $_ROOT/log/upgrademail.txt &> /dev/null
  else
    _SEND_UPGRADE_EMAIL=NO
  fi
elif [ "$_STATUS" = "INIT" ] ; then
 cd $_ROOT/log
 cp -af /opt/tmp/$_BOA_REPO_NAME/aegir/conf/setupmail.txt ./
 sed -i "s/aegir.dragon.ny.host8.biz/$_DOMAIN/g" $_ROOT/log/setupmail.txt &> /dev/null
 sed -i "s/dragon/$_USER/g" $_ROOT/log/setupmail.txt &> /dev/null
 sed -i "s/FN8rXcQn/$_PASWD/g" $_ROOT/log/setupmail.txt &> /dev/null
 sed -i "s/166.84.6.231/$_THISHTIP/g" $_ROOT/log/setupmail.txt &> /dev/null
fi
fi


###---### Sending setup e-mail.
#
_MAILX_TEST=`mail -V 2>&1`
if [ "$_STATUS" = "INIT" ] ; then
  msg "$_STATUS A: Sending setup e-mail on init..."
  TIME=`date`
  echo $TIME > $_ROOT/log/date-init.txt
  if [ -e "$_ROOT/log/setupmail.txt" ] ; then
    if [[ "$_MAILX_TEST" =~ "GNU Mailutils" ]] ; then
      cat $_ROOT/log/setupmail.txt | sed "s/[\~]//g" | mail -a "From: $_A_DM_EMAIL" -a "Bcc: $_A_DM_EMAIL" -e -s "Your Aegir Install on $TIME [$_USER]" $_AT_CLIENT_EMAIL
    elif [[ "$_MAILX_TEST" =~ "invalid" ]] ; then
      cat $_ROOT/log/setupmail.txt | mail -a "From: $_A_DM_EMAIL" -e -b $_A_DM_EMAIL -s "Your Aegir Install on $TIME [$_USER]" $_AT_CLIENT_EMAIL
    else
      cat $_ROOT/log/setupmail.txt | mail -r $_A_DM_EMAIL -e -b $_A_DM_EMAIL -s "Your Aegir Install on $TIME [$_USER]" $_AT_CLIENT_EMAIL
    fi
  fi
else
  if [ "$_DIST_INSTALL" = "YES" ] && [ "$_PLATFORMS_ONLY" = "NO" ] ; then
    msg "$_STATUS A: Resending setup e-mail on upgrade..."
    TIME=`date`
    echo $TIME > $_ROOT/log/date-upgrade-$_THIS_CORE.txt
    if [ -e "$_ROOT/log/upgrademail.txt" ] ; then
      if [[ "$_MAILX_TEST" =~ "GNU Mailutils" ]] ; then
        cat $_ROOT/log/upgrademail.txt | sed "s/[\~]//g" | mail -a "From: $_A_DM_EMAIL" -a "Bcc: $_A_DM_EMAIL" -e -s "Your Aegir Upgrade on $TIME [$_USER]" $_AT_CLIENT_EMAIL
      elif [[ "$_MAILX_TEST" =~ "invalid" ]] ; then
        cat $_ROOT/log/upgrademail.txt | mail -a "From: $_A_DM_EMAIL" -e -b $_A_DM_EMAIL -s "Your Aegir Upgrade on $TIME [$_USER]" $_AT_CLIENT_EMAIL
      else
        cat $_ROOT/log/upgrademail.txt | mail -r $_A_DM_EMAIL -e -b $_A_DM_EMAIL -s "Your Aegir Upgrade on $TIME [$_USER]" $_AT_CLIENT_EMAIL
      fi
    else
      if [[ "$_MAILX_TEST" =~ "GNU Mailutils" ]] ; then
        cat $_ROOT/log/setupmail.txt | sed "s/[\~]//g" | mail -a "From: $_A_DM_EMAIL" -a "Bcc: $_A_DM_EMAIL" -e -s "Your Aegir Upgrade on $TIME [$_USER]" $_AT_CLIENT_EMAIL
      elif [[ "$_MAILX_TEST" =~ "invalid" ]] ; then
        cat $_ROOT/log/setupmail.txt | mail -a "From: $_A_DM_EMAIL" -e -b $_A_DM_EMAIL -s "Your Aegir Upgrade on $TIME [$_USER]" $_AT_CLIENT_EMAIL
      else
        cat $_ROOT/log/setupmail.txt | mail -r $_A_DM_EMAIL -e -b $_A_DM_EMAIL -s "Your Aegir Upgrade on $TIME [$_USER]" $_AT_CLIENT_EMAIL
      fi
    fi
  else
    _SEND_UPGRADE_EMAIL=NO
  fi
fi


###---### Touch install logs.
#
TIME=`date`
if [ "$_STATUS" = "INIT" ] ; then
 echo $TIME > $_ROOT/log/date-init.txt
else
 echo $TIME > $_ROOT/log/date-upgrade-$_THIS_CORE.txt
fi
echo "`date` / `lsb_release -si`.`lsb_release -sc` `uname -m` / Aegir $_AEGIR_VERSION / Octopus $_INSTALLER_VERSION / FPM $_PHP_FPM_VERSION / CLI $_PHP_CLI_VERSION" >> $_ROOT/log/octopus_log.txt
msg "$_STATUS A: New entry added to $_ROOT/log/octopus_log.txt"


###---### Final cleaning.
#
msg "$_STATUS A: Final cleaning, please wait a moment..."
cd /
chmod 711 bin boot data dev emul etc home lib media mnt opt sbin selinux srv sys usr var &> /dev/null
chmod 700 root &> /dev/null
if [ ! -f "/data/all/permissions-fix-$_INSTALLER_VERSION-fix-all.info" ] ; then
  find /data/all/000 -type d -exec chmod 0755 {} \; &> /dev/null
  find /data/all/000 -type f -exec chmod 0644 {} \; &> /dev/null
  st_runner "chmod 755 /data/all/*/*/profiles" &> /dev/null
  st_runner "chmod 02775 /data/all/*/*/sites/all/{modules,libraries,themes}" &> /dev/null
  st_runner "chown -R root:root /data/all" &> /dev/null
  st_runner "chown -R root:users /data/all/*/*/sites" &> /dev/null
  echo fixed > /data/all/permissions-fix-$_INSTALLER_VERSION-fix-all.info
fi
chown root:root /data/all &> /dev/null
chown root:root $_CORE &> /dev/null
st_runner "chown -R root:root /data/src" &> /dev/null
st_runner "chown -R root:root /data/conf" &> /dev/null
st_runner "chown -R root:root $_CORE/o_contrib" &> /dev/null
st_runner "chown -R root:root $_CORE/o_contrib_seven" &> /dev/null
st_runner "chown -R root:root /data/all/$_LAST_ALL/o_contrib" &> /dev/null
st_runner "chown -R root:root /data/all/$_LAST_ALL/o_contrib_seven" &> /dev/null
st_runner "chown -R root:root /data/all/000" &> /dev/null
find /data/conf -type d -exec chmod 0755 {} \; &> /dev/null
find /data/conf -type f -exec chmod 0644 {} \; &> /dev/null
chown root:root /opt/tmp &> /dev/null
chmod 0711 $_CORE /data/all/000 /data/src /data /data/disk /data/conf &> /dev/null
chmod 0755 /data/all &> /dev/null
chmod 0700 /data/u &> /dev/null
chown root:root /data/u &> /dev/null
rm -f /data/u/*host8* &> /dev/null
mv -f $_ROOT/backups/drush_make-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/drush-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision_boost-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision_cdn-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision_civicrm-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision_platform_git-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision_site_backup-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision_tasks_extra-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/provision-pre* $_ROOT/backups/system/ &> /dev/null
mv -f $_ROOT/backups/registry_rebuild-pre* $_ROOT/backups/system/ &> /dev/null
mkdir -p /data/conf/arch
mv -f /data/conf/global.inc-pre* /data/conf/arch/ &> /dev/null
mv -f /data/conf/global.inc-before* /data/conf/arch/ &> /dev/null
mv -f /data/conf/global.inc-missing* /data/conf/arch/ &> /dev/null
find $_ROOT/static/*/module* -maxdepth 0 -mindepth 0 -type d -exec chmod 775 {} \; &> /dev/null
find $_ROOT/static/*/*/module* -maxdepth 0 -mindepth 0 -type d -exec chmod 775 {} \; &> /dev/null
find $_ROOT/static/*/*/*/module* -maxdepth 0 -mindepth 0 -type d -exec chmod 775 {} \; &> /dev/null
find $_ROOT/static/*/*/*/*/module* -maxdepth 0 -mindepth 0 -type d -exec chmod 775 {} \; &> /dev/null
if [ -d "$_CORE/$_DRUPAL7" ] ; then
  mkdir -p /var/backups/trash
  rm -f -r /var/backups/trash/*
  mv -f $_CORE/$_DRUPAL7 /var/backups/trash/ &> /dev/null
fi
if [ "$_STATUS" = "INIT" ] ; then
  if [ "$_DEBUG_MODE" = "YES" ] ; then
    _DO_NOTHING=YES
  else
    _AEGIR_LOGIN_URL=`grep --text "^http://$_DOMAIN" $_ROOT/log/install.log 2> /dev/null`
    if [ ! -z "$_AEGIR_LOGIN_URL" ] ; then
      echo " "
      msg "INFO: Congratulations, Aegir backend and frontend have been installed successfully"
      msg "NOTE! Please wait 1 minute before opening the following link in your web browser:"
      echo " "
      msg "LINK: $_AEGIR_LOGIN_URL"
      echo " "
    else
      msg "ALRT: Something went wrong"
      msg "ALRT: Please check the install log for details:"
      msg "ALRT: $_ROOT/log/install.log"
    fi
  fi
else
  feature_server_fix_002
  feature_server_fix_004
  o_contrib_update_global
  o_contrib_seven_update_global
fi
if [ ! -e "/root/.upstart.cnf" ] ; then
  msg "$_STATUS A: Starting the cron now"
  service cron start &> /dev/null
fi
msg "$_STATUS A: All done!"
msg "BYE!"

touch /opt/tmp/status-AegirSetupA-OK

###----------------------------------------###
###
###  Octopus Aegir Installer
###  Copyright (C) 2010-2014 Omega8.cc
###  noc@omega8.cc www.omega8.cc
###
###----------------------------------------###
