#!/bin/sh
## Compile TauDEM on Linux system  
# Author: Alexander Muriy (amuriy AT gmail DOT com). Moscow, 2011


# Check for <g++>, <libc6-dev>, <make>, <cmake>, <openmpi-bin>, <libopenmpi-dev> 
# Проверка на наличие пакетов <g++>, <libc6-dev>, <make>, <cmake>, <openmpi-bin>, <libopenmpi-dev> 
if [ ! -x "$(which g++)" ]; then
    echo ""
    echo "TauDEM compiling FAILED. Please install first packages: <g++>"
    echo ""
    exit 1
fi

IF_LIBC6_DEV=$(locate crt1.o) 
if [ "$?" -eq 1 ]; then
    echo ""
    echo "TauDEM compiling FAILED. Please install first packages: <libc6-dev>"
    echo ""
    exit 1
fi    

if [ ! -x "$(which make)" ]; then
    echo ""
    echo "TauDEM compiling FAILED. Please install first packages: <make>"
    echo ""
    exit 1
fi

if [ ! -x "$(which cmake)" ]; then
    echo ""
    echo "TauDEM compiling FAILED. Please install first packages: <cmake>"
    echo ""
    exit 1
fi

if [ ! -x "$(which mpicxx)" ]; then
    echo ""
    echo "TauDEM compiling FAILED. Please install first packages: <openmpi-bin>, <libopenmpi-dev>"
    echo ""
    exit 1
fi 

# ARGS check 
# Проверка аргументов скрипта
CURRENT_DIR=$(pwd)
TauDEM_DIR="$1"

if [ -z "$TauDEM_DIR" ]; then
    mkdir "$CURRENT_DIR"/TauDEM && cd "$CURRENT_DIR"/TauDEM
fi

if [ -n "$TauDEM_DIR" ]; then
    if [ ! -d "$TauDEM_DIR" ]; then
	mkdir -p "$TauDEM_DIR" && cd "$TauDEM_DIR"
    else
	cd "$TauDEM_DIR"
    fi
fi

########## Do it ##########
TauDEM_root=$(pwd)

if [ ! -f "TauDEM5PCsrc_506.zip" ]; then
    wget http://hydrology.usu.edu/taudem/taudem5.0/TauDEM5PCsrc_506.zip
fi

unzip -x -o  "TauDEM5PCsrc_506.zip"
 
cd src/

# patch linearpart.h
# before line "#include "mpi.h"" add line "#include <stdint.h>"
sed -i -e '40 a #include <stdint.h>' ./linearpart.h

## replace all "stdint.h" with <stdint.h>
find . -type f -name "*.cpp" -o -name "*.h" | xargs sed -i 's/#include "stdint.h"/#include <stdint.h>/' 


# create build directory
# создадим диреторию для сборки
mkdir build && cd build

TauDEM_bin="$TauDEM_root"/bin
mkdir "$TauDEM_bin"

# configure by cmake and make all
# конфигурируем и компилируем
CXX=mpicxx cmake .. && make all

# rename programs to match their Win-names 
# переименуем программы, чтобы их имена соответствовали Win-версии
if [ "$?" -eq 0 ]; then
    mv ./aread8 $TauDEM_bin/AreaD8 
    mv ./areadinf $TauDEM_bin/AreaDinf
    mv ./d8flowdir $TauDEM_bin/D8FlowDir
    mv ./d8flowpathextremeup $TauDEM_bin/D8FlowPathExtremeUp 
    mv ./d8hdisttostrm $TauDEM_bin/D8HDistToStrm
    mv ./dinfavalanche $TauDEM_bin/DinfAvalanche
    mv ./dinfconclimaccum $TauDEM_bin/DinfConcLimAccum
    mv ./dinfdecayaccum $TauDEM_bin/DinfDecayAccum
    mv ./dinfdistdown $TauDEM_bin/DinfDistDown
    mv ./dinfdistup $TauDEM_bin/DinfDistUp
    mv ./dinfflowdir $TauDEM_bin/DinfFlowDir
    mv ./dinfrevaccum $TauDEM_bin/DinfRevAccum 
    mv ./dinftranslimaccum $TauDEM_bin/DinfTransLimAccum
    mv ./dinfupdependence $TauDEM_bin/DinfUpDependence
    mv ./dropanalysis $TauDEM_bin/DropAnalysis
    mv ./gridnet $TauDEM_bin/GridNet
    mv ./lengtharea $TauDEM_bin/LengthArea
    mv ./moveoutletstostrm $TauDEM_bin/MoveOutletsToStrm
    mv ./peukerdouglas $TauDEM_bin/PeukerDouglas
    mv ./pitremove $TauDEM_bin/PitRemove
    mv ./slopearea $TauDEM_bin/SlopeArea
    mv ./slopearearatio $TauDEM_bin/SlopeAreaRatio
    mv ./slopeavedown $TauDEM_bin/SlopeAveDown
    mv ./streamnet $TauDEM_bin/StreamNet
    mv ./threshold $TauDEM_bin/Threshold
fi

    TauDEM_bin_path=$(readlink -f "$TauDEM_bin")
    
    if [ "$LANG" = "ru_RU.utf8" -o $LANG = "ru_RU.UTF-8" -o $LANG = "ru_RU.UTF8" ]; then
	echo ""
	echo "#####################################################################"
	echo ""
	echo "                  Похоже, сборка завершилась успешно  :^) "
	echo "                  Программы TauDEM могут быть найдены в "$TauDEM_bin_path""
	echo ""
	echo "#####################################################################"
    else
	echo ""
	echo "#####################################################################"
	echo ""
	echo "        All DONE :^)  TauDEM programs can be found in "$TauDEM_bin_path""
	echo ""
	echo "#####################################################################"
    fi
