#!/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
    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

    if [ `uname` -eq "Darwin" ]; then
       installDependenciesWithBrew
       exit 0
    fi

    printNotSupportedMessageAndExit
}

function installDependenciesWithBrew {
    brew &> /dev/null
    if [ $? -gt 1 ]; then
        echo "Please install HomeBrew. Instructions on http://brew.sh"
        exit 1
    fi

    brew install autoconf \
         automake \
         bison \
         cmake \
         enchant \
         flex \
         gettext \
         gobject-introspection \
         icu4c \
         intltool \
         itstool \
         libcroco \
         libgcrypt \
         libgpg-error \
         libtiff \
         libtool \
         ninja \
         pango \
         pkg-config \
         sqlite \
         webp \
         xz
}

function installDependenciesWithApt {
    # These are dependencies necessary for building WebKitGTK+.
    packages=" \
        autoconf \
        automake \
        autopoint \
        autotools-dev \
        bison \
        cmake \
        flex \
        gawk \
        gnome-common \
        gperf \
        gtk-doc-tools \
        intltool \
        itstool \
        libatk1.0-dev \
        libedit-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 \
        ninja-build \
        ruby \
        xfonts-utils"

    # These are dependencies necessary for running tests.
    packages="$packages \
        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.
    packages="$packages \
        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 \
        libtool-bin \
        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
    packages="$packages \
        git-svn \
        subversion"

    apt-get install $packages
}

function installDependenciesWithPacman {
    # These are dependencies necessary for building WebKitGTK+.
    packages=" \
        autoconf \
        automake \
        bison \
        cmake \
        libedit \
        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 installDependenciesWithDnf {
    # These are dependencies necessary for building WebKitGTK+.
    packages=" \
        atk-devel \
        autoconf \
        automake \
        bison \
        cairo-devel \
        cmake \
        enchant-devel \
        flex \
        fontconfig-devel \
        freetype-devel \
        gcc-c++ \
        geoclue2-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 \
        libedit-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 \
        llvm \
        llvm-devel \
        llvm-static \
        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.
    packages="$packages \
        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.
    packages="$packages \
        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 \
        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
    packages="$packages
        git-svn \
        subversion"

    dnf install $packages
}

checkInstaller

