#!/bin/bash

  #
  # SCRIPT GLOBAL CONSTANTS
  #

  SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"


  #
  # SCRIPT GLOBAL HELPERS
  #

  source ${SCRIPT_DIR}/commons


  #
  # HELPER FUNCTIONS
  #

  command_exists () {
    type "$1" &> /dev/null ;
  }

  clean() {
    rm -rf "${SCRIPT_DIR}"/build
  }

  # It is possible to install Python3 version within the same folder (python code is compatible), but:
  #   - The extensions have different names, so that the most appropriate can be selected dynamically.
  #   - pyc and pyo files are in different folders (for python2 within the same as .py files, while
  #     in python 3 they are within __pycache__ folder).
  # However, we keep them in different folders with the python major version as subfolder.
  install (){
    PYCOMPSS_HOME="${targetDir}/$1"
    export PYTHONPATH=$PYCOMPSS_HOME:$OLD_PYTHONPATH

    if command_exists python$1 ; then
      python$1 ${SCRIPT_DIR}/setup.py install --install-lib=${PYCOMPSS_HOME} -O2
      exitCode=$?
      if [ $1 == "3" ] && [ -d "${targetDir}/2" ] && [ "$unify_installs" = "true" ]; then
        unify_installed_versions "${targetDir}"
      fi
      if [ "$create_symlinks" = "true" ]; then
        # Expected when installing with buildlocal. Not from buildsc.
        # Pip package sets these symbolic links and updates the 'activate' script accordingly.
        create_symbolic_links $1 "${PYCOMPSS_HOME}"
      fi
      if [ $exitCode -ne 0 ]; then
        echo "ERROR: Cannot install PyCOMPSs for Python $1"
        exit $exitCode
      fi
    else
      echo "WARNING: PyCOMPSS FOR PYTHON $1 HAS NOT BEEN INSTALLED."
    fi
  }


  #
  # MAIN
  #

  # Retrieve script arguments
  targetDir=$1            # Target directory where to install the python binding.
  create_symlinks=$2      # true or false. Create symbolic links within site/dist-packages folders.
  unify_installs=$3       # true or false. Remove sources from 3 and link to 2 if exists.
  only_python_version=$4  # Optional argument

  # Add trap for clean
  trap clean EXIT

  # Install
  OLD_PYTHONPATH=$PYTHONPATH

  if [ -z "$only_python_version" ]; then
    # Try to install all
    # Check $targetDir = site-packages usual, dist-packages in deb based distributions.
    if [[ "$targetDir" = *site-packages* ]] || [[ "$targetDir" = *dist-packages* ]]; then
      # If it is within site-packages, install only the appropriate version.
      if [[ "$targetDir" = *python2* ]]; then
        echo "* Installing python 2 version."
        # within 2.X site-packages
        install 2
      elif [[ "$targetDir" = *python3* ]]; then
        echo "* Installing python 3 version."
        # within 3.X site-packages
        install 3
      else
        echo "ERROR! UNSUPPORTED TARGET DIRECTORY WITHIN site-packages OR dist-packages."
      fi
    else
      # Install all
      install 2
      install 3
    fi
  elif [ "$only_python_version" == "python2" ]; then
    echo "* Installing JUST python 2 version."
    # Install only python 2 version
    install 2
  elif [ "$only_python_version" == "python3" ]; then
    echo "* Installing JUST python 3 version."
    # Install only python 3 version
    install 3
  else
    echo "ERROR! UNSUPPORTED PYTHON VERSION."
  fi

  # Copy cleaning and commons scripts for setup or uninstalling
  cp "${SCRIPT_DIR}/commons" "$targetDir"
  cp "${SCRIPT_DIR}/clean" "$targetDir"

  # Normal exit
  exit 0
