kind: pipeline
name: clone-prepare-build-sign-release
# https://github.com/drone/drone

platform:
  os: linux
  arch: amd64

clone:
  disable: false
  depth: 50

steps:
- name: prepare
  image: plugins/docker
  # http://plugins.drone.io/drone-plugins/drone-docker/
  # https://github.com/drone-plugins/drone-docker
  when:
    event: [ tag ] # [ push, tag ]
  settings:
    dry_run: false
    registry: reg.unino.de
    insecure: false
    #auth: authtoken for registry
    username: { from_secret: reg-user }
    password: { from_secret: reg-pass }
    repo: reg.unino.de/develop/debian-${DRONE_REPO_NAMESPACE}-${DRONE_REPO_NAME}
    #mirror: registry-1.docker.io
    bip: 172.17.0.1/16 # set bridge if non-existent
    context: /drone/src # defaults to root of git repo
    #custom_dns: 127.0.0.11 # defaults to 8.8.8.8 and 8.8.4.4 
    storage_driver: overlay2 # aufs, overlay or vfs
    #debug: true
    #launch_debug: true
    target: builder # Dockerfile build target e.g. builder, debug, production
    dockerfile: Dockerfile
    auto_tag: false
    auto_tag_suffix: linux-amd64
    force_tag: true
    tags: [ latest, builder ]
    build_args: # custom args for docker build, e.g. additional BUILD_DEPS etc.
      - DEB_RELEASE=stretch
      - BUILD_DEPS=libhidapi-dev libssl-dev
      - BUILD_ARCH=armhf # amd64 arm64 armel armhf i386 mips mips64el mipsel powerpc ppc64el s390x

- name: build
  image: reg.unino.de/develop/debian-${DRONE_REPO_NAMESPACE}-${DRONE_REPO_NAME}:latest
  # https://readme.drone.io/user-guide/pipeline/
  # https://docs.drone.io/reference/environ/
  when:
    event: [ push, tag ]
  depends_on: [ clone, prepare ]
  environment:
    MAKEFLAGS: -j2
    # https://wiki.debian.org/CrossCompiling#Building_with_dpkg-buildpackage
    # https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
    # https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html#Catalogue-of-Rules
    # BINARIES for PREFIX: addr2line ar as c++filt cpp cpp-6 dwp elfedit g++ g++-6 gcc gcc-6 gcc-ar gcc-ar-6 gcc-nm gcc-nm-6 gcc-ranlib gcc-ranlib-6 gcov gcov-6 gcov-dump gcov-dump-6 gcov-tool gcov-tool-6 gprof ld ld.bfd ld.gold nm objcopy objdump ranlib readelf size strings strip 
    BUILD_ARCH: amd64 armhf # amd64 arm64 armel armhf i386 mips mips64el mipsel powerpc ppc64el s390x
  commands:
    - NAME="${DRONE_REPO_NAME}" ;
      VERSION="$(git describe --always)" ;
      if test -d "debian" ; then
        VERSION=$(dpkg-parsechangelog -SVersion | sed 's/-.*//') ;
      fi ;
      DESTDIR="dist" ;
      RELEASE_DIR="release" ;
      ARCHIVE_NAME="$${NAME}_$${VERSION}" ;
      ARCHIVE_ROOT="$${NAME}-$${VERSION}" ;
      SRC_DIR="$(pwd)" ;

    - mkdir -p "$${RELEASE_DIR}" &&
      tar --exclude='./.*' --exclude='debian' --exclude="$${RELEASE_DIR}"
        --transform "s/\./$${ARCHIVE_ROOT}/" 
        -cJf "$${SRC_DIR}/$${RELEASE_DIR}/$${ARCHIVE_NAME}.orig.tar.xz" .

    - for ARCH in $${BUILD_ARCH} ; do set -e ;
        echo " ░▒▓█ BUILDING for architecture \"$${ARCH}\" █▓▒░ " ;
        BUILD_DIR="$${SRC_DIR}/build/$${ARCH}/$${ARCHIVE_ROOT}" ;
        case "$${ARCH}" in 
          amd64)    PREFIX="x86_64-linux-gnu-"        ;;
          arm64)    PREFIX="aarch64-linux-gnu-"       ;;
          armel)    PREFIX="arm-linux-gnueabi-"       ;;
          armhf)    PREFIX="arm-linux-gnueabihf-"     ;;
          i386)     PREFIX="i686-linux-gnu-"          ;;
          mips)     PREFIX="mips-linux-gnu-"          ;;
          mips64el) PREFIX="mips64el-linux-gnuabi64-" ;;
          mipsel)   PREFIX="mipsel-linux-gnu-"        ;;
          powerpc)  PREFIX="powerpc-linux-gnu-"       ;;
          ppc64el)  PREFIX="powerpc64le-linux-gnu-"   ;;
          s390x)    PREFIX="s390x-linux-gnu-"         ;;
          *)        PREFIX=""                         ;;
        esac ;
        if test "$${ARCH}" = "$(dpkg --print-architecture)" ; then
          PREFIX="" ;
        else
          export CONFIG_SITE="/etc/dpkg-cross/cross-config.$${ARCH}" ;
        fi ;
        export CC="$${PREFIX}gcc" ;
        export CXX="$${PREFIX}g++" ;
        export LD="$${PREFIX}ld" ;
        export STRIP="$${PREFIX}strip" ;
        export MAKEFLAGS="$${MAKEFLAGS}" ;
        mkdir -p "$${BUILD_DIR}" ;
        cd "$${BUILD_DIR}" ;
        tar -xf "$${SRC_DIR}/$${RELEASE_DIR}/$${ARCHIVE_NAME}.orig.tar.xz"
          -C "$${BUILD_DIR}" --strip 1 ;
        cp -p "$${SRC_DIR}/$${RELEASE_DIR}/$${ARCHIVE_NAME}.orig.tar.xz"
              "$${BUILD_DIR}/../" ;
        if test -d "$${SRC_DIR}/debian" ; then
          cp -rp "$${SRC_DIR}/debian"
                 "$${BUILD_DIR}/" ;
          export DEBIAN_FRONTEND="noninteractive" ;
          export DEB_BUILD_OPTIONS="nocheck" ;
          apt-get -qy -o Dpkg::Use-Pty=0 build-dep -a $${ARCH} . ;
          dpkg-buildpackage -us -uc -a "$${ARCH}" &&
          cp ../*.tar.xz ../*.deb ../*.dsc "$${SRC_DIR}/$${RELEASE_DIR}/" &&
          ls -la .. ;
          make clean ;
        else
          make && 
          make install ;
          tar --exclude='./.*'
            --transform "s/\./$${ARCHIVE_ROOT}/"
            -C "$${BUILD_DIR}/$${DESTDIR}"
            -cJf "$${SRC_DIR}/$${RELEASE_DIR}/$${ARCHIVE_NAME}.$${ARCH}.tar.xz" . &&
          ls -la "$${SRC_DIR}/$${RELEASE_DIR}/$${ARCHIVE_NAME}.$${ARCH}.tar.xz" ;
          make clean ;
        fi ;
        cd "$${SRC_DIR}" ;
      done
    - ls -la "$${SRC_DIR}/$${RELEASE_DIR}" &&
      find "$${SRC_DIR}/$${RELEASE_DIR}" -type f -exec md5sum '{}' \;
    - exit 0 ; ░▒▓█ BUILD END █▓▒░


- name: sign
  image: plugins/gpgsign
  # http://plugins.drone.io/drone-plugins/drone-gpgsign/
  # https://github.com/drone-plugins/drone-gpgsign
  when:
    event: [ push, tag ]
  depends_on: [ build ]
  settings:
    key: { from_secret: gpg-key }
    passphrase: { from_secret: gpg-pass }
    detach_sign: true
    clear_sign: false
    files:
      - release/*
    excludes:
      - release/*.asc


# http://plugins.drone.io/drone-plugins/drone-gitea-release/
# https://github.com/drone-plugins/drone-gitea-release
- name: release
  image: plugins/gitea-release
  # http://plugins.drone.io/drone-plugins/drone-gitea-release/
  # https://github.com/drone-plugins/drone-gitea-release
  when:
    event: [ tag ]
  depends_on: [ sign ]
  settings:
    title: Release ${DRONE_REPO_NAME}-${DRONE_TAG} # file or string
    note: RELEASE.md # file or string
    draft: false
    prerelease: false
    base_url: https://git.unino.de
    insecure: false
    api_key: { from_secret: gitea-release }
    checksum: [ md5 ] # [ md5, sha1, sha256, sha512, adler32, crc32 ]
    file_exists: overwrite # overwrite, skip, fail
    files: # wildcard * matching files with prefix supported
      - release/*
