#!/bin/bash -e

required_pkgs_apt=(git # source code control
                   make pkg-config # build system
                   curl wget # for downloading libs
                   uuid-dev default-jre-headless # required by antlr
                   libreadline-dev # for memgraph console
                   libpython3-dev python3-dev # for query modules
                   libssl-dev
                   libseccomp-dev
                   python3 python-virtualenv python3-pip # for qa, macro_benchmark and stress tests
                   python3-yaml # for the configuration generator
                   libcurl4-openssl-dev # mg-requests
                   sbcl # for custom Lisp C++ preprocessing
                  )

optional_pkgs_apt=(doxygen graphviz # source documentation generators
                   php-cli # for user technical documentation generators
                   mono-runtime mono-mcs nodejs zip unzip default-jdk-headless # for driver tests
                  )


required_pkgs_yum=(# NOTE: git is too old on CentOS, install it manually from IUS
                   make pkgconfig # build system
                   curl wget # for downloading libs
                   libuuid-devel java-1.8.0-openjdk # required by antlr
                   readline-devel # for memgraph console
                   python3-devel # for query modules
                   openssl-devel
                   libseccomp-devel
                   python3 python-virtualenv python3-pip nmap-ncat # for qa, macro_benchmark and stress tests
                   # NOTE: python3-yaml doesn't exist on CentOS, install it manually using `pip3 install PyYAML`
                   libcurl-devel # mg-requests
                   sbcl # for custom Lisp C++ preprocessing
                   rpm-build rpmlint # for RPM package building
                  )

optional_pkgs_yum=(doxygen graphviz # source documentation generators
                   php-cli # for user technical documentation generators
                   mono-complete nodejs zip unzip java-1.8.0-openjdk-devel # for driver tests
                  )

use_sudo=0

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

function print_help () {
    echo "Usage: $0 [OPTION]"
    echo -e "Check for missing packages and install them if possible.\n"
    echo "Optional arguments:"
    echo -e "  -s\tuse sudo apt-get for installing packages"
    echo -e "  -h\tdisplay this help and exit"
}

function setup_virtualenv () {
    pushd $1 > /dev/null
    echo "Setting up virtualenv for: $1"

    # remove old virtualenv
    if [ -d ve3 ]; then
        rm -rf ve3
    fi

    # create new virtualenv
    virtualenv -p python3 ve3 || exit 1
    source ve3/bin/activate
    pip --timeout 1000 install -r requirements.txt || exit 1
    deactivate

    popd > /dev/null
}

if [[ $# -gt 1 ]]; then
    print_help
    exit 1
elif  [[ $# -eq 1 ]]; then
    case "$1" in
      -s)
          use_sudo=1
      ;;
      -h)
          print_help
          exit 0
      ;;
      *)
          # unknown option
          print_help
          exit 1
      ;;
  esac
fi

echo "Started installing dependencies for Memgraph"

required_missing=0

# install all dependencies on supported operating systems
if command -v apt-get >/dev/null; then
    for pkg in ${required_pkgs_apt[@]}; do
        if dpkg -s $pkg 2>/dev/null >/dev/null; then
            echo "Found $pkg"
        elif (( $use_sudo )); then
            echo "Installing $pkg"
            if [[ ! `sudo apt-get -y install $pkg` ]]; then
                echo "Didn't install $pkg [required]"
                required_missing=1
            fi
        else
            echo "Missing $pkg [required]"
            required_missing=1
        fi
    done
    for pkg in ${optional_pkgs_apt[@]}; do
        if dpkg -s $pkg 2>/dev/null >/dev/null; then
            echo "Found $pkg [optional]"
        else
            echo "Missing $pkg [optional]"
        fi
    done
elif command -v yum >/dev/null; then
    for pkg in ${required_pkgs_yum[@]}; do
        if yum list installed $pkg 2>/dev/null >/dev/null; then
            echo "Found $pkg"
        elif (( $use_sudo )); then
            echo "Installing $pkg"
            if [[ ! `sudo yum install -y $pkg` ]]; then
                echo "Didn't install $pkg [required]"
                required_missing=1
            fi
        else
            echo "Missing $pkg [required]"
            required_missing=1
        fi
    done
    for pkg in ${optional_pkgs_yum[@]}; do
        if yum list installed $pkg 2>/dev/null >/dev/null; then
            echo "Found $pkg [optional]"
        else
            echo "Missing $pkg [optional]"
        fi
    done
else
    echo "Unsupported distribution!"
    exit 1
fi

if (( $required_missing )); then
    echo "Missing required packages. EXITING!"
    echo "Please, install required packages and rerun $0 again."
    exit 2
fi

# create a default build directory
mkdir -p ./build

# quicklisp package manager for Common Lisp
quicklisp_install_dir="$HOME/quicklisp"
if [[ -v QUICKLISP_HOME ]]; then
  quicklisp_install_dir="${QUICKLISP_HOME}"
fi

if [[ ! -f "${quicklisp_install_dir}/setup.lisp" ]]; then
  wget -nv https://beta.quicklisp.org/quicklisp.lisp -O quicklisp.lisp || exit 1
  echo \
  "
  (load \"${DIR}/quicklisp.lisp\")
  (quicklisp-quickstart:install :path \"${quicklisp_install_dir}\")
  " | sbcl --script || exit 1
  rm -rf quicklisp.lisp || exit 1
fi
ln -Tfs "$DIR/src/lisp" "${quicklisp_install_dir}/local-projects/lcp"
# Install LCP dependencies
# TODO: We should at some point cache or have a mirror of packages we use.
# TODO: move the installation of LCP's dependencies into ./setup.sh
echo \
  "
  (load \"${quicklisp_install_dir}/setup.lisp\")
  (ql:quickload '(:lcp :lcp/test) :silent t)
  " | sbcl --script

# setup libs (download)
cd libs
./cleanup.sh
./setup.sh
cd ..

# setup qa dependencies
setup_virtualenv tests/qa

# setup stress dependencies
setup_virtualenv tests/stress

# setup integration/ldap dependencies
setup_virtualenv tests/integration/ldap

echo "Done installing dependencies for Memgraph"

echo "Linking git hooks"
for hook in $(find $DIR/.githooks -type f -printf "%f\n"); do
  ln -s -f "$DIR/.githooks/$hook" "$DIR/.git/hooks/$hook"
  echo "Added $hook hook"
done;