#!/usr/bin/env bash
#
system=$1
shift
IF_VERBOSE=:
SUDO=
PROMPT=
if [ -n "$OUTPUT_RST" ]; then
    COMMENT=""
else
    COMMENT="# "
fi
while :
do
    case "$1" in
        --verbose=*)
            IF_VERBOSE=
            COMMENT="${1#--verbose=}"
            ;;
        --verbose)
            IF_VERBOSE=
            ;;
        --sudo)
            # Whether to print sudo for package managers that need sudo for non-root users
            SUDO="sudo "
            ;;
        --prompt=*)
            PROMPT="${1#--prompt=}"
            ;;
        --prompt)
            PROMPT='  $ '
            ;;
        --no-install-recommends)
            NO_INSTALL_RECOMMENDS=yes
            ;;
        --yes)
            YES=yes
            ;;
        -*)
            echo >&2 "$0: unknown option $1"
            exit 1
            ;;
        *)
            break
    esac
    shift
done
command=$1
shift
if [ -z "$system" -o -z "$command" ]; then
    echo >&2 "usage: $0 {debian|arch|conda|...} [--verbose] [--sudo] [--prompt] {update|install|setup-build-env|remove|...} PACKAGES..."
    exit 1
fi
system_packages="$*"
options=
env=
shopt -s extglob

function print_shell_command()
{
    if [ -n "$OUTPUT_RST" ]; then
        echo
        echo ".. CODE-BLOCK:: bash"
        echo
    fi
    echo "${PROMPT}$1"
    if [ -n "$OUTPUT_RST" ]; then
        echo
    fi
}

function print_comment()
{
    echo "${COMMENT}$1"
}

case $system:$command in
    homebrew*:setup-build-env)
        $IF_VERBOSE echo "${COMMENT}"
        $IF_VERBOSE echo "${COMMENT}Homebrew can issue suggestions regarding keg-only packages."
        $IF_VERBOSE echo "${COMMENT}The following command is to automatically apply these suggestions"
        $IF_VERBOSE echo "${COMMENT}for packages relevant for Sage to make them available for the build."
        $IF_VERBOSE echo "${COMMENT}Run it once to apply the suggestions for the current session."
        $IF_VERBOSE echo "${COMMENT}Add it to your shell profile to apply them for all future sessions."
        $IF_VERBOSE echo "${COMMENT}"
        [ -n "$SAGE_ROOT" ] || SAGE_ROOT=.
        echo "${PROMPT}source $SAGE_ROOT/.homebrew-build-env"
        ;;
    *:setup-build-env)
        # Nothing needed
        ;;
    #
    # Verbs handled above are our own inventions. Verbs handled below are apt-get verbs.
    #
    @(debian*|ubuntu*):update)
        print_shell_command "${SUDO}apt-get $command $system_packages"
        ;;
    @(debian*|ubuntu*):*)
        [ "$NO_INSTALL_RECOMMENDS" = yes ] && options="$options --no-install-recommends"
        [ "$YES" = yes ] && options="$options --yes" env="DEBIAN_FRONTEND=noninteractive "
        [ -n "$system_packages" ] && print_shell_command "${SUDO}${env}apt-get $command $options $system_packages"
        ;;
    @(fedora*|redhat*|centos*):install)
        [ "$YES" = yes ] && options="$options -y"
        [ -n "$system_packages" ] && print_shell_command "${SUDO}yum install $options $system_packages"
        ;;
    gentoo*:install)
        [ -n "$system_packages" ] && print_shell_command "${SUDO}emerge $system_packages"
        ;;
    arch*:update)
        print_shell_command "${SUDO}pacman -Sy"
        ;;
    arch*:install)
        [ "$YES" = yes ] && options="$options --noconfirm"
        [ -n "$system_packages" ] && print_shell_command "${SUDO}pacman -S $options $system_packages"
        ;;
    void*:update)
        print_shell_command "${SUDO}xbps-install -Su"
        ;;
    void*:install)
        [ "$YES" = yes ] && options="$options --yes"
        [ -n "$system_packages" ] && print_shell_command "${SUDO}xbps-install $options $system_packages"
        ;;
    opensuse*:install)
        [ -n "$system_packages" ] && print_shell_command "${SUDO}zypper install $system_packages"
        ;;
    *conda*:install)
        [ "$YES" = yes ] && options="$options --yes"
        [ -n "$system_packages" ] && print_shell_command "conda install $system_packages"
        ;;
    homebrew*:install)
        [ -n "$system_packages" ] && print_shell_command "brew install $system_packages"
        ;;
    slackware*:install)
        [ -n "$system_packages" ] && print_shell_command "${SUDO}slackpkg install $system_packages"
        ;;
    cygwin*:update)
        print_comment "first install apt-cyg from https://github.com/transcode-open/apt-cyg"
        ;;
    cygwin*:install)
        [ -n "$system_packages" ] && print_shell_command "apt-cyg install $system_packages"
        ;;
    freebsd*:install)
        [ -n "$system_packages" ] && print_shell_command "${SUDO}pkg install $system_packages"
        ;;
    nix*:install)
        [ -n "$system_packages" ] && print_shell_command "nix-env --install $system_packages"
        ;;
    pip:install)
        [ -n "$system_packages" ] && print_shell_command "sage -pip install $system_packages"
        ;;
    cpan:install)
        [ -n "$system_packages" ] && print_shell_command "cpan -i $system_packages"
        ;;
    repology:install)
        if [ -n "$system_packages" ]; then
            links=""
            for pkg in $system_packages; do
                link="https://repology.org/project/$pkg/versions"
                if [ -n "$links" ]; then
                    links="$links, "
                fi
                links="$links$link"
            done
            print_comment "See ${links}"
        fi
        ;;
    *:update)
        # Nothing needed
        ;;
    *)
        print_comment "$command the following packages: $system_packages"
        ;;
esac
