#!/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 and yum."
    echo "Please add support for your distribution: http://webkit.org/b/110693"
    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 22 and above
    dnf --version &> /dev/null
    if [ $? -eq 0 ]; then
        installFedoraDependencies dnf
        exit 0
    fi

    # yum - Fedora 21 and below
    yum --version &> /dev/null
    if [ $? -eq 0 ]; then
        installFedoraDependencies yum
        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
}

function installDependenciesWithApt {
    # These are dependencies necessary for building WebKitGTK+.
    apt-get install \
        autoconf \
        automake \
        autopoint \
        autotools-dev \
        bison \
        cmake \
        flex \
        gawk \
        gnome-common \
        gperf \
        gtk-doc-tools \
        intltool \
        itstool \
        libatk1.0-dev \
        libenchant-dev \
        libfaad-dev \
        libgeoclue-dev \
        libgirepository1.0-dev \
        libgl1-mesa-dev \
        libgl1-mesa-glx \
        libgnutls28-dev \
        libgtk2.0-dev \
        libgtk-3-dev \
        libgudev-1.0-dev \
        libharfbuzz-dev \
        libhyphen-dev \
        libicu-dev \
        libjpeg-dev \
        libmpg123-dev \
        libnotify-dev \
        libopus-dev \
        liborc-0.4-dev \
        libpango1.0-dev \
        libpng12-dev \
        libpulse-dev \
        librsvg2-dev \
        libsecret-1-dev \
        libsoup2.4-dev \
        libsqlite3-dev \
        libtheora-dev \
        libtool \
        libvorbis-dev \
        libwebp-dev \
        libxcomposite-dev \
        libxslt1-dev \
        libxt-dev \
        libxtst-dev \
        libwayland-dev \
        ruby \
        xfonts-utils

    # These are dependencies necessary for running tests.
    apt-get install \
        apache2 \
        curl \
        dbus-x11 \
        libapache2-mod-bw \
        libapache2-mod-php5 \
        libgpg-error-dev \
        pulseaudio-utils \
        python-gi \
        ruby \
        ruby-json \
        ruby-highline \
        xvfb

    # These are dependencies necessary for building the jhbuild.
    apt-get install \
        git \
        gobject-introspection \
        icon-naming-utils \
        libcroco3-dev \
        libegl1-mesa-dev \
        libepoxy-dev \
        libgcrypt11-dev \
        libgpg-error-dev \
        libjson-glib-dev \
        liborc-0.4-dev \
        libp11-kit-dev \
        libpciaccess-dev \
        libssl-dev \
        libtiff5-dev \
        libv4l-dev \
        libxcb-xfixes0-dev \
        libxfont-dev \
        libxkbfile-dev \
        llvm \
        llvm-dev \
        python-dev \
        ragel \
        x11proto-bigreqs-dev \
        x11proto-composite-dev \
        x11proto-gl-dev \
        x11proto-input-dev \
        x11proto-randr-dev \
        x11proto-resource-dev \
        x11proto-scrnsaver-dev \
        x11proto-video-dev \
        x11proto-xcmisc-dev \
        x11proto-xf86dri-dev \
        xfonts-utils \
        xtrans-dev \
        xutils-dev

    # These are dependencies necessary for using webkit-patch
    apt-get install \
        git-svn \
        subversion

    # ninja is a faster build system than GNU make, but it doesn't
    # exist on Ubuntu 12.04
    apt-get install ninja-build || true
}

function installDependenciesWithPacman {
    # These are dependencies necessary for building WebKitGTK+.
    packages=" \
        autoconf \
        automake \
        bison \
        cmake \
        file \
        findutils \
        flex \
        gawk \
        gcc \
        gettext \
        gnome-common \
        gperf \
        grep \
        groff \
        gzip \
        hyphen \
        libtool \
        m4 \
        make \
        patch \
        pkg-config \
        sed \
        texinfo \
        util-linux \
        which \
        gtk-doc \
        intltool \
        itstool \
        atk \
        enchant \
        faad2 \
        geoclue \
        gobject-introspection \
        mesa \
        mesa-libgl \
        gnutls \
        gtk2 \
        gtk3 \
        libsystemd \
        harfbuzz \
        harfbuzz-icu \
        icu \
        libjpeg-turbo \
        mpg123 \
        opus \
        pango \
        libnotify \
        libpng \
        libpulse \
        librsvg \
        libsecret \
        libsoup \
        sqlite \
        libtheora \
        libtool \
        libvorbis \
        libwebp \
        libxcomposite \
        libxslt \
        libxt \
        libxtst \
        ninja \
        ruby \
        xorg-font-utils \
        orc \
        wayland"

    # These are dependencies necessary for running tests.
    # Note: apache-mod_bw is available in the AUR, but the main repos
    #       could not find ruby-json
    packages="$packages \
        apache \
        curl \
        hunspell \
        hunspell-en \
        php-apache \
        libgpg-error \
        pulseaudio \
        python-gobject \
        ruby \
        ruby-highline \
        xorg-server-xvfb"

    # These are dependencies necessary for building the jhbuild.
    # Note: Could not find libegl-mesa
    packages="$packages \
        git \
        gobject-introspection \
        icon-naming-utils \
        libcroco \
        libepoxy \
        libgcrypt \
        libgpg-error \
        p11-kit \
        libpciaccess \
        libtiff \
        libxfixes \
        libxfont \
        libxkbfile \
        llvm \
        python2 \
        python2-lxml \
        ragel \
        bigreqsproto \
        compositeproto \
        glproto \
        inputproto \
        randrproto \
        resourceproto \
        scrnsaverproto \
        videoproto \
        xcmiscproto \
        xf86driproto \
        xorg-font-utils \
        xorg-util-macros \
        xtrans \
        xorg-utils"

    # 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 installFedoraDependencies {
    # These are dependencies necessary for building WebKitGTK+.
    $1 install \
        atk-devel \
        autoconf \
        automake \
        bison \
        cairo-devel \
        cmake \
        enchant-devel \
        flex \
        fontconfig-devel \
        freetype-devel \
        gcc-c++ \
        geoclue-devel \
        gettext-devel \
        gobject-introspection-devel \
        gperf \
        gstreamer1-devel \
        gstreamer1-plugins-base-devel \
        gtk-doc \
        gtk2-devel \
        gtk3-devel \
        harfbuzz-devel \
        hyphen-devel \
	json-glib-devel \
        libXt-devel \
        libXtst-devel \
        libgudev1-devel \
        libicu-devel \
        libjpeg-turbo-devel \
        libnotify-devel \
        libpng-devel \
        libsecret-devel \
        libsoup-devel \
	libv4l-devel \
        libwebp-devel \
        libwayland-client-devel \
        libwayland-server-devel \
        libxslt-devel \
        mesa-libGL-devel \
        ninja-build \
	openssl-devel \
        pcre-devel \
        perl-Switch \
        perl-version \
	pulseaudio-libs-devel \
        python-devel \
        orc-devel \
        ruby \
        sqlite-devel

    # These are dependencies necessary for running tests.
    $1 install \
        curl \
        dbus-x11 \
        hunspell-en \
        httpd \
        libgpg-error-devel \
        mod_bw \
        mod_ssl \
        perl-CGI \
        php \
        pulseaudio-utils \
        pygobject3-base \
        ruby \
        rubygem-json \
        rubygem-highline \
        xorg-x11-server-Xvfb

    # These are dependencies necessary for building the jhbuild.
    $1 install \
        docbook-utils \
        docbook-utils-pdf \
        git \
        gobject-introspection \
        icon-naming-utils \
        itstool \
        libXfont-devel \
        libcroco-devel \
        libepoxy-devel \
        libgcrypt-devel \
        libgpg-error-devel \
        libp11-devel \
        libpciaccess-devel \
        libtiff-devel \
        libxkbfile-devel \
        llvm \
        llvm-devel \
        mesa-libEGL-devel \
        ragel \
        xorg-x11-font-utils \
        xorg-x11-proto-devel \
        xorg-x11-util-macros \
        xorg-x11-xtrans-devel

    # These are dependencies necessary for using webkit-patch
    $1 install \
        git-svn \
        subversion
}

checkInstaller

