name: Run Scripts on PVE Node
on:
  pull_request:
    branches:
      - main
    paths:
      - 'install/*.sh'
      - 'ct/*.sh'

jobs:
  run-install-script:
    runs-on: pvenode
    steps:          
      - name: Checkout PR branch
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0  
      - name: Add Git safe directory
        run: |
          git config --global --add safe.directory /__w/ProxmoxVE/ProxmoxVE
      
      - name: Set up GH_TOKEN
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          echo "GH_TOKEN=${GH_TOKEN}" >> $GITHUB_ENV

      - name: Get changed files in PR
        id: changed-files
        run: | 
          CHANGED_FILES=$(gh pr diff --name-only ${{ github.event.pull_request.number }})  
          CHANGED_FILES=$(echo "$CHANGED_FILES" | tr '\n' ' ')  
          echo "Changed files: $CHANGED_FILES"  
          echo "SCRIPT=$CHANGED_FILES" >> $GITHUB_ENV

      - name: Get scripts
        id: check-install-script
        run: |
          ALL_FILES=()
          ADDED_FILES=()  
          for FILE in ${{ env.SCRIPT }}; do           
            if [[ $FILE =~ ^install/.*-install\.sh$ ]] || [[ $FILE =~ ^ct/.*\.sh$ ]]; then             
              STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//')
              if [[ ! " ${ADDED_FILES[@]} " =~ " $STRIPPED_NAME " ]]; then
                ALL_FILES+=("$FILE")
                ADDED_FILES+=("$STRIPPED_NAME")  # Mark this base file as added (without the path)
              fi
            fi
          done
          ALL_FILES=$(echo "${ALL_FILES[@]}" | xargs)
          echo "$ALL_FILES"
          echo "ALL_FILES=$ALL_FILES" >> $GITHUB_ENV
        
      - name: Run scripts
        id: run-install
        continue-on-error: true
        run: |
          set +e  
          #run for each files in /ct
          for FILE in ${{ env.ALL_FILES }}; do            
            echo "Running: $FILE"
            STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//')
            if [[ $FILE =~ ^install/.*-install\.sh$ ]]; then
              CT_SCRIPT="ct/$STRIPPED_NAME.sh"
              if [[ ! -f $CT_SCRIPT ]]; then
                echo "No CT script found for $STRIPPED_NAME"
                exit 1
              fi
                echo "Found CT script for $STRIPPED_NAME"
                chmod +x "$CT_SCRIPT"
                RUNNING_FILE=$CT_SCRIPT
            elif [[ $FILE =~ ^ct/.*\.sh$ ]]; then
              INSTALL_SCRIPT="install/$STRIPPED_NAME-install.sh"
              if [[ ! -f $INSTALL_SCRIPT ]]; then
                echo "No install script found for $STRIPPED_NAME"
                exit 1
              fi
                echo "Found install script for $STRIPPED_NAME"
                chmod +x "$INSTALL_SCRIPT"
                RUNNING_FILE=$FILE
            fi
            git checkout origin/main .github/workflows/scripts/app-test/pr-build.func
            git checkout origin/main .github/workflows/scripts/app-test/pr-install.func
            git checkout origin/main .github/workflows/scripts/app-test/pr-alpine-install.func
            git checkout origin/main .github/workflows/scripts/app-test/pr-create-lxc.sh
            sed -i 's|source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)|source .github/workflows/scripts/app-test/pr-build.func|g' "$RUNNING_FILE"
            chmod +x $RUNNING_FILE         
            chmod +x .github/workflows/scripts/app-test/pr-create-lxc.sh
            chmod +x .github/workflows/scripts/app-test/pr-install.func
            chmod +x .github/workflows/scripts/app-test/pr-alpine-install.func
            chmod +x .github/workflows/scripts/app-test/pr-build.func

            ERROR_MSG=$(./$RUNNING_FILE 2>&1 > /dev/null)
            echo "Finished running $FILE"
            if [ -n "$ERROR_MSG" ]; then
              echo "ERROR in $STRIPPED_NAME: $ERROR_MSG"
              echo "$ERROR_MSG" > result_$STRIPPED_NAME.log
            fi
          done
          set -e  # Restore exit-on-error
      
      - name: Cleanup PVE Node
        run: |
          containers=$(pct list | tail -n +2 | awk '{print $0 " " $4}' | awk '{print $1}')
        
          for container_id in $containers; do
            status=$(pct status $container_id | awk '{print $2}')
            if [[ $status == "running" ]]; then
                pct stop $container_id
                pct destroy $container_id
            fi            
          done
      
      - name: Post error comments
        run: |
          ERROR="false"
          SEARCH_LINE=".github/workflows/scripts/app-test/pr-build.func: line"
          for FILE in ${{ env.ALL_FILES }}; do
            STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//')
            if [[ ! -f result_$STRIPPED_NAME.log ]]; then
              continue
            fi
            ERROR_MSG=$(cat result_$STRIPPED_NAME.log)
      
            if [ -n "$ERROR_MSG" ]; then
              CLEANED_ERROR_MSG=$(echo "$ERROR_MSG" | sed "s|$SEARCH_LINE.*||")
              echo "Posting error message for $FILE"
              echo ${CLEANED_ERROR_MSG}
              gh pr comment ${{ github.event.pull_request.number }} \
                --body ":warning: The script _**$FILE**_ failed with the following message: <br> <div><strong>${CLEANED_ERROR_MSG}</strong></div>"

              ERROR="true"
            fi
          done
          echo "ERROR=$ERROR" >> $GITHUB_ENV
      - name: Fail if error
        if: ${{ env.ERROR }} == 'true'
        run: exit 1 
