name: Build/release

on:
  push:
    tags:
      - 'v*'
  schedule:
    - cron: '0 10 * * *'
  workflow_dispatch:
    inputs:
      tmate_enabled:
        type: boolean
        description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
        required: false
        default: false

jobs:
  release:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 60

    env:
      is_tag: ${{ startsWith(github.ref, 'refs/tags/v') }}
      SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_TOKEN }}

    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest, windows-11-arm]
#        os: [windows-latest]

    steps:
      - name: Setup tmate session
        uses: mxschmitt/action-tmate@v3
        if: ${{ github.event_name == 'workflow_dispatch' && inputs.tmate_enabled }}
        with:
          detached: true
  
        # Windows fix. See https://github.com/actions/checkout/issues/226
      - run: git config --global core.autocrlf false

      - uses: actions/checkout@v6

      - uses: actions/setup-node@v6
        with:
          node-version: 22
          cache: 'yarn'

      # This global ffmpeg is used for screenshots (see below)
      - uses: FedericoCarboni/setup-ffmpeg@v3
        if: matrix.os != 'windows-11-arm' && env.is_tag == 'false'
        with:
          ffmpeg-version: release
          # arm not yet supported on macos
          architecture: ${{ matrix.os == 'macos-latest' && 'x64' || '' }}
          # Linking type of the binaries. Use "shared" to download shared binaries and 
          # "static" for statically linked ones. Shared builds are currently only available
          # for windows releases. Defaults to "static"
          linking-type: static
          # As of version 3 of this action, builds are no longer downloaded from GitHub
          # except on Windows: https://github.com/GyanD/codexffmpeg/releases.
          github-token: ${{ github.server_url == 'https://github.com' && github.token || '' }}

      - run: ffmpeg -version
        if: matrix.os != 'windows-11-arm'

       # Because of timeout issue https://github.com/yarnpkg/yarn/issues/4890
      - run: yarn install --immutable --network-timeout 1000000

      - name: (Linux) Install and login to Snapcraft
        uses: samuelmeuli/action-snapcraft@v2
        if: runner.os == 'Linux'
      
      - name: Save provisioning profile
        if: runner.os == 'macOS'
        env:
          PROVISIONING_PROFILE_BASE64: ${{ secrets.PROVISIONING_PROFILE_BASE64 }}
        run: |
          echo -n "$PROVISIONING_PROFILE_BASE64" | base64 --decode > LosslessCut_Mac_App_Store_provisioning_profile.provisionprofile

      - name: (MacOS) Prepare Apple API key
        # Import Apple API key for uploading to Mac App Store and for notarization
        if: runner.os == 'macOS'
        run: |
          mkdir -p ~/.appstoreconnect/private_keys/
          echo '${{ secrets.api_key }}' > ~/.appstoreconnect/private_keys/AuthKey_${{ secrets.api_key_id }}.p8

      - name: (MacOS) Download ffmpeg
        if: runner.os == 'macOS'
        run: |
          yarn download-ffmpeg-darwin-x64
          yarn download-ffmpeg-darwin-arm64

      - name: (Windows x64) Download ffmpeg
        if: matrix.os == 'windows-latest'
        run: yarn download-ffmpeg-win32-x64

      - name: (Windows ARM64) Download ffmpeg
        if: matrix.os == 'windows-11-arm'
        run: yarn download-ffmpeg-win32-arm64

      - name: (Linux) Download ffmpeg
        if: runner.os == 'Linux'
        run: yarn download-ffmpeg-linux-x64

      - run: ls -R ffmpeg

      - name: Build/release Electron app
        uses: samuelmeuli/action-electron-builder@v1
        with:
          # GitHub token, automatically provided to the action
          # (No need to define this secret in the repo settings)
          github_token: ${{ secrets.github_token }}

          # If the commit is tagged with a version (e.g. "v1.0.0"),
          # release the app after building
          release: ${{ env.is_tag == 'true' }}

          max_attempts: 1

          mac_certs: ${{ secrets.mac_certs }}
          mac_certs_password: ${{ secrets.mac_certs_password }}

# Temporary 30 day certificate, not needed for Microsoft Store
#          windows_certs: ${{ secrets.windows_certs }}
#          windows_certs_password: ${{ secrets.windows_certs_password }}
        env:
          APPLE_API_KEY: ~/.appstoreconnect/private_keys/AuthKey_${{ secrets.api_key_id }}.p8
          APPLE_API_KEY_ID: ${{ secrets.api_key_id }}
          APPLE_API_ISSUER: ${{ secrets.api_key_issuer_id }}


      - run: node script/e2e.ts 'dist/mac-arm64/LosslessCut.app/Contents/MacOS/LosslessCut' 0:none screenshot.jpeg
        if: runner.os == 'macOS' && env.is_tag == 'false'

      - run: node script/e2e.ts 'dist\win-unpacked\LosslessCut.exe' desktop screenshot.jpeg
        if: matrix.os == 'windows-latest' && env.is_tag == 'false'

      - run: |
          export DISPLAY=:0
          sudo Xvfb -ac :0 -screen 0 1280x1024x24 > /dev/null 2>&1 &
          sleep 5
          chmod +x dist/linux-unpacked/losslesscut
          node script/e2e.ts 'dist/linux-unpacked/losslesscut' ':0.0+0,0' screenshot.jpeg
        if: runner.os == 'Linux' && env.is_tag == 'false'

      - name: (MacOS) Upload to Mac App Store
        if: runner.os == 'macOS' && env.is_tag == 'true'
        run: |
          node script/xcrunWrapper.ts dist/mas-universal/LosslessCut-mac-universal-DONT-USE-THIS-FILE.pkg ${{ secrets.api_key_id }} ${{ secrets.api_key_issuer_id }} 1505323402 no.mifi.losslesscut-mac

      # https://stackoverflow.com/questions/74774114/using-docker-buildx-github-action-cache-without-official-actions/77225313#77225313
      - name: Expose actions cache variables
        uses: actions/github-script@v6
        with:
          script: |
            core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env['ACTIONS_RUNTIME_TOKEN'])
            core.exportVariable('ACTIONS_RESULTS_URL', process.env['ACTIONS_RESULTS_URL'])

      - name: (MacOS) Upload artifacts
        if: runner.os == 'macOS' && env.is_tag == 'false'
        env:
          ACTIONS_RUNTIME_TOKEN: ${{ env.ACTIONS_RUNTIME_TOKEN }}
          ACTIONS_RESULTS_URL: ${{ env.ACTIONS_RESULTS_URL }}
        run: |
          yarn upload-artifacts dist/LosslessCut-mac-arm64.dmg
          yarn upload-artifacts dist/LosslessCut-mac-x64.dmg
          yarn upload-artifacts --name screenshot-mac.jpeg screenshot.jpeg

      - name: (Windows) Upload artifacts
        if: runner.os == 'Windows' && env.is_tag == 'false'
        env:
          ACTIONS_RUNTIME_TOKEN: ${{ env.ACTIONS_RUNTIME_TOKEN }}
          ACTIONS_RESULTS_URL: ${{ env.ACTIONS_RESULTS_URL }}
        run: |
          yarn upload-artifacts dist/LosslessCut-win-${{ matrix.os == 'windows-11-arm' && 'arm64' || 'x64' }}.7z

      - name: (Windows x64) Upload screenshot
        if: matrix.os == 'windows-latest' && env.is_tag == 'false'
        env:
          ACTIONS_RUNTIME_TOKEN: ${{ env.ACTIONS_RUNTIME_TOKEN }}
          ACTIONS_RESULTS_URL: ${{ env.ACTIONS_RESULTS_URL }}
        run: yarn upload-artifacts --name screenshot-windows.jpeg screenshot.jpeg

      - name: (Linux) Upload artifacts
        if: runner.os == 'Linux' && env.is_tag == 'false'
        env:
          ACTIONS_RUNTIME_TOKEN: ${{ env.ACTIONS_RUNTIME_TOKEN }}
          ACTIONS_RESULTS_URL: ${{ env.ACTIONS_RESULTS_URL }}
        run: |
          yarn upload-artifacts dist/LosslessCut-linux-arm64.tar.bz2
          yarn upload-artifacts dist/LosslessCut-linux-armv7l.tar.bz2
          yarn upload-artifacts dist/LosslessCut-linux-x64.tar.bz2
          yarn upload-artifacts --name screenshot-linux.jpeg screenshot.jpeg

