name: build-shotcut-linux-unstable

on:
  workflow_dispatch:
  schedule:
    # nightly
    - cron: '0 8 * * *'

env:
  IMAGE_NAME: mltframework/shotcut-build:qt6.8.3-ubuntu22.04

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          ref: qt6

      - name: Build
        run: |
          echo Preparing version
          echo $(date +"%y.%-m.%-d") > VERSION
          VERSION=$(cat VERSION)
          echo VERSION=$VERSION
          FILENAME_VERSION=UNSTABLE-$VERSION
          cd scripts
          echo Making build script config file
          echo CLEANUP=0 > build-shotcut.conf
          echo SHOTCUT_VERSION=\"$VERSION\" >> build-shotcut.conf
          echo SHOTCUT_HEAD=0 >> build-shotcut.conf
          echo SHOTCUT_REVISION=origin/qt6 >> build-shotcut.conf
          docker run -v $PWD:/root/shotcut $IMAGE_NAME
          echo Renaming build artifacts
          mv shotcut.txz shotcut-linux-x86_64-$FILENAME_VERSION.txz
          mv src.txz shotcut-src-$FILENAME_VERSION.txz

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: linux-portable
          path: |
            scripts/shotcut-linux-x86_64-*.txz

      - name: Upload portable and source archives to S3
        if: ${{ github.repository_owner == 'mltframework' }}
        uses: mltframework/s3cmd-action@v0
        with:
          args: >-
            --access_key=${{ secrets.AWS_ACCESS_KEY }}
            --secret_key=${{ secrets.AWS_SECRET_KEY }}
            --acl-public
            --stop-on-error
            put scripts/*.txz s3://builds.us.meltytech/shotcut/

      - name: Package AppImage
        run: |
          sudo apt install desktop-file-utils
          mkdir packaging/linux/Shotcut
          cp VERSION packaging/linux/Shotcut/
          cd packaging/linux
          wget https://raw.githubusercontent.com/probonopd/AppImages/master/pkg2appimage
          bash -ex pkg2appimage appimage/appimage.yml
          FILENAME_VERSION=UNSTABLE-$(cat Shotcut/VERSION)
          mv out/*.AppImage out/shotcut-linux-x86_64-$FILENAME_VERSION.AppImage

      - name: Upload AppImage to S3
        if: ${{ github.repository_owner == 'mltframework' }}
        uses: mltframework/s3cmd-action@v0
        with:
          args: >-
            --access_key=${{ secrets.AWS_ACCESS_KEY }}
            --secret_key=${{ secrets.AWS_SECRET_KEY }}
            --acl-public
            --stop-on-error
            put packaging/linux/out/*.AppImage s3://builds.us.meltytech/shotcut/

      - name: Install Snapcraft
        if: ${{ github.repository_owner == 'mltframework' }}
        uses: samuelmeuli/action-snapcraft@v1.2.0
        with:
          use_lxd: true
      - name: Package snap
        if: ${{ github.repository_owner == 'mltframework' }}
        env:
          SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_TOKEN }}
        run: |
          VERSION=$(cat VERSION)
          mv scripts/shotcut-linux-x86_64-*.txz packaging/linux
          cd packaging/linux
          mkdir -p snap/gui
          cp ../../icons/shotcut-logo-64.png snap/gui
          sed 's|Icon=.*|Icon=${SNAP}/meta/gui/shotcut-logo-64.png|' org.shotcut.Shotcut.desktop > snap/gui/shotcut.desktop
          FILENAME=$(ls *.txz)
          sed "s/\$VERSION/${VERSION}/ ; s/\$FILENAME/${FILENAME}/" snapcraft.yaml.in > snap/snapcraft.yaml
          sg lxd -c 'snapcraft --use-lxd'
          snapcraft upload --release=edge shotcut_*.snap
