- job:
    name: tcwg-compare-results
    project-type: freestyle
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            everyone-flat:
                - job-build
                - job-cancel
                - job-workspace
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 300
    parameters:
        - string:
            name: ref_logs
            default: buildfarm/gcc-linaro-5-branch
            description: 'Reference log directory on $log_server'
        - string:
            name: new_logs
            default: buildfarm/gcc-linaro-5-branch
            description: 'Log directory on $log_server'
        - string:
            name: pass_thresh
            default: '0.95'
            description: 'Pass threshold, below which the comparison returns an error'
        - string:
            name: target_list
            default:
            description: 'List of targets to compare -- aarch64-linux-gnu armv8l-linux-gnueabihf arm-linux-gnueabi arm-linux-gnueabihf armeb-linux-gnueabihf aarch64-none-elf aarch64_be-none-elf tcwg-x86_64-build tcwg-x86_32-build tcwg-tk1_32-build tcwg-apm_64-build tcwg-apm_32-build'
        - string:
            name: log_server
            default: dev-01.tcwglab:/home/tcwg-buildslave/logs
            description: 'Log_Server'
        - bool:
            name: show_results
            default: true
            description: 'Show results in the console'
        - string:
            name: cmp_version
            default: refs/heads/master
            description: 'Comparison scripts version to use'
        - bool:
            name: dryrun
            default: false
            description: 'Dry-run, do nothing, just print steps'
    disabled: false
    node: tcwg-x86_64-dev-01-short
    retry-count: 3
    display-name: 'TCWG AAA Compare results'
    scm:
        - git:
            url: https://gitlab.com/LinaroLtd/tcwg/gcc-compare-results.git
            refspec: +refs/changes/*:refs/remotes/changes/*
            branches:
                - ${cmp_version}
            skip-tag: true
            shallow-clone: true
            wipe-workspace: true
    wrappers:
        - timeout:
            timeout: 60
        - timestamps
        - ssh-agent-credentials:
            # tcwg-buildslave user id
            users:
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
        - build-name:
            name: '#${BUILD_NUMBER}-${ENV,var="ref_logs"}-${ENV,var="new_logs"}'
    builders:
        - shell: |
            #!/bin/bash

            set -e
            set -x

            basedir="${log_server#*:}"
            log_server="${log_server%:*}"

            status=0

            dest="/tmp/CompareResults.$BASHPID"

            trap "ssh ${log_server} rm -rf ${dest}" EXIT HUP INT QUIT TRAP KILL PIPE TERM

            if ! $dryrun; then
                LOGSDIR=${WORKSPACE}/artifacts/logs

                mkdir -p ${LOGSDIR}

                ssh ${log_server} mkdir -p ${dest}
                scp ./compare_jobs.sh \
                    ./compare_tests \
                    ./compare_dg_tests.pl \
                    ./host-hw-dependent-tests.txt \
                    ./unstable-tests.txt ${log_server}:${dest} || status=1
                ssh ${log_server} bash ${dest}/compare_jobs.sh \
                    -pass-thresh ${pass_thresh} \
                    $basedir/$ref_logs $basedir/$new_logs ${target_list} || status=1

                # Copy the generated reports
                scp ${log_server}:${dest}/\*-diff-\*.txt ${LOGSDIR} || true
                scp ${log_server}:${dest}/\*.html report.css ${LOGSDIR} || true

                # Print results in console
                if $show_results; then
                    cat ${LOGSDIR}/*.txt
                fi
            else
                echo DRYRUN: compare_jobs.sh \
                     -pass-thresh ${pass_thresh} \
                     $basedir/$ref_logs $basedir/$new_logs ${target_list}
            fi

            exit $status
    publishers:
        - archive:
            artifacts: 'artifacts/logs/*.html,artifacts/logs/*.css,artifacts/logs/*.txt'
            latest-only: false
