# One click install script dumux
echo " "
echo " "
echo "*********************************************************************************************"
echo "(0/4) Checking all prerequistes. (git cmake gcc g++ wget pkg-config gnuplot umfpack)"
echo "*********************************************************************************************"

# check some prerequistes
for PRGRM in git cmake gcc g++ wget paraview pkg-config gnuplot; do
    if ! [ -x "$(command -v $PRGRM)" ]; then
        echo "Error: $PRGRM is not installed." >&2
        exit 1
    fi
done

# check some library prerequistes
for LIBRARY in libumfpack; do
    if ! [ "$(/sbin/ldconfig -p | grep $LIBRARY)" ]; then
        echo "Error: $LIBRARY is not installed." >&2
        exit 1
    fi
done

currentver="$(gcc -dumpversion)"
requiredver="7"
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" != "$requiredver" ]; then
    echo "gcc greater than or equal to $requiredver is required!" >&2
    exit 1
fi

if [ $? -ne 0 ]; then
    echo "*********************************************************************************************"
    echo "(0/4) An error occured while checking for prerequistes."
    echo "*********************************************************************************************"
    exit $?
else
    echo "*********************************************************************************************"
    echo "(1/4) All prerequistes found."
    echo "*********************************************************************************************"
fi

echo " "

echo "**************************************************************************************************************"
echo "(1/4) Downloading supplementary files (test script & cmake.opts). Make sure to be connected to the internet."
echo "**************************************************************************************************************"

# make a new folder containing everything
mkdir $(pwd)/DUMUX
cd DUMUX

# download the test script and the cmake.opts
if [ ! -f "test_dumux.sh" ]; then
    wget https://git.iws.uni-stuttgart.de/dumux-repositories/dumux-course/raw/releases/3.4/scripts/test_dumux.sh
fi
chmod +x test_dumux.sh

if [ ! -f "cmake.opts" ]; then
    wget https://git.iws.uni-stuttgart.de/dumux-repositories/dumux-course/raw/releases/3.4/cmake.opts
fi

if [ $? -ne 0 ]; then
    echo "*********************************************************************************************"
    echo "(1/4) An error occured while downloading supplementary files."
    echo "*********************************************************************************************"
    exit $?
else
    echo "*********************************************************************************************"
    echo "(2/4) All supplementary files downloaded."
    echo "*********************************************************************************************"
fi

echo " "

echo "*********************************************************************************************"
echo "(2/4) Cloning repositories. This may take a while. Make sure to be connected to the internet."
echo "*********************************************************************************************"
DUNE_VERSION=2.7
# the core modules
for MOD in common geometry grid localfunctions istl; do
    if [ ! -d "dune-$MOD" ]; then
        git clone -b releases/$DUNE_VERSION https://gitlab.dune-project.org/core/dune-$MOD.git
    else
        echo "Skip cloning dune-$MOD because the folder already exists."
        git checkout releases/$DUNE_VERSION
    fi
done

# extension modules
for MOD in dune-foamgrid dune-alugrid; do
    if [ ! -d "$MOD" ]; then
        git clone -b releases/$DUNE_VERSION https://gitlab.dune-project.org/extensions/$MOD.git
    else
        echo "Skip cloning $MOD because the folder already exists."
        git checkout releases/$DUNE_VERSION
    fi
done

# dune-subgrid
if [ ! -d "dune-subgrid" ]; then
    git clone -b releases/$DUNE_VERSION  https://gitlab.dune-project.org/extensions/dune-subgrid
else
    echo "Skip cloning dune-subgrid because the folder already exists."
    git checkout releases/$DUNE_VERSION
fi

# dumux
if [ ! -d "dumux" ]; then
    git clone -b releases/3.4 https://git.iws.uni-stuttgart.de/dumux-repositories/dumux.git
else
    echo "Skip cloning dumux because the folder already exists."
    cd dumux
    git checkout releases/3.4
    cd ..
fi

# dumux-course
if [ ! -d "dumux-course" ]; then
    git clone -b releases/3.4 https://git.iws.uni-stuttgart.de/dumux-repositories/dumux-course.git
else
    echo "Skip cloning dumux-course because the folder already exists."
    cd dumux-course
    git checkout releases/3.4
    cd ..
fi

if [ $? -ne 0 ]; then
    echo "*********************************************************************************************"
    echo "(2/4) Failed to clone the repositories. Look for repository specific errors."
    echo "*********************************************************************************************"
    exit $?
else
    echo "*********************************************************************************************"
    echo "(3/4) All repositories have been cloned into a containing folder."
    echo "*********************************************************************************************"
fi

echo " "

echo "**************************************************************************************************"
echo "(3/4) Configure and build dune modules and dumux using dunecontrol. This may take several minutes."
echo "**************************************************************************************************"

# run dunecontrol
./dune-common/bin/dunecontrol --opts=cmake.opts all

if [ $? -ne 0 ]; then
    echo "*********************************************************************************************"
    echo "(3/4) Failed to build the dune libaries."
    echo "*********************************************************************************************"
    exit $?
else
    echo "*****************************************************************************************************"
    echo "(4/4) Succesfully configured and built dune and dumux."
    echo "      Please change to the dumux folder and run the test_dumux.sh script to confirm everything works."
    echo "*****************************************************************************************************"
fi
