#!/bin/bash

# This script needs to be run with root rights.
if [ $UID -ne 0 ]; then
    sudo $0
    exit 0
fi

function printNotSupportedMessageAndExit() {
    echo
    echo "Currently this script only works for distributions supporting apt-get."
    echo "Please add support for your distribution."
    echo
    exit 1
}

function checkCmakeVersion() {
    CMAKE_VERSION=`cmake --version`

    VERSION=`echo "$CMAKE_VERSION" | awk '{split($3,num,".");
    if (!(num[1]>2 || num[2]>8 || num[3]>=10))
        printf $3}'`

    if [ -n "${VERSION}" ]; then
        echo "Warning: CMake version detected (${VERSION}) is lower then 2.8.10."
        echo "  This will probably cause errors, as older version didn't support CMAKE_NINJA_FORCE_RESPONSE_FILE,"
        echo "  which is needed now for building. (look at: https://lists.webkit.org/pipermail/webkit-gtk/2014-March/001809.html )"
        echo ""

        if [ -f "/etc/issue" ]; then
            ubuntu_version=`cat /etc/issue`
            if [[ $ubuntu_version == *Ubuntu\ 12.04* ]]; then
                echo "  For Ubuntu 12.04 or 12.10 You might consider adding ppa from https://launchpad.net/~kalakris/+archive/ubuntu/cmake"
            fi
        fi
    fi
}

function checkInstaller {
    # apt-get - Debian based distributions
    apt-get --version &> /dev/null
    if [ $? -eq 0 ]; then
        installDependenciesWithApt
        checkCmakeVersion;
        exit 0
    fi

    printNotSupportedMessageAndExit
}

function installDependenciesWithApt {
    # These are dependencies necessary for building WebKitEFL.
    apt-get install \
        bison \
        cmake \
        doxygen \
        flex \
        g++ \
        gperf \
        gtk-doc-tools \
        libatk1.0-dev \
        libdbus-1-dev \
        libedit-dev \
        libenchant-dev \
        libespeak-dev \
        libfaad-dev \
        libffi-dev \
        libfreetype6-dev \
        libgcrypt11-dev \
        libgeoclue-dev \
        libgif-dev \
        libgl1-mesa-dev \
        libgnutls-dev \
        libgpg-error-dev \
        libhyphen-dev \
        libicu-dev \
        libjpeg-dev \
        libjson-glib-dev \
        liblua5.1-0-dev \
        libmpg123-dev \
        liborc-0.4-dev \
        libp11-kit-dev \
        libpng12-dev \
        libpulse-dev \
        libsqlite3-dev \
        libssl-dev \
        libtheora-dev \
        libtiff5-dev \
        libudev-dev \
        libvorbis-dev \
        libwebp-dev \
        libxcomposite-dev \
        libxcursor-dev \
        libxinerama-dev \
        libxrandr-dev \
        libxrender-dev \
        libxslt1-dev \
        libxss-dev \
        libxt-dev \
        libxtst-dev \
        ninja-build \
        ragel \
        ruby \
        subversion \
        x11proto-print-dev

    # These are dependencies necessary for building WebKitEFL and not available on ARM64.
    apt-get install \
        luajit

    # These are dependencies necessary for running tests.
    apt-get install \
        apache2 \
        libruby \
        xvfb

    installPHPWithApt
}

function installPHPWithApt {
    if [ -f "/etc/os-release" ]; then
        ubuntu_version=`grep VERSION_ID /etc/os-release`
        if [[ $ubuntu_version == *VERSION_ID=\"16.04* ]]; then
            apt-get install libapache2-mod-php7.0
        else
            apt-get install libapache2-mod-php5
        fi
    fi
}

checkInstaller

