name: Release Builds
on: push
env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  build-and-store-artifact:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ ubuntu-16.04, ubuntu-latest, macos-latest, windows-latest ]


    steps:
    # I DO NOT LIKE THIS, BUT WE CREATE THE RELEASE BEFORE BUILD IS SUCCESSFUL 
    # BECAUSE OF SERIOUS LIMITATIONS IN GITHUB ACTIONS
    - name: Create Release
      id: create_release
      if: startsWith(github.ref, 'refs/tags/v') && matrix.os == 'ubuntu-latest'
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}

    # SAVE RELEASE URL ARTIFACT SO OTHER PLATFORMS CAN FIND WHERE TO UPLOAD RELEASES
    - name: Output Release URL File
      if: startsWith(github.ref, 'refs/tags/v') && matrix.os == 'ubuntu-latest'
      run: echo "${{ steps.create_release.outputs.upload_url }}" > release_url.txt
    - name: Save release URL file for later
      if: startsWith(github.ref, 'refs/tags/v') && matrix.os == 'ubuntu-latest'
      uses: actions/upload-artifact@v1
      with:
        name: release_url
        path: release_url.txt


    - uses: actions/checkout@v1
    - uses: ilammy/msvc-dev-cmd@v1
    - uses: jurplel/install-qt-action@v2
   
    - name: Make GNU
      if: matrix.os != 'windows-latest'
      working-directory: ./buckshot
      run: |
        qmake
        make

    # - name: Mac debug
    #   if: matrix.os == 'macos-latest'
    #   working-directory: ./buckshot
    #   run: |
    #     ls
    #     ls -al buckshot.app
    #     ls -al buckshot.app/Contents
    #     ls -al buckshot.app/Contents/MacOS

    - name: Prep Windows (Ico)
      if: matrix.os == 'windows-latest'
      shell: bash
      run: |
        cp assets/icon.ico buckshot
        echo "RC_ICONS = icon.ico" >> buckshot.pro

       
    - name: Make Windows
      if: matrix.os == 'windows-latest'
      working-directory: ./buckshot
      run: |
        qmake
        nmake
       
        
    # PACKAGING POST BUILD
    - name: Package OSX
      if: matrix.os == 'macos-latest'
      run: |
        ./package_mac.sh
        pip3 install dmgbuild
        dmgbuild -s dmg-settings.json "buckshot" buckshot.dmg

    - name: Package Linux 
      if: startsWith(matrix.os, 'ubuntu')
      run: |
        ./package_linux.sh

    - name: Package Windows
      if: matrix.os == 'windows-latest'
      working-directory: ./buckshot
      run: |
        mkdir buckshot
        cp release\buckshot.exe buckshot
        windeployqt buckshot
        cp ../README.md buckshot
        cp ../LICENSE.txt buckshot
        cp ../assets/examples/* buckshot
        $url = "https://github.com/digarok/b2d/releases/download/v1.4/b2d-windows-latest-v1.4.zip"
        Invoke-WebRequest -Uri $url -OutFile b2d.zip
        7z.exe x b2d.zip
        cp b2d.exe buckshot
        $url = "https://github.com/digarok/cadius/releases/download/0.0.0/cadius-windows-latest-0.0.0.zip"
        Invoke-WebRequest -Uri $url -OutFile cadius.zip
        7z.exe x cadius.zip
        cp build/Src/Debug/Cadius.exe buckshot
        7z.exe a buckshot.zip buckshot\*
        mv buckshot.zip ..


    # EVERYTHING BELOW IS ONLY WHEN VERSION TAGS PUSHED (i.e. tag like "v0.1")


    # LOAD THE RELEASE URL FOR ALL PLATFORMS
    - name: Load Release URL from artifact
      if: startsWith(github.ref, 'refs/tags/v')
      uses: actions/download-artifact@v1
      with:
        name: release_url
    - name: Set upload_url
      if: startsWith(github.ref, 'refs/tags/v')
      shell: bash
      id: release_info
      run: |
        value=`cat release_url/release_url.txt`
        echo ::set-output name=upload_url::$value

    # UPLOAD RELEASES
    - name: Upload Mac Release
      if: startsWith(github.ref, 'refs/tags/v') && matrix.os == 'macos-latest'
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.release_info.outputs.upload_url }}
        asset_path: ./buckshot.dmg
        asset_name: buckshot.dmg
        asset_content_type: application/octet-stream

    - name: Upload Ubuntu Latest Release 
      if: startsWith(github.ref, 'refs/tags/v') && startsWith(matrix.os, 'ubuntu')
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.release_info.outputs.upload_url }}
        asset_path: ./buckshot-linux.zip
        asset_name: buckshot-${{ matrix.os }}.zip
        asset_content_type: application/octet-stream

    - name: Upload Windows Release
      if: startsWith(github.ref, 'refs/tags/v') && startsWith(matrix.os, 'windows')
      id: upload-release-asset
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.release_info.outputs.upload_url }}
        asset_path: ./buckshot.zip
        asset_name: buckshot-${{ matrix.os }}.zip
        asset_content_type: application/zip
