stages:
  - build
  - release
  - pages

build_android:
  stage: build
  image: fabernovel/android:api-35-v1.10.1
  rules:
    - if: $CI_COMMIT_TAG
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './'
  script:
    - apt update
    - apt install -y curl openjdk-17-jdk-headless
    - update-alternatives --auto java
    - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
    - ./gradlew assembleRelease --no-daemon
  after_script:
    - echo "ANDROID_APP_URL=https://gitlab.com/Hague/forkyz/-/jobs/$CI_JOB_ID/artifacts/raw/androidApp/build/outputs/apk/release/androidApp-release.apk" >> android_job.env
  artifacts:
    paths:
      - androidApp/build/outputs/apk/release
    expire_in: never
    reports:
      dotenv: android_job.env

build_flatpak:
  stage: build
  image: ubuntu:24.04
  rules:
    - if: $CI_COMMIT_TAG
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './'
  script:
    - export APP_VERSION=$(echo $CI_COMMIT_TAG | sed 's/\(v\|alpha\)\([0-9]*\).*/\2/')
    - apt update
    - apt install -y curl binutils fakeroot flatpak-builder
    - flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
    - flatpak install --assumeyes org.freedesktop.Platform//25.08
    - flatpak install --assumeyes org.freedesktop.Sdk//25.08
    - flatpak install --assumeyes org.freedesktop.Sdk.Extension.openjdk17//25.08
    - pushd flatpak
    - flatpak-builder --repo=repo-local build-local local-manifest.json
    - flatpak build-bundle repo-local forkyz-${APP_VERSION}.flatpak com.gitlab.Hague.forkyz --runtime-repo=https://flathub.org/repo/flathub.flatpakrepo
    - mkdir share
    - mv forkyz-${APP_VERSION}.flatpak share/
  after_script:
    - export APP_VERSION=$(echo $CI_COMMIT_TAG | sed 's/\(v\|alpha\)\([0-9]*\).*/\2/')
    - echo "FLATPAK_URL=https://gitlab.com/Hague/forkyz/-/jobs/$CI_JOB_ID/artifacts/raw/flatpak/share/forkyz-${APP_VERSION}.flatpak" >> flatpak_job.env
  artifacts:
    paths:
      - flatpak/share
    expire_in: never
    reports:
      dotenv: flatpak_job.env

build_linux:
  stage: build
  image: ubuntu:24.04
  rules:
    - if: $CI_COMMIT_TAG
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './'
  script:
    - export APP_VERSION=$(echo $CI_COMMIT_TAG | sed 's/\(v\|alpha\)\([0-9]*\).*/\2/')
    - apt update
    - apt install -y curl openjdk-17-jdk binutils fakeroot
    - update-alternatives --auto java
    - ./gradlew packageReleaseDeb
    - ./gradlew packageReleaseUberJarForCurrentOS
    - ./gradlew --stop
  after_script:
    - export APP_VERSION=$(echo $CI_COMMIT_TAG | sed 's/\(v\|alpha\)\([0-9]*\).*/\2/')
    - echo "DEB_APP_URL=https://gitlab.com/Hague/forkyz/-/jobs/$CI_JOB_ID/artifacts/raw/composeApp/build/compose/binaries/main-release/deb/app.crossword.yourealwaysbe.forkyz_${APP_VERSION}.0.0-1_amd64.deb" >> linux_job.env
    - echo "DEB_JAR_URL=https://gitlab.com/Hague/forkyz/-/jobs/$CI_JOB_ID/artifacts/raw/composeApp/build/compose/jars/app.crossword.yourealwaysbe.forkyz-linux-x64-${APP_VERSION}.0.0-release.jar" >> linux_job.env
  artifacts:
    paths:
      - composeApp/build/compose/binaries/main-release/deb
      - composeApp/build/compose/jars
    expire_in: never
    reports:
      dotenv: linux_job.env

#build_mac:
#  stage: build
#  tags:
#    - saas-macos-medium-m1
#  rules:
#    - if: $CI_COMMIT_TAG
#  variables:
#    SECURE_FILES_DOWNLOAD_PATH: './'
#  script:
#    - ./gradlew packageReleaseDmg
#    - ./gradlew packageReleaseUberJarForCurrentOS
#    - ./gradlew --stop
#  after_script:
#    - export APP_VERSION=$(echo $CI_COMMIT_TAG | sed 's/\(v\|alpha\)\([0-9]*\).*/\2/')
#    - echo "MAC_APP_URL=https://gitlab.com/Hague/forkyz/-/jobs/$CI_JOB_ID/artifacts/raw/compoaseApp/build/compose/binaries/main-release/dmg/app.crossword.yourealwaysbe.forkyz_${APP_VERSION}.0.0.dmg" >> mac_job.env
#    - echo "MAC_JAR_URL=https://gitlab.com/Hague/forkyz/-/jobs/$CI_JOB_ID/artifacts/raw/composeApp/build/compose/jars/app.crossword.yourealwaysbe.forkyz-mac-x64-${APP_VERSION}.0.0-release.jar" >> mac_job.env
#  artifacts:
#    paths:
#      - composeApp/build/compose/binaries/main-release/dmg
#      - composeApp/build/compose/jars
#    expire_in: never
#    reports:
#      dotenv: mac_job.env

build_windows:
  stage: build
  tags:
    - saas-windows-medium-amd64
  rules:
    - if: $CI_COMMIT_TAG
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './'
  script:
    - ./gradlew packageReleaseMsi --no-daemon
    - ./gradlew packageReleaseUberJarForCurrentOS
    - ./gradlew --stop
  after_script:
    - $env:APP_VERSION = $env:CI_COMMIT_TAG -replace '^(v|alpha)(\d+).*','$2'
    - Add-Content -Path windows_job.env -Value "WINDOWS_APP_URL=https://gitlab.com/Hague/forkyz/-/jobs/$env:CI_JOB_ID/artifacts/raw/composeApp/build/compose/binaries/main-release/msi/app.crossword.yourealwaysbe.forkyz-$($env:APP_VERSION).0.0.msi"
    - Add-Content -Path windows_job.env -Value "WINDOWS_JAR_URL=https://gitlab.com/Hague/forkyz/-/jobs/$env:CI_JOB_ID/artifacts/raw/composeApp/build/compose/jars/app.crossword.yourealwaysbe.forkyz-windows-x64-$($env:APP_VERSION).0.0-release.jar"
  artifacts:
    paths:
      - composeApp/build/compose/binaries/main-release/msi
      - composeApp/build/compose/jars
    expire_in: never
    reports:
      dotenv: windows_job.env

release_job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: build_android
      artifacts: true
    - job: build_flatpak
      artifacts: true
    - job: build_linux
      artifacts: true
#    - job: build_mac
#      artifacts: true
    - job: build_windows
      artifacts: true
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - export APP_VERSION=$(echo $CI_COMMIT_TAG | sed 's/\(v\|alpha\)\([0-9]*\).*/\2/')
    - export CHANGELOG_FILE="${APP_VERSION}00000.txt"
    - export CHANGELOG_PATH="fastlane/metadata/android/en-US/changelogs/$CHANGELOG_FILE"
    - touch release_changelog.txt
    - |
      if [ -f "$CHANGELOG_PATH" ]
      then
        echo -e "# Changes\n" > release_changelog.txt
        cat "$CHANGELOG_PATH" >> release_changelog.txt
      fi
  release:
    tag_name: '$CI_COMMIT_TAG'
    description: 'release_changelog.txt'
    assets:
      links:
        - name: Android APK Install File
          direct_asset_path: /apk
          url: "$ANDROID_APP_URL"
          link_type: 'package'
        - name: Linux Flatpak (x86_64/amd64)
          direct_asset_path: /flatpak
          url: "$FLATPAK_URL"
          link_type: 'package'
        - name: Linux (.deb) Install File (x86_64/amd64)
          direct_asset_path: /deb
          url: "$DEB_APP_URL"
          link_type: 'package'
        - name: Linux Java Jar File (x86_64/amd64)
          direct_asset_path: /linuxjar
          url: "$DEB_JAR_URL"
          link_type: 'other'
#        - name: Mac Install File
#          direct_asset_path: /dmg
#          url: "$DMG_APP_URL"
#          link_type: 'package'
#        - name: Mac Java Jar File
#          direct_asset_path: /macjar
#          url: "$DMG_JAR_URL"
        - name: Windows Install File
          direct_asset_path: /msi
          url: "$WINDOWS_APP_URL"
          link_type: 'package'
        - name: Windows Java Jar File
          direct_asset_path: /windowsjar
          url: "$WINDOWS_JAR_URL"
          link_type: 'other'

create-pages:
  stage: pages
  image: node:lts
  script:
    - apt-get update && apt-get install -y python3
    - npm install showdown
    - cd docs
    - ./generate.sh
  pages: true
  artifacts:
    paths:
      - public
  rules:
    - when: always

