{#
Copyright (C) 2010-2022 Gentoo tatt project
https://gitweb.gentoo.org/proj/tatt.git/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
#}
#!/bin/bash

main() {
    trap "echo 'signal captured, exiting the entire script...'; exit" SIGHUP SIGINT SIGTERM
    echo -e "USE tests started on $(date)\n" >> "{{ report_file }}"

    local test_ret=0

    {% for atom, is_test, use_flags in jobs %}
    {% if is_test %}
    TUSE="{{ use_flags }}" tatt_test_pkg '{{ atom }}' --test || test_ret=1
    {% else %}
    TUSE="{{ use_flags }}" tatt_test_pkg '{{ atom }}' || test_ret=1
    {% endif %}
    {% endfor %}

    exit ${test_ret}
}

cleanup() {
    echo "Cleaning up"
    {% for file in cleanup_files %}
    rm -v -f -r '{{ file }}'
    {% endfor %}
    rm -v -f $0
}

tatt_pkg_error() {
    local eout=${2}

    echo "${eout}"

    if [[ -n ${TUSE} ]]; then
        echo -n "USE='${TUSE}'" >> "{{ report_file }}"
    fi
    if [[ -n ${FEATURES} ]]; then
        echo -n " FEATURES='${FEATURES}'" >> "{{ report_file }}"
    fi

    if [[ ${eout} =~ REQUIRED_USE ]] ; then
        echo " : REQUIRED_USE not satisfied (probably) for ${1:?}" >> "{{ report_file }}"
    elif [[ ${eout} =~ USE\ changes ]] ; then
        echo " : USE dependencies not satisfied (probably) for ${1:?}" >> "{{ report_file }}"
    elif [[ ${eout} =~ keyword\ changes ]]; then
        echo " : unkeyworded dependencies (probably) for ${1:?}" >> "{{ report_file }}"
    elif [[ ${eout} =~ Error:\ circular\ dependencies: ]]; then
        echo " : circular dependencies (probably) for ${1:?}" >> "{{ report_file }}"
    elif [[ ${eout} =~ Blocked\ Packages ]]; then
        echo " : blocked packages (probably) for ${1:?}" >> "{{ report_file }}"
    else
        echo " failed for ${1:?}" >> "{{ report_file }}"
    fi

    local CP=${1#=}
    local BUILDDIR=/var/tmp/portage/${CP}
    local BUILDLOG=${BUILDDIR}/temp/build.log
    if [[ -s ${BUILDLOG} ]]; then
        mkdir -p {{ log_dir }}
        local LOGNAME=$(mktemp -p {{ log_dir }} "${CP/\//_}_use_XXXXX")
        cp "${BUILDLOG}" "${LOGNAME}"
        echo "    log has been saved as ${LOGNAME}" >> "{{ report_file }}"
        TESTLOGS=($(find ${BUILDDIR}/work -iname '*test*log*'))
        if [[ {{ "${#TESTLOGS[@]}" }} -gt 0 ]]; then
            tar cf ${LOGNAME}.tar ${TESTLOGS[@]}
            echo "    test-suite logs have been saved as ${LOGNAME}.tar" >> "{{ report_file }}"
        fi
    fi
}

tatt_test_pkg() {
    local CP=${1#=}
    CP=${CP/\//_}

    if [[ ${2} == "--test" ]]; then
        # Do a first pass to avoid circular dependencies
        # --onlydeps should mean we're avoiding (too much) duplicate work
        USE="minimal -doc" emerge --onlydeps -q1 --with-test-deps {{ emerge_opts }} "${1:?}"

        if ! emerge --onlydeps -q1 --with-test-deps {{ emerge_opts }} "${1:?}"; then
            echo "merging test dependencies of ${1} failed" >> "{{ report_file }}"
            return 1
        fi
        printf "%s pkgdev_tatt_{{ job_name }}_test\n" "${1:?}"> "/etc/portage/package.env/pkgdev_tatt_{{ job_name }}/${CP}"
        local TFEATURES="${FEATURES} test"
    else
        printf "%s pkgdev_tatt_{{ job_name }}_no_test\n" "${1:?}" > "/etc/portage/package.env/pkgdev_tatt_{{ job_name }}/${CP}"
        local TFEATURES="${FEATURES}"
    fi
    {% for env in extra_env_files %}
    printf "%s {{env}}\n" "${1}" >> "/etc/portage/package.env/pkgdev_tatt_{{ job_name }}/${CP}"
    {% endfor %}

    printf "%s %s\n" "${1:?}" "${TUSE}" > "/etc/portage/package.use/pkgdev_tatt_{{ job_name }}/${CP}"

    # --usepkg-exclude needs the package name, so let's extract it
    # from the atom we have
    local name=$(portageq pquery "${1:?}" -n)

    eout=$( emerge -1 --getbinpkg=n --usepkg-exclude="${name}" {{ emerge_opts }} "${1:?}" 2>&1 1>/dev/tty )
    local RES=$?

    rm -v -f /etc/portage/package.{env,use}/pkgdev_tatt_{{ job_name }}/${CP}

    if [[ ${RES} == 0 ]] ; then
        if [[ -n ${TFEATURES} ]]; then
            echo -n "FEATURES='${TFEATURES}' " >> "{{ report_file }}"
        fi
        echo "USE='${TUSE}' succeeded for ${1:?}" >> "{{ report_file }}"
    else
        FEATURES="${TFEATURES}" tatt_pkg_error "${1:?}" "${eout}"
        return 1
    fi
}

if [[ ${1} == "--clean" ]]; then
    cleanup
else
    main
fi
