name: Upload binaries to release

on:
  release:
    types:
      - created
      - published
      - edited

jobs:
  Ubuntu:
    name: Build for Ubuntu 20.04
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v2
    - name: prepare
      run: |
        sudo apt-get update
        sudo apt-get install -y autoconf automake autopoint autotools-dev \
                                gettext libdbus-1-dev pkg-config xmlto \
                                devscripts build-essential lintian \
                                debhelper-compat \
                                dh-exec libdbus-1-dev \
                                libmagick++-dev libv4l-dev python3-dev \
                                libgtk-3-dev lftp \
                                dh-sequence-python3 libgtk2.0-dev \
                                libqt5x11extras5-dev qtbase5-dev
    - name: build
      run: |
        .github/workflows/debuilder.sh
    - name: generating tarball
      run: |
        OS_VERSION=$(. /etc/os-release && echo $ID-$VERSION_ID)
        tar cvfz ${DIR}/zbar-${OS_VERSION}.tar.gz *.deb
        echo "OS_VERSION=$OS_VERSION" >> $GITHUB_ENV

    - name: upload
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      uses: actions/upload-release-asset@v1
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: ./zbar-${OS_VERSION}.tar.gz
        asset_name: zbar-${OS_VERSION}.tar.gz
        asset_content_type: application/gzip

  Mac_OS:
    name: Build for Mac OS
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v2
    - name: prepare
      run: |
        brew install gettext autoconf automake libiconv libtool \
                    gs graphicsmagick python xmlto pkg-config ccache

        brew unlink libtool && brew link libtool
        brew unlink gettext && brew link gettext
    - name: configure
      run:
        autoreconf -vfi

        ./configure --disable-video --disable-nls --with-python=python3

    - name: build
      run: |
        make
        DESTDIR=${PWD}/macos make install
    - name: generating tarball
      run: |
        DIR="$PWD"
        tar c -C ${PWD}/macos -f ${DIR}/zbar-macos.tar.gz -z .
    - name: upload
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      uses: actions/upload-release-asset@v1
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: ./zbar-macos.tar.gz
        asset_name: zbar-macos.tar.gz
        asset_content_type: application/gzip

  Windows:
    name: Build for Windows
    runs-on: windows-latest
    strategy:
      matrix:
        arch: [x86_64, i686]
        video: [VfW, DShow]
        include:
          - arch: x86_64
            msystem: MINGW64
            grep: x86-64
          - arch: i686
            msystem: MINGW32
            grep: 386
          - video: DShow
            extra: --with-directshow
          - video: VfW
            extra:
    defaults:
      run:
        shell: msys2 {0}

    env:
      CPPFLAGS: -D__USE_MINGW_ANSI_STDIO=1

    steps:
    - uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.msystem }}
        update: false
        install: >-
          mingw-w64-${{ matrix.arch }}-gcc
          mingw-w64-${{ matrix.arch }}-iconv
          mingw-w64-${{ matrix.arch }}-imagemagick
          mingw-w64-${{ matrix.arch }}-python
          base-devel git xmlto zip
          autoconf libtool automake gettext make autoconf-archive pkg-config

    - uses: actions/checkout@v2

    - name: configure
      shell: msys2 {0}
      run: |
        autoreconf -vfi
        ./configure LDFLAGS="-static" \
                    --enable-pthread --disable-dependency-tracking \
                    ${{ matrix.extra }}

    - name: build
      shell: msys2 {0}
      run:
        make
    - name: generating zip
      shell: msys2 {0}
      run: |
        DESTDIR=${PWD}/win_${{ matrix.arch }}-${{ matrix.video }} make install
        (cd ${PWD}/win_${{ matrix.arch }}-${{ matrix.video }}; zip -r ../zbar-win_${{ matrix.arch }}-${{ matrix.video }}.zip .)

    - name: upload
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      uses: actions/upload-release-asset@v1
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: ./zbar-win_${{ matrix.arch }}-${{ matrix.video }}.zip
        asset_name: zbar-win_${{ matrix.arch }}-${{ matrix.video }}.zip
        asset_content_type: application/gzip
