#!/usr/bin/env bash
########################################################################################################################
# This script setups dependencies for the InterOp library using Docker
#
#
#
# Build the Image
#
# $ docker build --rm -t ezralanglois/interop_centos7 -f ./tools/docker/DockerFile_Centos7 tools
# $ docker images
# $ docker tag <image-id> ezralanglois/interop:last_good
# $ docker tag ezralanglois/interop:last_good ezralanglois/interop:latest
# $ docker push ezralanglois/interop:latest
#
# Clean containers and images
#
# $ docker rm $(docker ps -a -f status=exited -q)
# $ docker rmi $(docker images -f dangling=true -q)
#
########################################################################################################################
#set -ex
set -e

CMAKE_URL="http://www.cmake.org/files/v3.8/cmake-3.8.2-Linux-x86_64.tar.gz"
CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v3.20.5/cmake-3.20.5-linux-x86_64.tar.gz"
SWIG_URL="http://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz"
SWIG_URL="http://prdownloads.sourceforge.net/swig/swig-4.0.2.tar.gz"
NUGET_URL="https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
GOOGLETEST_URL="https://github.com/google/googletest/archive/release-1.8.0.tar.gz"
JUNIT_URL="http://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar"
NUNIT_URL="https://github.com/nunit/nunitv2/releases/download/2.6.4/NUnit-2.6.4.zip"
JAVA_URL="http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm"
#VALGRIND_URL="http://www.valgrind.org/downloads/valgrind-3.14.0.tar.bz2"
DOTNET_URL="https://download.visualstudio.microsoft.com/download/pr/022d9abf-35f0-4fd5-8d1c-86056df76e89/477f1ebb70f314054129a9f51e9ec8ec/dotnet-sdk-2.2.207-linux-x64.tar.gz"
DOTNET_URL="https://download.visualstudio.microsoft.com/download/pr/42f39f2f-3f24-4340-8c57-0a3133620c21/0a353696275b00cbddc9f60069867cfc/dotnet-sdk-2.2.110-linux-x64.tar.gz"
#DOTNET_URL="https://download.visualstudio.microsoft.com/download/pr/c505a449-9ecf-4352-8629-56216f521616/bd6807340faae05b61de340c8bf161e8/dotnet-sdk-6.0.201-linux-x64.tar.gz"
PROG_HOME=/opt
SWIG_HOME=${PROG_HOME}/swig4
JUNIT_HOME=${PROG_HOME}/junit
NUNIT_HOME=${PROG_HOME}/nunit

sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
yum install -y wget libicu
#yum -y update
#yum install -y wget which libunwind lttng-ust libcurl openssl-libs libuuid krb5-libs libicu zlib
#yum install -y gcc gcc-c++ libtool git make rpm-build
#yum install -y python-devel

mkdir /opt/dotnet
wget --no-check-certificate --quiet -O - ${DOTNET_URL} | tar --strip-components=1 -xz -C /opt/dotnet

#if hash cmake  2> /dev/null; then
#    wget --no-check-certificate --quiet -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C /opt/_internal/tools
#else
#    wget --no-check-certificate --quiet -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C /usr
#fi

if hash mono  2> /dev/null; then
    echo "Found mono"
else
    yum install yum-utils -y
    rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
    yum-config-manager --add-repo http://download.mono-project.com/repo/centos/
    yum install mono-complete  -y
fi

if hash swig  2> /dev/null; then
    echo "Found Swig"
else
    if [ ! -e ${SWIG_HOME} ]; then
        mkdir -p ${SWIG_HOME}/src
    fi
    wget --no-check-certificate --quiet -O - ${SWIG_URL} | tar --strip-components=1 -xz -C ${SWIG_HOME}/src
    cd ${SWIG_HOME}/src

    # TODO test if on centos or ubuntu
    yum install pcre-devel -y


    ./configure --prefix=/usr --with-python=/opt/python/cp27-cp27m/bin/python --with-python3=/opt/python/cp36-cp36m/bin/python
    make swig -j4 > /dev/null
    make install
    rm -fr ${SWIG_HOME}
    cd -
fi

if [ -e /usr/include/gtest/gtest.h ]; then
    echo "Found GTest and GMock"
else
    mkdir /gtest
    wget --no-check-certificate --quiet -O - ${GOOGLETEST_URL} | tar --strip-components=1 -xz -C /gtest
    mkdir /gtest/build
    cmake -H/gtest -B/gtest/build
    cmake --build /gtest/build -- -j4
    cp -r /gtest/googletest/include/gtest /usr/include/
    cp -r /gtest/googlemock/include/gmock /usr/include/
    cp /gtest/build/googlemock/gtest/lib*.a /usr/lib/
    cp /gtest/build/googlemock/lib*.a /usr/lib/

    rm -fr /gtest
fi

if hash java  2> /dev/null; then
    echo "Found Java"
else
    wget --quiet --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "${JAVA_URL}" -O ${JAVA_URL##*/}
    rpm -Uvh ${JAVA_URL##*/}
    rm -f  ${JAVA_URL##*/}
fi

#if [ ! -e ${NUNIT_HOME}/NUnit-2.6.4 ]; then
#    mkdir ${NUNIT_HOME}
#    wget --no-check-certificate --quiet ${NUNIT_URL} -O  ${NUNIT_HOME}/${NUNIT_URL##*/}
#    unzip ${NUNIT_HOME}/${NUNIT_URL##*/} -d ${NUNIT_HOME}
#    rm -f ${NUNIT_HOME}/${JUNIT_URL##*/}
#else
#    echo "Found NUnit at ${NUNIT_HOME}/NUnit-2.6.4"
#fi
#NUNIT_HOME=${NUNIT_HOME}/NUnit-2.6.4

if [ ! -e ${JUNIT_HOME}/${JUNIT_URL##*/} ]; then
    mkdir ${JUNIT_HOME}
    wget --no-check-certificate --quiet ${JUNIT_URL} -O  ${JUNIT_HOME}/${JUNIT_URL##*/}
else
    echo "Found JUnit at ${JUNIT_HOME}/${JUNIT_URL##*/}"
fi

#mkdir tmp_build
#wget --no-check-certificate --quiet -O - ${VALGRIND_URL} | tar --strip-components=1 -xj -C ./tmp_build
#cd tmp_build
#./configure --prefix=/usr
#make -j 4
#make install
#cd -
#rm -fr tmp_build


wget --no-check-certificate --quiet ${NUGET_URL} -O /usr/lib/nuget.exe
echo "mono /usr/lib/nuget.exe \$@" > /usr/bin/nuget
chmod +x /usr/bin/nuget

/opt/python/cp38-cp38/bin/python -m pip install numpy==1.17.3 pandas
/opt/python/cp39-cp39/bin/python -m pip install numpy==2.0.0 pandas
/opt/python/cp310-cp310/bin/python -m pip install numpy==2.0.0 pandas
/opt/python/cp311-cp311/bin/python -m pip install numpy==2.0.0 pandas
/opt/python/cp312-cp312/bin/python -m pip install numpy==2.0.0 pandas

export JAVA_HOME=/usr/java/jdk1.8.0_131
export PATH=${PATH}:/opt/dotnet

which gcc
which g++
which swig
which java
which cmake
which mono

gcc --version
swig -version
java -version
cmake --version
mono --version
dotnet --version
which dotnet

yum clean all

