name: Cache Dependencies

on:
  workflow_dispatch:
  push:
    branches: [ main ]

jobs:
  test:

    runs-on: ubuntu-20.04

    steps:
      - name: Free Disk Space (Ubuntu)
        uses: insightsengineering/disk-space-reclaimer@v1
        with:
          tools-cache: true
          android: false
          dotnet: true
          haskell: true
          large-packages: true
          swap-storage: true
          docker-images: true

      - uses: actions/checkout@v2
      - uses: actions/setup-java@v2
        with:
          distribution: "temurin"
          java-version: "17"
      - name: Configure placeholder git details
        run: |
          git config --global user.email "CI@cakewallet.com"
          git config --global user.name "Cake Github Actions"
      - name: Flutter action
        uses: subosito/flutter-action@v1
        with:
          flutter-version: "3.24.4"
          channel: stable

      - name: Install package dependencies
        run: sudo apt-get install -y curl unzip automake build-essential file pkg-config git python libtool libtinfo5 cmake clang

      - name: Execute Build and Setup Commands
        run: |
          sudo mkdir -p /opt/android
          sudo chown $USER /opt/android
          cd /opt/android
          -y curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
          cargo install cargo-ndk
          git clone https://github.com/cake-tech/cake_wallet.git --branch main
          cd cake_wallet/scripts/android/
          ./install_ndk.sh
          source ./app_env.sh cakewallet
          chmod +x pubspec_gen.sh
          ./app_config.sh

      - name: Cache Externals
        id: cache-externals
        uses: actions/cache@v3
        with:
          path: |
            /opt/android/cake_wallet/cw_haven/android/.cxx
            /opt/android/cake_wallet/scripts/monero_c/release
          key: ${{ hashFiles('**/prepare_moneroc.sh' ,'**/build_monero_all.sh' ,'**/cache_dependencies.yml') }}

      - if: ${{ steps.cache-externals.outputs.cache-hit != 'true' }}
        name: Generate Externals
        run: |
          cd /opt/android/cake_wallet/scripts/android/
          source ./app_env.sh cakewallet
          ./build_monero_all.sh

      - name: Cache Keystore
        id: cache-keystore
        uses: actions/cache@v3
        with:
          path: /opt/android/cake_wallet/android/app
          key: keystore

      - if: ${{ steps.cache-keystore.outputs.cache-hit != 'true' }}
        name: Generate KeyStore
        run: |
          cd /opt/android/cake_wallet/android/app
          keytool -genkey -v -keystore key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias testKey -noprompt -dname "CN=CakeWallet, OU=CakeWallet, O=CakeWallet, L=Florida, S=America, C=USA" -storepass $STORE_PASS -keypass $KEY_PASS