#!/bin/bash

# read arguments
clone_dependencies=true
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
    --no-clone-dependencies)
    clone_dependencies=false
    shift # past argument
    ;;
    *)
	# unknown option
    ;;
esac
shift # past argument
done

# exit if any subcommand returns a non-zero status
set -e

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

# optional argument; if diff_id exists arcanist patch will be applied
arcanist_diff_id=${1:-""}

# install all dependencies on debian based operating systems
for pkg in git arcanist python3-virtualenv python3-pip; do
    dpkg -s $pkg 2>/dev/null >/dev/null || sudo apt-get -y install $pkg
done

# TODO: use pullbot and read a password from program arg or env var
#       (in order to use this script inside CI infrastructure)

# clean tests folder
tests_folder=${script_dir}/tests
mkdir -p ${tests_folder}

# clone dressipi's tests
cd ${tests_folder}
if ${clone_dependencies} ; then
rm -rf ${tests_folder}/*
git clone https://phabricator.memgraph.io/source/pilot_dressipi.git
fi

# clean dbms folder
dbms_folder=${script_dir}/dbms
mkdir -p ${dbms_folder}

# clone memgraph & checkout right commit
cd ${dbms_folder}
if ${clone_dependencies} ; then
rm -rf ${dbms_folder}/*
git clone https://phabricator.memgraph.io/diffusion/MG/memgraph.git
fi
memgraph_folder=${dbms_folder}/memgraph
cd ${memgraph_folder}
git checkout dev
# optionally apply arcanist patch
if [[ !  -z  ${arcanist_diff_id}  ]]; then
    # nocommit is here because arc tries to commit the patch
    # and it fails if user isn't set (inside CI infrastructure the user
    # probably won't be defined because CI infrastructure doesn't push
    # any code back to the repository)
    arc parch ${arcanist_diff_id} --nocommit
fi
./init