name: Make Release

on:
  workflow_dispatch:

jobs:

  release-build:
    runs-on: ubuntu-latest
    steps:

    - name: Checkout code
      uses: actions/checkout@v3
      with:
        submodules: 'recursive'

    - name: Get versionName
      run: echo "versionName=$(grep -oP 'versionName \"\K[^\"]+' ./app/build.gradle)" >> $GITHUB_ENV

    - name: Validate Gradle wrapper
      uses: gradle/wrapper-validation-action@v1

    - name: Load secrets
      run: |
        rm ./app/google-services.json
        echo $GSERVICES > ./app/google-services.json
        echo $KEYSTORE | base64 -di > ./app/$KEYSTORE_FILENAME
        echo "$KEYSTORE_PROPERTIES" > ./keystore.properties
      env:
          GSERVICES: ${{ secrets.GSERVICES }}
          KEYSTORE: ${{ secrets.KEYSTORE }}
          KEYSTORE_FILENAME: ${{ secrets.KEYSTORE_FILENAME }}
          KEYSTORE_PROPERTIES: ${{ secrets.KEYSTORE_PROPERTIES }}

    - name: Set up JDK 11
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'zulu'
        # Note: we don't use caches on release builds because we don't want to accidentally not have a virgin build machine

    - name: Find firmware release tag
      id: firmware_version
      uses: oprypin/find-latest-tag@v1
      with:
        repository: meshtastic/Meshtastic-device
        releases-only: true
        token: ${{ secrets.GITHUB_TOKEN }}

    - name: Create version strings
      run: |
        echo "CUR_FIRMWARE_VERSION=$(echo ${{ steps.firmware_version.outputs.tag }}|sed -e 's/^v//')" >> $GITHUB_ENV
        echo "SHORT_FIRMWARE_VERSION=$(echo ${{ steps.firmware_version.outputs.tag }}|sed -e 's/^v//' -e 's/.\{8\}$//')" >> $GITHUB_ENV

    - name: Create curfirmwareversion.xml
      run: |
        rm ./app/src/main/res/values/curfirmwareversion.xml
        echo -e "<resources>\n    <string name=\"cur_firmware_version\" translatable=\"false\">${{ env.CUR_FIRMWARE_VERSION }}</string>\n    <string name=\"short_firmware_version\" translatable=\"false\">${{ env.SHORT_FIRMWARE_VERSION }}</string>\n</resources>" > ./app/src/main/res/values/curfirmwareversion.xml

#    - name: Fetch firmware release
#      uses: dsaltares/fetch-gh-release-asset@master
#      with:
#        repo: meshtastic/Meshtastic-device
#        file: firmware-${{ env.CUR_FIRMWARE_VERSION }}.zip
#        version: tags/${{ steps.firmware_version.outputs.tag }}
#        target: firmware.zip
#        token: ${{ secrets.GITHUB_TOKEN }}

#    - name: Unzip firmware into assets/firmware
#      run: |
#        rm -rf ./app/src/main/assets/firmware
#        mkdir -p ./app/src/main/assets/firmware
#        unzip -qq ./firmware.zip 'littlefs-*.bin' 'firmware-heltec*.bin' 'firmware-tbeam*.bin' 'firmware-tlora*.bin' 'firmware-nano*.bin' -d ./app/src/main/assets/firmware
#        rm ./firmware.zip

    - name: Mock firmware assets for release
      run: |
        rm -rf ./app/src/main/assets/firmware
        mkdir -p ./app/src/main/assets/firmware

    - name: Build F-Droid release
      run: ./gradlew assembleFdroidRelease

    - name: Enable Crashlytics
      run: sed -i 's/useCrashlytics = false/useCrashlytics = true/g' ./build.gradle

    - name: Build Play Store release
      run: ./gradlew bundleGoogleRelease assembleGoogleRelease

#    - name: Upload Release to Play Store
#      run: cp ./app/src/main/play/release-notes/en-US/production.txt ./app/src/main/play/release-notes/whatsnew-en-US.txt
#    - uses: r0adkll/upload-google-play@v1
#      with:
#        serviceAccountJsonPlainText: ${{ secrets.GSERVICES }}
#        packageName: com.geeksville.mesh
#        releaseFiles: app/build/outputs/bundle/release/app-release.aab
#        track: internal
#        whatsNewDirectory: app/src/main/play/release-notes
#        mappingFile: app/build/outputs/mapping/release/mapping.txt

    - name: Create GitHub release
      uses: actions/create-release@v1
      id: create_release
      with:
        draft: true
        prerelease: true
        release_name: Meshtastic Android ${{ env.versionName }} alpha
        tag_name: ${{ env.versionName }}
        body: |
          Autogenerated by github action, developer should edit as required before publishing...
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Add F-Droid APK to release
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: app/build/outputs/apk/fdroid/release/app-fdroid-release.apk
        asset_name: fdroidRelease-${{ env.versionName }}.apk
        asset_content_type: application/zip

    - name: Add Play Store AAB to release
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: app/build/outputs/bundle/googleRelease/app-google-release.aab
        asset_name: googleRelease-${{ env.versionName }}.aab
        asset_content_type: application/zip

    - name: Add Play Store APK to release
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: app/build/outputs/apk/google/release/app-google-release.apk
        asset_name: googleRelease-${{ env.versionName }}.apk
        asset_content_type: application/zip

#    - name: Gradle publishBundle
#      run: ./gradlew publishBundle
