variables:
    VERSION_PATTERN: '/^v\d+\.\d+(\.\d+)?$/'
    GIT_SUBMODULE_STRATEGY: recursive
    LIB_RELEASE_FOLDER: 'Borepin\Borepin\bin\Release'
    UWP_RELEASE_FOLDER: 'Borepin\Borepin.UWP\bin\x86\Release'
    TEST_FOLDER: 'Tests\bin\Release'

    WIN_NUGET_PATH: 'C:\ProgramData\chocolatey\bin\nuget.exe'
    WIN_MSBUILD_PATH: 'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\msbuild.exe'
    WIN_NUNIT_PATH: 'C:\ProgramData\chocolatey\bin\nunit3-console.exe'

stages:
    - build
    - test
    - pack
    - deploy

build_Base:
    stage: build
    tags:
        - fabinfra
        - public
        - windows
        - powershell
        - xamarin
    script:
        - '& "$WIN_NUGET_PATH" restore'
        - '& "$WIN_MSBUILD_PATH" /t:Restore /p:Configuration=Debug /t:Borepin'

build_Android:
    stage: build
    needs:
        - build_Base
    tags:
        - fabinfra
        - public
        - windows
        - powershell
        - xamarin
    script:
        - '& "$WIN_NUGET_PATH" restore'
        - '& "$WIN_MSBUILD_PATH" /t:Restore /p:Configuration=Debug /t:Borepin_Android'

build_UWP:
    stage: build
    needs:
        - build_Base
    tags:
        - fabinfra
        - public
        - windows
        - powershell
        - xamarin
    script:
        - '& "$WIN_NUGET_PATH" restore'
        - '& "$WIN_MSBUILD_PATH" /t:Restore /p:Configuration=Debug /t:Borepin_UWP'

build_iOS:
    stage: build
    needs:
        - build_Base
    tags:
        - fabinfra
        - public
        - macos
        - shell
        - xamarin
    script:
        - 'nuget restore'
        - 'msbuild /t:Restore'
        - 'msbuild /p:Configuration=Debug /t:Borepin_iOS'

build_GTK:
    stage: build
    needs:
        - build_Base
    tags:
        - docker
    image: registry.gitlab.com/fabinfra/gtk-sharp-build:latest
    script:
        - 'nuget restore'
        - 'msbuild -t:Restore'
        - 'msbuild -p:Configuration=Debug -t:Borepin_GTK'

pack_Android_AAB:
    stage: pack
    needs:
        - build_Android
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - fabinfra
        - internal
        - windows
        - powershell
        - xamarin
    before_script:
        - '$Env:VERSION_NUMBER="$CI_COMMIT_TAG".SubString(1)'
        - '$Env:BUILD_NUMBER="$CI_PIPELINE_ID"'
    script:
        - '& "$WIN_NUGET_PATH" restore'
        - '[System.IO.File]::WriteAllBytes("$(pwd)/fabaccess.keystore", [System.Convert]::FromBase64String($AndroidKeyStore))'
        - '& "$WIN_MSBUILD_PATH" /t:Restore /t:Borepin_Android:SetVersion /p:Configuration=Release /target:Borepin_Android:PackageForAndroid /target:Borepin_Android:SignAndroidPackage /p:AndroidKeyStore="True" /p:AndroidSigningKeyStore="$(pwd)/fabaccess.keystore" /p:AndroidSigningKeyPass="$AndroidKeyStore_Password" /p:AndroidSigningKeyAlias="$AndroidKeyStore_ID" /p:AndroidSigningStorePass="$AndroidKeyStore_Password"'
        - 'rm "$(pwd)/fabaccess.keystore"'
    artifacts:
        expire_in: 1 week
        paths:
            - Borepin/Borepin.Android/bin/Release/org.fab_infra.fabaccess-Signed.aab

pack_Android_APK:
    stage: pack
    needs:
        - build_Android
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - fabinfra
        - internal
        - windows
        - powershell
        - xamarin
    before_script:
        - '$Env:VERSION_NUMBER="$CI_COMMIT_TAG".SubString(1)'
        - '$Env:BUILD_NUMBER="$CI_PIPELINE_ID"'
        - '$Env:ANDROID_PKG_FORMAT="apk"'
    script:
        - '& "$WIN_NUGET_PATH" restore'
        - '[System.IO.File]::WriteAllBytes("$(pwd)/fabaccess.keystore", [System.Convert]::FromBase64String($AndroidKeyStore))'
        - '& "$WIN_MSBUILD_PATH" /t:Restore /t:Borepin_Android:SetVersion /p:Configuration=Release /target:Borepin_Android /target:Borepin_Android:SignAndroidPackage /p:AndroidKeyStore="True" /p:AndroidSigningKeyStore="$(pwd)/fabaccess.keystore" /p:AndroidSigningKeyPass="$AndroidKeyStore_Password" /p:AndroidSigningKeyAlias="$AndroidKeyStore_ID" /p:AndroidSigningStorePass="$AndroidKeyStore_Password"'
        - 'rm "$(pwd)/fabaccess.keystore"'
    artifacts:
        expire_in: 1 week
        paths:
            - Borepin/Borepin.Android/bin/Release/org.fab_infra.fabaccess-Signed.apk

pack_FDroid_APK:
    stage: pack
    needs:
        - build_Android
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - fabinfra
        - internal
        - windows
        - powershell
        - xamarin
    before_script:
        - '$Env:VERSION_NUMBER="$CI_COMMIT_TAG".SubString(1)'
        - '$Env:BUILD_NUMBER="$CI_PIPELINE_ID"'
        - '$Env:ANDROID_PKG_FORMAT="apk"'
    script:
        - '& "$WIN_NUGET_PATH" restore'
        - '[System.IO.File]::WriteAllBytes("$(pwd)/fabaccess.keystore", [System.Convert]::FromBase64String($FDroidKeyStore))'
        - '& "$WIN_MSBUILD_PATH" /t:Restore /t:Borepin_Android:SetVersion /p:Configuration=Release /target:Borepin_Android /target:Borepin_Android:SignAndroidPackage /p:AndroidKeyStore="True" /p:AndroidSigningKeyStore="$(pwd)/fabaccess.keystore" /p:AndroidSigningKeyPass="$FDroidKeyStore_Password" /p:AndroidSigningKeyAlias="$FDroidKeyStore_ID" /p:AndroidSigningStorePass="$FDroidKeyStore_Password"'
        - 'rm "$(pwd)/fabaccess.keystore"'
    artifacts:
        expire_in: 1 week
        paths:
            - Borepin/Borepin.Android/bin/Release/org.fab_infra.fabaccess-Signed.apk

pack_iOS:
    stage: pack
    needs:
        - build_iOS
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - fabinfra
        - internal
        - macos
        - shell
        - xamarin
    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
    script:
        - 'nuget restore'
        - 'msbuild /t:Restore'
        - 'msbuild /t:Borepin_iOS:SetVersion /t:Borepin_iOS /p:Configuration=Release /p:Platform=iPhone /p:ArchiveOnBuild=true /p:BuildIpa=true'
    artifacts:
        expire_in: 1 week
        paths:
            - Borepin/Borepin.iOS/bin/iPhone/Release/Borepin.iOS.ipa

deploy_Android_Internal:
    stage: deploy
    needs:
        - pack_Android_AAB
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - docker
    image: registry.gitlab.com/fabinfra/gtk-sharp-build:latest
    variables:
        LC_ALL: 'en_US.UTF-8'
        LANG: 'en_US.UTF-8'

    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'echo $play_store_credentials > play-store-credentials.json'
    script:
        - 'bundle install'
        - 'bundle exec fastlane supply --aab Borepin/Borepin.Android/bin/Release/org.fab_infra.fabaccess-Signed.aab --track internal'
    after_script:
        - 'rm play-store-credentials.json'

pages:
    stage: deploy
    needs:
        - pack_FDroid_APK
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - docker
    image:
        name: ubuntu
        # name: registry.gitlab.com/fdroid/docker-executable-fdroidserver:master
    variables: 
        DEBIAN_FRONTEND: noninteractive
        TZ: Europe/Berlin
    before_script:
        - 'apt-get update -yqq && apt-get install -yqq fdroidserver'
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'cp -r fdroid/* .'
        - 'echo "keystorepass: ${FDroidKeyStore_Password}" >> config.yml'
        - 'echo "keypass: ${FDroidKeyStore_Password}" >> config.yml'
        - 'echo ${FDroidKeyStore} | base64 -d > keystore.p12'
        # - 'cat config.yml'
        # - sha1sum keystore.p12
    script:
        - 'mkdir -p repo'
        - 'cp Borepin/Borepin.Android/bin/Release/org.fab_infra.fabaccess-Signed.apk repo/'
        - 'fdroid update'
        - 'mkdir -p public/fdroid'
        - 'cp -r repo public/fdroid/'
        - 'rm keystore.p12'
    artifacts:
        paths:
            - public

upload_binaries:
  stage: deploy
  needs:
    - pack_Android_APK
  tags:
    - docker
  image: curlimages/curl:latest
  before_script: []
  cache: []
  script:
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file Borepin/Borepin.Android/bin/Release/org.fab_infra.fabaccess-Signed.apk "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/borepin/${CI_COMMIT_TAG}/org.fab_infra.fabaccess-Signed.apk"'
  rules:
    - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN

release_job:
  stage: deploy
  needs:
    - upload_binaries
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  tags:
    - docker
  rules:
    - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    - if: $CI_COMMIT_TAG =~ "release/.*"
      when: never
  when: manual
  script:
    - echo "Creating GitLab release…"
  release:
    name: "Borepin ${CI_COMMIT_TAG}"
    description: "GitLab CI auto-created release"
    tag_name: "release/${CI_COMMIT_TAG}"
    assets:
      links:
        - name: 'Borepin signed apk'
          url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/borepin/${CI_COMMIT_TAG}/org.fab_infra.fabaccess-Signed.apk"

deploy_Android_Beta:
    stage: deploy
    needs:
        - deploy_Android_Internal
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    when: manual
    tags:
        - docker
    image: registry.gitlab.com/fabinfra/gtk-sharp-build:latest
    variables:
        LC_ALL: 'en_US.UTF-8'
        LANG: 'en_US.UTF-8'
    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'echo $play_store_credentials > play-store-credentials.json'
    script:
        - 'bundle install'
        - 'bundle exec fastlane supply --track internal --track_promote_to beta'
    after_script:
        - 'rm play-store-credentials.json'

deploy_Android_Production:
    stage: deploy
    needs:
        - deploy_Android_Beta
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    when: manual
    tags:
        - docker
    image: registry.gitlab.com/fabinfra/gtk-sharp-build:latest
    variables:
        LC_ALL: 'en_US.UTF-8'
        LANG: 'en_US.UTF-8'
    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'echo $play_store_credentials > play-store-credentials.json'
    script:
        - 'bundle install'
        - 'bundle exec fastlane supply --track internal --track_promote_to production'
    after_script:
        - 'rm play-store-credentials.json'

deploy_iOS_Internal:
    stage: deploy
    needs:
        - pack_iOS
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    tags:
        - fabinfra
        - internal
        - macos
        - shell
        - xamarin
    variables:
        LC_ALL: 'en_US.UTF-8'
        LANG: 'en_US.UTF-8'
    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'echo $app_store_credentials > app-store-credentials.json'
        - 'export PATH="/usr/local/opt/ruby/bin:$PATH"'
    script:
        - 'bundle install'
        - 'bundle update fastlane'
        - 'bundle exec fastlane pilot upload --api_key_path app-store-credentials.json --ipa Borepin/Borepin.iOS/bin/iPhone/Release/Borepin.iOS.ipa'
    after_script:
        - 'rm app-store-credentials.json'

deploy_iOS_Beta:
    stage: deploy
    needs:
        - pack_iOS
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    when: manual
    tags:
        - fabinfra
        - internal
        - macos
        - shell
        - xamarin
    variables:
        LC_ALL: 'en_US.UTF-8'
        LANG: 'en_US.UTF-8'
    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'echo $app_store_credentials > app-store-credentials.json'
        - 'export PATH="/usr/local/opt/ruby/bin:$PATH"'
    script:
        - 'bundle install'
        - 'bundle update fastlane'
        - 'bundle exec fastlane pilot upload --api_key_path app-store-credentials.json --ipa Borepin/Borepin.iOS/bin/iPhone/Release/Borepin.iOS.ipa --distribute_external true --groups Beta --changelog "Bugfixes and/or new features"'
    after_script:
        - 'rm app-store-credentials.json'

deploy_iOS_Production:
    stage: deploy
    needs:
        - deploy_iOS_Beta
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ $VERSION_PATTERN
    when: manual
    tags:
        - fabinfra
        - internal
        - macos
        - shell
        - xamarin
    variables:
        LC_ALL: 'en_US.UTF-8'
        LANG: 'en_US.UTF-8'
    before_script:
        - 'export VERSION_NUMBER=${CI_COMMIT_TAG:1}'
        - 'export BUILD_NUMBER="$CI_PIPELINE_ID"'
        - 'echo $app_store_credentials > app-store-credentials.json'
        - 'export PATH="/usr/local/opt/ruby/bin:$PATH"'
    script:
        - 'bundle install'
        - 'bundle update fastlane'
        - 'bundle exec fastlane deliver submit_build --build_number $BUILD_NUMBER --submit_for_review true --automatic_release true --force true --skip_metadata true --skip_screenshots true --skip_binary_upload true --api_key_path app-store-credentials.json'
    after_script:
        - 'rm app-store-credentials.json'