name: Update_Raspbian_Addons_apps

# Controls when the workflow will run
on:
  # run every hour
  schedule:
    - cron: 5 * * * *
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  update-raspbian-addons:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a set of commands using the runners shell
      - name: Run app update scripts
        run: |
          #source pi-apps functions

          #export all functions and variables
          set -a
          #make DIRECTORY equal to GITHUB_WORKSPACE, for subscripts and api functions
          DIRECTORY=$GITHUB_WORKSPACE
          source $GITHUB_WORKSPACE/api

          #add special functions
          get_release() {
            curl -s --header "Authorization: token ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}" "https://api.github.com/repos/$1/releases/latest" | jq -r '.tag_name' | sed s/v//g
          }

          get_prerelease() {
            curl -s --header "Authorization: token ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}" "https://api.github.com/repos/$1/releases" | jq -r 'map(select(.prerelease)) | first | .tag_name' | sed s/v//g
          }

          function validate_url(){
            if command wget --timeout=5 --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36" -q --spider "$1"; then
              return 0
            else
              return 1
            fi
          }

          #stop exporting functions
          set +a

          #make sure all update scripts are executable
          chmod +x $GITHUB_WORKSPACE/.github/workflows/updates/*.sh
          cd $GITHUB_WORKSPACE
          apps=( .github/workflows/updates/*.sh )
          for app_directory in "${apps[@]}"; do
            # make sure we are still in the main workspace (incase an update script left off elsewhere)
            cd $GITHUB_WORKSPACE
            # check if this is a raspbian addons application by checking for string https://apt.raspbian-addons.org
            if grep -q "https://apt.raspbian-addons.org" "$app_directory"; then
              export app_name="$(echo ${app_directory%.*} | sed 's:.*/::')"
              echo "$app_name"
              status "Checking $app_name for updates from apt.raspbian-addons.org"
              # move to app folder
              cd "$GITHUB_WORKSPACE/apps/$app_name"

              # run app update script
              "$GITHUB_WORKSPACE/$app_directory"
            fi
          done
          cd
          if test -f /tmp/updated_apps; then
            sort -u /tmp/updated_apps > /tmp/updated_apps_sorted
            echo "UPDATED_APPS<<EOF" >> $GITHUB_ENV
            cat /tmp/updated_apps_sorted >> $GITHUB_ENV
            echo "EOF" >> $GITHUB_ENV
          fi

          if test -f /tmp/failed_apps; then
            echo "FAILED_APPS<<EOF" >> $GITHUB_ENV
            cat /tmp/failed_apps | sed '0~1 a\\' >> $GITHUB_ENV
            echo "EOF" >> $GITHUB_ENV
          fi

      - name: Push any changes to repo
        uses: EndBug/add-and-commit@v9
        with:
          default_author: github_actions
          message: |
            Update App Versions, run by GitHub Actions - Apps updated:

            ${{ env.UPDATED_APPS }}

