#!/bin/bash
set -e
# On OSX use coreutils package to fix absense of readlink command
if [[ $OSTYPE == darwin* ]]; then
  brew install coreutils || true
  PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
fi

# Import common functions
DIR=$(dirname "$(readlink -f "$0")") && RDM_DIR=$DIR/../
. $RDM_DIR/build/common_functions

GetOSVersion

RDM_QT_VERSION=${RDM_QT_VERSION:-594}
RDM_QT_BRANCH=${RDM_QT_VERSION:0:2}

if [ "$1" == "breakpad" ]; then
    print_title "Build only Breakpad"
    build_breakpad
    exit 0
fi

if [ "$1" == "libssh2" ]; then
    print_title "Build only libssh2"
    build_libssh2
    exit 0
fi

if [ "$os_VENDOR" == "Ubuntu" ] && [ "$#" -gt 2 ]; then
    print_title "Run configure in DEB Script packaging"
    build_breakpad
    build_libssh2
    exit 0
fi


if [ "$os_VENDOR" == "Ubuntu" ] || [ "$os_VENDOR" == "LinuxMint" ] || [ "$os_VENDOR" == "elementary" ]; then

    if [ "$os_VENDOR" == "elementary" ]; then
        ubuntu_ver=${os_RELEASE:0:3}
    else
        ubuntu_ver=${os_RELEASE:0:2}
    fi

    print_title "Build RDM on $os_VENDOR: $ubuntu_ver"

    if [ "$os_VENDOR" == "Ubuntu" ] && [ "$ubuntu_ver" == "12" ]; then
        echo "RedisDesktopManager doesn't support Ubuntu 12 since 0.8.8 release."
        exit 1
    fi

    if [ "$os_VENDOR" == "LinuxMint" ]; then
        if [ "$ubuntu_ver" == "17" ]; then
            ubuntu_ver="14"
        elif [ "$ubuntu_ver" == "18" ]; then
            ubuntu_ver="16"
        elif [ "$ubuntu_ver" == "19" ]; then
            ubuntu_ver="18"
        else
            echo "RedisDesktopManager only supports linux mint >=17."
            exit 1
        fi
    fi

    if [ "$os_VENDOR" == "elementary" ]; then
        if [ "$ubuntu_ver" == "0.4" ]; then
            ubuntu_ver="16"
        elif [ "$ubuntu_ver" == "0.5" ]; then
            ubuntu_ver="18"
        else
            echo "RedisDesktopManager only supports elementary >=0.4."
            exit 1
        fi
    fi

    if [ "$ubuntu_ver" -gt 16 ]; then
        print_title "Install Qt ${RDM_QT_BRANCH}"
        sudo apt-get install qt5-default qtdeclarative5-dev libqt5charts5-dev \
             qml-module-qtquick-controls qml-module-qtquick-controls2 qml-module-qt-labs-settings \
             qml-module-qtquick-dialogs qml-module-qtquick-layouts qml-module-qt-labs-folderlistmodel \
             qml-module-qtqml-models2 qml-module-qtcharts libssl1.0-dev -y
    elif [ ! -f /opt/qt${RDM_QT_BRANCH}/bin/qt${RDM_QT_BRANCH}-env.sh ]; then
        print_title "Install Qt ${RDM_QT_VERSION} from ppa"

        if [ "$ubuntu_ver" == "14" ]; then
            sudo add-apt-repository --yes ppa:beineri/opt-qt${RDM_QT_VERSION}-trusty
        elif [ "$ubuntu_ver" == "16" ]; then
            sudo add-apt-repository --yes ppa:beineri/opt-qt${RDM_QT_VERSION}-xenial
        fi

        sudo apt-get update -y
        sudo apt-get install qt${RDM_QT_BRANCH}base qt${RDM_QT_BRANCH}imageformats qt${RDM_QT_BRANCH}tools \
            qt${RDM_QT_BRANCH}declarative qt${RDM_QT_BRANCH}quickcontrols qt${RDM_QT_BRANCH}quickcontrols2 qt${RDM_QT_BRANCH}charts-no-lgpl qt${RDM_QT_BRANCH}svg -y
            
        sudo apt-get install libssl-dev libgl1-mesa-dev -y    
    fi

    print_title "Install packages required by 3rdparty deps"
    sudo apt-get install automake libtool cmake -y

    build_breakpad
    build_libssh2

    if [ "$ubuntu_ver" -lt 17 ]; then
    	print_title "Run: source /opt/qt${RDM_QT_BRANCH}/bin/qt${RDM_QT_BRANCH}-env.sh && qmake && make "
    fi

elif [ "$os_VENDOR" == "Fedora" ] || [[ "$os_VENDOR" == "CentOS" ]]; then
    print_title "Build RDM on $os_VENDOR: $os_RELEASE"

    print_title "Check deps"
    sudo yum install libssh2 libssh2-devel qt5-qtbase qt5-qtsvg-devel \
        qt5-qtdeclarative-devel qt5-qtgraphicaleffects qt5-qtquickcontrols qt5-qtquickcontrols2  \
        qt5-qttools qt5-qtcharts-devel gcc gcc-c++ libstdc++-static git cmake rsync \
        redhat-rpm-config -y

    if [[ $? == 1 && "$os_VENDOR" == "CentOS" ]]; then
        # Some of the deps might need to come from Epel, so output a nice message
        print_title "Not all dependencies were found. Ensure you have the epel repo installed and enabled."
        exit
    fi

    build_breakpad
    build_libssh2

    print_title "Run: qmake-qt5 && make "

elif [ "$os_VENDOR" == "openSUSE" ]; then
    print_title "Check deps"
    sudo zypper install -y libopenssl-devel libssh2-devel \
        libX11-devel gcc gcc-c++ libstdc++-devel libqt5-qtbase-common-devel libqt5-qtquick1-devel \
        libqt5-qtdeclarative-devel libqt5-qtimageformats libqt5-qtquickcontrols \
        libQt5Concurrent-devel automake cmake \

    build_breakpad
    build_libssh2

    print_title "Run: qmake-qt5 && make "

elif [[ $OSTYPE == darwin* ]]; then
    cd $DEPS_DIR
    find gbreakpad -type d -exec chmod 755 {} + && cd gbreakpad
    git apply ../breakpad_osx.patch
    xcodebuild -quiet -sdk macosx -project src/client/mac/Breakpad.xcodeproj -configuration Release -target Breakpad -xcconfig ../../src/gbreakpad.xcconfig ARCHS=x86_64 -UseModernBuildSystem=NO
    xcodebuild -quiet -sdk macosx -project src/client/mac/Breakpad.xcodeproj -configuration Release -target Breakpad -xcconfig ../../src/gbreakpad.xcconfig install DSTROOT=$DEPS_DIR/../bin/osx INSTALL_PATH=/Frameworks ARCHS=x86_64 -UseModernBuildSystem=NO
    xcodebuild -quiet -sdk macosx -project src/client/mac/Breakpad.xcodeproj -configuration Release -target Breakpad -xcconfig ../../src/gbreakpad.xcconfig install DSTROOT=$DEPS_DIR/../bin INSTALL_PATH=/Frameworks ARCHS=x86_64 -UseModernBuildSystem=NO
    cd ..
    brew upgrade openssl || brew install openssl || true
    export OPENSSL_ROOT_DIR=/usr/local/opt/openssl/
    build_libssh2    
else
    echo "Your operating system is not supported!"
fi
