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: -j 1
    # https://wiki.debian.org/CrossCompiling#Building_with_dpkg-buildpackage
    BUILD_ARCH: amd64 armhf # amd64 arm64 armel armhf i386 mips mips64el mipsel powerpc ppc64el s390x
  commands:
    # DEB_BUILD_ARCH DEB_HOST_ARCH TARGET=armhf
    # 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 
    - PKG_NAME="${DRONE_REPO_NAME}" &&
      PKG_VERSION="$(git describe --always)" &&
      PKG_BUILDHOST_ARCH="amd64" &&
      PKG_SRC_ROOT="$(pwd)" &&
      PKG_RELEASE_DIR="$${PKG_SRC_ROOT}/release" &&
      PKG_DIST_DIR="dist" &&
      mkdir -p "$${PKG_RELEASE_DIR}" &&
      PKG_DEB_ARCHIVE="$${PKG_NAME}_$${PKG_VERSION}.orig.tar.xz" &&
      PKG_DEB_ARCHIVE_ROOT="$${PKG_NAME}-$${PKG_VERSION}" &&
      tar --exclude='./.*' --exclude='debian' --exclude="$${PKG_RELEASE_DIR}"
        --exclude="$${PKG_DEB_ARCHIVE}" 
        --transform "s/\./$${PKG_DEB_ARCHIVE_ROOT}/" 
        -cJf "$${PKG_RELEASE_DIR}/$${PKG_DEB_ARCHIVE}" . &&
      for PKG_ARCH in $${BUILD_ARCH} ; do cd "$${PKG_SRC_ROOT}" && 
        echo " ░▒▓█ BUILDING FOR $${PKG_ARCH} █▓▒░ " &&
        PKG_RELEASE_ARCHIVE="$${PKG_NAME}_$${PKG_VERSION}_$${PKG_ARCH}.tar.xz" &&
        PKG_RELEASE_ARCHIVE_ROOT="$${PKG_NAME}-$${PKG_VERSION}" &&
        PKG_BUILD_DIR="$${PKG_SRC_ROOT}/build/$${PKG_ARCH}/$${PKG_RELEASE_ARCHIVE_ROOT}" &&
        echo "PKG_BUILD_DIR=$${PKG_BUILD_DIR}" &&
        mkdir -p "$${PKG_BUILD_DIR}" &&
        tar -xf "$${PKG_RELEASE_DIR}/$${PKG_DEB_ARCHIVE}" -C "$${PKG_BUILD_DIR}" --strip 1 &&
        cd "$${PKG_BUILD_DIR}" &&
        case "$${PKG_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-"         ;;
        esac &&
        if test "$${PKG_ARCH}" = "$${PKG_BUILDHOST_ARCH}" ; then PREFIX="" ; fi &&
        export CC="$${PREFIX}gcc" &&
        export LD="$${PREFIX}ld" &&
        export STRIP="$${PREFIX}strip" &&
        export MAKEFLAGS="$${MAKEFLAGS}" &&
        if test -d "debian" ; then
          echo apt-get build-dep -a $${PKG_ARCH} . &&
          CONFIG_SITE=/etc/dpkg-cross/cross-config.$${PKG_ARCH}
          DEB_BUILD_OPTIONS=nocheck
          dpkg-buildpackage -us -uc -a ${PKG_ARCH} &&
          find .. -type f -exec cp {} $${PKG_RELEASE_DIR}/ \; ;
        else
          make &&
          tar --exclude='./.*'
            --transform "s/\./$${PKG_RELEASE_ARCHIVE_ROOT}/"
            -C "$${PKG_DIST_DIR}"
            -cJf "$${PKG_RELEASE_DIR}/$${PKG_RELEASE_ARCHIVE}" . &&
          make clean ;
        fi ;
      done &&
      ls -la "$${PKG_RELEASE_DIR}" &&
      find "$${PKG_RELEASE_DIR}" -type f -exec md5sum '{}' \;
    - exit 0 ; ░▒▓█ BUILD END █▓▒░


#        tar --exclude='./.*'
#          --transform "s/$${PKG_DIST_DIR}/$${PKG_RELEASE_ARCHIVE_ROOT}/"
#          -cJvf "$${PKG_RELEASE_DIR}/$${PKG_RELEASE_ARCHIVE}" "${PKG_DIST_DIR}" &&
      

#    - export MAKEFLAGS && make
#    - CONTENT=${DRONE_REPO_NAME} ;
#      test -n "${DRONE_TAG}" && 
#      RELEASE=${DRONE_REPO_NAME}-${DRONE_TAG}-bin || 
#      RELEASE=${DRONE_REPO_NAME}-bin
#    - cp -rp dist/. $CONTENT/ ; 
#      ls -la $CONTENT
#    - find $CONTENT -type f -exec md5sum '{}' \;
#    - rm -rf release ; 
#      mkdir release
#    # - zip -rpq release/$RELEASE.zip    $CONTENT
#    - tar -czf release/$RELEASE.tar.gz $CONTENT
#    # - tar -cJf release/$RELEASE.tar.xz $CONTENT
#    - make clean ; rm -rf $CONTENT ;
#      ls -la release
#    - find release -type f -exec md5sum '{}' \;


#    - for ARCH in ${BUILD_ARCH} ; do
#      apt-get build-dep -a $ARCH . ; 
#      done

#    - for ARCH in ${BUILD_ARCH} ; do
#      CONFIG_SITE=/etc/dpkg-cross/cross-config.$ARCH
#      DEB_BUILD_OPTIONS=nocheck 
#      dpkg-buildpackage -us -uc -a $ARCH ;
#      done
#    - 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/*
