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 (x86)\Microsoft Visual Studio\2019\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 =~ /v\d+\.\d+(\.\d+)?/
    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 =~ /v\d+\.\d+(\.\d+)?/
    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_iOS:
    stage: pack
    needs:
        - build_iOS
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ /v\d+\.\d+(\.\d+)?/
    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 =~ /v\d+\.\d+(\.\d+)?/
    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'

deploy_Android_Beta:
    stage: deploy
    needs:
        - deploy_Android_Internal
    rules:
        - if: $CI_COMMIT_TAG != null && $CI_COMMIT_TAG =~ /v\d+\.\d+(\.\d+)?/
    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 =~ /v\d+\.\d+(\.\d+)?/
    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 =~ /v\d+\.\d+(\.\d+)?/
    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 =~ /v\d+\.\d+(\.\d+)?/
    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 =~ /v\d+\.\d+(\.\d+)?/
    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'