# .github/workflows/release.yml

name: Lightweight Release

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build files
        run: |
          mkdir -p build

          cp meta.json README.md build/

          sed \
            -e 's|\.\/skins\/skynet\/styles\/styles\.css|./skins/skynet/styles/styles.css.min|g' \
            -e 's|\.\/skins\/skynet\/script\.js|./skins/skynet/script.js.min|g' \
            src/login.html > build/login.html

          cat src/styles.css \
            | tr -d '\n\t' \
            | sed 's/  */ /g' \
            | sed 's/ *{ */{/g' \
            | sed 's/ *} */}/g' \
            | sed 's/ *; */;/g' \
            | sed 's/ *: */:/g' \
            | sed 's/ *, */,/g' \
            > build/styles.min.css

          cat src/script.js \
            | tr -d '\n\t' \
            | sed 's/  */ /g' \
            > build/script.min.js

      - name: Create archive
        run: |
          cd build
          zip -r ../release.zip .

      - name: Generate next tag
        id: tag
        run: |
          git fetch --tags

          LAST_TAG=$(git tag -l "v*" | sort -V | tail -n1)

          if [ -z "$LAST_TAG" ]; then
            NEXT=1
          else
            NUM=${LAST_TAG#v}
            NEXT=$((NUM + 1))
          fi

          NEW_TAG="v$NEXT"

          echo "tag=$NEW_TAG" >> $GITHUB_OUTPUT

      - name: Create and push tag
        run: |
          git config user.name "gitea-actions"
          git config user.email "actions@local"

          git tag ${{ steps.tag.outputs.tag }}
          git push origin ${{ steps.tag.outputs.tag }}

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: roundcube-theme-skynet-${{ steps.tag.outputs.tag }}.zip
          tag_name: ${{ steps.tag.outputs.tag }}
          files: release.zip
