# Release from a dispatch event from the danklinux repo
name: Create Release from DMS

on:
  repository_dispatch:
    types: [dms_release]

permissions:
  contents: write

concurrency:
  group: release-${{ github.event.client_payload.tag }}
  cancel-in-progress: true

jobs:
  create_release_from_dms:
    runs-on: ubuntu-24.04
    env:
      TAG: ${{ github.event.client_payload.tag }}
      DMS_REPO: ${{ github.event.client_payload.dms_repo }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Ensure VERSION and tag
        run: |
          set -euxo pipefail
          git config user.name  "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

          # create/update VERSION file to match incoming tag
          echo "${TAG}" > VERSION
          if ! git diff --quiet -- VERSION; then
            git add VERSION
            git commit -m "Add VERSION file for ${TAG} (from DMS)"
          fi

          # If tag doesn't exist (or differs), (re)create it
          if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null; then
            echo "Tag ${TAG} already exists"
          else
            git tag "${TAG}"
          fi

          # Push commit (if any) and tag
          git push --follow-tags origin HEAD

      - name: Generate Changelog
        id: changelog
        run: |
          set -e
          PREVIOUS_TAG=$(git describe --tags --abbrev=0 "${TAG}^" 2>/dev/null || echo "")
          if [ -z "$PREVIOUS_TAG" ]; then
            CHANGELOG=$(git log --oneline --pretty=format:"- %s (%h)" | head -50)
          else
            CHANGELOG=$(git log --oneline --pretty=format:"- %s (%h)" "${PREVIOUS_TAG}..${TAG}")
          fi

          cat > RELEASE_BODY.md << 'EOF'
          ## Assets

          ### Complete Packages
          - **`dms-full-amd64.tar.gz`** - Complete package for x86_64 systems (CLI binaries + QML source + installation guide)
          - **`dms-full-arm64.tar.gz`** - Complete package for ARM64 systems (CLI binaries + QML source + installation guide)

          ### Individual Components
          - **`dms-cli-amd64.gz`** - DMS CLI binary for x86_64 systems
          - **`dms-cli-arm64.gz`** - DMS CLI binary for ARM64 systems
          - **`dms-distropkg-amd64.gz`** - DMS CLI binary built with distro_package tag for AMD64 systems
          - **`dms-distropkg-arm64.gz`** - DMS CLI binary built with distro_package tag for ARM64 systems
          - **`dms-qml.tar.gz`** - QML source code only

          ### Checksums
          - **`*.sha256`** - SHA256 checksums for verifying download integrity

          **Installation:** Extract the `dms-full-*.tar.gz` package for your architecture and follow the `INSTALL.md` instructions inside.

          ---

          EOF

          cat >> RELEASE_BODY.md << EOF
          ## What's Changed

          $CHANGELOG

          **Full Changelog**: https://github.com/${{ github.repository }}/compare/${PREVIOUS_TAG}...${TAG}
          EOF

          echo "changelog<<EOF" >> $GITHUB_OUTPUT
          cat RELEASE_BODY.md >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create/Update DankMaterialShell Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.TAG }}
          name: Release ${{ env.TAG }}
          body: ${{ steps.changelog.outputs.changelog }}
          draft: false
          prerelease: ${{ contains(env.TAG, '-') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Download and prepare release assets
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -euxo pipefail

          mkdir -p _release_assets

          # Download DMS CLI binaries from the danklinux repo
          gh release download "${TAG}" -R "${DMS_REPO}" --dir ./_dms_assets

          # Rename CLI binaries to dms-cli-* format and copy distropkg binaries
          for file in _dms_assets/dms-*.gz*; do
            if [ -f "$file" ]; then
              basename=$(basename "$file")
              if [[ "$basename" == dms-distropkg-* ]]; then
                cp "$file" "_release_assets/$basename"
              else
                newname=$(echo "$basename" | sed 's/^dms-/dms-cli-/')
                cp "$file" "_release_assets/$newname"
              fi
            fi
          done

          # Create QML source package (exclude .git, .github, build artifacts)
          tar --exclude='.git' \
              --exclude='.github' \
              --exclude='_dms_assets' \
              --exclude='_release_assets' \
              --exclude='*.tar.gz' \
              -czf _release_assets/dms-qml.tar.gz .

          # Generate checksum for QML package
          (cd _release_assets && sha256sum dms-qml.tar.gz > dms-qml.tar.gz.sha256)

          # Create full packages for each architecture
          for arch in amd64 arm64; do
            mkdir -p _temp_full/dms
            mkdir -p _temp_full/bin

            # Extract QML source to temp directory
            tar -xzf _release_assets/dms-qml.tar.gz -C _temp_full/dms

            # Copy CLI binary if it exists
            if [ -f "_dms_assets/dms-${arch}.gz" ]; then
              gunzip -c "_dms_assets/dms-${arch}.gz" > _temp_full/bin/dms
              chmod +x _temp_full/bin/dms
            fi

            # Copy distropkg binary if it exists
            if [ -f "_dms_assets/dms-distropkg-${arch}.gz" ]; then
              gunzip -c "_dms_assets/dms-distropkg-${arch}.gz" > _temp_full/bin/dms-distropkg
              chmod +x _temp_full/bin/dms-distropkg
            fi

            # Create INSTALL.md
            cat > _temp_full/INSTALL.md << 'EOF'
          # DankMaterialShell Installation

          ## Requirements

          - Wayland compositor (niri or Hyprland recommended)
          - Quickshell framework
          - Qt6

          ## Installation Steps

          1. **Install quickshell assets:**
             ```bash
             mkdir -p ~/.config/quickshell
             cp -r dms ~/.config/quickshell/
             ```

          2. **Install the DMS CLI binaries:**
             ```bash
             sudo install -m 755 bin/dms /usr/local/bin/dms
             # or install to a local directory:
             mkdir -p ~/.local/bin
             install -m 755 bin/dms ~/.local/bin/dms
             ```

          3. **Start the shell:**
             ```bash
             dms run
             # or directly with quickshell (will lack some dbus integrations & plugin management):
             quickshell -p ~/.config/quickshell/dms
             ```

          ## Configuration

          - Settings are stored in `~/.config/DankMaterialShell/settings.json`
          - Plugins go in `~/.config/DankMaterialShell/plugins/`
          - See the documentation in the `dms/` directory for more details

          ## Troubleshooting

          - Run with verbose output: `quickshell -v -p ~/.config/quickshell/dms`
          - Check logs in `~/.local/state/DankMaterialShell/`
          - Ensure all dependencies are installed
          EOF

            # Create the full package
            (cd _temp_full && tar -czf "../_release_assets/dms-full-${arch}.tar.gz" .)

            # Generate checksum
            (cd _release_assets && sha256sum "dms-full-${arch}.tar.gz" > "dms-full-${arch}.tar.gz.sha256")

            # Cleanup
            rm -rf _temp_full
          done

      - name: Attach all assets to release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.TAG }}
          files: _release_assets/**
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}