# .github/workflows/release.yml

name: Release

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  release:
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install esbuild
        run: npm install -g esbuild

      - name: Install html-minifier-terser
        run: npm install -g html-minifier-terser

      - 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: Prepare build
        run: |
          mkdir -p build

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

           html-minifier-terser login.raw.html \
            --collapse-whitespace \
            --remove-comments \
            --remove-redundant-attributes \
            --remove-script-type-attributes \
            --remove-style-link-type-attributes \
            --minify-css true \
            --minify-js true \
            -o build/login.html

          esbuild src/styles-skynet.css \
            --minify \
            --legal-comments=none \
            --outfile=build/styles-skynet.min.css

          esbuild src/script-skynet.js \
            --minify \
            --legal-comments=none \
            --outfile=build/script-skynet.min.js

      - name: Create archive
        run: |
          VERSION=${{ steps.tag.outputs.tag }}

          cd build
          zip -r "../roundcube-theme-skynet-${VERSION}.zip" .

      - 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:
          tag_name: ${{ steps.tag.outputs.tag }}
          files: roundcube-theme-skynet-${{ steps.tag.outputs.tag }}.zip
