#!/bin/bash


###----------------------------------------###
###
###  BOA Meta 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
###
###----------------------------------------###


###----------------------------------------###
### HOW-TO: run it with bash, not with sh  ###
###----------------------------------------###
###
###   bash BOA.sh.txt
###

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

export PATH=$PATH:/opt/local/bin
SHELL=/bin/bash
_TODAY=`date +%y%m%d`

setup ()
{
  if [ ! -e "/etc/resolv.conf" ] ; then
    echo "nameserver 8.8.8.8" >/etc/resolv.conf
    echo "nameserver 8.8.4.4" >>/etc/resolv.conf
  else
    _RESOLV_TEST=$(grep "nameserver" /etc/resolv.conf)
    if [[ "$_RESOLV_TEST" =~ "nameserver" ]] ; then
      _DO_NOTHING=YES
    else
      rm -f /etc/resolv.conf
      echo "nameserver 8.8.8.8" >/etc/resolv.conf
      echo "nameserver 8.8.4.4" >>/etc/resolv.conf
    fi
  fi
  _CURL_TEST=$(curl -L --max-redirs 10 -k -s -I "http://files.aegir.cc" 2> /dev/null)
  if [[ "$_CURL_TEST" =~ "200 OK" ]] ; then
    _DO_NOTHING=YES
  else
    apt-get clean -qq &> /dev/null
    apt-get update -qq &> /dev/null
    apt-get install curl -y --force-yes --reinstall &> /dev/null
  fi
  _PATH_TEST=$(grep '/opt/local/bin' /root/.bashrc)
  if [[ "$_PATH_TEST" =~ "/opt/local/bin" ]] ; then
    _DO_NOTHING=YES
  else
    sed -i "s/^export PATH=.*/export PATH=\$PATH:\/usr\/local\/bin:\/opt\/local\/bin/g" /root/.bashrc &> /dev/null
  fi
  mkdir -p /usr/local/bin
  mkdir -p /opt/local/bin
  rm -f /opt/local/bin/.boa*
  if [ ! -f "/opt/local/bin/.fix.bins.txt" ] ; then
    rm -f /usr/local/bin/{barracuda*,boa*,drushextra*,octopus*,randpass*,sqlmagic*,syncpass*,thinkdifferent*}
    touch /opt/local/bin/.fix.bins.txt
  fi
  if [ -L "/usr/local/bin/barracuda" ] || [ -L "/usr/local/bin/boa" ] || [ -L "/usr/local/bin/octopus" ] ; then
    rm -f /usr/local/bin/{barracuda*,boa*,octopus*}
  fi
  if [ ! -e "/data/disk" ] ; then
    ln -s /opt/local/bin/boa /usr/local/bin/boa
  fi
  rm -f /opt/local/bin/{barracuda*,boa*,drushextra*,octopus*,randpass*,sqlmagic*,syncpass*,thinkdifferent*}
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/barracuda"      -o /opt/local/bin/barracuda
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/boa"            -o /opt/local/bin/boa
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/drushextra"     -o /opt/local/bin/drushextra
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/octopus"        -o /opt/local/bin/octopus
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/randpass"       -o /opt/local/bin/randpass
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/sqlmagic"       -o /opt/local/bin/sqlmagic
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/syncpass"       -o /opt/local/bin/syncpass
  curl -L --max-redirs 10 -k -s --retry 10 --retry-delay 5 -A iCab "http://files.aegir.cc/versions/master/aegir/tools/bin/thinkdifferent" -o /opt/local/bin/thinkdifferent
  echo "### $_TODAY ###" >> /opt/local/bin/boa
  echo "### $_TODAY ###" >> /opt/local/bin/barracuda
  echo "### $_TODAY ###" >> /opt/local/bin/octopus
  chmod 700 /opt/local/bin/{boa,barracuda,octopus,syncpass}
  chmod 755 /opt/local/bin/{randpass,sqlmagic,drushextra,thinkdifferent}
  echo
  echo "BOA Meta Installer setup completed"
  echo "Please check INSTALL.txt and UPGRADE.txt at http://bit.ly/boa-docs for how-to"
  echo "Bye"
  echo
}

###--------------------###
if [ `whoami` = "root" ] ; then
  chmod a+w /dev/null
  if [ ! -e "/dev/fd" ] ; then
    if [ -e "/proc/self/fd" ] ; then
      rm -rf /dev/fd
      ln -s /proc/self/fd /dev/fd
    fi
  fi
  setup
  exit 0
else
  msg "ERROR: This script should be ran as a root user - please `sudo -i` first"
  exit 1
fi

