#!/usr/bin/env bash

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

declare -ga PACKAGES

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 guessPackageManager() {
    local ret

    if apt-get --version &>/dev/null; then
        # apt-get - Debian based distributions
        ret="apt"
    elif dnf --version &>/dev/null; then
        # dnf - Fedora
        ret="dnf"
    elif pacman -Ss &>/dev/null; then
        # pacman - Arch Linux
        # pacman --version and pacman --help both return non-0
        ret="pacman"
    else
        printNotSupportedMessageAndExit
    fi

    echo $ret
}

function installDependencies {
    local packageManager

    packageManager=$(guessPackageManager)
    source "$(dirname "$0")/dependencies/$packageManager"
    installDependenciesWith${packageManager^}
}

function installDependenciesWithApt {
    apt-get install -y --no-install-recommends ${PACKAGES[@]}

    # Ubuntu Bionic doesn't ship pipenv. So fallback to the pip3 install path.
    if apt-cache show pipenv &>/dev/null; then
        apt-get install -y --no-install-recommends pipenv
    else
        apt-get install -y --no-install-recommends python3-pip
        pip3 install pipenv
    fi
}

function installDependenciesWithDnf {
    dnf install ${PACKAGES[@]}
}

function installDependenciesWithPacman {
    pacman -S --needed ${PACKAGES[@]}

    cat <<-EOF

The following packages are available from AUR, and needed for running tests:

    ruby-json ruby-highline

Instructions on how to use the AUR can be found on the Arch Wiki:

    https://wiki.archlinux.org/index.php/Arch_User_Repository

You will also need to follow the instructions on the wiki to make 'python'
call python2 in the WebKit folder:

    https://wiki.archlinux.org/index.php/Python#Dealing_with_version_problem_in_build_scripts

Alternatively, you may use a Python 2.x virtualenv while hacking on WPE:

    https://wiki.archlinux.org/index.php/Python/Virtual_environment

EOF
}

installDependencies
