#!/bin/bash
cartridge_type="python-2.7"

#  Setup variables.
python27_dir=${OPENSHIFT_PYTHON_DIR}
python27_binary=$python27_dir/opt/bin/python
virtenv_dir=$python27_dir/virtenv

#  Add the python2.7 shared library to the load path.
export LD_LIBRARY_PATH="${python27_dir}/opt/lib:${LD_LIBRARY_PATH}"

#
#  Utility functions to create/update, delete, activate and make relocatable
#  the python-2.7 virtualenv. 
#

function create_virtualenv() {
   if [ -d "$virtenv_dir" ]; then
      pushd "$virtenv_dir" > /dev/null
   else
      pushd . > /dev/null
   fi

   # Hack to fix symlink on rsync issue
   /bin/rm -f lib64
   virtualenv -p $python27_binary --system-site-packages "$virtenv_dir"

   popd > /dev/null

}  #  End of function  create_virtualenv. 


function delete_virtualenv() {
   rm -rf $python27_dir/virtenv/*

}  #  End of function  delete_virtualenv. 


function activate_virtualenv() {
   source "$python27_dir/bin/activate_virtenv"

}  #  End of function  activate_virtualenv. 


function make_virtualenv_relocatable() {
   virtualenv --relocatable $python27_dir/virtenv

}  #  End of function  make_virtualenv_relocatable.


#
#  Misc functions - add/remove environment variables and extract package files.
#

function _extract_files() {
   pkgdir=${1:-"."}
   destdir=${2:-"."}

   [ -d "$pkgdir" ]  ||  return 1
 
   #  In place extract the tarball files -- supports .tgz, .tar.gz, .tar and
   #  zip files and in that order.
   mkdir -p "$destdir"
   pushd "$destdir" > /dev/null

   for f in `ls $pkgdir/*.tgz $pkgdir/*.tar.gz 2> /dev/null`; do
      tar -zxf "$f"
   done

   for f in `ls $pkgdir/*.tar 2> /dev/null`; do
      tar -xf "$f"
   done

   for f in `ls $pkgdir/*.zip 2> /dev/null`; do
      unzip "$f"
   done

   popd > /dev/null

}  #  End of function  _extract_files.


function get_distro_name_and_release() {
   distro=$(uname |  tr '[:upper:]' '[:lower:]')
   version=$(uname -r)
   if [ "$distro" = "linux" ]; then
      if [ -f "/etc/redhat-release" ]; then
         if grep "Red Hat" /etc/redhat-release > /dev/null 2>&1; then
            distro="rhel"
         else
            if grep "Fedora" /etc/redhat-release > /dev/null 2>&1; then
               distro="fedora"
            fi
         fi
         version=$(lsb_release -r | awk '{print $2}')
     fi
   fi

   echo "$distro:$version"

}  #  End of function  get_distro_name_and_release.


function extract_package_files() {
   #  Try the distro name + version + machine specific packages first and
   #  then the distro + machine specific package and then the top level
   #  packages.
   name_and_release=$(get_distro_name_and_release)
   distro=$(echo "$name_and_release" | cut -f 1 -d ':')
   version=$(echo "$name_and_release" | cut -f 2 -d ':')
   #_extract_files "$1/$distro/$version/$(uname -m)" "$2"
   _extract_files "$1/$distro/$(uname -m)" "$2"
   #_extract_files "$1/$distro" "$2"
   #_extract_files "$1" "$2"

}  #  End of function  extract_package_files.

function create_virtenv_activate_file() {
   #  Write a file that can be sourced for setting the LD_LIBRARY_PATH and
   #  activating the virtualenv on the shell.
   cat > "${OPENSHIFT_PYTHON_DIR}/bin/activate_virtenv"  <<AVEOF
#  Set the library path so that the python shared library can be found.
export LD_LIBRARY_PATH="\${OPENSHIFT_PYTHON_DIR}/opt/lib:\${LD_LIBRARY_PATH}" 
export LIBRARY_PATH="\${OPENSHIFT_PYTHON_DIR}/opt/lib:\${LIBRARY_PATH}" 
source \${OPENSHIFT_PYTHON_DIR}/virtenv/bin/activate
AVEOF

   chmod 555 "${OPENSHIFT_PYTHON_DIR}/bin/activate_virtenv"
} # End of function create_virtenv_activate_file()
