name: TrafficCue CI
on:
  push:
    branches: [main, staging]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: latest
      - uses: oven-sh/setup-bun@v2
      - name: Install dependencies
        run: bun i
      - name: Get licenses
        run: ./updateLicenses.sh
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
      - run: bun run build
      - name: Upload build artifact
        uses: actions/upload-artifact@v3
        with:
          name: dist
          path: dist/
      - name: Install rsync
        run: sudo apt-get update && sudo apt-get install -y rsync
      - name: Deploy build
        env:
          DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
          DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
          DEPLOY_PATH_MAIN: ${{ secrets.DEPLOY_PATH_MAIN }}
          DEPLOY_PATH_STAGING: ${{ secrets.DEPLOY_PATH_STAGING }}
          SSH_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
        run: |
          echo "$SSH_KEY" > private_key
          chmod 600 private_key

          if [ "${{ gitea.ref_name }}" = "main" ]; then
            TARGET_PATH=$DEPLOY_PATH_MAIN
          else
            TARGET_PATH=$DEPLOY_PATH_STAGING
          fi

          rsync -avz -e "ssh -i private_key -o StrictHostKeyChecking=no" dist/ $DEPLOY_USER@$DEPLOY_HOST:$TARGET_PATH/

          rm private_key
  build-android:
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: latest
      - uses: oven-sh/setup-bun@v2
      - name: Install dependencies
        run: bun i
      - name: Get licenses
        run: ./updateLicenses.sh
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: "zulu"
          java-version: "17"
      - name: Cache NDK
        uses: actions/cache@v4
        with:
          # Path where NDK version 27.0.11902837 is expected to be installed by setup-android
          path: /usr/local/lib/android/sdk/ndk/27.0.11902837
          key: ndk-${{ runner.os }}-27.0.11902837
      - name: Setup Android SDK
        uses: android-actions/setup-android@v3
      - name: Install NDK
        run: sdkmanager "ndk;27.0.11902837"
      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-linux-android,armv7-linux-androideabi,i686-linux-android,x86_64-linux-android
      - name: Setup Android signing
        run: |
          cd src-tauri/gen/android
          echo "keyAlias=$(echo ${{ secrets.ANDROID_KEY_ALIAS }} | base64 -d)" > keystore.properties
          echo "password=${{ secrets.ANDROID_KEY_PASSWORD }}" >> keystore.properties
          base64 -d <<< "${{ secrets.ANDROID_KEY_BASE64 }}" > $RUNNER_TEMP/keystore.jks
          echo "storeFile=$RUNNER_TEMP/keystore.jks" >> keystore.properties
          echo "Keystore properties created."
          cd ../../.. # Go back to root
      - name: Build app bundle
        run: |
          if [ "${{ gitea.ref_name }}" = "main" ]; then
            bun tauri android build -v --apk
          elif [ "${{ gitea.ref_name }}" = "staging" ]; then
            bun tauri android build -v --apk -d
          else
            echo "Branch not main or staging, skipping build."
          fi
        env:
          NDK_HOME: ${{ env.ANDROID_HOME }}/ndk/27.0.11902837
      - name: Upload release apk
        uses: actions/upload-artifact@v3
        with:
          name: trafficcue.apk
          path: ./src-tauri/gen/android/app/build/outputs/apk/universal/*/app-universal-*.apk
