name: 'Maximize build disk space'
description: 'Maximize the available disk space by removing unneeded software'

inputs:
  purge-packages:
    description: whether to remove apt packages
    required: false
    default: 'false'

runs:
  using: "composite"
  steps:
    - name: Disk space report before modification
      shell: bash
      run: |
          echo "==> Available space before cleanup"
          echo
          df -h

    - name: Maximize build disk space
      shell: bash
      run: |
          set -xeuo pipefail
          shopt -s globstar

          non_manifest_packages() {
            comm -2 -3 \
              <(sudo apt-mark showmanual | sort -u) \
              <(grep --perl-regexp --only-matching '^[\w-.+]+' "${{ github.action_path }}"/ubuntu-24.04.1-live-server-amd64.manifest | sort -u)
          }

          echo "Removing unwanted software... "

          if [[ "${{ inputs.purge-packages }}" == 'true' ]]; then
            sudo apt-get purge \
              --allow-remove-essential \
              $(non_manifest_packages)
          fi


          sudo rm -rf \
            /var/lib/docker/ \
            "$AGENT_TOOLSDIRECTORY" \
            /opt &

          sudo find /usr/{share,local} \
            -mindepth 1 -maxdepth 1 \
            -type d \
            -not -wholename '/usr/share/git-core' \
            -exec rm -rf {} + &

          {
            sudo swapoff -a
            sudo rm -f /mnt/swapfile
          } &

          wait
          echo "... done"


    - name: Disk space report after modification
      shell: bash
      run: |
          echo "==> Available space after cleanup"
          echo
          df -h