name: Create apps

on:
  workflow_dispatch:

jobs:
  create_msi_app:
    name: Create MSI App
    runs-on: windows-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: adopt
          java-version: '21'
      - name: Generate windows app
        run: ./gradlew packageReleaseMsi
      - name: upload unsigned artifact
        id: upload-unsigned-artifact
        uses: actions/upload-artifact@v4
        with:
          name: Writeopia-windows
          path: application/composeApp/build/compose/binaries/main-release/msi
      - id: optional_step_id
        name: Sign Windows app
        uses: signpath/github-action-submit-signing-request@v1.1
        with:
          api-token: '${{ secrets.SIGN_PATH_API_TOKEN }}'
          organization-id: '${{ secrets.SIGN_PATH_ORG_ID }}'
          project-slug: 'Writeopia'
          signing-policy-slug: 'test-signing'
          github-artifact-id: '${{ steps.upload-unsigned-artifact.outputs.artifact-id }}'
          artifact-configuration-slug: 'msisign'
          wait-for-completion: true
  create_deb_app:
    name: Create Deb App
    runs-on: ubuntu-22.04
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: adopt
          java-version: '21'
      - name: Generate linux app
        run: ./gradlew packageReleaseDeb
      - name: Upload App
        uses: actions/upload-artifact@v4
        with:
          name: Writeopia-deb
          path: application/composeApp/build/compose/binaries/main-release/deb/
  notarize_mac_os:
    name: Notarize Mac OS app
    runs-on: macos-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: adopt
          java-version: '21'
      - name: import certs
        uses: apple-actions/import-codesign-certs@v3
        with:
          p12-file-base64: ${{ secrets.CERTIFICATES_P12 }}
          p12-password: ${{ secrets.CERTIFICATES_P12_PASSWORD }}
      - name: Generate signed mac app
        run: ./gradlew packageReleaseDmg --no-daemon
        env:
          SIGN_IDENTITY: ${{ secrets.SIGN_IDENTITY }}
      - name: Notarize
        run: xcrun  notarytool submit application/composeApp/build/compose/binaries/main-release/dmg/Writeopia-1.0.0.dmg --wait  --apple-id $APPLE_ID_NOTARIZATION --password $NOTARIZATION_PASSWORD --team-id $APPSTORE_TEAM_ID
        env:
          APPLE_ID_NOTARIZATION: ${{ secrets.APPLE_ID_NOTARIZATION }}
          APPSTORE_TEAM_ID: ${{ secrets.APPSTORE_TEAM_ID }}
          NOTARIZATION_PASSWORD: ${{ secrets.NOTARIZATION_PASSWORD }}
      - name: Upload App
        uses: actions/upload-artifact@v4
        with:
          name: Writeopia-macos
          path: application/composeApp/build/compose/binaries/main-release/dmg/Writeopia-1.0.0.dmg
  notarize_mac_os_intel:
    name: Notarize Mac OS app - Intel
    runs-on: macos-13
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          architecture: 'x64'
      - name: import certs
        uses: apple-actions/import-codesign-certs@v3
        with:
          p12-file-base64: ${{ secrets.CERTIFICATES_P12 }}
          p12-password: ${{ secrets.CERTIFICATES_P12_PASSWORD }}
      - name: Generate signed mac app
        run: ./gradlew packageReleaseDmg -Pcompose.desktop.macos.arch=x64 --no-daemon
        env:
          SIGN_IDENTITY: ${{ secrets.SIGN_IDENTITY }}
      - name: Notarize
        run: xcrun notarytool submit application/composeApp/build/compose/binaries/main-release/dmg/Writeopia-1.0.0.dmg --wait --apple-id $APPLE_ID_NOTARIZATION --password $NOTARIZATION_PASSWORD --team-id $APPSTORE_TEAM_ID
        env:
          APPLE_ID_NOTARIZATION: ${{ secrets.APPLE_ID_NOTARIZATION }}
          APPSTORE_TEAM_ID: ${{ secrets.APPSTORE_TEAM_ID }}
          NOTARIZATION_PASSWORD: ${{ secrets.NOTARIZATION_PASSWORD }}
      - name: Upload App
        uses: actions/upload-artifact@v4
        with:
          name: Writeopia-macos-intel
          path: application/composeApp/build/compose/binaries/main-release/dmg/Writeopia-1.0.0.dmg
