#!/usr/bin/env bash

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

function printNotSupportedMessageAndExit() {
    echo
    echo "Currently this script only works for distributions supporting apt-get, dnf or pacman."
    echo "Please add support for your distribution and submit the patch at https://bugs.webkit.org"
    echo
    exit 1
}

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

    # dnf - Fedora
    dnf --version &> /dev/null
    if [ $? -eq 0 ]; then
        installDependenciesWithDnf
        exit 0
    fi

    # pacman - Arch Linux
    # pacman --version and pacman --help both return non-0
    pacman -Ss &> /dev/null
    if [ $? -eq 0 ]; then
        installDependenciesWithPacman
        exit 0
    fi

    printNotSupportedMessageAndExit
}


# If the package $1 is available, prints it. Otherwise prints $2.
# Useful for handling when a package is renamed on new versions of Debian/Ubuntu.
function aptIfElse {
    if apt-cache show $1 &>/dev/null; then
        echo $1
    else
        echo $2
    fi
}

function installDependenciesWithApt {
    # These are dependencies necessary for building WPE.
    packages=" \
        autoconf \
        automake \
        autopoint \
        autotools-dev \
        bison \
        cmake \
        flex \
        g++ \
        gawk \
        gcc \
        gperf \
        intltool \
        itstool \
        libgnutls28-dev \
        libgstreamer1.0-dev \
        libgstreamer-plugins-bad1.0-dev \
        libgstreamer-plugins-base1.0-dev \
        libicu-dev \
        libjpeg-dev \
        libfile-copy-recursive-perl \
        $(aptIfElse libpng-dev libpng12-dev) \
        libsqlite3-dev \
        libtasn1-6-dev \
        libtool \
        libwebp-dev \
        libxml2-dev \
        libxslt1-dev \
        ninja-build \
        patch \
        pkg-config \
        ruby \
        zlib1g-dev"

    # These are dependencies necessary for running tests.
    packages="$packages \
        apache2 \
        curl \
        gdb \
        libapache2-mod-bw \
        libapache2-mod-php \
        libcgi-pm-perl \
        psmisc \
        pulseaudio-utils \
        python-gi \
        python-psutil \
        ruby \
        ruby-highline \
        ruby-json"

    # These are dependencies necessary for building the jhbuild.
    packages="$packages \
        git \
        gsettings-desktop-schemas-dev \
        libexpat1-dev \
        libgles2-mesa-dev \
        libluajit-5.1-dev \
        libmount-dev \
        libopus-dev \
        liborc-0.4-dev \
        libproxy-dev \
        libpulse-dev \
        libsrtp0-dev \
        libtheora-dev \
        libtool-bin \
        libva-dev \
        libvorbis-dev \
        libvpx-dev \
        libxcb-xkb-dev \
        libxml-libxml-perl \
        libwayland-dev \
        luajit"

    # These are dependencies necessary for using webkit-patch
    packages="$packages \
        git-svn \
        subversion"

    apt-get install $packages
}

function installDependenciesWithPacman {
    # These are dependencies necessary for building WPE.
    packages=" \
        autoconf \
        automake \
        bison \
        cmake \
        file \
        findutils \
        flex \
        gawk \
        gcc \
        gnutls \
        gperf \
        grep \
        groff \
        gstreamer \
        gst-plugins-bad \
        gst-plugins-base-libs \
        gzip \
        icu \
        intltool \
        itstool \
        libjpeg-turbo \
        libpng \
        libtasn1 \
        libtool \
        libwebp \
        libxml2 \
        libxslt \
        m4 \
        make \
        ninja \
        patch \
        perl-file-copy-recursive \
        pkg-config \
        ruby
        sed \
        sqlite \
        texinfo \
        which \
        zlib"

    # These are dependencies necessary for running tests.
    # Note: apache-mod_bw and ruby-json is available in the AUR
    packages="$packages \
        apache \
        curl \
        gdb \
        perl-cgi \
        php-apache \
        psmisc \
        pulseaudio \
        python2 \
        python2-gobject \
        python2-lxml \
        python2-psutil \
        ruby \
        ruby-highline"

    # These are dependencies necessary for building the jhbuild.
    packages="$packages \
        expat \
        git \
        gsettings-desktop-schemas \
        libproxy \
        libpulse \
        libsrtp \
        libtheora \
        libv4l-dev \
        libva \
        libvorbis \
        libvpx \
        libxcb \
        luajit \
        mesa \
        mesa-libgl \
        opus \
        orc \
        perl-xml-libxml \
        util-linux \
        v4l-utils \
        wayland"

    # These are dependencies necessary for using webkit-patch
    packages="$packages \
        svn"
    pacman -S --needed $packages

    echo "You will also need to follow the instructions on the Arch Wiki to make"
    echo "'python' call python2 in the webkit folder"
    echo "https://wiki.archlinux.org/index.php/Python#Dealing_with_version_problem_in_build_scripts"
}

function installDependenciesWithDnf {
    # These are dependencies necessary for building WebKitGTK+.
    packages=" \
        autoconf \
        automake \
        bison \
        cmake \
        flex \
        gcc-c++ \
        gnutls-devel \
        gperf \
        gstreamer1-devel \
        gstreamer1-plugins-bad-free-devel \
        gstreamer1-plugins-base-devel \
        intltool \
        itstool \
        libicu-devel \
        libjpeg-turbo-devel \
        libpng-devel \
        libtasn1-devel \
        libtool \
        libwebp-devel \
        libxml2-devel \
        libxslt-devel \
        ninja-build \
        patch \
        perl-File-Copy-Recursive \
        perl-Time-HiRes \
        perl-version \
        ruby \
        sqlite-devel \
        zlib-devel"

    # These are dependencies necessary for running tests.
    packages="$packages \
        curl \
        gdb \
        httpd \
        mod_bw \
        mod_ssl \
        perl-CGI \
        php \
        psmisc \
        pulseaudio-utils \
        python2-psutil \
        python-gobject-base \
        ruby \
        rubygem-highline \
        rubygem-json"

    # These are dependencies necessary for building the jhbuild.
    packages="$packages \
        expat-devel \
        git \
        gsettings-desktop-schemas-devel \
        libmount-devel \
        libproxy-devel \
        libsrtp-devel \
        libtheora-devel \
        libv4l-devel \
        libva-devel \
        libvorbis-devel \
        libvpx-devel \
        libxcb-devel \
        luajit \
        luajit-devel \
        mesa-libGLES-devel \
        opus-devel \
        orc-devel \
        perl-XML-LibXML \
        pulseaudio-libs-devel \
        wayland-devel"

    # These are dependencies necessary for using webkit-patch
    packages="$packages
        git-svn \
        subversion"

    dnf install $packages
}

checkInstaller
