name: CutBox Test/Build CI

on:
  push:
    branches: [ "master" ]
  pull_request:  
  workflow_dispatch:

jobs:
  build:
    name: CutBox Build
    runs-on: macos-11
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0
          ref: master

      - name: CI diagnostics
        run: |
          echo "============================================================"
          for b in ci_is_production ci_get_release_info ci_update_info_plist ci_update_cutbox_homebrew setup test build
          do
            if [[ -x bin/$b ]]; then
              echo "[✓] bin/$b executable."
            else
              echo "[❌] bin/$b - missing or non-executable" && exit 1
            fi
          done

      - name: Check production release
        run: |
          bin/ci_is_production

      - name: Setup
        run: |
          bin/setup

      - name: Assemble release notes
        run: |
          mkdir tmp/
          bin/get_release_notes | tee tmp/release-notes.md

      - name: Test
        run: |
          bin/test

      - name: Build
        run: |
          bin/build

      - name: Make DMG
        run: |
          bin/cutbox_make_dmg CutBox.dmg

      - name: Get Release Info
        run: |
          bin/ci_get_release_info

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ env.RELEASE }}
          release_name: ${{ env.RELEASE }}
          draft: ${{ env.PRODUCTION_RUN != 1}}
          prerelease: false
          body_path: tmp/release-notes.md


      - name: Upload CutBox.dmg
        uses: actions/upload-release-asset@v1
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: CutBox.dmg
          asset_name: CutBox-${{env.RELEASE}}.dmg
          asset_content_type: application/octet-stream

      - name: Update Info.plist
        run: |
          bin/ci_update_info_plist

      - name: Notify brew tap cutbox
        run: |
          if [[ "$PRODUCTION_RUN" ==  "1" ]]; then
            curl \
               -s \
               -XPOST \
               -H "Accept: application/vnd.github+json" \
               -H "Authorization: Bearer ${{ secrets.CUTBOX_PAT }}" \
               https://api.github.com/repos/cutbox/homebrew-cutbox/actions/workflows/35793720/dispatches \
               --data '{"ref":"main","inputs":{"release":"${{ env.RELEASE }}"}}'
          fi

      - name: Notify cutbox.github.io
        run: |
          if [[ "$PRODUCTION_RUN" ==  "1" ]]; then
            curl \
               -s \
               -XPOST \
               -H "Accept: application/vnd.github+json" \
               -H "Authorization: Bearer ${{ secrets.CUTBOX_PAT }}" \
               https://api.github.com/repos/cutbox/cutbox.github.io/actions/workflows/35834026/dispatches \
               --data '{"ref":"master,"inputs":{"release":"${{ env.RELEASE }}"}}'
          fi

