- job:
    name: 96boards-hikey960-aosp-master
    project-type: freestyle
    defaults: global
    logrotate:
        daysToKeep: 60
        numToKeep: 60
    properties:
        - authorization:
            anonymous:
                - job-read
            everyone-flat:
                - job-build
                - job-cancel
    parameters:
        - string:
            name: BUILD_CONFIG_FILENAME
            default: 'hikey960-aosp-master'
    disabled: true
    node: docker-focal-aosp
    display-name: 'DELETE ME'
    concurrent: true
    triggers:
        - timed : '@daily'
    wrappers:
        - timestamps
        - timeout:
            timeout: 500
        - ssh-agent-credentials:
                users:
                    - '768c1ba1-6052-4b97-a531-7e4fabda6979'
        # needed to publish to snapshots
        - credentials-binding:
            - text:
                credential-id: snapshots-api-key
                variable: PUBLISH_KEY
    builders:
        - shell: |
            #!/bin/bash
            export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8

            sudo apt-get update

            # Here is for packages not listed by the source.android.com site below.
            # But the following packages might not be really necessary,
            # as the docker image for docker-focal-aosp labelled nodes is linaro/jenkins-amd64-ubuntu:focal
            # which is defined here: https://git.linaro.org/infrastructure/ci-yadp-builder.git/tree/docker_templates_aosp_amd64.yml#n2
            # And the packages listed here might be already defined there.
            sudo apt-get install -y --allow-change-held-packages wget sudo python3 python-is-python3 openjdk-11-jdk-headless git gperf python-mako time genisoimage patch mtools
            java -version

            ## https://source.android.com/docs/setup/start/requirements#hardware-requirements
            sudo apt-get install -y --allow-change-held-packages git-core gnupg flex bison build-essential zip curl zlib1g-dev libc6-dev-i386 libncurses5 x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig

            mkdir -p ${HOME}/bin ${WORKSPACE}/build/out
            curl https://storage.googleapis.com/git-repo-downloads/repo > ${HOME}/bin/repo
            wget https://git.linaro.org/ci/publishing-api.git/blob_plain/HEAD:/linaro-cp.py -O ${HOME}/bin/linaro-cp.py
            chmod a+x ${HOME}/bin/*
            export PATH=${HOME}/bin:${PATH}

            if [ ! -d "/home/buildslave/srv/aosp-master" ]; then
              sudo mkdir -p /home/buildslave/srv/aosp-master
              sudo chmod 777 /home/buildslave/srv/aosp-master
            fi
            cd /home/buildslave/srv/aosp-master

            git config --global user.email "ci_notify@linaro.org"
            git config --global user.name "Linaro CI"

            # Runs as ubuntu
            set -xe
            export BUILD_CONFIG_FILENAME=${BUILD_CONFIG_FILENAME:-${JOB_NAME#android-*}}

            # Download vendor binaries for hikey.
            mkdir -p build
            # Delete sources before build to save space
            cd build/
            rm -rf art/ dalvik/ kernel/ bionic/ developers/ libcore/ sdk/ bootable/ development/ libnativehelper/ system/ build/ device/ test/ build-info/ docs/ packages/ toolchain/ .ccache/ external/ pdk/ tools/ compatibility/ frameworks/ platform_testing/ vendor/ cts/ hardware/ prebuilts/ linaro* clang-src hikey-linaro/configs
            rm -fr android-build-configs android-patchsets
            # to build with clean environment, and it helps make sure
            # there will be only one pinned-manifest under out/pinned-manifest/
            rm -fr out
            git clone --depth 1 http://android-git.linaro.org/git/android-build-configs.git android-build-configs
            ./android-build-configs/linaro-build.sh -c ${BUILD_CONFIG_FILENAME}
            cp -a out/pinned-manifest/*-pinned-manifest.xml "${WORKSPACE}/pinned-manifest.xml"
            cp -a out/target/product/hikey960/*.json ${WORKSPACE}/

            source build/envsetup.sh
            lunch hikey960-trunk_staging-userdebug
            sed -i "s/eng.\`whoami\`/${BUILD_NUMBER}/g" device/linaro/hikey/factory-images/generate-factory-images-hikey960.sh
            sed -i "s/target\/product\/hikey960//g" device/linaro/hikey/factory-images/generate-factory-images-hikey960.sh
            cd -
            cd /home/buildslave/srv/aosp-master/build/out/dist/
            set +xe
            . ../../device/linaro/hikey/factory-images/generate-factory-images-package.sh
            cp /home/buildslave/srv/aosp-master/build/out/*.xml  /home/buildslave/srv/aosp-master/build/out/dist/
            rm -rf pub/
            mkdir pub/
            cp *.zip *.xml pub/
            ## the size of hikey960-symbols-*.zip is bigger than 5GB
            ## drop it for now until there is a new solution
            rm -f pub/hikey960-symbols-*.zip
            cd -
            cd /home/buildslave/srv/aosp-master/build/device/linaro/hikey/; git reset --hard; cd -;

            pushd /home/buildslave/srv/aosp-master/build/out/
            for img in "boot.img dt.img vendor.img system.img userdata.img super.img"; do
              xz -9 $img
            done
            cp ramdisk.img *.img.xz dist/pub/
            cp -a pinned-manifest/*-pinned-manifest.xml "dist/pub/pinned-manifest.xml"
            popd

            wget https://git.linaro.org/ci/job/configs.git/blob_plain/HEAD:/android-lcr/hikey/build-info/aosp-master-template.txt -O build/out/dist/pub/BUILD-INFO.txt

            # Delete sources after build to save space
            cd build
            rm -rf art/ dalvik/ kernel/ bionic/ developers/ libcore/ sdk/ bootable/ development/ libnativehelper/ system/ build/ device/ test/ build-info/ docs/ packages/ toolchain/ .ccache/ external/ pdk/ tools/ compatibility/ frameworks/ platform_testing/ vendor/ cts/ hardware/ prebuilts/ linaro*
            cd -

            # Publish parameters
            cat << EOF > ${WORKSPACE}/publish_parameters
            PUB_DEST=96boards/hikey960/linaro/aosp-master/${BUILD_NUMBER}
            PUB_SRC=${PWD}/build/out/dist/pub
            PUB_EXTRA_INC=^[^/]+zip
            LINK_LATEST=false
            EOF
        - inject:
            properties-file: publish_parameters
        - linaro-publish-token
        - shell:
            !include-raw:
                - android/linaro-publisher.sh
    publishers:
        - archive:
            artifacts: '*.xml,*.json'
            latest-only: true
        - logparser:
            parse-rules: '/var/jenkins_home/userContent/android.parse'
            unstable-on-warning: false
            fail-on-error: false
        - fingerprint:
            files: 'build/fingerprints/*'
        - email:
            recipients: 'vishal.bhoj@linaro.org john.stultz@linaro.org, yongqin.liu@linaro.org'
