#!/bin/bash

#==== SITE SPECIFIC PARAMETERS - CHANGE AS NEEDED =====

# Source ESGF environment variables file
ESGF_ENVFILE=/etc/esg.env
if [ -e "$ESGF_ENVFILE" ]; then
  source $ESGF_ENVFILE
fi
LD_LIBRARY_PATH=/opt/esgf/python/lib:$LD_LIBRARY_PATH
# ESGF_INSTALL_PREFIX
ESGF_INSTALL_PREFIX=${ESGF_INSTALL_PREFIX:-"/usr/local"}

# CoG software root directory
COG_DIR=${COG_DIR:-${ESGF_INSTALL_PREFIX}/cog}

# true if CoG is installed on ESGF node
ESGF=true

# the user and group running the CoG application
USER=apache
GROUP=apache

# the specific CoG branch OR version to install
# ESGF version

VERSION=v3.13.0

#COG_TAG=devel
COG_TAG=$VERSION

# must start from bare-bone Python 2.7.9 installation that contains only setup_tools, pip and virtualenv
PATH=/opt/esgf/python/bin:$PATH

# location to unpack the Python eggs - must be writable by $USER
PYTHON_EGG_CACHE_DIR=/var/www/.python-eggs

#=== ESGF INSTALLATION LOGIC - DO NOT CHANGE =========

# Sourcing ESGF Functions
ESG_FUNCTIONS_FILE=$ESGF_INSTALL_PREFIX/bin/esg-functions
source $ESG_FUNCTIONS_FILE

# ESGF installation path
INSTALLPATH=$COG_DIR

# ESGF common installation script
INSTALLARG_SCRIPT=$ESGF_INSTALL_PREFIX/bin/esg-installarg
if [ -e "$INSTALLARG_SCRIPT" ]; then
  source $INSTALLARG_SCRIPT
fi

#=== CoG INSTALLATION LOGIC - DO NOT CHANGE ==========

# pre-requisites
pip install virtualenv

# root directory for installation
mkdir -p $INSTALLPATH

# directory for local settings
export COG_CONFIG_DIR=$INSTALLPATH/cog_config
mkdir -p $COG_CONFIG_DIR

# installation directory
# will be re-created during git checkout
COG_INSTALL_DIR=$INSTALLPATH/cog_install

# use Python virtual environment (rebuild from scratch each time)
cd $INSTALLPATH
rm -rf venv
virtualenv -p /opt/esgf/python/bin/python2.7 venv
source venv/bin/activate
echo 'Using Python:' `which python`

# pre-requisite: update pip
pip install -U pip

# pre-requisite: install from wheel otherwise it does not work on MacOSX
pip install wheel
pip install --use-wheel Pillow==3.1.0

# checkout CoG software stack
# remove previous installation if existing
cd $INSTALLPATH
if [ -d "$COG_INSTALL_DIR" ]; then
  rm -rf $COG_INSTALL_DIR
fi
git clone https://github.com/EarthSystemCoG/COG cog_install

# checkout a specific tag or version
cd $COG_INSTALL_DIR
git checkout master
git pull
git checkout $COG_TAG
if [ $? != 0 ]; then
  echo "Unknown COG_TAG: $COG_TAG"
  exit 1
fi
git pull

# install CoG dependencies within Python virtual environment
cd $COG_INSTALL_DIR
pip install -r requirements.txt
# setup CoG configuration
python setup.py install

# install mkproxy. Mkproxy is used by Globus Transfer API client to create an X.509 proxy credential. 
# (Globus Transfer REST API sends a public key. i
# Mkproxy signs the public key using a private key extracted from an ESGF X.509 user credential.)
cd $COG_DIR
rm -rf transfer-api-client-python
git clone https://github.com/globusonline/transfer-api-client-python.git
cd transfer-api-client-python
git pull
cd mkproxy
make
cp mkproxy $INSTALLPATH/venv/lib/python2.7/site-packages/globusonline/transfer/api_client/x509_proxy/

# FIXME: must download and install django-openid-auth fork manually
cd $COG_DIR
rm -rf django-openid-auth
git clone https://github.com/EarthSystemCoG/django-openid-auth.git
cd django-openid-auth
python setup.py install

# create or upgrade CoG installation
cd $COG_INSTALL_DIR
python setup.py setup_cog --esgf=$ESGF

# cleanup CoG egg
cd $INSTALLPATH
rm -rf venv/lib/python2.7/site-packages/cog*

# change ownership of COG_CONFIG_DIR/site_media
chown -R $USER:$GROUP $COG_CONFIG_DIR
chown -R $USER:$GROUP $COG_INSTALL_DIR

# change ownership of COG_DIR/venv
chown -R $USER:$GROUP $COG_DIR/venv

# create location where Python eggs can be unpacked by user 'apache'
mkdir -p $PYTHON_EGG_CACHE_DIR
chown -R $USER:$GROUP $PYTHON_EGG_CACHE_DIR

# Copying installation script in installation directory
#rm -f $INSTALLPATH/$0
#cp $0 $INSTALLPATH

# Write CoG environment variable
echo "export COG_DIR=$INSTALLPATH" >> $ESGF_ENVFILE
echo "export COG_INSTALL_DIR=$INSTALLPATH/cog_install" >> $ESGF_ENVFILE
echo "export COG_CONFIG_DIR=$INSTALLPATH/cog_config" >> $ESGF_ENVFILE
echo "export SSL_CERT_DIR=/etc/grid-security/certificates" >> $ESGF_ENVFILE
echo "export PYTHON_EGG_CACHE=$PYTHON_EGG_CACHE_DIR" >> $ESGF_ENVFILE
dedup $ESGF_ENVFILE && source $ESGF_ENVFILE