---
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [main]
  pull_request:
    branches: [main]
    types: [opened, synchronize, ready_for_review, edited, labeled]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      run_always:
        description: 'Run even if no files are changed'
        required: true
        type: boolean
# Check if we indeed modified distrobox stuff
jobs:
  check_changes:
    runs-on: ubuntu-latest
    if: >-
      contains( github.event.pull_request.labels.*.name, 'CI') ||
      github.ref == 'refs/heads/main'
    outputs:
      distrobox_changed: ${{ steps.check_file_changed.outputs.distrobox_changed }}
    steps:

      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          # Checkout as many commits as needed for the diff
          fetch-depth: 2

      # Fetch from compatibility table all the distros supported
      - id: check_file_changed
        run: |
          if git diff --name-only HEAD^ HEAD | grep -E '\.go$|go\.mod|go\.sum|Makefile|docs/compatibility\.md|internal/inside-distrobox/assets/'; then
            echo "::set-output name=distrobox_changed::True"
          else
            echo "::set-output name=distrobox_changed::False"
          fi

  # Prepare distros matrix
  setup:
    runs-on: ubuntu-latest
    needs: check_changes
    outputs:
      targets: ${{ steps.set-matrix.outputs.targets }}
    if: >-
      needs.check_changes.outputs.distrobox_changed == 'True' ||
      github.event.inputs.run_always == 'True'
    steps:

      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      # Fetch from compatibility table all the distros supported
      - id: set-matrix
        run: |
            skip_list="bazzite|chimera|slackware|stream8|ublue|neon|steamos"

            echo "::set-output name=targets::$(sed -n -e '/| Alma/,/| Void/ p' docs/compatibility.md |
            cut -d'|' -f 4 |
            sed 's/<br>/\n/g' |
            tr -d ' ' |
            sed '/^[[:space:]]*$/d' |
            sort -u | grep -Ev "${skip_list}" |
            jq -R -s -c 'split("\n")[:-1]')"

  run:
    runs-on: ubuntu-latest
    needs: setup
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        distribution: ${{fromJSON(needs.setup.outputs.targets)}}
        container_manager: ["podman"] #, "docker"]
    env:
      XDG_CACHE_HOME: "/tmp/"
      DBX_CONTAINER_MANAGER: ${{ matrix.container_manager }}

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Setup Go
        uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
        with:
          go-version-file: go.mod

      - name: Build distrobox binary
        run: |
          make build
          cp ./bin/distrobox ./distrobox

      # Ensure distrobox create works:
      - name: Distrobox create
        shell: 'script -q -e -c "bash {0}"'
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ${DBX_CONTAINER_MANAGER} pull "${image}"
          # ./distrobox create --yes --absolutely-disable-root-password-i-am-really-positively-sure -i "${image}" --name "${container_name}"
          case "${container_name}" in
            *init*)
              echo "SYSTEMD DETECTED: creating container with --init..."
              ./distrobox create --yes -i "${image}" --hostname "${container_name}" --name "${container_name}" --init --unshare-all
              ;;
            *)
              ./distrobox create --yes -i "${image}" --hostname "${container_name}" --name "${container_name}"
              ;;
          esac

      # Ensure distrobox enter and init works:
      - name: Distrobox enter - init
        shell: 'script -q -e -c "bash {0}"'
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          case "${container_name}" in
            *init*)
              echo "SYSTEMD DETECTED: performing systemctl check..."
              ./distrobox enter --name "${container_name}" -- systemctl is-system-running | grep -E "degraded|running|starting"
              ;;
            *)
              ./distrobox enter --name "${container_name}" -- whoami
              ;;
          esac

      # Ensure distrobox enter and init works:
      - name: Distrobox enter - user
        shell: 'script -q -e -c "bash {0}"'
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          # Assert that distrobox exported binary indeed works
          set -x
          command_output="$(./distrobox enter --name "${container_name}" -- whoami | tr -d '\r' | tr -d '^@')"
          expected_output="$(whoami)"
          if [ "$command_output" != "$expected_output" ]; then
            exit 1
          fi

      # Ensure distrobox enter and init works:
      - name: Distrobox enter - command
        shell: 'script -q -e -c "bash {0}"'
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          # Assert that distrobox exported binary indeed works
          set -x
          command_output="$(./distrobox enter --name "${container_name}" -- uname -n | tr -d '\r' | tr -d '^@')"
          expected_output="${container_name}"
          if [ "$command_output" != "$expected_output" ]; then
            exit 1
          fi

      # Ensure distrobox export works:
      - name: Distrobox export
        shell: 'script -q -e -c "bash {0}"'
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ./distrobox enter "${container_name}" -- distrobox-export --bin /bin/uname --export-path ${HOME}/
          # Assert that distrobox exported binary indeed works
          set -x
          command_output="$(${HOME}/uname -n | tr -d '\r' | tr -d '^@')"
          expected_output="${container_name}"
          if [ "$command_output" != "$expected_output" ]; then
            exit 1
          fi

      # Ensure distrobox export works:
      - name: Distrobox export - sudo
        if: matrix.container_manager != 'docker'
        shell: 'script -q -e -c "bash {0}"'
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ./distrobox enter "${container_name}" -- distrobox-export --sudo --bin /bin/uname --export-path ${HOME}/
          # Assert that distrobox exported binary indeed works
          set -x
          command_output="$(${HOME}/uname -n | tr -d '\r' | tr -d '^@')"
          expected_output="${container_name}"
          if [ "$command_output" != "$expected_output" ]; then
            exit 1
          fi

      # Ensure distrobox upgrade works:
      - name: Distrobox upgrade
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ./distrobox upgrade "${container_name}"

      # Ensure distrobox list works:
      - name: Distrobox list
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ./distrobox list | grep "${container_name}" | grep "${image}" | grep -E "Up|running"

      # Ensure distrobox stop works:
      - name: Distrobox stop
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ./distrobox stop --yes "${container_name}"

      # Ensure distrobox rm works:
      - name: Distrobox logs
        if: ${{ always() }}
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          $DBX_CONTAINER_MANAGER logs "${container_name}"

      # Ensure distrobox rm works:
      - name: Distrobox rm
        if: ${{ always() }}
        run: |
          image=${{ matrix.distribution }}
          container_name="$(basename "${image}" | sed -E 's/[:.]/-/g')"
          ./distrobox rm --force "${container_name}"
          $DBX_CONTAINER_MANAGER rmi -f "${image}"
