#!/bin/bash

# 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:-561}

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" ]; then
    ubuntu_ver=${os_RELEASE:0:2}
    print_title "Build RDM on Ubuntu: $ubuntu_ver"

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

    # Check Qt ppa
    if [ ! -f /opt/qt${RDM_QT_VERSION:0:2}/bin/qt${RDM_QT_VERSION:0:2}-env.sh ]; then
        print_title "Install Qt ${RDM_QT_VERSION}"

	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_VERSION:0:2}base qt${RDM_QT_VERSION:0:2}imageformats qt${RDM_QT_VERSION:0:2}tools \
            qt${RDM_QT_VERSION:0:2}declarative qt${RDM_QT_VERSION:0:2}quickcontrols -y
    fi

    print_title "Check deps"
    sudo apt-get install automake libtool libssl-dev \
        libssh2-1-dev g++ libgl1-mesa-dev cmake -y

    build_breakpad
    build_libssh2

    print_title "Run: source /opt/qt${RDM_QT_VERSION:0:2}/bin/qt${RDM_QT_VERSION:0:2}-env.sh && qmake && make "
    
elif [  "$os_VENDOR" == "Fedora" ] || [[ "$os_VENDOR" == "CentOS" && "$os_RELEASE" -ge "7" ]]; 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-qttools 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
    chmod -R 755 gbreakpad && cd gbreakpad/
    xcodebuild -sdk macosx -project src/client/mac/Breakpad.xcodeproj -configuration Release -target Breakpad ARCHS=x86_64 ONLY_ACTIVE_ARCH=YES MACOSX_DEPLOYMENT_TARGET=10.10 GCC_VERSION=com.apple.compilers.llvm.clang.1_0 > /dev/null
    xcodebuild -sdk macosx -project src/client/mac/Breakpad.xcodeproj -configuration Release -target Breakpad install DSTROOT=$DEPS_DIR/../bin/osx INSTALL_PATH=/Frameworks ARCHS=x86_64 ONLY_ACTIVE_ARCH=YES MACOSX_DEPLOYMENT_TARGET=10.10 GCC_VERSION=com.apple.compilers.llvm.clang.1_0 > /dev/null
    xcodebuild -sdk macosx -project src/client/mac/Breakpad.xcodeproj -configuration Release -target Breakpad install DSTROOT=$DEPS_DIR/../bin INSTALL_PATH=/Frameworks ARCHS=x86_64 ONLY_ACTIVE_ARCH=YES MACOSX_DEPLOYMENT_TARGET=10.10 GCC_VERSION=com.apple.compilers.llvm.clang.1_0 > /dev/null
    brew upgrade libssh2 || brew install libssh2 || true
else
    echo "Your operation system is not supported!"
fi
