name: CI

on: [push, pull_request]

jobs:
  build:
    name: Native Build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        tag: [bionic, centos7]
    container: infactum/tg2sip-builder:${{ matrix.tag }}
      
    steps:
    - uses: actions/checkout@v2
    
    - name: Build
      run: |
        [ -d "/opt/rh" ] && source /opt/rh/devtoolset-7/enable
        mkdir build && cd build
        export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
        cmake -DCMAKE_BUILD_TYPE=Release ..
        cmake --build . -j $(grep -c ^processor /proc/cpuinfo);
        
    - uses: actions/upload-artifact@v2
      with:
        name: tg2sip_${{ matrix.tag }}
        path: |
          build/tg2sip
          build/gen_db
          build/settings.ini
    
    # AppImage generation cannot be fully done inside container
    # because of FUSE requirements
    - name: Prepare AppImage
      if: matrix.tag == 'centos7'
      run: |
        linuxdeploy --appdir tg2sip -e build/tg2sip
        linuxdeploy --appdir gen_db -e build/gen_db
        
    - uses: actions/upload-artifact@v2
      if: matrix.tag == 'centos7'
      with:
        name: tmp
        path: |
          tg2sip/
          gen_db/

  appimage:
    name: Pack AppImage
    needs: [build]
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v2
      
    - uses: actions/download-artifact@v2
      with:
        name: tmp
        path: tmp
        
    - name: Prepare
      run: |
        wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage -O appimagetool.AppImage
        chmod +x appimagetool.AppImage
        wget https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage -O linuxdeploy.AppImage
        chmod +x linuxdeploy.AppImage
        
    - name: Pack
      run: |
        ./linuxdeploy.AppImage --appdir tmp/tg2sip -d buildenv/tg2sip.desktop -i buildenv/icon.svg
        ./linuxdeploy.AppImage --appdir tmp/gen_db -d buildenv/gen_db.desktop -i buildenv/icon.svg
        chmod +x tmp/tg2sip/usr/bin/tg2sip
        chmod +x tmp/gen_db/usr/bin/gen_db
        ./appimagetool.AppImage tmp/tg2sip
        ./appimagetool.AppImage tmp/gen_db
    
    - uses: actions/upload-artifact@v2
      with:
        name: tg2sip_universal
        path: |
          tg2sip-x86_64.AppImage
          gen_db-x86_64.AppImage
          settings.ini;
