name: release

on:
  workflow_dispatch:

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: Set up Java
        uses: actions/setup-java@v4
        with:
          distribution: "oracle"
          java-version: "21"

      # - name: Set up Python
      #   uses: actions/setup-python@v4
      #   with:
      #     python-version: '3.x'

      - name: Set up Python and FontForge
        run: |
          sudo apt-get install -y software-properties-common
          sudo add-apt-repository ppa:fontforge/fontforge
          sudo apt-get install -y python3 fontforge python3-fontforge

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          registry-url: https://registry.npmjs.org/

      - name: Set up fontTools
        run: pip3 install fonttools[woff]

      - name: Set up AFDKO
        run: |
          pip3 install afdko==4.0.1
          echo "$HOME/.local/bin" >> $GITHUB_PATH

      - name: Set up Bits'N'Picas
        run: gh release download --repo 'kreativekorp/bitsnpicas' --pattern 'BitsNPicas.jar'
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - name: Clean up distribution files
        run: |
          rm -rf dist/*.bdf
          rm -rf dist/*.ttf
          rm -rf dist/*.woff2

      - name: Update version
        run: |
          python3 files/version.py files/g11.fea package.json
          python3 files/version.py files/g11.fea package-lock.json
          python3 files/versionBits.py files/g11.fea src/Galmuri14.kbitx
          python3 files/versionBits.py files/g11.fea src/Galmuri11.kbitx
          python3 files/versionBits.py files/g11.fea src/Galmuri11-Bold.kbitx
          python3 files/versionBits.py files/g11.fea src/Galmuri11-Condensed.kbitx
          python3 files/versionBits.py files/g11.fea src/Galmuri9.kbitx
          python3 files/versionBits.py files/g11.fea src/Galmuri7.kbitx
          python3 files/versionBits.py files/g11.fea src/GalmuriMono11.kbitx
          python3 files/versionBits.py files/g11.fea src/GalmuriMono9.kbitx
          python3 files/versionBits.py files/g11.fea src/GalmuriMono7.kbitx
          python3 files/versionFea.py files/g11.fea files/g14.fea
          python3 files/versionFea.py files/g11.fea files/g11b.fea
          python3 files/versionFea.py files/g11.fea files/g11c.fea
          python3 files/versionFea.py files/g11.fea files/g9.fea
          python3 files/versionFea.py files/g11.fea files/g7.fea
          python3 files/versionFea.py files/g11.fea files/gm11.fea
          python3 files/versionFea.py files/g11.fea files/gm9.fea
          python3 files/versionFea.py files/g11.fea files/gm7.fea

      - name: Get npm package version
        id: package-version
        uses: martinbeentjes/npm-get-version-action@main

      - name: Convert to BDF font
        run: |
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/Galmuri14.bdf src/Galmuri14.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/Galmuri11.bdf src/Galmuri11.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/Galmuri11-Bold.bdf src/Galmuri11-Bold.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/Galmuri11-Condensed.bdf src/Galmuri11-Condensed.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/Galmuri9.bdf src/Galmuri9.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/Galmuri7.bdf src/Galmuri7.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/GalmuriMono11.bdf src/GalmuriMono11.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/GalmuriMono9.bdf src/GalmuriMono9.kbitx
          java -jar BitsNPicas.jar convertbitmap -f bdf -o dist/GalmuriMono7.bdf src/GalmuriMono7.kbitx
          python3 mkttf/mkttf.py -n "QPLE" -f Galmuri14Bitmap -n Galmuri14Bitmap-Regular -N "Galmuri14Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/Galmuri14.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f Galmuri11Bitmap -n Galmuri11Bitmap-Regular -N "Galmuri11Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/Galmuri11.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f Galmuri11Bitmap -n Galmuri11Bitmap-Bold -N "Galmuri11Bitmap Bold" -V ${{ steps.package-version.outputs.current-version }} dist/Galmuri11-Bold.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f Galmuri11Bitmap -n Galmuri11Bitmap-Condensed -N "Galmuri11Bitmap Condensed" -V ${{ steps.package-version.outputs.current-version }} dist/Galmuri11-Condensed.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f Galmuri9Bitmap -n Galmuri9Bitmap-Regular -N "Galmuri9Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/Galmuri9.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f Galmuri7Bitmap -n Galmuri7Bitmap-Regular -N "Galmuri7Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/Galmuri7.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f GalmuriMono11Bitmap -n GalmuriMono11Bitmap-Regular -N "GalmuriMono11Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/GalmuriMono11.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f GalmuriMono9Bitmap -n GalmuriMono9Bitmap-Regular -N "GalmuriMono9Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/GalmuriMono9.bdf
          python3 mkttf/mkttf.py -n "QPLE" -f GalmuriMono7Bitmap -n GalmuriMono7Bitmap-Regular -N "GalmuriMono7Bitmap Regular" -V ${{ steps.package-version.outputs.current-version }} dist/GalmuriMono7.bdf
          mv *.ttf dist
          rm -rf *.sfd

      - name: Convert to TrueType font and compress to WOFF2
        run: |
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuri14.ttf src/Galmuri14.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuri11.ttf src/Galmuri11.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuri11-Bold.ttf src/Galmuri11-Bold.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuri11-Condensed.ttf src/Galmuri11-Condensed.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuri9.ttf src/Galmuri9.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuri7.ttf src/Galmuri7.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuriMono11.ttf src/GalmuriMono11.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuriMono9.ttf src/GalmuriMono9.kbitx
          java -jar BitsNPicas.jar convertbitmap -f ttf -o tGalmuriMono7.ttf src/GalmuriMono7.kbitx
          makeotf -mf files/FontMenuNameDB -f tGalmuri14.ttf -ff files/g14.fea -o dist/Galmuri14.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuri11.ttf -ff files/g11.fea -o dist/Galmuri11.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuri11-Bold.ttf -ff files/g11b.fea -o dist/Galmuri11-Bold.ttf -osbOn 5
          makeotf -mf files/FontMenuNameDB -f tGalmuri11-Condensed.ttf -ff files/g11c.fea -o dist/Galmuri11-Condensed.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuri9.ttf -ff files/g9.fea -o dist/Galmuri9.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuri7.ttf -ff files/g7.fea -o dist/Galmuri7.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuriMono11.ttf -ff files/gm11.fea -o dist/GalmuriMono11.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuriMono9.ttf -ff files/gm9.fea -o dist/GalmuriMono9.ttf -osbOn 6
          makeotf -mf files/FontMenuNameDB -f tGalmuriMono7.ttf -ff files/gm7.fea -o dist/GalmuriMono7.ttf -osbOn 6
          otf2otc -o dist/Galmuri.ttc dist/Galmuri14.ttf dist/Galmuri11.ttf dist/Galmuri11-Bold.ttf dist/Galmuri11-Condensed.ttf dist/Galmuri9.ttf dist/Galmuri7.ttf
          otf2otc -o dist/GalmuriMono.ttc dist/GalmuriMono11.ttf dist/GalmuriMono9.ttf dist/GalmuriMono7.ttf
          fonttools ttLib.woff2 compress dist/Galmuri14.ttf
          fonttools ttLib.woff2 compress dist/Galmuri11.ttf
          fonttools ttLib.woff2 compress dist/Galmuri11-Bold.ttf
          fonttools ttLib.woff2 compress dist/Galmuri11-Condensed.ttf
          fonttools ttLib.woff2 compress dist/Galmuri9.ttf
          fonttools ttLib.woff2 compress dist/Galmuri7.ttf
          fonttools ttLib.woff2 compress dist/GalmuriMono11.ttf
          fonttools ttLib.woff2 compress dist/GalmuriMono9.ttf
          fonttools ttLib.woff2 compress dist/GalmuriMono7.ttf

      - name: Clean up temporary files
        run: rm -rf dist/*_mod.bdf

      - name: Make zip
        run: |
          cd dist
          zip Galmuri-v${{ steps.package-version.outputs.current-version }}.zip ./*

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: dist/Galmuri-v${{ steps.package-version.outputs.current-version }}.zip
          tag_name: "v${{ steps.package-version.outputs.current-version }}"

      - name: Clean up temporary files
        run: |
          rm -rf dist/Galmuri-v*.zip
          rm -rf tGalmuri*.ttf
          rm -rf BitsNPicas.jar

      - name: Publish to npm
        run: |
          npm ci
          npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Commit changed files
        uses: stefanzweifel/git-auto-commit-action@v5
