#!/bin/bash -e

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

# Cleanup
cd ${script_dir}
./cleanup

# Find remote endpoints
if [ "$USER" == "apollo" ]; then
    deps_http_url="http://89.201.166.70:46744"
    deps_git_url="git://89.201.166.70:46745"
    deps_pypi_url="http://89.201.166.70:46746/root/pypi"
    deps_pypi_trusted="89.201.166.70"
else
    deps_http_url="http://deps.memgraph.io"
    deps_git_url="git://deps.memgraph.io"
    deps_pypi_url="http://deps.memgraph.io:3141/root/pypi"
    deps_pypi_trusted="deps.memgraph.io"
fi

# Download Maven
cd ${script_dir}
wget -nv $deps_http_url/ldbc/apache-maven-3.5.0-bin.tar.gz -O maven.tar.gz
tar -xzf maven.tar.gz
mv apache-maven-3.5.0 maven
rm maven.tar.gz
cd maven
sed -r "s@</settings>@<localRepository>${script_dir}/maven/.m2</localRepository>\n</settings>@g" -i conf/settings.xml
wget -nv $deps_http_url/ldbc/maven-cache.tar.gz
tar -xzf maven-cache.tar.gz
rm maven-cache.tar.gz
mvn=${script_dir}/maven/bin/mvn

# Download Hadoop
cd ${script_dir}
wget -nv $deps_http_url/ldbc/hadoop-2.7.4.tar.gz -O hadoop.tar.gz
tar -xzf hadoop.tar.gz
mv hadoop-2.7.4 hadoop
rm hadoop.tar.gz
# Edit Java Xmx settings because Hadoop has stupid default run scripts and they duplicate the -Xmx flag
# https://stackoverflow.com/questions/2740725/duplicated-java-runtime-options-what-is-the-order-of-preference
# "Depends on the JVM, perhaps the version...perhaps even how many paper clips you have on your desk at the time. It might not even work. Don't do that."
sed -r "s@-Xmx512m @@g" -i hadoop/etc/hadoop/hadoop-env.sh

# Download LDBC logo
cd ${script_dir}/plots
wget -nv $deps_http_url/ldbc/ldbc-logo.png

# Setup ldbc_snb_datagen
cd ${script_dir}
#git clone https://github.com/ldbc/ldbc_snb_datagen.git
git clone $deps_git_url/ldbc_snb_datagen.git
cd ldbc_snb_datagen
git checkout 46ccf9340c20d8cfde0e7e11c9297a4061117bd3
sed -r "s@#!/bin/bash@#!/bin/bash -e@g" -i run.sh

# Setup ldbc_driver
cd ${script_dir}
#git clone https://github.com/ldbc/ldbc_driver.git
git clone $deps_git_url/ldbc_driver.git
cd ldbc_driver
git checkout 1bb441394c3cd3e23d4df5a87689b9d1c5e6f48f
$mvn clean package -DskipTests
$mvn install -DskipTests

# Setup ldbc-snb-impls
cd ${script_dir}
#git clone https://phabricator.memgraph.io/source/ldbc-snb-impls.git
git clone $deps_git_url/ldbc-snb-impls.git
cd ldbc-snb-impls
git checkout 3f3f4deccc6260b79469aa89e6088d449032761e
sed -r '/(snb-interactive-tools|snb-interactive-titan|snb-interactive-torc)/s@^( +)(.+)$@\1<!--\2-->@' -i pom.xml
$mvn install
$mvn clean compile assembly:single

# Setup Python3 Virtualenv
cd ${script_dir}
virtualenv -p python3 ve3
source ve3/bin/activate
# Temporary workaround, our deps PyPI cache is sometimes stupid...
# pip install -i $deps_pypi_url \
#             --trusted-host $deps_pypi_trusted -r requirements.txt
pip install -r requirements.txt
deactivate
