name: Nightly

on:
  schedule:
    # every night at midnight
    - cron:  '0 0 * * *'

jobs:

  release:
    runs-on: ubuntu-22.04
    steps:

      - name: checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: temurin

      - name: Decrypt files
        env:
          KEYSTORE: ${{ secrets.keystore }}
          SSH_KEY: ${{ secrets.ssh_key }}
        run: |
          echo "$KEYSTORE" | base64 -d > kiwix-android.keystore
          echo "$SSH_KEY" | base64 -d > ssh_key
          chmod 600 ssh_key
          
      - name: build debug
        env:
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
          KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
        run: ./gradlew assembleNightly

      - name: Set date variable
        run: echo "DATE=$(echo $(date +%Y-%m-%d))" >> $GITHUB_ENV

      - name: release debug to kiwix.download.org
        env:
          UNIVERSAL_DEBUG_APK: app/build/outputs/apk/nightly/*universal*.apk
        run: |
          mkdir $DATE
          cp $UNIVERSAL_DEBUG_APK $DATE
          scp -P 30022 -vrp -i ssh_key -o StrictHostKeyChecking=no $DATE ci@master.download.kiwix.org:/data/download/nightly/
