clone:
   git:
     image: woodpeckerci/plugin-git
     settings:
       partial: false

steps:
  check-if-released:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    environment:
      HC_URL_NIGHTLY_DEPLOY:
        from_secret: hc_url_nightly_deploy
    commands: |
      curl -fsS -m 10 --retry 5 "$${HC_URL_NIGHTLY_DEPLOY}/start"

      RELEASED_VERSION_CODE=$(curl https://freeyourgadget.codeberg.page/fdroid/repo/index-v1.json | jq '.packages."nodomain.freeyourgadget.gadgetbridge.nightly"[0].versionCode')
      CURRENT_VERSION_CODE=$(git rev-list HEAD --count)

      echo $CURRENT_VERSION_CODE
      echo $RELEASED_VERSION_CODE
      if [ "$RELEASED_VERSION_CODE" = "$CURRENT_VERSION_CODE" ]
      then
        echo "This version is already released, we quit!"
        touch .do_not_build
        curl -fsS -m 10 --retry 5 "$${HC_URL_NIGHTLY_DEPLOY}"
      fi


  build-apks:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - if [ -f .do_not_build ]; then return 0; fi
      # build the apks
      - echo "$${SIGNING_KEY}" | base64 -d > app/keystore.p12
      - ./gradlew assembleMainlineNightly -Dnightly_store_file="keystore.p12" -Dnightly_store_password="$${KEYSTOREPASS}" -Dnightly_key_alias="gadgetbridge" -Dnightly_key_password="$${KEYPASS}"
      - ./gradlew assembleMainlineNopebble -Dnightly_store_file="keystore.p12" -Dnightly_store_password="$${KEYSTOREPASS}" -Dnightly_key_alias="gadgetbridge" -Dnightly_key_password="$${KEYPASS}"
      - ./gradlew assembleBanglejsNightly -Dnightly_store_file="keystore.p12" -Dnightly_store_password="$${KEYSTOREPASS}" -Dnightly_key_alias="gadgetbridge" -Dnightly_key_password="$${KEYPASS}"
    environment:
      SIGNING_KEY:
        from_secret: signing_key
      KEYSTOREPASS:
        from_secret: keystorepass
      KEYPASS:
        from_secret: keypass

  pull-repo-config:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - if [ -f .do_not_build ]; then return 0; fi
      - cd ..
      - pwd
      - mkdir repoconfig
      - cd repoconfig
      - pwd
      - git clone --depth 1 https://codeberg.org/Freeyourgadget/fdroid-repo-config.git
      - ./fdroid-repo-config/repoconfig/copy_files_to_repo_config.sh
    environment:
      COMMIT_TOKEN:
        from_secret: commit_token

  pull-pages:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - if [ -f .do_not_build ]; then return 0; fi
      # prepare the pages repo, so fdroidclient can push files into it later
      - cd ..
      - pwd
      - mkdir pages
      - cd pages
      - pwd
      - git clone --depth=1 https://codeberg.org/Freeyourgadget/pages.git
      - rm -rf pages/.git

  update-fdroid-data:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - if [ -f .do_not_build ]; then return 0; fi
      - pwd
      - ../repoconfig/fdroid-repo-config/repoconfig/prepare_changelog.sh
      - cd ../repoconfig/fdroid-repo-config/repoconfig
      # get secrets from storage  
      - echo "$${SIGNING_KEY}" | base64 -d > keystore.p12
      - sed -i "s#KEYSTOREPASS_PLACEHOLDER#$${KEYSTOREPASS}#g" config.yml
      - sed -i "s#KEYPASS_PLACEHOLDER#$${KEYPASS}#g" config.yml
      - fdroid update
      - cp ./repo/nodomain.freeyourgadget.gadgetbridge.nightly/en-US/icon.png ./repo/icons/icon.png
      - python3 repo_listing.py > listing.html
      - sed -i "s#</body>#LISTING\n</body>#" ./repo/index.html
      - sed -i -e "/LISTING/r listing.html" -e '//d' ./repo/index.html
      - fdroid deploy
      - ls -laR repo
    environment:
      SIGNING_KEY:
        from_secret: signing_key
      KEYSTOREPASS:
        from_secret: keystorepass
      KEYPASS:
        from_secret: keypass

  push-pages-update-repo:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - if [ -f .do_not_build ]; then return 0; fi
      # We can't use the git-pages-cli docker image as it does not contain sh and we can't check if we built or not
      - curl -fsSL https://codeberg.org/git-pages/git-pages-cli/releases/download/v1.9.0/git-pages-cli.linux-amd64 -o ../git-pages-cli
      - chmod +x ../git-pages-cli
      - ../git-pages-cli https://freeyourgadget.codeberg.page --token "$${COMMIT_TOKEN}" --upload-dir ../pages/pages/
    environment:
      COMMIT_TOKEN:
        from_secret: commit_token

  on-success:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - curl -fsS -m 10 --retry 5 "$${HC_URL_NIGHTLY_DEPLOY}"
    when:
      status: success
    environment:
      HC_URL_NIGHTLY_DEPLOY:
        from_secret: hc_url_nightly_deploy

  on-error:
    image: codeberg.org/freeyourgadget/android-fdroid-tools:36.0.0-androguard-4.1.2-java-21
    commands:
      - curl -fsS -m 10 --retry 5 "$${HC_URL_NIGHTLY_DEPLOY}/fail"
      - cd ..
      - mkdir -p repoconfig
      - cd repoconfig
      - git clone --depth 1 https://codeberg.org/Freeyourgadget/fdroid-repo-config.git || true
      - ./fdroid-repo-config/repoconfig/create_issue_comment.sh "CI failed"
    when:
      status: failure
    environment:
      HC_URL_NIGHTLY_DEPLOY:
        from_secret: hc_url_nightly_deploy
      COMMIT_TOKEN:
        from_secret: commit_token

when:
  - event: cron
  - event: deployment
    branch: master
    evaluate: 'CI_PIPELINE_DEPLOY_TARGET == "nightly"'
