#!/bin/bash

. /dbld/functions.sh
DBLD_DIR=/dbld

YUM_INSTALL="yum install -y"
DNF_INSTALL="dnf install -y"
APT_INSTALL="apt-get install -y --no-install-recommends"

set -e
set -x

# this function is run first and is responsible for installing stuff that is
# needed by this script _before_ installing packages from packages.manifest.
#
# NOTE: If at all possible don't put anything here.
function install_dbld_dependencies()
{
    case "${OS_PLATFORM}" in
        centos-6)
            $YUM_INSTALL wget
            wget -O /usr/bin/yum-builddep https://raw.githubusercontent.com/rpm-software-management/yum-utils/master/yum-builddep.py
            chmod +x /usr/bin/yum-builddep
            sha1sum -c /dbld/images/yum-builddep.sha1
            ;;
        centos-*)
            $YUM_INSTALL wget yum-utils
            ;;
        debian-*|ubuntu-*)
            apt-get update
            $APT_INSTALL wget gnupg2
            if [ "${OS_PLATFORM}" = "ubuntu-xenial" ]; then
                $APT_INSTALL -t xenial-backports debhelper dh-systemd
            fi
            ;;
        fedora-*)
            ;;
    esac
}

function install_cmake() {
    CMAKE_VERSION=3.12.2
    CMAKE_SHORT_VERSION=$(echo ${CMAKE_VERSION} | cut -d"." -f1-2)
    download_target "https://cmake.org/files/v${CMAKE_SHORT_VERSION}/cmake-${CMAKE_VERSION}-Linux-x86_64.sh" /tmp/cmake.sh
    chmod +x /tmp/cmake.sh
    mkdir -p /opt/cmake
    /tmp/cmake.sh --skip-license --prefix=/opt/cmake/
    ln -s /opt/cmake/bin/cmake /usr/bin/cmake
    rm -rf /tmp/cmake.sh
}

function install_criterion() {
    CRITERION_VERSION=2.3.3

    download_target "https://github.com/Snaipe/Criterion/releases/download/v${CRITERION_VERSION}/criterion-v${CRITERION_VERSION}.tar.bz2" /tmp/criterion.tar.bz2
    cd /tmp/
    tar xvf /tmp/criterion.tar.bz2
    cd /tmp/criterion-v${CRITERION_VERSION}
    cmake -DCMAKE_INSTALL_PREFIX=/usr .
    make install
    ldconfig
    rm -rf /tmp/criterion.tar.bz2 /tmp/criterion-v${CRITERION_VERSION}

}

function install_gosu() {
    GOSU_VERSION=1.10
    ARCHITECTURE=$1
    cat $DBLD_DIR/images/gosu.pubkey | gpg --import
    download_target "https://github.com/tianon/gosu/releases/download/${GOSU_VERSION}/gosu-${ARCHITECTURE}" /usr/local/bin/gosu
    download_target "https://github.com/tianon/gosu/releases/download/${GOSU_VERSION}/gosu-${ARCHITECTURE}.asc" /usr/local/bin/gosu.asc
    gpg --verify /usr/local/bin/gosu.asc
    rm /usr/local/bin/gosu.asc
    chmod +x /usr/local/bin/gosu
}

function install_gradle {
    GRADLE_VERSION=4.10
    download_target "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip" /tmp/gradle.zip
    mkdir -p /opt/gradle
    unzip -d /opt/gradle /tmp/gradle.zip
    rm -rf /tmp/gradle.zip
    ln -s /opt/gradle/gradle-${GRADLE_VERSION}/bin/gradle /usr/bin/gradle
    find / -name 'libjvm.so' | sed 's@/libjvm.so@@g' | tee --append /etc/ld.so.conf.d/openjdk-libjvm.conf
    ldconfig
}

function install_pip2 {
    download_target "https://bootstrap.pypa.io/get-pip.py" get-pip.py
    python2 get-pip.py
}

function download_target() {
    target=$1
    output=$2
    wget --no-check-certificate $target --output-document=$output
}

function filter_packages_by_platform {
    FILENAME=$1
    OS_GROUP=$(echo ${OS_PLATFORM} | cut -d"-" -f1)
    grep -v "#" ${FILENAME} | grep -e "${OS_PLATFORM}" -e "${OS_GROUP}[^-]" | cut -d"[" -f1
}

function add_obs_repo {
    PLATFORM=$1
    echo "deb http://download.opensuse.org/repositories/home:/laszlo_budai:/syslog-ng/x${PLATFORM} ./" | tee /etc/apt/sources.list.d/lbudai.list
    cat | tee /etc/apt/preferences.d/lbudai <<EOF
Package: *
Pin: origin "download.opensuse.org"
Pin-Priority: 1
EOF
    wget -qO - http://download.opensuse.org/repositories/home:/laszlo_budai:/syslog-ng/x${PLATFORM}/Release.key | apt-key add -
    apt-get update
}

function add_copr_repo {

    # NOTE: we are removing dnf/yum plugins after enabling copr as they
    # install a couple of Python dependencies which then conflict with our
    # PIP installation later.
    case "${OS_PLATFORM}" in
        centos-*)
            $YUM_INSTALL yum-plugin-copr
            yum copr enable -y czanik/syslog-ng-githead
            ;;
        fedora-*)
            $DNF_INSTALL -y dnf-plugins-core
            dnf copr enable -y czanik/syslog-ng-githead
            ;;
    esac
}

function add_epel_repo {
    $YUM_INSTALL epel-release
}

function install_apt_packages {
    apt-get update -qq -o Acquire::CompressionTypes::Order::=gz
    filter_packages_by_platform $DBLD_DIR/packages.manifest | xargs -t $APT_INSTALL --yes
}

function install_debian_build_deps {
    DEBIAN_CONTROL_FILE=$DBLD_DIR/extra-files/packaging-debian-control
    if ! [ -f ${DEBIAN_CONTROL_FILE} ]; then
        echo "install_debian_build_deps() called from dockerfile but without a Debian control file, make sure that control file is copied over to ${DEBIAN_CONTROL_FILE} by the prepare step"
        exit 1
    fi
    deb_run_build_command mk-build-deps -i ${DEBIAN_CONTROL_FILE} -t "apt-get -y -o Debug::pkgProblemResolver=yes --no-install-recommends"
}

function install_rpm_build_deps {
    RPM_SPEC_FILE=$DBLD_DIR/extra-files/syslog-ng.spec
    if ! [ -f ${RPM_SPEC_FILE} ]; then
        echo "install_rpm_build_deps() called from dockerfile but without a syslog-ng.spec file, make sure that control file is copied over to ${RPM_SPEC_FILE} by the prepare step"
        exit 1
    fi

    case "${OS_PLATFORM}" in
        centos-*)
            rpm_run_build_command yum-builddep -y ${RPM_SPEC_FILE}
            ;;
        fedora-*)
            rpm_run_build_command dnf builddep -y ${RPM_SPEC_FILE}
            ;;
    esac
}

function install_yum_packages {
    $YUM_INSTALL findutils
    filter_packages_by_platform $DBLD_DIR/packages.manifest | xargs $YUM_INSTALL
}

function install_pip2_packages {
    python2 -m pip install --upgrade pip
    filter_packages_by_platform $DBLD_DIR/pip_packages.manifest | xargs python2 -m pip install --ignore-installed -U
}

function install_pip3_packages {
    python3 -m pip install --upgrade pip
    filter_packages_by_platform $DBLD_DIR/pip_packages.manifest | xargs python3 -m pip install --ignore-installed -U
}

function install_pip_packages {
    case "${OS_PLATFORM}" in
        centos-6)
            pip install --upgrade pip==9.0.3
            ;;
        centos-7)
            python -m pip install --upgrade pip==9.0.3
            ;;
        ubuntu-focal)
            pip3 install --upgrade pip
            ;;
        *)
            python -m pip install --upgrade pip
            ;;
    esac
    filter_packages_by_platform $DBLD_DIR/pip_packages.manifest | xargs pip install --ignore-installed -U
}

function install_lsb_release {
    apt-get update && $APT_INSTALL lsb-release
}

function enable_dbgsyms_on_ubuntu {
    install_lsb_release
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse" | tee -a /etc/apt/sources.list.d/ddebs.list
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs)-updates main restricted universe multiverse" | tee -a /etc/apt/sources.list.d/ddebs.list
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 428D7C01 C8CAB6595FDFF622
}

function install_perf {
    apt-cache search linux-tools | grep 'linux-tools-.*-generic' | cut -d" " -f1 | tail -n1 | cut -d"-" -f1-4 | xargs $APT_INSTALL
}


# DO NOT REMOVE!
"$@"
