name: CI build

on:
  push:
  release:
    types:
      - created

jobs:
  build-unix-native:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include: 
          - os: ubuntu-18.04
    steps:
    - uses: actions/checkout@v2
    - run: git fetch --prune --unshallow
    - name: Install Linux packages
      run: |
        sudo apt-get update
        sudo apt-get install libgl1-mesa-dev libxkbcommon-x11-0 libpcre2-16-0 qt5-default
    - name: Build
      run: |
        cd $GITHUB_WORKSPACE
        mkdir build
        cd build
        qmake ..
        make -j 2
        make check
  build-unix:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include: 
          - os: ubuntu-16.04
            QT_FILE: qtBase_5.15.1_xenial.zip
            LIBDE265_REMOTE: libde265.so
            LIBDE265_LOCAL: libde265-internals.so
            ARTIFACT_NAME: YUView.AppImage
          - os: ubuntu-18.04
            QT_FILE: qtBase_5.15.1_bionic.zip
            LIBDE265_REMOTE: libde265.so
            LIBDE265_LOCAL: libde265-internals.so
          - os: macos-10.15
            QT_FILE: qtBase_5.15.1_mac.zip
            LIBDE265_REMOTE: libde265.dylib
            LIBDE265_LOCAL: libde265-internals.dylib
            ARTIFACT_NAME: YUView-Mac.zip
          - os: macos-11.0
            QT_FILE: qtBase_5.15.1_mac.zip
            LIBDE265_REMOTE: libde265.dylib
            LIBDE265_LOCAL: libde265-internals.dylib
            ARTIFACT_NAME: YUView-Mac-BigSur.zip
    steps:
    - uses: actions/checkout@v2
    - run: git fetch --prune --unshallow
    - name: Install Qt base
      run: |
        cd ../../
        mkdir -p YUViewQt/YUViewQt
        cd YUViewQt/YUViewQt
        curl -L https://github.com/ChristianFeldmann/YUViewQt/releases/download/QtBase-5.15.1/${{matrix.QT_FILE}} -o Qt.zip
        unzip -qa Qt.zip
        echo "$GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qt/bin" >> $GITHUB_PATH
      shell: bash
    - name: Install MacDeployQT
      run: |
        cd $GITHUB_WORKSPACE/../../YUViewQt/YUViewQt
        curl -L https://github.com/ChristianFeldmann/YUViewQt/releases/download/QtDeployTools-5.15.1/qtTools_5.15.1_mac.zip -o deployQt.zip
        unzip -qa deployQt.zip
      shell: bash
      if: matrix.os == 'macos-10.15' || matrix.os == 'macos-11.0'
    - name: Install Macdeploy Qt
      run: |
        cp $GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qttools/bin/macdeployqt $GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qt/bin/macdeployqt
        strip $GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qt/bin/macdeployqt
      if: matrix.os == 'macos-10.15' || matrix.os == 'macos-11.0'
    - name: Install Linuxdeployqt
      run: |
        curl -L https://github.com/probonopd/linuxdeployqt/releases/download/continuous/linuxdeployqt-continuous-x86_64.AppImage -o linuxdeployqt-6-x86_64.AppImage
        chmod a+x linuxdeployqt-6-x86_64.AppImage
      if: matrix.os == 'ubuntu-16.04'
    - name: Install Linux packages
      run: |
        sudo apt-get update
        sudo apt-get install libgl1-mesa-dev libxkbcommon-x11-0 libpcre2-16-0 '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev libatspi2.0-dev
      if: matrix.os == 'ubuntu-16.04' || matrix.os == 'ubuntu-18.04'
    - name: Install Newer GCC Ubuntu 16.04
      run: |
        sudo add-apt-repository ppa:ubuntu-toolchain-r/test
        sudo apt-get update
        sudo apt-get install gcc-9 g++-9
        sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9
        sudo update-alternatives --config gcc
      if: matrix.os == 'ubuntu-16.04'
    - name: Install libde265
      run: |
        curl -L https://github.com/ChristianFeldmann/libde265/releases/download/v1.1/${{matrix.LIBDE265_REMOTE}} -o ${{matrix.LIBDE265_LOCAL}}
        curl -L https://raw.githubusercontent.com/ChristianFeldmann/libde265/master/COPYING -o libde265License.txt
      shell: bash
    - name: Build Linux/Mac
      run: |
        cd $GITHUB_WORKSPACE
        export PATH=$GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qt/bin:$PATH
        mkdir build
        cd build
        qmake ..
        make -j 2
        make check
    - name: Build App (Mac)
      run: |
        macdeployqt build/YUViewApp/YUView.app -always-overwrite -verbose=2
        cp ${{matrix.LIBDE265_LOCAL}} build/YUViewApp/YUView.app/Contents/MacOS/.
        cd build/YUViewApp
        # Zip
        zip -r ${{matrix.ARTIFACT_NAME}} YUView.app/
        mkdir $GITHUB_WORKSPACE/artifacts
        cp ${{matrix.ARTIFACT_NAME}} $GITHUB_WORKSPACE/artifacts/
      if: matrix.os == 'macos-10.15' || matrix.os == 'macos-11.0'
    - name: Build Appimage (Linux)
      run: |
        cd build
        make INSTALL_ROOT=appdir install
        $GITHUB_WORKSPACE/linuxdeployqt-6-x86_64.AppImage YUViewApp/appdir/usr/local/share/applications/de.rwth_aachen.ient.YUView.desktop -appimage -bundle-non-qt-libs -verbose=2
        mv YUView-*.AppImage YUView.AppImage
        mkdir $GITHUB_WORKSPACE/artifacts
        cp YUView.AppImage $GITHUB_WORKSPACE/artifacts/
        cd $GITHUB_WORKSPACE
        ls -l
        cd $GITHUB_WORKSPACE/artifacts
        ls -l
      if: matrix.os == 'ubuntu-16.04'
    - name: Upload Artifact
      uses: actions/upload-artifact@v2
      with:
        name: ${{matrix.ARTIFACT_NAME}}
        path: artifacts
      if: matrix.os != 'ubuntu-18.04'
    - name: Upload Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: artifacts/${{matrix.ARTIFACT_NAME}}
        asset_name: ${{matrix.ARTIFACT_NAME}}
        asset_content_type: application/zip
      if: github.event_name == 'release' && matrix.os != 'ubuntu-18.04'
  build-windows:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include: 
          - os: windows-2019
            auto_update: true
            ARTIFACT_NAME: YUView-Win.zip
          - os: windows-2019
            auto_update: false
            ARTIFACT_NAME: YUView-Win-noautoupdate.zip
    steps:
    - uses: actions/checkout@v2
    - run: git fetch --prune --unshallow
    - name: Install Qt base
      run: |
        cd ../../
        mkdir -p YUViewQt/YUViewQt
        cd YUViewQt/YUViewQt
        curl -L https://github.com/ChristianFeldmann/YUViewQt/releases/download/QtBase-5.15.1/qtBase_5.15.1_win.zip -o Qt.zip
        7z x  Qt.zip
        echo "$GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qt/bin" >> $GITHUB_PATH
      shell: bash
    - name: Install Qt deploy tools
      run: |
        cd $GITHUB_WORKSPACE/../../YUViewQt/YUViewQt
        curl -L https://github.com/ChristianFeldmann/YUViewQt/releases/download/QtDeployTools-5.15.1/qtTools_5.15.1_win.zip -o deployQt.zip
        7z x deployQt.zip
      shell: bash
    - name: Install libde265
      run: |
        curl -L https://github.com/ChristianFeldmann/libde265/releases/download/v1.1/libde265.dll -o libde265.dll
        curl -L https://raw.githubusercontent.com/ChristianFeldmann/libde265/master/COPYING -o libde265License.txt
      shell: bash
    - name: Install jom
      run: |
        cd $GITHUB_WORKSPACE
        curl -L https://download.qt.io/official_releases/jom/jom.zip -o jom.zip
        7z x jom.zip -ojom
        cp jom/jom.exe $GITHUB_WORKSPACE/../../YUViewQt/YUViewQt/Qt/bin/
      shell: bash
    - name: Install openSSL
      run: |
        mkdir openSSL
        cd openSSL
        curl -L https://github.com/ChristianFeldmann/YUViewQt/releases/download/openSSL1.1.1g/openSSL_1_1_1g_win.zip -o openSSL.zip
        7z x openSSL.zip
        cd ..
      shell: bash
    - name: Activate auto update
      run: sed -i -- "s/#define UPDATE_FEATURE_ENABLE 0/#define UPDATE_FEATURE_ENABLE 1/g" YUViewLib/src/common/typedef.h
      shell: bash
      if: matrix.auto_update == true
    - name: Build Windows
      run: |
        mkdir build
        cd build
        call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
        d:\a\YUViewQt\YUViewQt\Qt\bin\qmake ..
        jom
        nmake check
        cd ..
      shell: cmd
    - name: WindeployQT
      run: |
        mkdir deploy
        cd deploy
        cp ../build/YUViewApp/YUView.exe .
        d:\a\YUViewQt\YUViewQt\Qttools\bin\windeployqt.exe --release --dir d:\a\YUView\YUView\deploy --no-compiler-runtime YUView.exe
        cp ../openSSL/*.dll .
        mkdir decoder
        cp ..\libde265.dll decoder
        cp ..\libde265License.txt decoder
        cp ../LICENSE.GPL3 .
        cd ..
        python deployment/versioning.py -d deploy -o deploy/versioninfo.txt
        mkdir artifacts
        7z a artifacts/YUView-Win.zip ./deploy/*
      shell: cmd
    - name: Wix Windows
      run: |
        cd $GITHUB_WORKSPACE/deployment/wix
        cp /c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Enterprise/VC/Redist/MSVC/v142/MergeModules/Microsoft_VC142_CRT_x64.msm .
        "${WIX}\bin\heat.exe" dir ../../deploy -gg -dr APPLICATIONFOLDER -srd -sreg -cg YUViewComponents -out harvestedDirectory.wxs
        "${WIX}\bin\candle.exe" -dConfiguration=Release -dOutDir=bin/Release/ -dTargetExt=.msi -dTargetFileName=YUViewSetup.msi -dTargetName=YUViewSetup -out obj/Release/ -arch x64 -ext "${WIX}\bin\WixUIExtension.dll" YUView.wxs
        "${WIX}\bin\candle.exe" -dConfiguration=Release -dOutDir=bin/Release/ -dTargetExt=.msi -dTargetFileName=YUViewSetup.msi -dTargetName=YUViewSetup -out obj/Release/ -arch x64 harvestedDirectory.wxs
        "${WIX}\bin\Light.exe" -b ../../deploy -out bin/Release/YUViewSetup.msi -pdbout bin/Release/YUViewSetup.wixpdb -cultures:null -ext "${WIX}\bin\WixUIExtension.dll" -contentsfile obj/Release/YUViewSetup.wixproj.BindContentsFileListnull.txt -outputsfile obj/Release/YUViewSetup.wixproj.BindOutputsFileListnull.txt -builtoutputsfile obj/Release/YUViewSetup.wixproj.BindBuiltOutputsFileListnull.txt obj/Release/YUView.wixobj obj/Release/harvestedDirectory.wixobj
        cd $GITHUB_WORKSPACE
        cp deployment/wix/bin/Release/YUViewSetup.msi ./
      shell: bash
      if: matrix.auto_update == true
    - name: Upload Artifact
      uses: actions/upload-artifact@v2
      with:
        name: ${{matrix.ARTIFACT_NAME}}
        path: artifacts
    - name: Upload Windows installer Artifact
      uses: actions/upload-artifact@v2
      with:
        name: YUViewSetup.msi
        path: ./YUViewSetup.msi
      if: matrix.auto_update == true
    - name: Upload Windows zip to Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: artifacts/YUView-Win.zip
        asset_name: ${{matrix.ARTIFACT_NAME}}
        asset_content_type: application/zip
      if: github.event_name == 'release'
    - name: Upload Windows installer to Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: YUViewSetup.msi
        asset_name: YUViewSetup.msi
        asset_content_type: application/zip
      if: github.event_name == 'release' && matrix.auto_update == true
    
  # How to upload files to the release:
  # https://github.com/Blacksmoke16/oq/pull/47/files#diff-082c28d748ad2e3eecc5508d740d9417R9-R29
  # Mime type list
  # https://www.iana.org/assignments/media-types/media-types.xhtml
