#=========================================ANDROID===================================================
# This is the workflow responsible for verifying a PR before it gets merged. It will build the
# project on every PR opened to the main branch

name: Android 02 - Snapshot & Distribution

on:
    workflow_dispatch:
    push:
        paths:
            - 'android/**'
            - '.github/workflows/android*'
        branches: [ main ]

jobs:
    build:
        name: Build release version of the project
        runs-on: ubuntu-latest
        timeout-minutes: 15
        steps:
            -   name: Checkout repo including tags
                uses: actions/checkout@v3
                # Fetch depth needs to be 0 in order to pull the merge messages into the release notes
                with:
                    fetch-depth: 0
            -   run: git fetch origin 'refs/tags/*:refs/tags/*'

            # Setup caches
            -   name: Cache Gradle.
                uses: actions/cache@v3
                with:
                    path: |
                        ~/.gradle/caches
                        ~/.gradle/wrapper
                    key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
                    restore-keys: |
                        ${{ runner.os }}-gradle-
            -   name: Cache Maven.
                uses: actions/cache@v3
                with:
                    path: ~/.m2/repository
                    key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
                    restore-keys: |
                        ${{ runner.os }}-maven-
            -   name: Configure Java for the runner
                uses: actions/setup-java@v1
                with:
                    java-version: '17'

            -   name: Auto-populate incremented build number & create tag
                run: |
                    ./android/scripts/increment_snapshot_number.sh
                    ./android/scripts/create_new_release_tag.sh androidsnapshot

            -   name: Get the firebase credentials from secrets and put them in a json file
                run: |
                    echo $GOOGLE_APPLICATION_CREDENTIALS_JSON > firebase.json
                env:
                    GOOGLE_APPLICATION_CREDENTIALS_JSON: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
            - name: Create eduroam keystore
              run: |
                  echo "${{ secrets.PRODUCTION_KEYSTORE }}" > release.keystore.asc
                  mkdir -p ./android/app/keystore/
                  gpg -d --passphrase "${{ secrets.PRODUCTION_KEYSTORE_PASSWORD }}" --batch release.keystore.asc > ./android/app/keystore/release.keystore
                  rm release.keystore.asc
            -   name: Build & upload the release version of the geteduroam app to Firebase App Distribution
                env:
                    GOOGLE_APPLICATION_CREDENTIALS: ./firebase.json
                    PRODUCTION_KEYSTORE_PASSWORD: ${{ secrets.PRODUCTION_KEYSTORE_PASSWORD }}
                    PRODUCTION_KEYSTORE_ALIAS: ${{ secrets.PRODUCTION_KEYSTORE_ALIAS }}
                    FIREBASE_APP_ID: "1:918090700616:android:c3e9a20483c21df224857d"
                run: |
                    ./gradlew :android:app:assembleEduroamGooglePlayRelease :android:app:appDistributionUploadEduroamGooglePlayRelease
            -   name: Build & upload the release version of the geteduroam app to the Google Play Console
                env:
                    PRODUCTION_KEYSTORE_PASSWORD: ${{ secrets.PRODUCTION_KEYSTORE_PASSWORD }}
                    PRODUCTION_KEYSTORE_ALIAS: ${{ secrets.PRODUCTION_KEYSTORE_ALIAS }}
                    ANDROID_PUBLISHER_CREDENTIALS: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT }}
                run: |
                    ./gradlew :android:app:publishEduroamGooglePlayReleaseApk
            - name: Create govroam keystore
              run: |
                  rm ./android/app/keystore/release.keystore
                  echo "${{ secrets.GOVROAM_KEYSTORE }}" > release.keystore.asc
                  mkdir -p ./android/app/keystore/
                  gpg -d --passphrase "${{ secrets.GOVROAM_KEYSTORE_PASSWORD }}" --batch release.keystore.asc > ./android/app/keystore/release.keystore
                  rm release.keystore.asc
            -   name: Build & upload the release version of the getgovroam app to Firebase App Distribution
                env:
                    GOOGLE_APPLICATION_CREDENTIALS: ./firebase.json
                    PRODUCTION_KEYSTORE_PASSWORD: ${{ secrets.GOVROAM_KEYSTORE_PASSWORD }}
                    PRODUCTION_KEYSTORE_ALIAS: ${{ secrets.GOVROAM_KEYSTORE_ALIAS }}
                    FIREBASE_APP_ID: "1:918090700616:android:005572e384c12a7024857d"
                run: |
                    ./gradlew :android:app:assembleGovroamGooglePlayRelease :android:app:appDistributionUploadGovroamGooglePlayRelease
            -   name: Build & upload the release version of the getgovroam app to the Google Play Console
                env:
                    PRODUCTION_KEYSTORE_PASSWORD: ${{ secrets.GOVROAM_KEYSTORE_PASSWORD }}
                    PRODUCTION_KEYSTORE_ALIAS: ${{ secrets.GOVROAM_KEYSTORE_ALIAS }}
                    ANDROID_PUBLISHER_CREDENTIALS: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT }}
                run: |
                    ./gradlew :android:app:publishGovroamGooglePlayReleaseApk
            -   name: Push tag for snapshot
                env:
                    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
                run: |
                    git config user.name "github-actions"
                    git config user.email "github-actions@github.com"
                    git push --tags
