default:
  image: "gitlab.futo.org:5050/keyboard/latinime/gradle:latest"

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  GIT_SUBMODULE_FORCE_HTTPS: "true"
  GIT_STRATEGY: clone
  GIT_DEPTH: 0

stages:          # List of stages for jobs, and their order of execution
  - build

buildUnstable:
  tags:
    - docker
  stage: build
  before_script:
    - git fetch origin master:master
    - export VERSION_NAME=`git describe --tags --dirty`
    - export VERSION_CODE=`git rev-list --first-parent --count master`
    - echo $VERSION_CODE $VERSION_NAME
    - ./setUpPropertiesCI.sh
  script:
    - gradle assembleUnstableRelease -s
    - mv build/outputs/apk/unstable/release/latinime-unstable-release.apk ./keyboard-unstable-$VERSION_NAME.apk
    - touch VERSION_CODE_$VERSION_CODE.txt
    - touch VERSION_NAME_$VERSION_NAME.txt
    - echo "Keyboard (Unstable) $VERSION_NAME - https://gitlab.futo.org/alex/latinime/-/jobs/$CI_JOB_ID/artifacts/raw/keyboard-unstable-$VERSION_NAME.apk"
    - echo $VERSION_CODE $VERSION_NAME
    - ./uploadNightly.sh ./keyboard-unstable-$VERSION_NAME.apk $VERSION_CODE $VERSION_NAME-unstable
  artifacts:
    name: "keyboard-unstable-$VERSION_NAME"
    paths:
      - ./*.apk
      - ./*.aab
      - ./*.txt
  when: manual

buildStable:
  tags:
    - docker
  stage: build
  before_script:
    - git fetch origin master:master
    - export VERSION_NAME=`git describe --tags`
    - export VERSION_CODE=`git rev-list --first-parent --count master`
    - echo $VERSION_CODE $VERSION_NAME
    - ./setUpPropertiesCI.sh
  script:
    - gradle bundlePlaystoreRelease -s
    - gradle assembleStableRelease -s
    - if grep -q "android.permission.INTERNET" build/intermediates/merged_manifests/playstoreRelease/AndroidManifest.xml; then echo "Internet permission check failed for play store"; exit 1; fi
    - if grep -q "android.permission.INTERNET" build/intermediates/merged_manifests/stableRelease/AndroidManifest.xml; then echo "Internet permission check failed for stable"; exit 1; fi
    - echo "All checks OK"
    - echo "Standalone permissions" && grep "uses-permission" build/intermediates/merged_manifests/stableRelease/AndroidManifest.xml
    - echo "Play Store permissions" && grep "uses-permission" build/intermediates/merged_manifests/playstoreRelease/AndroidManifest.xml
    - mv build/outputs/apk/stable/release/latinime-stable-release.apk ./keyboard-$VERSION_NAME.apk
    - mv build/outputs/bundle/playstoreRelease/latinime-playstore-release.aab ./keyboard-playstore-$VERSION_NAME.aab
    - touch VERSION_CODE_$VERSION_CODE.txt
    - touch VERSION_NAME_$VERSION_NAME.txt
    - echo "Keyboard $VERSION_NAME - https://gitlab.futo.org/alex/latinime/-/jobs/$CI_JOB_ID/artifacts/raw/keyboard-$VERSION_NAME.apk"
    - echo $VERSION_CODE $VERSION_NAME
  artifacts:
    name: "keyboard-stable-$VERSION_NAME"
    paths:
      - ./*.apk
      - ./*.aab
      - ./*.txt
  when: manual