#!/usr/bin/env bash

###########################

# Author: Russell de Moose (<rdemoose@theorchard.com>)
# Copyright (C) 2016 The Orchard

# -------------------------

# 1. Bootstap host
# 2. Expand disk drive if needed
# 3. Update knife node from file
# 4. Chef-client
# 5. Run this script
# 6. Add DNS records for subdomain
# 7. Add ssh key this script generates to github devdeploy-theorchard user
# 8. Add real oauth token for devdeploy-theorchard user to /home/devorch/.config/composer/auth.json

# NOTE: Since this removes the Chef data dag secret, you must re-add it before subsequent client runs.

###########################

# First update all packages
yum -y update

# Remove Chef secret 
rm -rf /etc/chef/encrypted_data_bag_secret

# Add mount. This is a dev password and is not sensitive.
if grep -iRq "/mnt/rbuploads" /etc/fstab; then
    echo "rbuploads mount already present"
else
    echo '//192.168.31.88/mp3storage /mnt/rbuploads cifs username=devagent,password=Orchard23 0 0' >> /etc/fstab
fi

# Remove deploy oauth token
rm -rf /home/deploy/.config

# Make and own composer config directories. This is not a real token. Replace with one.
mkdir -p /home/devorch/.config/composer
cat << EOF > /home/devorch/.config/composer/auth.json
{
      "github-oauth": {
          "github.com": "123456789012345678901234567890123456789"
      }
}
EOF
chown -R devorch:devorch /home/devorch/.config

# Remove auth key
rm -rf /var/www/ows-auth.pem

# Remove placeholder oa site
/usr/sbin/a2dissite dev-oa
rm -rf /etc/httpd/sites-available/dev-oa.conf

# Add devorch to the webdev group
usermod -a -G webdev devorch

# Make sure devorch owns the web directories
chown -R devorch:webdev /var/www

# Install nodejs 4 and ant
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash - && yum -y install nodejs 
yum -y install ant

# Install and configure PHP Xdebug for open access
yum -y install php56-php-pecl-xdebug.x86_64

if [ -f '/opt/remi/php56/root/etc/php.d/15-xdebug.ini' ]; then 
  cat << EOF >> /opt/remi/php56/root/etc/php.d/15-xdebug.ini  
xdebug.max_nesting_level = 10000
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.remote_log = /tmp/xdebug.log
xdebug.extended_info = 1
EOF

else
  echo "No xdebug configuration snippet found. Package is either not installed or file location has changed."
fi


# Install Docker
yum -y install docker
curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
systemctl enable docker
systemctl restart docker

# Install rvm
yum -y install autoconf automake bison bzip2 gcc-c++ glibc-devel glibc-headers libffi-devel libtool libyaml-devel openssl-devel patch readline-devel sqlite-devel zlib-devel
su devorch -c "gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3"
su devorch -c "curl -sSL https://get.rvm.io | bash -s stable --ruby"
echo "source ~/.profile" >> /home/devorch/.bash_profile

# Fix Python 3.4 virtualenv
yum -y install python-flake8 python-pip python34 python34-debug python34-devel
wget -P /lib64/python3.4/ensurepip/_bundled https://pypi.python.org/packages/cb/e7/63e4efa1bd3396d0d7d0697d896c7c9c39635a8929424c4016b0abcf6e4d/setuptools-18.4-py2.py3-none-any.whl
wget -P /lib64/python3.4/ensurepip/_bundled https://pypi.python.org/packages/b2/d0/cd115fe345dd6f07ec1c780020a7dfe74966fceeb171e0f20d1d4905b0b7/pip-7.1.2-py2.py3-none-any.whl
# Change setuptools version
sed -i 's/12.0.5/18.4/g' /lib64/python3.4/ensurepip/__init__.py
# Change pip version
sed -i 's/6.0.8/7.1.2/g' /lib64/python3.4/ensurepip/__init__.py

# Disable the firewall for dev users
systemctl stop firewalld
systemctl disable firewalld

# Create a keypair, and add the public key as an ssh key for the devdeploy-theorchard user. 
su devorch -c 'ssh-keygen -b 2048 -t rsa -C $(hostname) -f /home/devorch/.ssh/$(hostname) -N ""'
ln -s /home/devorch/.ssh/$(hostname) /home/devorch/.ssh/id_rsa
ln -s /home/devorch/.ssh/$(hostname).pub /home/devorch/.ssh/id_rsa.pub
cat /home/devorch/.ssh/$(hostname).pub

# Add proxy and ssl modules, then enable them.
cat << EOF > /etc/httpd/mods-available/proxy.load
LoadModule proxy_module /usr/lib64/httpd/modules/mod_proxy.so
LoadModule proxy_http_module /usr/lib64/httpd/modules/mod_proxy_http.so
EOF

cat << EOF > /etc/httpd/mods-available/ssl.load
LoadModule ssl_module /usr/lib64/httpd/modules/mod_ssl.so
EOF

ln -s /etc/httpd/mods-available/proxy.load /etc/httpd/mods-enabled/proxy.load
ln -s /etc/httpd/mods-available/ssl.load /etc/httpd/mods-enabled/ssl.load

systemctl restart httpd

echo "Now fix apache 2.2 to 2.4 vhost configuration..." && sleep 1
