Full Code of 89luca89/distrobox for AI

main c6cc6a3d5bf5 cached
113 files
626.5 KB
186.8k tokens
1 requests
Download .txt
Showing preview only (660K chars total). Download the full file or copy to clipboard to get everything.
Repository: 89luca89/distrobox
Branch: main
Commit: c6cc6a3d5bf5
Files: 113
Total size: 626.5 KB

Directory structure:
gitextract_321fe374/

├── .gitattributes
├── .github/
│   ├── ISSUE_TEMPLATE/
│   │   ├── bug_report.md
│   │   ├── discussion-issue-template.md
│   │   └── feature_request.md
│   ├── dependabot.yml
│   └── workflows/
│       ├── compatibility.yml
│       ├── main.yml
│       └── manpages.yml
├── .gitignore
├── .markdownlint.yaml
├── .shellcheckrc
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── COPYING.md
├── Containerfile
├── completions/
│   ├── bash/
│   │   ├── distrobox
│   │   ├── distrobox-assemble
│   │   ├── distrobox-create
│   │   ├── distrobox-enter
│   │   ├── distrobox-ephemeral
│   │   ├── distrobox-generate-entry
│   │   ├── distrobox-list
│   │   ├── distrobox-rm
│   │   ├── distrobox-stop
│   │   └── distrobox-upgrade
│   └── zsh/
│       ├── _distrobox
│       ├── _distrobox-assemble
│       ├── _distrobox-create
│       ├── _distrobox-enter
│       ├── _distrobox-ephemeral
│       ├── _distrobox-export
│       ├── _distrobox-generate-entry
│       ├── _distrobox-host-exec
│       ├── _distrobox-init
│       ├── _distrobox-list
│       ├── _distrobox-rm
│       ├── _distrobox-stop
│       ├── _distrobox-upgrade
│       ├── _distrobox_containers
│       ├── _distrobox_images
│       └── _distrobox_running_containers
├── distrobox
├── distrobox-assemble
├── distrobox-create
├── distrobox-enter
├── distrobox-ephemeral
├── distrobox-export
├── distrobox-generate-entry
├── distrobox-host-exec
├── distrobox-init
├── distrobox-list
├── distrobox-rm
├── distrobox-stop
├── distrobox-upgrade
├── docs/
│   ├── 404.md
│   ├── CNAME
│   ├── Gemfile
│   ├── README.md
│   ├── _config.yml
│   ├── _includes/
│   │   ├── footer.html
│   │   ├── head.html
│   │   └── header.html
│   ├── _layouts/
│   │   └── default.html
│   ├── assets/
│   │   ├── brand/
│   │   │   └── png/
│   │   │       └── distros/
│   │   │           └── base-distrobox-1.xcf
│   │   └── credits.md
│   ├── compatibility.md
│   ├── featured_articles.md
│   ├── posts/
│   │   ├── distrobox_custom.md
│   │   ├── execute_commands_on_host.md
│   │   ├── install_lilipod_static.md
│   │   ├── install_podman_static.md
│   │   ├── integrate_vscode_distrobox.md
│   │   ├── posts.md
│   │   ├── run_latest_gnome_kde_hyprland_on_distrobox.md
│   │   ├── run_libvirt_in_distrobox.md
│   │   └── steamdeck_guide.md
│   ├── style.css
│   ├── usage/
│   │   ├── distrobox-assemble.md
│   │   ├── distrobox-create.md
│   │   ├── distrobox-enter.md
│   │   ├── distrobox-ephemeral.md
│   │   ├── distrobox-export.md
│   │   ├── distrobox-generate-entry.md
│   │   ├── distrobox-host-exec.md
│   │   ├── distrobox-init.md
│   │   ├── distrobox-list.md
│   │   ├── distrobox-rm.md
│   │   ├── distrobox-stop.md
│   │   ├── distrobox-upgrade.md
│   │   └── usage.md
│   └── useful_tips.md
├── extras/
│   ├── distrobox-example-manifest.ini
│   ├── docker-host
│   ├── install-podman
│   ├── podman-host
│   └── vscode-distrobox
├── install
├── man/
│   ├── gen-man
│   └── man1/
│       ├── distrobox-assemble.1
│       ├── distrobox-compatibility.1
│       ├── distrobox-create.1
│       ├── distrobox-enter.1
│       ├── distrobox-ephemeral.1
│       ├── distrobox-export.1
│       ├── distrobox-generate-entry.1
│       ├── distrobox-host-exec.1
│       ├── distrobox-init.1
│       ├── distrobox-list.1
│       ├── distrobox-rm.1
│       ├── distrobox-stop.1
│       ├── distrobox-upgrade.1
│       └── distrobox.1
└── uninstall

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitattributes
================================================
* text=auto eol=lf


================================================
FILE: .github/ISSUE_TEMPLATE/bug_report.md
================================================
---
name: Bug report
about: Create a report to help us improve
title: "[Error]"
labels: bug
assignees: ''

---

Please, before opening a bug:

- make sure you've read the documentation.
- Ensure there isn't already an open issue about this.
- Ensure there isn't already a closed/resolved issue about this.

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior

**Expected behavior**
A clear and concise description of what you expected to happen.

**Logs**
Run the commands with `--verbose` and post the log here as a file upload
Attach also the output of `podman logs` or `docker logs`, possibly with `--latest` flag

**Desktop (please complete the following information):**

- Are you using podman, docker or lilipod?
- Which version or podman, docker or lilipod?
- Which version of distrobox?
- Which host distribution?
- How did you install distrobox?

**Additional context**
Add any other context about the problem here.


================================================
FILE: .github/ISSUE_TEMPLATE/discussion-issue-template.md
================================================
---
name: Discussion issue template
about: Describe this issue template's purpose here.
title: "[Discussion]"
labels: question
assignees: ''

---

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.


================================================
FILE: .github/ISSUE_TEMPLATE/feature_request.md
================================================
---
name: Feature request
about: Suggest an idea for this project
title: "[Suggestion]"
labels: enhancement
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.


================================================
FILE: .github/dependabot.yml
================================================
version: 2
updates:
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "monthly"


================================================
FILE: .github/workflows/compatibility.yml
================================================
---
# 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@v6
        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 -Ev "host-exec|generate-entry|ephemeral|upgrade" | grep -E "^distrobox|compatibility.md"; 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@v6

      # 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@v6

      # 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}"


================================================
FILE: .github/workflows/main.yml
================================================
# This is a basic workflow to help you get started with Actions

name: Lint

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

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  dash:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Run dash -n
        run: |
          result=0
          for file in $(find . -type f -not -path "*.git*" -a -not -path "*completions*"); do
            if file "$file" | grep -qi shell; then
              echo "### Checking file $file..."
              dash -n $file
              result=$(( result + $? ))
            fi
          done
          exit $result

  shfmt:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Run shfmt
        run: |
          result=0
          podman pull docker.io/peterdavehello/shfmt:latest
          for file in $(find . -type f -not -path "*.git*"); do
            if file "$file" | grep -qi shell; then
              echo "### Checking file $file..."
              podman run --rm -v "$PWD:/mnt" docker.io/peterdavehello/shfmt:latest shfmt -d -s -ci -sr -kp -fn -i=0 -p /mnt/$file
              result=$(( result + $? ))
            fi
          done
          exit $result


  shellcheck:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      # Exclude from bashate the following rules:
      #   - SC2310 we don't want to exit if errors happen inside a check, that's why we have a check...
      #   - SC2311 don't care if we inherit errexit inside substitutions, we do checks for that.
      #   - SC2312 we already check errors and adding "|| true" everywhere hinders readability.
      - name: Run shellcheck
        run: |
          result=0
          podman pull docker.io/koalaman/shellcheck:stable
          for file in $(find . -type f -name ".*" -prune -o -print | grep -v '.git'); do
            if file "$file" | grep -qi shell; then
              echo "### Checking file $file..."
              # Should read the .shellcheckrc file to behave like  -s sh -a -o all -Sstyle -Calways -x -e SC2310,SC2311,SC2312
              podman run --rm -v "$PWD:/mnt" docker.io/koalaman/shellcheck:stable -a -Sstyle -Calways $file
              result=$(( result + $? ))
            fi
          done
          exit $result

  differential-shellcheck:
    runs-on: ubuntu-latest

    permissions:
      contents: read
      security-events: write

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Run Differential ShellCheck
        uses: redhat-plumbers-in-action/differential-shellcheck@v5
        with:
          severity: style
          token: ${{ secrets.GITHUB_TOKEN }}

  bashate:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      # Exclude from bashate the following rules:
      #   - E002 we use tab indentation as suggested by shfmt.
      #   - E003 we use tab indentation as suggested by shfmt.
      #   - E010 for readability allow if/then and for/do to be on different lines.
      #   - E011 for readability allow if/then and for/do to be on different lines.
      - name: Run bashate
        run: |
          sudo pip3 install -U bashate
          for file in $(find . -type f -not -path "*.git*"); do
            if file "$file" | grep -qi shell; then
              echo "### Checking file $file..."
               bashate -i E002,E003,E010,E011 --max-line-length 120 $file
              result=$(( result + $? ))
            fi
          done
          exit $result

  markdownlint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Run markdownlint
        run: |
          sudo npm install -g markdownlint-cli
          markdownlint $(find . -name '*.md' | grep -vF './.git')

  codespell:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: codespell-project/actions-codespell@v2
        with:
          skip: .git,*.pdf,*.1,*.css,*.lock

  shell-funcheck:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Run shell-funcheck
        run: |
          curl -L -O https://github.com/89luca89/shell-funcheck/releases/download/v0.0.1/shell-funcheck-amd64
          chmod +x ./shell-funcheck-amd64
          for i in distrobox*; do
          ./shell-funcheck-amd64 check "$i"
          done


================================================
FILE: .github/workflows/manpages.yml
================================================
---
# This is a basic workflow to help you get started with Actions

name: Docs

# 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]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:

  # Check changes, cancel job is not.
  check_changes:
    runs-on: ubuntu-latest
    outputs:
      distrobox_changed: ${{ steps.check_file_changed.outputs.distrobox_changed }}
    steps:

      - uses: actions/checkout@v6
        with:
          # Checkout as many commits as needed for the diff
          repository: 89luca89/distrobox
          ref: main
          persist-credentials: false
          fetch-depth: 2
          token: ${{ secrets.GITHUB_TOKEN }}

      # Fetch from compatibility table all the distros supported
      - id: check_file_changed
        run: |
          if git diff --name-only HEAD^ HEAD  | grep -E "^docs|gen-man"; then
            echo "::set-output name=distrobox_changed::True"
          else
            echo "::set-output name=distrobox_changed::False"
          fi

  gen_man:
    runs-on: ubuntu-latest
    needs: check_changes
    if: needs.check_changes.outputs.distrobox_changed == 'True'
    permissions:
      contents: write
    steps:

      - uses: actions/checkout@v6
        with:
          token: ${{ secrets.PAT }}

      # Fetch from compatibility table all the distros supported
      - id: generate
        run: |
          VERSION=3.6.1
          RELEASE="jgm/pandoc/releases/download/${VERSION}/pandoc-${VERSION}-1-amd64.deb"
          NAME=$(echo $RELEASE | rev | cut -d'/' -f1 | rev)

          curl -L https://github.com/$RELEASE -o $NAME
          sudo apt-get update
          sudo apt-get install -y ./$NAME
          sudo apt-get install -y ronn

          rm -f $NAME

          man/gen-man

      - uses: stefanzweifel/git-auto-commit-action@v7
        with:
          branch: main
          commit_message: Automatic Man Page Update
          commit_options: '--no-verify --signoff'
          commit_user_name: distrobox-docs-bot
          commit_user_email: distrobox-docs-bot@users.noreply.github.com
          commit_author: distrobox-docs-bot <actions@github.com>


================================================
FILE: .gitignore
================================================
tags
*.vim
TODO.txt


================================================
FILE: .markdownlint.yaml
================================================
---
MD013:
  line_length: 120
  code_blocks: false
  tables: false
  headings: false
  headers: false
MD025: false
MD033: false
MD041: false
MD045: false
MD059: false


================================================
FILE: .shellcheckrc
================================================
# Overrides the shell detected from the shebang. This is useful for files meant to be included (and thus lacking a shebang), or possibly as a more targeted alternative to 'disable=SC2039'.
shell=sh

# Always allow ShellCheck to open arbitrary files from 'source' statements.
external-sources=true

# Enable all optional checks
enable=all

# This function is invoked in an 'if' condition so set -e will be disabled. Invoke separately if failures should cause the script to exit.
# - We don't want to exit if errors happen inside a check, that's why we have a check...
disable=SC2310

# Bash implicitly disabled set -e for this function invocation because it's inside a command substitution. Add set -e; before it or enable inherit_errexit.
# - Don't care if we inherit errexit inside substitutions, we do checks for that.
disable=SC2311

# Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).
# - We already check errors and adding "|| true" everywhere hinders readability.
disable=SC2312


================================================
FILE: CODE_OF_CONDUCT.md
================================================
# Code of Conduct

Let's just all be nice to each other and ourselves. Do we really need this?

Just follow them:

![wholesome](https://i.kym-cdn.com/photos/images/newsfeed/001/407/983/841.jpg)

## Let's just all be wholesome to each other please


================================================
FILE: CONTRIBUTING.md
================================================
# Contributing to Distrobox

We greatly appreciate your input! We want to make contributing to this project
as easy and transparent as possible, whether it's:

- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features

## Creating a Pull Requests

Pull requests are the best way to propose changes to the codebase.  
We actively welcome your pull requests:

1. Fork the repo and create your branch from `main`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. Issue that pull request!

## Any contributions you make will be under the GPLv3 Software License

In short, when you submit code changes, your submissions are understood to be
under the same [GPLv3 License](https://choosealicense.com/licenses/gpl-3.0/) that
covers the project.
Feel free to contact the maintainers if that's a concern.

## Suggestions

Suggestions are welcome, be sure:

- It is not already being discussed in the [issue tracker](https://github.com/89luca89/distrobox/issues)
  - If it has and is marked as OPEN, go ahead and share your own
    thoughts about the topic!
  - If it has and is marked as CLOSED, please read the ticket and depending on
    whether the suggestion was accepted consider if it is worth opening
    a new issue or not.
- Consider if the suggestion is not too out of scope of the project.
- Mark them with a [Suggestion] in the title.

## Report bugs using GitHub's [issues](https://github.com/89luca89/distrobox/issues)

We use GitHub issues to track public bugs.
Report a bug by
[opening a new issue](https://github.com/89luca89/distrobox/issues); it's that easy!

### Write bug reports with detail, background, and sample code

**A good bug report** should have:

- Check that the bug is not already discussed in the [issue tracker](https://github.com/89luca89/distrobox/issues)
- See our [documentation](https://github.com/89luca89/distrobox/tree/main/docs)
  if there are some steps that could help you solve your issue
- Mark them with an [Error] in the title
- A quick summary and/or background
- Steps to reproduce
  - Be specific!
  - Provide logs (terminal output, runs with verbose mode)
- What you expected would happen
- What actually happens
- Notes (possibly including why you think this might be happening, or stuff you
  tried that didn't work)

## Use a Consistent Coding Style

- check if files have some problems with POSIX using the following:

```shell
for file in $(find . -type f -not -path "*.git*"); do
    if file "$file" | grep -qi shell; then
      echo "### Checking file $file..."
      dash -n $file
      result=$(( result + $? ))
      echo "Result: $result"
    fi
done
```

  Here we're using `dash` to verify if there are any non-POSIX code inside the
  scripts. Distrobox aims to be POSIX compliant so it's important to use a
  strict POSIX compliant shell to verify. `dash` is available in all major distributions'
  repositories.

- use `shellcheck` to check for posix compliance and bashisms using:
  - install from: [HERE](https://github.com/koalaman/shellcheck)
    following [this](https://github.com/koalaman/shellcheck#installing)
  - `shellcheck -s sh -a -o all -Sstyle -Calways -x -e SC2310,SC2311,SC2312`
- use `shfmt` to style the code using:
  - install from [HERE](https://github.com/mvdan/sh) using `go install mvdan.cc/sh/v3/cmd/shfmt@latest`
  - `shfmt shfmt -d -s -ci -sr -kp`
- use `bashate` to check the code:
  - install using `pip3 install bashate`
  - `bashate -i E002,E003,E010,E011 --max-line-length 120`
- use `markdownlint`
  - install using `npm -i -g markdownlint-cli`
  - run `markdownlint $(find . -name '*.md' | grep -vF './.git')`
- Legibility of the code is more important than code golfing, try to be
  expressive in the code
- Try to **follow the happy path**:
  - [This guide](https://maelvls.dev/go-happy-line-of-sight/) is for golang,
    but it's a very insightful source to follow
- Error checking is important! Ensure to LBYL (Look Before You Leap), check for
  variables and for code success exit codes
- If a command or function can fail, ensure you check the outcome:
  - `if ! my_function; then ...`
    this is important to handle errors gracefully and to potentially warn users
    of what's happening
- Use snake_case for variable naming. Keep variable names lowercase if they are
  not an environment variable
- Don't hesitate to comment your code! We're placing high importance on this to
  maintain the code readable and understandeable
- Update documentation to reflect your changes - Manual pages can be found in
  directory `docs`

If you are using Visual Studio Code, there are [plugins](https://marketplace.visualstudio.com/items?itemName=timonwong.shellcheck)
that include all this functionality and throw a warning if you're doing
something wrong.
If you are using Vim or Emacs there are plenty of linters and checkers that will
integrate with the 2 tools listed above.

## License

By contributing, you agree that your contributions will be licensed under
its GPLv3 License.

## References

This document was adapted from the open-source contribution guidelines
for [Facebook's Draft](https://github.com/facebook/draft-js/blob/a9316a723f9e918afde44dea68b5f9f39b7d9b00/CONTRIBUTING.md).


================================================
FILE: COPYING.md
================================================
# GNU GENERAL PUBLIC LICENSE

Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc.
<https://fsf.org/>

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.

## Preamble

The GNU General Public License is a free, copyleft license for
software and other kinds of works.

The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom
to share and change all versions of a program--to make sure it remains
free software for all its users. We, the Free Software Foundation, use
the GNU General Public License for most of our software; it applies
also to any other work released this way by its authors. You can apply
it to your programs, too.

When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.

To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you
have certain responsibilities if you distribute copies of the
software, or if you modify it: responsibilities to respect the freedom
of others.

For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.

Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.

For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.

Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the
manufacturer can do so. This is fundamentally incompatible with the
aim of protecting users' freedom to change the software. The
systematic pattern of such abuse occurs in the area of products for
individuals to use, which is precisely where it is most unacceptable.
Therefore, we have designed this version of the GPL to prohibit the
practice for those products. If such problems arise substantially in
other domains, we stand ready to extend this provision to those
domains in future versions of the GPL, as needed to protect the
freedom of users.

Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish
to avoid the special danger that patents applied to a free program
could make it effectively proprietary. To prevent this, the GPL
assures that patents cannot be used to render the program non-free.

The precise terms and conditions for copying, distribution and
modification follow.

### TERMS AND CONDITIONS

#### 0. Definitions

"This License" refers to version 3 of the GNU General Public License.

"Copyright" also means copyright-like laws that apply to other kinds
of works, such as semiconductor masks.

"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.

To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of
an exact copy. The resulting work is called a "modified version" of
the earlier work or a work "based on" the earlier work.

A "covered work" means either the unmodified Program or a work based
on the Program.

To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.

To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user
through a computer network, with no transfer of a copy, is not
conveying.

An interactive user interface displays "Appropriate Legal Notices" to
the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.

#### 1. Source Code

The "source code" for a work means the preferred form of the work for
making modifications to it. "Object code" means any non-source form of
a work.

A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.

The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.

The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.

The Corresponding Source need not include anything that users can
regenerate automatically from other parts of the Corresponding Source.

The Corresponding Source for a work in source code form is that same
work.

#### 2. Basic Permissions

All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.

You may make, run and propagate covered works that you do not convey,
without conditions so long as your license otherwise remains in force.
You may convey covered works to others for the sole purpose of having
them make modifications exclusively for you, or provide you with
facilities for running those works, provided that you comply with the
terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for
you must do so exclusively on your behalf, under your direction and
control, on terms that prohibit them from making any copies of your
copyrighted material outside their relationship with you.

Conveying under any other circumstances is permitted solely under the
conditions stated below. Sublicensing is not allowed; section 10 makes
it unnecessary.

#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law

No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.

When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such
circumvention is effected by exercising rights under this License with
respect to the covered work, and you disclaim any intention to limit
operation or modification of the work as a means of enforcing, against
the work's users, your or third parties' legal rights to forbid
circumvention of technological measures.

#### 4. Conveying Verbatim Copies

You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.

You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.

#### 5. Conveying Modified Source Versions

You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these
conditions:

- a) The work must carry prominent notices stating that you modified
    it, and giving a relevant date.
- b) The work must carry prominent notices stating that it is
    released under this License and any conditions added under
    section 7. This requirement modifies the requirement in section 4
    to "keep intact all notices".
- c) You must license the entire work, as a whole, under this
    License to anyone who comes into possession of a copy. This
    License will therefore apply, along with any applicable section 7
    additional terms, to the whole of the work, and all its parts,
    regardless of how they are packaged. This License gives no
    permission to license the work in any other way, but it does not
    invalidate such permission if you have separately received it.
- d) If the work has interactive user interfaces, each must display
    Appropriate Legal Notices; however, if the Program has interactive
    interfaces that do not display Appropriate Legal Notices, your
    work need not make them do so.

A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.

#### 6. Conveying Non-Source Forms

You may convey a covered work in object code form under the terms of
sections 4 and 5, provided that you also convey the machine-readable
Corresponding Source under the terms of this License, in one of these
ways:

- a) Convey the object code in, or embodied in, a physical product
    (including a physical distribution medium), accompanied by the
    Corresponding Source fixed on a durable physical medium
    customarily used for software interchange.
- b) Convey the object code in, or embodied in, a physical product
    (including a physical distribution medium), accompanied by a
    written offer, valid for at least three years and valid for as
    long as you offer spare parts or customer support for that product
    model, to give anyone who possesses the object code either (1) a
    copy of the Corresponding Source for all the software in the
    product that is covered by this License, on a durable physical
    medium customarily used for software interchange, for a price no
    more than your reasonable cost of physically performing this
    conveying of source, or (2) access to copy the Corresponding
    Source from a network server at no charge.
- c) Convey individual copies of the object code with a copy of the
    written offer to provide the Corresponding Source. This
    alternative is allowed only occasionally and noncommercially, and
    only if you received the object code with such an offer, in accord
    with subsection 6b.
- d) Convey the object code by offering access from a designated
    place (gratis or for a charge), and offer equivalent access to the
    Corresponding Source in the same way through the same place at no
    further charge. You need not require recipients to copy the
    Corresponding Source along with the object code. If the place to
    copy the object code is a network server, the Corresponding Source
    may be on a different server (operated by you or a third party)
    that supports equivalent copying facilities, provided you maintain
    clear directions next to the object code saying where to find the
    Corresponding Source. Regardless of what server hosts the
    Corresponding Source, you remain obligated to ensure that it is
    available for as long as needed to satisfy these requirements.
- e) Convey the object code using peer-to-peer transmission,
    provided you inform other peers where the object code and
    Corresponding Source of the work are being offered to the general
    public at no charge under subsection 6d.

A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.

A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal,
family, or household purposes, or (2) anything designed or sold for
incorporation into a dwelling. In determining whether a product is a
consumer product, doubtful cases shall be resolved in favor of
coverage. For a particular product received by a particular user,
"normally used" refers to a typical or common use of that class of
product, regardless of the status of the particular user or of the way
in which the particular user actually uses, or expects or is expected
to use, the product. A product is a consumer product regardless of
whether the product has substantial commercial, industrial or
non-consumer uses, unless such uses represent the only significant
mode of use of the product.

"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to
install and execute modified versions of a covered work in that User
Product from a modified version of its Corresponding Source. The
information must suffice to ensure that the continued functioning of
the modified object code is in no case prevented or interfered with
solely because modification has been made.

If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).

The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or
updates for a work that has been modified or installed by the
recipient, or for the User Product in which it has been modified or
installed. Access to a network may be denied when the modification
itself materially and adversely affects the operation of the network
or violates the rules and protocols for communication across the
network.

Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.

#### 7. Additional Terms

"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.

When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.

Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders
of that material) supplement the terms of this License with terms:

- a) Disclaiming warranty or limiting liability differently from the
    terms of sections 15 and 16 of this License; or
- b) Requiring preservation of specified reasonable legal notices or
    author attributions in that material or in the Appropriate Legal
    Notices displayed by works containing it; or
- c) Prohibiting misrepresentation of the origin of that material,
    or requiring that modified versions of such material be marked in
    reasonable ways as different from the original version; or
- d) Limiting the use for publicity purposes of names of licensors
    or authors of the material; or
- e) Declining to grant rights under trademark law for use of some
    trade names, trademarks, or service marks; or
- f) Requiring indemnification of licensors and authors of that
    material by anyone who conveys the material (or modified versions
    of it) with contractual assumptions of liability to the recipient,
    for any liability that these contractual assumptions directly
    impose on those licensors and authors.

All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.

If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.

Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions; the
above requirements apply either way.

#### 8. Termination

You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).

However, if you cease all violation of this License, then your license
from a particular copyright holder is reinstated (a) provisionally,
unless and until the copyright holder explicitly and finally
terminates your license, and (b) permanently, if the copyright holder
fails to notify you of the violation by some reasonable means prior to
60 days after the cessation.

Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.

Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.

#### 9. Acceptance Not Required for Having Copies

You are not required to accept this License in order to receive or run
a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.

#### 10. Automatic Licensing of Downstream Recipients

Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.

An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.

You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.

#### 11. Patents

A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".

A contributor's "essential patent claims" are all patent claims owned
or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.

Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.

In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.

If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.

If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.

A patent license is "discriminatory" if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on
the non-exercise of one or more of the rights that are specifically
granted under this License. You may not convey a covered work if you
are a party to an arrangement with a third party that is in the
business of distributing software, under which you make payment to the
third party based on the extent of your activity of conveying the
work, and under which the third party grants, to any of the parties
who would receive the covered work from you, a discriminatory patent
license (a) in connection with copies of the covered work conveyed by
you (or copies made from those copies), or (b) primarily for and in
connection with specific products or compilations that contain the
covered work, unless you entered into that arrangement, or that patent
license was granted, prior to 28 March 2007.

Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.

#### 12. No Surrender of Others' Freedom

If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under
this License and any other pertinent obligations, then as a
consequence you may not convey it at all. For example, if you agree to
terms that obligate you to collect a royalty for further conveying
from those to whom you convey the Program, the only way you could
satisfy both those terms and this License would be to refrain entirely
from conveying the Program.

#### 13. Use with the GNU Affero General Public License

Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.

#### 14. Revised Versions of this License

The Free Software Foundation may publish revised and/or new versions
of the GNU General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in
detail to address new problems or concerns.

Each version is given a distinguishing version number. If the Program
specifies that a certain numbered version of the GNU General Public
License "or any later version" applies to it, you have the option of
following the terms and conditions either of that numbered version or
of any later version published by the Free Software Foundation. If the
Program does not specify a version number of the GNU General Public
License, you may choose any version ever published by the Free
Software Foundation.

If the Program specifies that a proxy can decide which future versions
of the GNU General Public License can be used, that proxy's public
statement of acceptance of a version permanently authorizes you to
choose that version for the Program.

Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.

#### 15. Disclaimer of Warranty

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION.

#### 16. Limitation of Liability

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

#### 17. Interpretation of Sections 15 and 16

If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.

END OF TERMS AND CONDITIONS

### How to Apply These Terms to Your New Programs

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.

To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively state
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.

        <one line to give the program's name and a brief idea of what it does.>
        Copyright (C) <year>  <name of author>

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <https://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper
mail.

If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

        <program>  Copyright (C) <year>  <name of author>
        This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
        This is free software, and you are welcome to redistribute it
        under certain conditions; type `show c' for details.

The hypothetical commands \`show w' and \`show c' should show the
appropriate parts of the General Public License. Of course, your
program's commands might be different; for a GUI interface, you would
use an "about box".

You should also get your employer (if you work as a programmer) or
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. For more information on this, and how to apply and follow
the GNU GPL, see <https://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your
program into proprietary programs. If your program is a subroutine
library, you may consider it more useful to permit linking proprietary
applications with the library. If this is what you want to do, use the
GNU Lesser General Public License instead of this License. But first,
please read <https://www.gnu.org/licenses/why-not-lgpl.html>.


================================================
FILE: Containerfile
================================================
FROM  docker.io/mgoltzsche/podman:latest

COPY ./distrobox* /usr/bin/


================================================
FILE: completions/bash/distrobox
================================================
# shellcheck disable=all

_generate_from_help() {
	command=$1
	# if command does not exist, try with `distrobox subcommand`
	if ! $command --help >/dev/null 2>/dev/null ; then
		command="$(echo $command | tr '-' ' ')"
	fi

	local list cur prev totalopts opts diropts

	COMPREPLY=()

	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD - 1]}"
	opts="$($command --help 2>/dev/null | sed 's|^\t||g' | grep '^--' | cut -d':' -f1 | tr '/' ' ' | tr '\n' ' ')"
	totalopts="$(echo $opts | tr ' ' '|')"
	diropts="-H|--home|--volume"
	fileopts="--file"

	if [[ ${prev} =~ ${diropts} ]]; then
		mapfile -t COMPREPLY < <(
			IFS=$'\n'
			realpath $(compgen -d -- ${cur}) 2>/dev/null
		)
		compopt -o filenames
		return 0
	fi

	if [[ ${prev} =~ ${fileopts} ]]; then
		COMPREPLY=($(compgen -f -- ${cur}))
		compopt -o filenames
		return 0
	fi
	if [[ ${prev} == "assemble" ]]; then
		COMPREPLY+=($(compgen -W "create rm" -- ${cur}))
		return 0
	fi

	if [[ ${prev} == "--image" ]] || [[ ${prev} == "-i" ]]; then
		COMPREPLY+=($(compgen -W "$(distrobox-create --compatibility)" -- ${cur}))
		return 0
	fi

	if [[ ${cur} == -* ]]; then
		COMPREPLY+=($(compgen -W "${opts}" -- ${cur}))
		return 0
	elif [[ ${command} != *"create"* ]] && [[ ${command} != *"ephemeral"* ]] &&
		[[ ${command} != *"list"* ]] && [[ ${command} != *"assemble"* ]] &&
		[[ ${command} != *"version"* ]] && [[ ${command} != *"help"* ]]; then
		while IFS= read -r line; do
			list+="$line "
		done < <(distrobox-list --no-color | tail -n+2 | cut -d'|' -f2)
		COMPREPLY=($(compgen -W "${list}" "${cur}"))
		return 0
	fi
}

__distrobox() {
	if [ "${#COMP_WORDS[@]}" == "2" ]; then
		VALID_WORDS=$(distrobox | tail -n+4 | tr -d '|' | xargs echo)
		VALID_WORDS+=" --version --help"
		COMPREPLY=($(compgen -W "${VALID_WORDS}" -- "${COMP_WORDS[1]}"))
	elif [ "${#COMP_WORDS[@]}" -gt "2" ]; then
		_generate_from_help distrobox-"${COMP_WORDS[1]}"
	fi
}

complete -F __distrobox distrobox


================================================
FILE: completions/bash/distrobox-assemble
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-assemble


================================================
FILE: completions/bash/distrobox-create
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-create


================================================
FILE: completions/bash/distrobox-enter
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-enter


================================================
FILE: completions/bash/distrobox-ephemeral
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-ephemeral


================================================
FILE: completions/bash/distrobox-generate-entry
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-generate-entry


================================================
FILE: completions/bash/distrobox-list
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-list


================================================
FILE: completions/bash/distrobox-rm
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-rm


================================================
FILE: completions/bash/distrobox-stop
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-stop


================================================
FILE: completions/bash/distrobox-upgrade
================================================
# shellcheck disable=all

if [ -e /usr/share/bash-completion/completions/distrobox ]; then
	source /usr/share/bash-completion/completions/distrobox
fi
if [ -e /usr/local/share/bash-completion/completions/distrobox ]; then
	source /usr/local/share/bash-completion/completions/distrobox
fi
if [ -e "${HOME}/.local/share/bash-completion/completions/distrobox" ]; then
	source "${HOME}/.local/share/bash-completion/completions/distrobox"
fi

complete -F _generate_from_help distrobox-upgrade


================================================
FILE: completions/zsh/_distrobox
================================================
#compdef distrobox

local curcontext="$curcontext" state line
typeset -A opt_args

_distrobox_commands() {
  local -a commands
  commands=(
    'assemble:Handle distrobox assembly tasks'
    'create:Create a new distrobox container'
    'enter:Enter an existing distrobox container'
    'list:List all distrobox containers'
    'ls:Alias for list'
    'rm:Remove a distrobox container'
    'stop:Stop a running distrobox container'
    'upgrade:Upgrade a distrobox container'
    'ephemeral:Create a temporary distrobox container'
    'generate-entry:Generate a desktop entry for a distrobox container'
    'version:Show distrobox version'
  )
  _describe -t commands 'distrobox command' commands
}

_arguments -C \
  '1: :_distrobox_commands' \
  '*:: :->args'

case $state in
  args)
    case $line[1] in
      assemble)
        _distrobox-assemble
        ;;
      create)
        _distrobox-create
        ;;
      enter)
        _distrobox-enter
        ;;
      list|ls)
        _distrobox-list
        ;;
      rm)
        _distrobox-rm
        ;;
      stop)
        _distrobox-stop
        ;;
      upgrade)
        _distrobox-upgrade
        ;;
      ephemeral)
        _distrobox-ephemeral
        ;;
      generate-entry)
        _distrobox-generate-entry
        ;;
      version)
        # No additional completions needed for version
        ;;
    esac
esac


================================================
FILE: completions/zsh/_distrobox-assemble
================================================
#compdef distrobox-assemble

_message -r "Create or remove containers in batches, based on a manifest file."
_arguments \
  '1:command:(create rm)' \
  '--file[path to the distrobox manifest/ini file]:file:_files' \
  '(--name -n)'{-n,--name}'[run against a single entry in the manifest/ini file]:entry name:' \
  '(--replace -R)'{-R,--replace}'[replace already existing distroboxes with matching names]' \
  '(--dry-run -d)'{-d,--dry-run}'[only print the container manager command generated]' \
  '(--verbose -v)'{-v,--verbose}'[show more verbosity]' \
  '(--version -V)'{-V,--version}'[show version]'


================================================
FILE: completions/zsh/_distrobox-create
================================================
#compdef distrobox-create

_distrobox-create() {
    local -a options
    local expl
    local state
    # Array of optargs to pass to _arguments for matching and completion
    options=(
        '(-i --image)'{-i,--image}'[Specify image to use for the container]:image:_distrobox_images'
        '(-n --name)'{-n,--name}'[Specify name for the distrobox]:distrobox name:'
        '--hostname[Specify hostname for the distrobox]:hostname:'
        '(-p --pull)'{-p,--pull}'[Pull the image even if it exists locally (implies --yes)]'
        '(-Y --yes)'{-Y,--yes}'[Non-interactive, pull images without asking]'
        '(-r --root)'{-r,--root}'[Launch with root privileges using podman/docker/lilipod]'
        '(-c --clone)'{-c,--clone}'[Name of the distrobox container to use as base for a new container]:clone container name:'
        '(-H --home)'{-H,--home}'[Select a custom HOME directory for the container]:path:_files -/'
        '--volume[Add additional volumes to the container]:volume:_files'
        '(-a --additional-flags)'{-a,--additional-flags}'[Additional flags to pass to the container manager command]:flags:'
        '(-ap --additional-packages)'{-ap,--additional-packages}'[Additional packages to install during setup]:package:'
        '--init-hooks[Commands to execute during container initialization]:command:'
        '--pre-init-hooks[Commands to execute prior to container initialization]:command:'
        '(-I --init)'{-I,--init}'[Use an init system inside the container]'
        '--nvidia[Try to integrate host nVidia drivers into the guest]'
        '--unshare-devsys[Do not share host devices and sysfs dirs from host]'
        '--unshare-groups[Do not forward users additional groups into the container]'
        '--unshare-ipc[Do not share ipc namespace with host]'
        '--unshare-netns[Do not share the net namespace with host]'
        '--unshare-process[Do not share process namespace with host]'
        '--unshare-all[Activate all the unshare flags]'
        '(-C --compatibility)'{-C,--compatibility}'[Show list of compatible images]'
        '(-h --help)'{-h,--help}'[Show this message]'
        '--no-entry[Do not generate a container entry in the application list]'
        '(-d --dry-run)'{-d,--dry-run}'[Only print the container manager command generated]'
        '(-v --verbose)'{-v,--verbose}'[Show more verbosity]'
        '(-V --version)'{-V,--version}'[Show version]'
        '--absolutely-disable-root-password-i-am-really-positively-sure[Skip user password setup, leaving it blank]'
    )
    # Simple logic
    _message -r "Create new distroboxes."
    _arguments \
      '1:containers:->container' \
      '*:options:->options' \
      $options[@]
}

_distrobox-create


================================================
FILE: completions/zsh/_distrobox-enter
================================================
#compdef distrobox-enter

_distrobox-enter() {
    local -a options
    local expl
    local state
    local _db_cc

    _db_cc=("${(@f)$(distrobox list | sed 1d | awk -F'|' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')}")

    options=(
      '(--name -n)'{-n,--name}'[name for the distrobox]:container:_distrobox_containers'
      '--[end arguments and execute the rest as command to execute at login]:command:_command_names'
      '(--no-tty -T)'{-T,--no-tty}'[do not instantiate a tty]'
      '(--no-workdir -nw)'{-nw,--no-workdir}'[always start the container from container home directory]'
      '(--additional-flags -a)'{-a,--additional-flags}'[additional flags to pass to the container manager command]:flags:'
      '(--help -h)'{-h,--help}'[show this message]'
      '(--root -r)'{-r,--root}'[launch podman/docker/lilipod with root privileges]'
      '(--dry-run -d)'{-d,--dry-run}'[only print the container manager command generated]'
      '(--verbose -v)'{-v,--verbose}'[show more verbosity]'
      '(--version -V)'{-V,--version}'[show version]'
    )
    _message -r "Start and enter a distrobox."
    if [[ -n "$_db_cc" ]]; then
      _arguments -C \
        '1:containers:_distrobox_containers' \
        $options[@]
    else
      _message -r "No containers exist."
      _arguments $options[@]
    fi

}

_distrobox-enter


================================================
FILE: completions/zsh/_distrobox-ephemeral
================================================
#compdef distrobox-ephemeral

_distrobox-ephemeral() {
    local -a options
    local expl
    local state
    # Array of optargs to pass to _arguments for matching and completion
    options=(
        '(-i --image)'{-i,--image}'[Specify image to use for the container]:image:_distrobox_images'
        '(-n --name)'{-n,--name}'[Specify name for the distrobox]:distrobox name:'
        '--hostname[Specify hostname for the distrobox]:hostname:'
        '(-p --pull)'{-p,--pull}'[Pull the image even if it exists locally (implies --yes)]'
        '(-Y --yes)'{-Y,--yes}'[Non-interactive, pull images without asking]'
        '(-r --root)'{-r,--root}'[Launch with root privileges using podman/docker/lilipod]'
        '(-c --clone)'{-c,--clone}'[Name of the distrobox container to use as base for a new container]:clone container name:'
        '(-H --home)'{-H,--home}'[Select a custom HOME directory for the container]:path:_files -/'
        '--volume[Add additional volumes to the container]:volume:_files'
        '(-a --additional-flags)'{-a,--additional-flags}'[Additional flags to pass to the container manager command]:flags:'
        '(-ap --additional-packages)'{-ap,--additional-packages}'[Additional packages to install during setup]:package:'
        '--init-hooks[Commands to execute during container initialization]:command:'
        '--pre-init-hooks[Commands to execute prior to container initialization]:command:'
        '(-I --init)'{-I,--init}'[Use an init system inside the container]'
        '--nvidia[Try to integrate host nVidia drivers into the guest]'
        '--unshare-devsys[Do not share host devices and sysfs dirs from host]'
        '--unshare-groups[Do not forward users additional groups into the container]'
        '--unshare-ipc[Do not share ipc namespace with host]'
        '--unshare-netns[Do not share the net namespace with host]'
        '--unshare-process[Do not share process namespace with host]'
        '--unshare-all[Activate all the unshare flags]'
        '(-C --compatibility)'{-C,--compatibility}'[Show list of compatible images]'
        '(-h --help)'{-h,--help}'[Show this message]'
        '--no-entry[Do not generate a container entry in the application list]'
        '(-d --dry-run)'{-d,--dry-run}'[Only print the container manager command generated]'
        '(-v --verbose)'{-v,--verbose}'[Show more verbosity]'
        '(-V --version)'{-V,--version}'[Show version]'
        '--absolutely-disable-root-password-i-am-really-positively-sure[Skip user password setup, leaving it blank]'
    )
    # Simple logic
    _message -r "Create temporary distroboxes that are auto destroyed."
    _arguments \
      '1:containers:->container' \
      '*:options:->options' \
      $options[@]
}

_distrobox-ephemeral


================================================
FILE: completions/zsh/_distrobox-export
================================================
#compdef distrobox-export

_message -r "Export an app or a binary from the container to the host."
_arguments \
  '(--app -a)'{-a,--app}'[name of the application to export]:application name:' \
  '(--bin -b)'{-b,--bin}'[absolute path of the binary to export]:path to binary:_files' \
  '(--delete -d)'{-d,--delete}'[delete exported application or binary]' \
  '(--export-label -el)'{-el,--export-label}'[label to add to exported application name, use "none" to disable]:label:' \
  '(--export-path -ep)'{-ep,--export-path}'[path where to export the binary]:export path:_files' \
  '(--extra-flags -ef)'{-ef,--extra-flags}'[extra flags to add to the command]:extra flags:' \
  '(--enter-flags -nf)'{-nf,--enter-flags}'[flags to add to distrobox-enter]:enter flags:' \
  '--list-apps[list applications exported from this container]' \
  '--list-binaries[list binaries exported from this container, use -ep to specify custom paths to search]' \
  '(--sudo -S)'{-S,--sudo}'[specify if the exported item should be run as sudo]' \
  '(--help -h)'{-h,--help}'[show this message]' \
  '(--verbose -v)'{-v,--verbose}'[show more verbosity]' \
  '(--version -V)'{-V,--version}'[show version]'


================================================
FILE: completions/zsh/_distrobox-generate-entry
================================================
#compdef distrobox-generate-entry

_distrobox-generate-entry() {
    # expl is an internal zsh array that gets passed to _arguments bts
    local expl
    local -a options
    local _db_cc
    # Check for existing containers and store the result into var _db_cc
    _db_cc=("${(@f)$(distrobox list | sed 1d | awk -F'|' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')}")
    # Array of optargs to pass to _arguments for matching and completion
    options=(
        '(-h --help)'{-h,--help}'[show this message]'
        '(-a --all)'{-a,--all}'[perform for all distroboxes]'
        '(-d --delete)'{-d,--delete}'[delete the entry]'
        '(-i --icon)'{-i,--icon}'[specify a custom icon (default auto)]:icon path:(auto _files)'
        '(-r --root)'{-r,--root}'[perform on rootful distroboxes]'
        '(-v --verbose)'{-v,--verbose}'[show more verbosity]'
        '(-V --version)'{-V,--version}'[show version]'
    )
    _message -r "Create a desktop icon for a distrobox"
    # If containers exist then do an action
    if [[ -n "$_db_cc" ]]; then
      # Match both container names and optargs
      _arguments \
        '*:containers:_distrobox_containers' \
        $options[@]
    # If no containers exist then do an action
    else
      # Display message to user and match optargs only
      _message -r "No containers exist."
      _arguments $options[@]
    fi
}

_distrobox-generate-entry


================================================
FILE: completions/zsh/_distrobox-host-exec
================================================
#compdef distrobox-host-exec

_message -r "Execute a command on the host while in a container"
_arguments \
  '*:command:_command' \
  '(--help -h)'{-h,--help}'[show this message]' \
  '(--verbose -v)'{-v,--verbose}'[show more verbosity]' \
  '(--version -V)'{-V,--version}'[show version]' \
  '(--yes -Y)'{-Y,--yes}'[Automatically answer yes to prompt host-spawn will be installed on the guest system if not detected]'


================================================
FILE: completions/zsh/_distrobox-init
================================================
#compdef distrobox-init

_message -r "Init the distrobox (not to be launched manually)"
_arguments \
  '(--name -n)'{-n,--name}'[user name]:user name:' \
  '(--user -u)'{-u,--user}'[uid of the user]:uid:' \
  '(--group -g)'{-g,--group}'[gid of the user]:gid:' \
  '(--home -d)'{-d,--home}'[path/to/home of the user]:home path:_files' \
  '(--help -h)'{-h,--help}'[show this message]' \
  '--additional-packages[packages to install in addition]:packages:' \
  '(--init -I)'{-I,--init}'[whether to use or not init]' \
  '--pre-init-hooks[commands to execute prior to init]:commands:' \
  '--nvidia[try to integrate hosts nVidia drivers in the guest]' \
  '(--upgrade -U)'{-U,--upgrade}'[run init in upgrade mode]' \
  '(--verbose -v)'{-v,--verbose}'[show more verbosity]' \
  '(--version -V)'{-V,--version}'[show version]' \
  '--:[end arguments execute the rest as command to execute during init]:command:_command'


================================================
FILE: completions/zsh/_distrobox-list
================================================
#compdef distrobox-list

_message -r "List available distroboxes."
_arguments \
  '(--help -h)'{-h,--help}'[show this message]' \
  '--no-color[disable color formatting]' \
  '(--root -r)'{-r,--root}'[launch podman/docker/lilipod with root privileges]' \
  '(--verbose -v)'{-v,--verbose}'[show more verbosity]' \
  '(--version -V)'{-V,--version}'[show version]'


================================================
FILE: completions/zsh/_distrobox-rm
================================================
#compdef distrobox-rm

_distrobox-rm() {
    # expl is an internal zsh array that gets passed to _arguments bts
    local expl
    local -a options
    local _db_cc
    # Check for existing containers and store the result into var _db_cc
    _db_cc=("${(@f)$(distrobox list | sed 1d | awk -F'|' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')}")
    # Array of optargs to pass to _arguments for matching and completion
    options=(
        '(-a --all)'{-a,--all}'[delete all distroboxes]'
        '(-f --force)'{-f,--force}'[force deletion]'
        '--rm-home[remove the mounted home if it differs from the host users one]'
        '(-r --root)'{-r,--root}'[launch podman/docker/lilipod with root privileges]'
        '(-h --help)'{-h,--help}'[show this message]'
        '(-v --verbose)'{-v,--verbose}'[show more verbosity]'
        '(-V --version)'{-V,--version}'[show version]'
    )
    _message -r "Delete one or more distroboxes."
    # If containers exist then do an action
    if [[ -n "$_db_cc" ]]; then
      # Match both container names and optargs
      _arguments \
        '*:containers:_distrobox_containers' \
        $options[@]
    # If no containers exist then do an action
    else
      # Display message to user and match optargs only
      _message -r "No containers exist."
      _arguments $options[@]
    fi

}

_distrobox-rm


================================================
FILE: completions/zsh/_distrobox-stop
================================================
#compdef distrobox-stop

_distrobox-stop() {
    local expl
    local -a options
    local _db_rcc

     _db_rcc=($(distrobox list | awk -F'|' '$3 ~ /Up/ { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2 }'))                                                                     

    options=(
        '(-a --all)'{-a,--all}'[stop all distroboxes]'
        '(-Y --yes)'{-Y,--yes}'[non-interactive, stop without asking]'
        '(-r --root)'{-r,--root}'[launch podman/docker/lilipod with root privileges]'
        '(-h --help)'{-h,--help}'[show this message]'
        '(-v --verbose)'{-v,--verbose}'[show more verbosity]'
        '(-V --version)'{-V,--version}'[show version]'
    )

    _message -r "Stop running distrobox containers."
    if [[ -n "$_db_rcc" ]]; then
      _arguments -C \
        '*:containers:_distrobox_running_containers' \
        $options[@]
    else
      _message -r "No running containers."
      _arguments $options[@]
    fi

}

_distrobox-stop


================================================
FILE: completions/zsh/_distrobox-upgrade
================================================
#compdef distrobox-upgrade

_distrobox-upgrade() {
    local expl
    local -a options
    local _db_cc

    _db_cc=("${(@f)$(distrobox list | sed 1d | awk -F'|' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')}")
    
    options=(
        '(-a --all)'{-a,--all}'[upgrade all distroboxes]'
        '--running[perform only for running distroboxes]'
        '(-r --root)'{-r,--root}'[launch podman/docker/lilipod with root privileges]'
        '(-h --help)'{-h,--help}'[show this message]'
        '(-v --verbose)'{-v,--verbose}'[show more verbosity]'
        '(-V --version)'{-V,--version}'[show version]'
    )

    _message -r "Upgrade distroboxes using container's package manager."
    if [[ -n "$_db_cc" ]]; then
      _arguments \
        '*:containers:_distrobox_containers' \
        $options[@]
    else
      _message -r "No containers exist."
      _arguments $options[@]
    fi

}

_distrobox-upgrade


================================================
FILE: completions/zsh/_distrobox_containers
================================================
#autoload

_distrobox_containers() {
    local -a containers
    containers=("${(@f)$(distrobox list | sed 1d | awk -F'|' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')}")
    _describe -t containers 'available containers' containers
}


================================================
FILE: completions/zsh/_distrobox_images
================================================
#autoload

_distrobox_images() {
    local -a images
    images=("${(@f)$(distrobox-create --compatibility | awk 'NF {print $0}')}")
    _wanted images expl 'compatible image' compadd -a images
}


================================================
FILE: completions/zsh/_distrobox_running_containers
================================================
#autoload

_distrobox_running_containers() {
    local -a containers
    containers=($(distrobox list | awk -F'|' '$3 ~ /Up/ { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2 }'))
    if (( ${#containers} > 0 )); then
      _describe -t running-containers 'running containers' containers
    fi
}


================================================
FILE: distrobox
================================================
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
#    https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# POSIX
set -o errexit
set -o nounset

version="1.8.2.4"

# show_help will print usage to stdout.
# Arguments:
#   None
# Expected global variables:
#   version: distrobox version
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
distrobox version: ${version}

Choose one of the available commands:
	assemble
	create
	enter
	list | ls
	rm
	stop
	upgrade
	ephemeral
	generate-entry
	version
	help
EOF
}

if [ $# -eq 0 ]; then
	show_help
	exit
fi

distrobox_path="$(dirname "${0}")"
distrobox_command="${1}"
shift

# Simple wrapper to the distrobox utilities.
# We just detect the 1st argument and launch the matching distrobox utility.
case "${distrobox_command}" in
	assemble)
		"${distrobox_path}"/distrobox-assemble "$@"
		;;
	create)
		"${distrobox_path}"/distrobox-create "$@"
		;;
	enter)
		"${distrobox_path}"/distrobox-enter "$@"
		;;
	ls | list)
		"${distrobox_path}"/distrobox-list "$@"
		;;
	stop)
		"${distrobox_path}"/distrobox-stop "$@"
		;;
	rm)
		"${distrobox_path}"/distrobox-rm "$@"
		;;
	upgrade)
		"${distrobox_path}"/distrobox-upgrade "$@"
		;;
	generate-entry)
		"${distrobox_path}"/distrobox-generate-entry "$@"
		;;
	ephemeral)
		"${distrobox_path}"/distrobox-ephemeral "$@"
		;;
	-V | --version | version)
		printf "distrobox: %s\n" "${version}"
		exit 0
		;;
	help | --help | -h)
		show_help
		exit 0
		;;
	*) # Default case: If no more options then break out of the loop.
		printf >&2 "Error: invalid command\n"
		show_help
		exit 1
		;;
esac


================================================
FILE: distrobox-assemble
================================================
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
#    https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# Ensure we have our env variables correctly set
[ -z "${USER}" ] && USER="$(id -run)"
[ -z "${HOME}" ] && HOME="$(getent passwd "${USER}" | cut -d':' -f6)"
[ -z "${SHELL}" ] && SHELL="$(getent passwd "${USER}" | cut -d':' -f7)"

# POSIX
#
default_input_file="./distrobox.ini"
delete=-1
distrobox_path="$(dirname "${0}")"
dryrun=0
boxname=""
input_file=""
replace=0
root_flag=""
# tmpfile will be used as a little buffer to pass variables without messing up
# quoting and escaping
tmpfile="$(mktemp -u)"
tmp_download_file="$(mktemp -u)"
verbose=0
version="1.8.2.4"
# initializing block of variables used in the manifest
additional_flags=""
additional_packages=""
entry=""
home=""
hostname=""
image=""
clone=""
init=""
init_hooks=""
nvidia=""
pre_init_hooks=""
pull=""
root=""
start_now=""
unshare_groups=""
unshare_ipc=""
unshare_netns=""
unshare_process=""
unshare_devsys=""
unshare_all=""
volume=""
exported_apps=""
exported_bins=""
exported_bins_path="${HOME}/.local/bin"

# Cleanup tmpfiles on exit
trap 'rm -f ${tmpfile} ${tmp_download_file}' EXIT

# Despite of running this script via SUDO/DOAS being not supported (the
# script itself will call the appropriate tool when necessary), we still want
# to allow people to run it as root, logged in in a shell, and create rootful
# containers.
#
# SUDO_USER is a variable set by SUDO and can be used to check whether the script was called by it. Same thing for DOAS_USER, set by DOAS.
if [ -n "${SUDO_USER}" ] || [ -n "${DOAS_USER}" ]; then
	printf >&2 "Running %s via SUDO/DOAS is not supported." "$(basename "${0}")"
	printf >&2 "Instead, please try using root=true property in the distrobox.ini file.\n"
	exit 1
fi

# Source configuration files, this is done in an hierarchy so local files have
# priority over system defaults
# leave priority to environment variables.
#
# On NixOS, for the distrobox derivation to pick up a static config file shipped
# by the package maintainer the path must be relative to the script itself.
self_dir="$(dirname "$(realpath "$0")")"
nix_config_file="${self_dir}/../share/distrobox/distrobox.conf"

config_files="
	${nix_config_file}
	/usr/share/distrobox/distrobox.conf
	/usr/share/defaults/distrobox/distrobox.conf
	/usr/etc/distrobox/distrobox.conf
	/usr/local/share/distrobox/distrobox.conf
	/etc/distrobox/distrobox.conf
	${XDG_CONFIG_HOME:-"${HOME}/.config"}/distrobox/distrobox.conf
	${HOME}/.distroboxrc
"
for config_file in ${config_files}; do
	# Shellcheck will give error for sourcing a variable file as it cannot follow
	# it. We don't care so let's disable this linting for now.
	# shellcheck disable=SC1090
	[ -e "${config_file}" ] && . "$(realpath "${config_file}")"
done

[ -n "${DBX_VERBOSE}" ] && verbose="${DBX_VERBOSE}"

# Fixup variable=[true|false], in case we find it in the config file(s)
[ "${verbose}" = "true" ] && verbose=1
[ "${verbose}" = "false" ] && verbose=0

# show_help will print usage to stdout.
# Arguments:
#   None
# Expected global variables:
#   version: string distrobox version
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
distrobox version: ${version}

Usage:

	distrobox assemble create
	distrobox assemble rm
	distrobox assemble create --file /path/to/file.ini
	distrobox assemble rm --file /path/to/file.ini
	distrobox assemble create --replace --file /path/to/file.ini

Options:

	--file:			path or URL to the distrobox manifest/ini file
	--name/-n:		run against a single entry in the manifest/ini file
	--replace/-R:		replace already existing distroboxes with matching names
	--dry-run/-d:		only print the container manager command generated
	--verbose/-v:		show more verbosity
	--version/-V:		show version
EOF
}

# Parse arguments
while :; do
	case $1 in
		create)
			delete=0
			shift
			;;
		rm)
			delete=1
			shift
			;;
		--file)
			# Call a "show_help" function to display a synopsis, then exit.
			if [ -n "$2" ]; then
				input_file="${2}"
				shift
				shift
			fi
			;;
		-n | --name)
			# Call a "show_help" function to display a synopsis, then exit.
			if [ -n "$2" ]; then
				boxname="${2}"
				shift
				shift
			fi
			;;
		-h | --help)
			# Call a "show_help" function to display a synopsis, then exit.
			show_help
			exit 0
			;;
		-d | --dry-run)
			shift
			dryrun=1
			;;
		-v | --verbose)
			verbose=1
			shift
			;;
		-R | --replace)
			replace=1
			shift
			;;
		-V | --version)
			printf "distrobox: %s\n" "${version}"
			exit 0
			;;
		--) # End of all options.
			shift
			break
			;;
		-*) # Invalid options.
			printf >&2 "ERROR: Invalid flag '%s'\n\n" "$1"
			show_help
			exit 1
			;;
		*) # Default case: If no more options then break out of the loop.
			# If we have a flagless option and container_name is not specified
			# then let's accept argument as container_name
			if [ -n "$1" ]; then
				input_file="$1"
				shift
			else
				break
			fi
			;;
	esac
done

set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
fi

# check if we're getting the right inputs
if [ "${delete}" -eq -1 ]; then
	printf >&2 "Please specify create or rm.\n"
	show_help
	exit 1
fi

# Fallback to distrobox.ini if no file is passed
if [ -z "${input_file}" ]; then
	input_file="${default_input_file}"
fi

# Check if file effectively exists
if [ ! -e "${input_file}" ]; then

	if command -v curl > /dev/null 2>&1; then
		download="curl --connect-timeout 3 --retry 1 -sLo"
	elif command -v wget > /dev/null 2>&1; then
		download="wget --timeout=3 --tries=1 -qO"
	fi

	if ! ${download} - "${input_file}" > "${tmp_download_file}"; then
		printf >&2 "File %s does not exist.\n" "${input_file}"
		exit 1
	else
		input_file="${tmp_download_file}"
	fi
fi

# run_distrobox will create distrobox with parameters parsed from ini file.
# Arguments:
#   name: name of the distrobox.
# Expected global variables:
#   boxname: string name of the target container
#   tmpfile: string name of the tmpfile to read
#   delete:  bool delete container
#   replace: bool replace container
#   dryrun:  bool dryrun (only print, no execute)
#   verbose: bool verbose
# Expected env variables:
#   None
# Outputs:
#   execution of the proper distrobox-create command.
run_distrobox()
{
	name="${1}"
	additional_flags=""
	additional_packages=""
	entry=""
	home=""
	hostname=""
	image=""
	clone=""
	init=""
	init_hooks=""
	nvidia=""
	pre_init_hooks=""
	pull=""
	root=""
	start_now=""
	unshare_groups=""
	unshare_ipc=""
	unshare_netns=""
	unshare_process=""
	unshare_devsys=""
	unshare_all=""
	volume=""
	exported_apps=""
	exported_bins=""
	exported_bins_path="${HOME}/.local/bin"

	# Skip item if --name used and no match is found
	if [ "${boxname}" != "" ] && [ "${boxname}" != "${name}" ]; then
		rm -f "${tmpfile}"
		return
	fi

	# Source the current block variables
	if [ -e "${tmpfile}" ]; then
		# shellcheck disable=SC1090
		. "${tmpfile}" && rm -f "${tmpfile}"
	fi

	if [ -n "${root}" ] && [ "${root}" -eq 1 ]; then
		root_flag="--root"
	fi

	# We're going to delete, not create!
	if [ "${delete}" -ne 0 ] || [ "${replace}" -ne 0 ]; then
		printf " - Deleting %s...\n" "${name}"

		if [ "${dryrun}" -eq 0 ]; then
			# shellcheck disable=SC2086,2248
			"${distrobox_path}"/distrobox rm ${root_flag} -f "${name}" > /dev/null || :
		fi

		if [ "${delete}" -ne 0 ]; then
			return
		fi
	fi

	# We're going to create!
	printf " - Creating %s...\n" "${name}"

	# If distrobox already exist, and we have replace enabled, destroy the container
	# we have to recreate it.
	# shellcheck disable=SC2086,2248
	if "${distrobox_path}"/distrobox-list ${root_flag} | grep -qw " ${name} " && [ "${dryrun}" -eq 0 ]; then
		printf >&2 "%s already exists\n" "${name}"
		return 0
	fi

	# Now we dynamically generate the distrobox-create command based on the
	# declared flags.
	result_command="${distrobox_path}/distrobox-create --yes"
	if [ "${verbose}" -ne 0 ]; then
		result_command="${result_command} -v"
	fi
	if [ -n "${name}" ]; then
		result_command="${result_command} --name $(sanitize_variable "${name}")"
	fi
	if [ -n "${image}" ]; then
		result_command="${result_command} --image $(sanitize_variable "${image}")"
	fi
	if [ -n "${clone}" ]; then
		result_command="${result_command} --clone $(sanitize_variable "${clone}")"
	fi
	if [ -n "${init}" ] && [ "${init}" -eq 1 ]; then
		result_command="${result_command} --init"
	fi
	if [ -n "${root}" ] && [ "${root}" -eq 1 ]; then
		result_command="${result_command} --root"
	fi
	if [ -n "${pull}" ] && [ "${pull}" -eq 1 ]; then
		result_command="${result_command} --pull"
	fi
	if [ -n "${entry}" ] && [ "${entry}" -eq 0 ]; then
		result_command="${result_command} --no-entry"
	fi
	if [ -n "${nvidia}" ] && [ "${nvidia}" -eq 1 ]; then
		result_command="${result_command} --nvidia"
	fi
	if [ -n "${unshare_netns}" ] && [ "${unshare_netns}" -eq 1 ]; then
		result_command="${result_command} --unshare-netns"
	fi
	if [ -n "${unshare_groups}" ] && [ "${unshare_groups}" -eq 1 ]; then
		result_command="${result_command} --unshare-groups"
	fi
	if [ -n "${unshare_ipc}" ] && [ "${unshare_ipc}" -eq 1 ]; then
		result_command="${result_command} --unshare-ipc"
	fi
	if [ -n "${unshare_process}" ] && [ "${unshare_process}" -eq 1 ]; then
		result_command="${result_command} --unshare-process"
	fi
	if [ -n "${unshare_devsys}" ] && [ "${unshare_devsys}" -eq 1 ]; then
		result_command="${result_command} --unshare-devsys"
	fi
	if [ -n "${unshare_all}" ] && [ "${unshare_all}" -eq 1 ]; then
		result_command="${result_command} --unshare-all"
	fi
	if [ -n "${home}" ]; then
		result_command="${result_command} --home $(sanitize_variable "${home}")"
	fi
	if [ -n "${hostname}" ]; then
		result_command="${result_command} --hostname $(sanitize_variable "${hostname}")"
	fi
	if [ -n "${init_hooks}" ]; then
		IFS="¤"
		args=": ;"
		separator=""
		for arg in ${init_hooks}; do
			if [ -z "${arg}" ]; then
				continue
			fi

			# Convert back from base64
			arg="$(echo "${arg}" | base64 -d)"
			args="${args} ${separator} ${arg}"

			# Prepare for the next line, if we already have a ';' or '&&', do nothing
			# else prefer adding '&&'
			if ! echo "${arg}" | grep -qE ';[[:space:]]{0,1}$' &&
				! echo "${arg}" | grep -qE '&&[[:space:]]{0,1}$'; then
				separator="&&"
			else
				separator=""
			fi
		done
		result_command="${result_command} --init-hooks $(sanitize_variable "${args}")"
	fi
	# Replicable flags
	if [ -n "${pre_init_hooks}" ]; then
		IFS="¤"
		args=": ;"
		separator=""
		for arg in ${pre_init_hooks}; do
			if [ -z "${arg}" ]; then
				continue
			fi

			# Convert back from base64
			arg="$(echo "${arg}" | base64 -d)"
			args="${args} ${separator} ${arg}"

			# Prepare for the next line, if we already have a ';' or '&&', do nothing
			# else prefer adding '&&'
			if ! echo "${arg}" | grep -qE ';[[:space:]]{0,1}$' &&
				! echo "${arg}" | grep -qE '&&[[:space:]]{0,1}$'; then
				separator="&&"
			else
				separator=""
			fi
		done
		result_command="${result_command} --pre-init-hooks $(sanitize_variable "${args}")"
	fi
	if [ -n "${additional_packages}" ]; then
		IFS="¤"
		args=""
		for packages in ${additional_packages}; do
			if [ -z "${packages}" ]; then
				continue
			fi
			args="${args} ${packages}"
		done
		result_command="${result_command} --additional-packages $(sanitize_variable "${args}")"
	fi
	if [ -n "${volume}" ]; then
		IFS="¤"
		for vol in ${volume}; do
			if [ -z "${vol}" ]; then
				continue
			fi
			result_command="${result_command} --volume $(sanitize_variable "${vol}")"
		done
	fi
	if [ -n "${additional_flags}" ]; then
		IFS="¤"
		for flag in ${additional_flags}; do
			if [ -z "${flag}" ]; then
				continue
			fi
			result_command="${result_command} --additional-flags $(sanitize_variable "${flag}")"
		done
	fi
	if [ "${dryrun}" -ne 0 ]; then
		result_command="${result_command} --dry-run"
	fi

	# Execute the distrobox-create command
	eval "${result_command}"

	if [ "${dryrun}" -ne 0 ]; then
		return
	fi

	# If we need to start immediately, do it, so that the container
	# is ready to be entered.
	if [ -n "${start_now}" ] && [ "${start_now}" -eq 1 ]; then
		# Here we execute the `distrobox enter` command with a `/dev/null` stdin.
		# This is due to the fact that this command is very likely to be executed inside
		# the read loop in the prse_file function. This way we avoid stdin to be swallowed by
		# this command execution. This is valid for all the `distrobox enter` calls from now on.
		#
		# shellcheck disable=SC2086,2248
		"${distrobox_path}"/distrobox enter ${root_flag} "${name}" -- touch /dev/null < /dev/null
	fi

	# if there are exported bins and apps declared, let's export them
	if [ -n "${exported_apps}" ] || [ -n "${exported_bins}" ]; then
		# First we start the container
		# shellcheck disable=SC2086,2248
		"${distrobox_path}"/distrobox enter ${root_flag} "${name}" -- touch /dev/null < /dev/null

		IFS="¤"
		for apps in ${exported_apps}; do
			# Split the string by spaces
			IFS=" "
			for app in ${apps}; do
				# Export the app
				# shellcheck disable=SC2086,2248
				"${distrobox_path}"/distrobox enter ${root_flag} "${name}" -- distrobox-export --app "${app}" < /dev/null
			done
		done

		IFS="¤"
		for bins in ${exported_bins}; do
			# Split the string by spaces
			IFS=" "
			for bin in ${bins}; do
				# Export the bin
				# shellcheck disable=SC2086,2248
				"${distrobox_path}"/distrobox enter ${root_flag} "${name}" -- distrobox-export --bin "${bin}" \
					--export-path "${exported_bins_path}" < /dev/null
			done
		done
	fi
}

# encode_variable will encode an input in base64, removing surrounding single/double quotes.
# Arguments:
#   variable: string
# Expected global variables:
#   None
# Expected env variables:
#   None
# Outputs:
#   a value string encoded in base64
encode_variable()
{
	variable="${1}"
	# remove surrounding quotes possibly added by the user
	if echo "${variable}" | grep -qE '^"'; then
		variable="$(echo "${variable}" | sed -e 's/^"//' -e 's/"$//')"
	elif echo "${variable}" | grep -qE "^'"; then
		variable="$(echo "${variable}" | sed -e "s/^'//" -e "s/'$//")"
	fi

	echo "${variable}" | base64 -w 0
}

# sanitize_variable will sanitize an input, add single/double quotes and escapes
# Arguments:
#   variable: string
# Expected global variables:
#   None
# Expected env variables:
#   None
# Outputs:
#   a value string sanitized
sanitize_variable()
{
	variable="${1}"

	# If there are spaces but no quotes, let's add them
	if echo "${variable}" | grep -q " " &&
		! echo "${variable}" | grep -Eq "^'|^\""; then

		# if we have double quotes we should wrap the whole line in single quotes
		# in order to not "undo" them
		if echo "${variable}" | grep -q '"'; then
			variable="'${variable}'"
		else
			variable="\"${variable}\""
		fi
	fi

	# Return
	echo "${variable}"
}

# parse_file will read and parse input file and call distrobox-create accordingly
# Arguments:
#   file: string path of the manifest file to parse
# Expected global variables:
#   tmpfile: string name of the tmpfile to read
# Expected env variables:
#   None
# Outputs:
#   None
parse_file()
{
	file="${1}"
	name=""

	IFS='
	'
	while read -r line; do
		# Remove comments and trailing spaces
		line="$(echo "${line}" |
			sed 's/\t/ /g' |
			sed 's/^#.*//g' |
			sed 's/].*#.*//g' |
			sed 's/ #.*//g' |
			sed 's/\s*$//g')"

		if [ -z "${line}" ]; then
			# blank line, skip
			continue
		fi

		# Detect start of new section
		if [ "$(echo "${line}" | cut -c 1)" = '[' ]; then
			# We're starting a new section
			if [ -n "${name}" ]; then
				# We've finished the previous section, so this is the time to
				# perform the distrobox command, before going to the new section.
				run_distrobox "${name}"
			fi

			# Remove brackets and spaces
			name="$(echo "${line}" | tr -d '][ ')"
			continue
		fi

		# Get key-values from the file
		key="$(echo "${line}" | cut -d'=' -f1 | tr -d ' ')"
		value="$(echo "${line}" | cut -d'=' -f2-)"

		# Normalize true|false to 0|1
		[ "${value}" = "true" ] && value=1
		[ "${value}" = "false" ] && value=0

		# Sanitize value, by whitespaces, quotes and escapes
		if [ "${key}" = "init_hooks" ] || [ "${key}" = "pre_init_hooks" ]; then
			# in case of shell commands (so the hooks) we prefer to pass the variable
			# around encoded, so that we don't accidentally execute stuff
			# and, we will execute sanitize_variable on the final string flag at the
			# end, instead of key/value base.
			value="$(encode_variable "${value}")"
		else
			value="$(sanitize_variable "${value}")"
		fi

		# Save options to tempfile, to source it later
		touch "${tmpfile}"
		if [ -n "${key}" ] && [ -n "${value}" ]; then
			if grep -q "^${key}=" "${tmpfile}"; then
				# make keys cumulative
				value="\${${key}}¤${value}"
			fi
			echo "${key}=${value}" >> "${tmpfile}"
		fi
	done < "${file}"
	# Execute now one last time for the last block
	run_distrobox "${name}"
}

# read_section reads the content of a section from a TOML file.
# Arguments:
#   section: Name of the section to find (string).
#   file: Path to the file to search into (string).
# Expected global variables:
#   None.
# Expected env variables:
#   None.
# Outputs:
#   Writes the found section body to stdout.
# Exit behavior / errors:
#   Does not exit non‑zero on missing section; caller should treat empty output as needed.
read_section()
{
	section="$1"
	file="$2"
	awk -v sec="[${section}]" '
    $0 == sec {show=1; next}
    show && /^\[/ {exit}
    show && NF {print}
  ' "${file}"
}

# resolve_includes Resolve 'include' keys in a manifest by inlining referenced sections.
# Arguments:
#   input_file: Path to the original manifest file that may contain 'include' keys.
#   output_file: Path to the file where the resolved manifest will be written (if empty, a temp file is used).
# Expected global variables:
#   read_section (function)  - used to extract referenced section bodies from input_file.
#   replace_line (function)  - used to replace include lines with extracted content.
# Expected env variables:
#   TMPDIR (optional) - may influence mktemp location.
# Outputs:
#   Prints the path to the output file to stdout when completed.
# Exit behavior / errors:
#   Exits with status 1 and writes to stderr on missing definitions, circular includes, or helper failures.
resolve_includes()
{
	input_file="$1"
	output_file="$2"
	include_stack=""

	# At the starting point, the output file is equal to input_file
	# Later on, the output file will be edited as includes will be resolved
	cat "${input_file}" > "${output_file}"

	n=0
	while true; do
		total_lines=$(wc -l < "${output_file}")
		[ "${n}" -gt "${total_lines}" ] && break

		line=$(sed -n "$((n + 1))p" "${output_file}")

		# Detected begin of a section: clear the include stack and go on
		if [ "$(echo "${line}" | cut -c 1)" = '[' ]; then
			include_stack=""
			n=$((n + 1))
			continue
		fi

		# Match key=value from the current line
		key="$(echo "${line}" | cut -d'=' -f1 | tr -d ' ')"
		value="$(echo "${line}" | cut -d'=' -f2-)"

		if [ "${key}" = "include" ]; then
			# Detect circular references while including other distrobox definitions
			# The reference stack is handled as a string of shape [name].[name].[name]
			if expr "${include_stack}" : ".*\[${value}\]" > /dev/null; then
				printf >&2 "ERROR circular reference detected: including [%s] again after %s\n" "${value}" "${include_stack}"
				exit 1
			else
				include_stack="[${value}]¤${include_stack}"
			fi

			# Read the definition for the distrobox to include from the original file
			# and replace the current line with the found lines.
			# The line counter is not incremented to allow recursive include resolution
			inc=$(read_section "$(echo "${value}" | tr -d '"')" "${input_file}")
			if [ -z "${inc}" ]; then
				printf >&2 "ERROR cannot include '%s': definition not found\n" "${value}"
				exit 1
			fi
			l=$((n + 1))
			replace_line "${l}" "${inc}" "${output_file}" "${output_file}" > /dev/null

			continue
		fi

		# Nothing to do, increment counter and go on
		n=$((n + 1))

	done

	echo "${output_file}"

}

# replace_line Replace a 1-based numbered line in a file with provided (possibly multiline) text.
# Arguments:
#   line_number: 1-based index of the line to replace.
#   new_value: String to insert (may contain newlines).
#   input_file: Path to the original file to read from.
#   output_file: Path to write the resulting file (if empty, a temp file will be created).
# Expected global variables:
#   None.
# Expected env variables:
#   TMPDIR (optional) - may influence mktemp location.
# Outputs:
#   Prints the path to the output file to stdout when complete.
# Exit behavior / errors:
#   Exits with status 1 on fatal errors (e.g., mktemp failure).
replace_line()
{
	line_number="$1"
	new_value="$2"
	input_file="$3"
	output_file="$4"

	# if no output file, use a temp file
	if [ -z "${output_file}" ]; then
		output_file=$(mktemp -u)
	fi

	tmpfile=$(mktemp) || exit 1

	# Split the file into two parts around the line to replace and combine with new_value
	# Print lines before line_number
	sed "$((line_number - 1))q" "${input_file}" > "${tmpfile}"

	# Append the new_value
	printf "%s\n" "${new_value}" >> "${tmpfile}"

	# Append lines after line_number
	sed -n "$((line_number + 1)),\$p" "${input_file}" >> "${tmpfile}"

	# Replace original file with tmpfile
	mv "${tmpfile}" "${output_file}"

	echo "${output_file}"
}

# Exec
expanded_file=$(mktemp -u)
resolve_includes "${input_file}" "${expanded_file}" > /dev/null
parse_file "${expanded_file}"


================================================
FILE: distrobox-create
================================================
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
#    https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# POSIX
# Expected env variables:
#	HOME
#	USER
# Optional env variables:
#	DBX_CONTAINER_ALWAYS_PULL
#	DBX_CONTAINER_CUSTOM_HOME
#	DBX_CONTAINER_HOME_PREFIX
#	DBX_CONTAINER_IMAGE
#	DBX_CONTAINER_MANAGER
#	DBX_CONTAINER_NAME
#	DBX_CONTAINER_HOSTNAME
#	DBX_CONTAINER_GENERATE_ENTRY
#	DBX_NON_INTERACTIVE
#	DBX_VERBOSE
#	DBX_SUDO_PROGRAM
#	DBX_USERNS_NOLIMIT

# Despite of running this script via SUDO/DOAS being not supported (the
# script itself will call the appropriate tool when necessary), we still want
# to allow people to run it as root, logged in in a shell, and create rootful
# containers.
#
# SUDO_USER is a variable set by SUDO and can be used to check whether the script was called by it. Same thing for DOAS_USER, set by DOAS.
if {
	[ -n "${SUDO_USER}" ] || [ -n "${DOAS_USER}" ]
} && [ "$(id -ru)" -eq 0 ]; then
	printf >&2 "Running %s via SUDO/DOAS is not supported. Instead, please try running:\n" "$(basename "${0}")"
	printf >&2 "  %s --root %s\n" "$(basename "${0}")" "$*"
	exit 1
fi

# Ensure we have our env variables correctly set
[ -z "${USER}" ] && USER="$(id -run)"
[ -z "${HOME}" ] && HOME="$(getent passwd "${USER}" | cut -d':' -f6)"
[ -z "${SHELL}" ] && SHELL="$(getent passwd "${USER}" | cut -d':' -f7)"

# Defaults
container_additional_packages=""
container_additional_volumes=""
container_always_pull=0
container_clone=""
container_generate_entry=1
container_home_prefix=""
container_image=""
container_image_default="registry.fedoraproject.org/fedora-toolbox:latest"
container_init_hook=""
container_manager="autodetect"
container_manager_additional_flags=""
container_platform=""
container_name=""
container_name_default="my-distrobox"
container_hostname=""
container_pre_init_hook=""
container_user_custom_home=""
container_user_gid="$(id -rg)"
container_user_home="${HOME:-"/"}"
container_user_name="${USER}"
container_user_uid="$(id -ru)"
dryrun=0
init=0
non_interactive=0
nvidia=0
nopasswd=0
unshare_ipc=0
unshare_groups=0
unshare_netns=0
unshare_process=0
unshare_devsys=0

# Use cd + dirname + pwd so that we do not have relative paths in mount points
# We're not using "realpath" here so that symlinks are not resolved this way
# "realpath" would break situations like Nix or similar symlink based package
# management.
distrobox_entrypoint_path="$(cd "$(dirname "${0}")" && pwd)/distrobox-init"
distrobox_export_path="$(cd "$(dirname "${0}")" && pwd)/distrobox-export"
distrobox_genentry_path="$(cd "$(dirname "${0}")" && pwd)/distrobox-generate-entry"
distrobox_hostexec_path="$(cd "$(dirname "${0}")" && pwd)/distrobox-host-exec"
# In case some of the scripts are not in the same path as create, let's search
# in PATH for them.
[ ! -e "${distrobox_entrypoint_path}" ] && distrobox_entrypoint_path="$(command -v distrobox-init)"
[ ! -e "${distrobox_export_path}" ] && distrobox_export_path="$(command -v distrobox-export)"
[ ! -e "${distrobox_genentry_path}" ] && distrobox_genentry_path="$(command -v distrobox-generate-entry)"
[ ! -e "${distrobox_hostexec_path}" ] && distrobox_hostexec_path="$(command -v distrobox-host-exec)"
# If the user runs this script as root in a login shell, set rootful=1.
# There's no need for them to pass the --root flag option in such cases.
[ "${container_user_uid}" -eq 0 ] && rootful=1 || rootful=0
userns_nolimit=0
verbose=0
version="1.8.2.4"

app_cache_dir=${XDG_CACHE_HOME:-"${HOME}/.cache"}/distrobox

# Source configuration files, this is done in an hierarchy so local files have
# priority over system defaults
# leave priority to environment variables.
#
# On NixOS, for the distrobox derivation to pick up a static config file shipped
# by the package maintainer the path must be relative to the script itself.
self_dir="$(dirname "$(realpath "$0")")"
nix_config_file="${self_dir}/../share/distrobox/distrobox.conf"

config_files="
	${nix_config_file}
	/usr/share/distrobox/distrobox.conf
	/usr/share/defaults/distrobox/distrobox.conf
	/usr/etc/distrobox/distrobox.conf
	/usr/local/share/distrobox/distrobox.conf
	/etc/distrobox/distrobox.conf
	${XDG_CONFIG_HOME:-"${HOME}/.config"}/distrobox/distrobox.conf
	${HOME}/.distroboxrc
"
for config_file in ${config_files}; do
	# Shellcheck will give error for sourcing a variable file as it cannot follow
	# it. We don't care so let's disable this linting for now.
	# shellcheck disable=SC1090
	[ -e "${config_file}" ] && . "$(realpath "${config_file}")"
done
# If we're running this script as root -- as in logged in in the shell as root
# user, and not via SUDO/DOAS --, we don't need to set distrobox_sudo_program
# as it's meaningless for this use case.
if [ "${container_user_uid}" -ne 0 ]; then
	# If the DBX_SUDO_PROGRAM/distrobox_sudo_program variable was set by the
	# user, use its value instead of "sudo". But only if not running the script
	# as root (UID 0).
	distrobox_sudo_program=${DBX_SUDO_PROGRAM:-${distrobox_sudo_program:-"sudo"}}
fi

[ -n "${DBX_CONTAINER_ALWAYS_PULL}" ] && container_always_pull="${DBX_CONTAINER_ALWAYS_PULL}"
[ -n "${DBX_CONTAINER_CUSTOM_HOME}" ] && container_user_custom_home="${DBX_CONTAINER_CUSTOM_HOME}"
[ -n "${DBX_CONTAINER_HOME_PREFIX}" ] && container_home_prefix="${DBX_CONTAINER_HOME_PREFIX}"
[ -n "${DBX_CONTAINER_IMAGE}" ] && container_image="${DBX_CONTAINER_IMAGE}"
[ -n "${DBX_CONTAINER_MANAGER}" ] && container_manager="${DBX_CONTAINER_MANAGER}"
[ -n "${DBX_CONTAINER_NAME}" ] && container_name="${DBX_CONTAINER_NAME}"
[ -n "${DBX_CONTAINER_HOSTNAME}" ] && container_hostname="${DBX_CONTAINER_HOSTNAME}"
[ -n "${DBX_CONTAINER_GENERATE_ENTRY}" ] && container_generate_entry="${DBX_CONTAINER_GENERATE_ENTRY}"
[ -n "${DBX_NON_INTERACTIVE}" ] && non_interactive="${DBX_NON_INTERACTIVE}"
[ -n "${DBX_VERBOSE}" ] && verbose="${DBX_VERBOSE}"
[ -n "${DBX_USERNS_NOLIMIT}" ] && userns_nolimit="${DBX_USERNS_NOLIMIT}"

# Fixup variable=[true|false], in case we find it in the config file(s)
[ "${non_interactive}" = "true" ] && non_interactive=1
[ "${non_interactive}" = "false" ] && non_interactive=0
[ "${verbose}" = "true" ] && verbose=1
[ "${verbose}" = "false" ] && verbose=0
[ "${userns_nolimit}" = "true" ] && userns_nolimit=1
[ "${userns_nolimit}" = "false" ] && userns_nolimit=0

# show_help will print usage to stdout.
# Arguments:
#   None
# Expected global variables:
#   version: string distrobox version
#   container_image_default: string default container image to use
#   container_name_default:  string default container name to use
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
distrobox version: ${version}

Usage:

	distrobox create --image alpine:latest --name test --init-hooks "touch /var/tmp/test1 && touch /var/tmp/test2"
	distrobox create --image fedora:39 --name test --additional-flags "--env MY_VAR-value"
	distrobox create --image fedora:39 --name test --volume /opt/my-dir:/usr/local/my-dir:rw --additional-flags "--pids-limit 100"
	distrobox create -i docker.io/almalinux/8-init --init --name test --pre-init-hooks "dnf config-manager --enable powertools && dnf -y install epel-release"
	distrobox create --clone fedora-39 --name fedora-39-copy
	distrobox create --image alpine my-alpine-container
	distrobox create --image registry.fedoraproject.org/fedora-toolbox:latest --name fedora-toolbox-latest
	distrobox create --pull --image centos:stream9 --home ~/distrobox/centos9
	distrobox create --image alpine:latest --name test2 --additional-packages "git tmux vim"
	distrobox create --image ubuntu:22.04 --name ubuntu-nvidia --nvidia

	DBX_NON_INTERACTIVE=1 DBX_CONTAINER_NAME=test-alpine DBX_CONTAINER_IMAGE=alpine distrobox-create

Options:

	--image/-i:		image to use for the container	default: ${container_image_default}
	--name/-n:		name for the distrobox          default: ${container_name_default}
	--hostname:		hostname for the distrobox      default: $(uname -n)
	--pull/-p:		pull the image even if it exists locally (implies --yes)
	--yes/-Y:		non-interactive, pull images without asking
	--root/-r:		launch podman/docker/lilipod with root privileges. This is the only supported way to run with root
				privileges. Do not use "sudo distrobox". If you need to specify a different program (e.g. 'doas') for root privileges,
				use the DBX_SUDO_PROGRAM environment variable or the 'distrobox_sudo_program' config variable.
	--clone/-c:		name of the distrobox container to use as base for a new container
				this will be useful to either rename an existing distrobox or have multiple copies
				of the same environment.
	--home/-H:		select a custom HOME directory for the container. Useful to avoid host's home littering with temp files.
	--volume:		additional volumes to add to the container
	--additional-flags/-a:	additional flags to pass to the container manager command
	--additional-packages/-ap:	additional packages to install during initial container setup
	--init-hooks:		additional commands to execute at the end of container initialization
	--pre-init-hooks:	additional commands to execute at the start of container initialization
	--init/-I:		use init system (like systemd) inside the container.
				this will make host's processes not visible from within the container. (assumes --unshare-process)
				may require additional packages depending on the container image: https://github.com/89luca89/distrobox/blob/main/docs/useful_tips.md#using-init-system-inside-a-distrobox
	--nvidia:		try to integrate host's nVidia drivers in the guest
	--platform:		specify which platform to use, eg: linux/arm64
	--unshare-devsys:          do not share host devices and sysfs dirs from host
	--unshare-groups:          do not forward user's additional groups into the container
	--unshare-ipc:          do not share ipc namespace with host
	--unshare-netns:        do not share the net namespace with host
	--unshare-process:          do not share process namespace with host
	--unshare-all:          activate all the unshare flags below
	--compatibility/-C:	show list of compatible images
	--help/-h:		show this message
	--no-entry:		do not generate a container entry in the application list
	--dry-run/-d:		only print the container manager command generated
	--verbose/-v:		show more verbosity
	--version/-V:		show version

	--absolutely-disable-root-password-i-am-really-positively-sure: ⚠️ ⚠️  when setting up a rootful distrobox, this will skip user password setup, leaving it blank. ⚠️ ⚠️

Compatibility:

	for a list of compatible images and container managers, please consult the man page:
		man distrobox-compatibility
	or run
		distrobox create --compatibility
	or consult the documentation page on: https://github.com/89luca89/distrobox/blob/main/docs/compatibility.md
EOF
}

# show_compatibility will print the list of compatible images to stdout, caching locally in a file.
# Arguments:
#   None
# Expected global variables:
#   app_cache_dir: cache dir to write to
#   version:       distrobox version
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_compatibility()
{
	if [ ! -e "${app_cache_dir}/distrobox-compatibility-${version}" ] ||
		[ ! -s "${app_cache_dir}/distrobox-compatibility-${version}" ]; then
		mkdir -p "${app_cache_dir}"

		# If we don't have a cache file, we need connectivity. Ensure we have
		# one and return error if not.
		if ! curl -s "https://github.com" > /dev/null; then
			printf >&2 "ERROR: no cache file and no connectivity found, cannot retrieve compatibility list.\n"
			exit 1
		fi
		# We want to download the correspondent version of the compatibility table and extract a list from it.
		# Always use the docs as source of truth for this.
		curl -s \
			"https://raw.githubusercontent.com/89luca89/distrobox/${version}/docs/compatibility.md" |
			sed -n -e '/| Alma/,/| Void/ p' |
			cut -d '|' -f 4 |
			sed 's|<br>|\n|g' |
			tr -d ' ' |
			sort -u > "${app_cache_dir}/distrobox-compatibility-${version}"
	fi
	cat "${app_cache_dir}/distrobox-compatibility-${version}"
}

# Parse arguments
while :; do
	case $1 in
		-h | --help)
			# Call a "show_help" function to display a synopsis, then exit.
			show_help
			exit 0
			;;
		-v | --verbose)
			verbose=1
			shift
			;;
		-V | --version)
			printf "distrobox: %s\n" "${version}"
			exit 0
			;;
		--no-entry)
			shift
			container_generate_entry=0
			;;
		-d | --dry-run)
			shift
			dryrun=1
			;;
		-r | --root)
			shift
			rootful=1
			;;
		--absolutely-disable-root-password-i-am-really-positively-sure)
			shift
			nopasswd=1
			;;
		-I | --init)
			shift
			init=1
			unshare_groups=1
			unshare_process=1
			;;
		--unshare-ipc)
			shift
			unshare_ipc=1
			;;
		--unshare-groups)
			shift
			unshare_groups=1
			;;
		--unshare-netns)
			shift
			unshare_netns=1
			;;
		--unshare-process)
			shift
			unshare_process=1
			;;
		--unshare-devsys)
			shift
			unshare_devsys=1
			;;
		--unshare-all)
			shift
			unshare_devsys=1
			unshare_groups=1
			unshare_ipc=1
			unshare_netns=1
			unshare_process=1
			;;
		-C | --compatibility)
			show_compatibility
			exit 0
			;;
		-i | --image)
			if [ -n "$2" ]; then
				container_image="$2"
				shift
				shift
			fi
			;;
		-n | --name)
			if [ -n "$2" ]; then
				container_name="$2"
				shift
				shift
			fi
			;;
		--hostname)
			if [ -n "$2" ]; then
				container_hostname="$2"
				shift
				shift
			fi
			;;
		-c | --clone)
			if [ -n "$2" ]; then
				container_clone="$2"
				shift
				shift
			fi
			;;
		-H | --home)
			if [ -n "$2" ]; then
				# Remove trailing slashes
				container_user_custom_home="$(echo "$2" | sed 's:/*$::')"
				shift
				shift
			fi
			;;
		-p | --pull)
			container_always_pull=1
			shift
			;;
		--nvidia)
			shift
			nvidia=1
			;;
		-Y | --yes)
			non_interactive=1
			shift
			;;
		--volume)
			if [ -n "$2" ]; then
				container_additional_volumes="${container_additional_volumes} ${2}"
				shift
				shift
			fi
			;;
		--platform)
			if [ -n "$2" ]; then
				container_platform="--platform=${2}"
				shift
				shift
			fi
			;;
		-a | --additional-flags)
			if [ -n "$2" ]; then
				container_manager_additional_flags="${container_manager_additional_flags} ${2}"
				shift
				shift
			fi
			;;
		-ap | --additional-packages)
			if [ -n "$2" ]; then
				container_additional_packages="${container_additional_packages} ${2}"
				shift
				shift
			fi
			;;
		--init-hooks)
			if [ -n "$2" ]; then
				container_init_hook="$2"
				shift
				shift
			fi
			;;
		--pre-init-hooks)
			if [ -n "$2" ]; then
				container_pre_init_hook="${2}"
				shift
				shift
			fi
			;;
		--) # End of all options.
			shift
			break
			;;
		-*) # Invalid options.
			printf >&2 "ERROR: Invalid flag '%s'\n\n" "$1"
			show_help
			exit 1
			;;
		*) # Default case: If no more options then break out of the loop.
			# If we have a flagless option and container_name is not specified
			# then let's accept argument as container_name
			if [ -n "$1" ]; then
				container_name="$1"
				shift
			else
				break
			fi
			;;
	esac
done

set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
fi

# If no clone option and no container image, let's choose a default image to use.
# Fedora toolbox is a sensitive default
if [ -z "${container_clone}" ] && [ -z "${container_image}" ]; then
	container_image="${container_image_default}"
fi

# If no name is specified and we're using the default container_image, then let's
# set a default name for the container, that is distinguishable from the default
# toolbx one. This will avoid problems when using both toolbx and distrobox on
# the same system.
if [ -z "${container_name}" ] && [ "${container_image}" = "${container_image_default}" ]; then
	container_name="${container_name_default}"
fi

# If no container_name is declared, we build our container name starting from the
# container image specified.
#
# Examples:
#	alpine -> alpine
#	ubuntu:20.04 -> ubuntu-20.04
#	registry.fedoraproject.org/fedora-toolbox:39 -> fedora-toolbox-39
#	ghcr.io/void-linux/void-linux:latest-full-x86_64 -> void-linux-latest-full-x86_64
if [ -z "${container_name}" ]; then
	container_name="$(basename "${container_image}" | sed -E 's/[:.]/-/g')"
fi

# set the container hostname to default value
if [ -z "${container_hostname}" ]; then
	container_hostname="$(uname -n)"

	if [ "${unshare_netns}" -eq 1 ]; then
		container_hostname="${container_name}.${container_hostname}"
	fi
fi

# check if container hostname is less than 64 chars to prevent issues
if [ "$(printf "%s" "${container_hostname}" | wc -m)" -gt 64 ]; then
	printf >&2 "ERROR: Invalid hostname '%s', longer than 64 characters\n" "${container_hostname}"
	printf >&2 "ERROR: Use use --hostname argument to set it manually\n"
	exit 1
fi

# We depend on a container manager let's be sure we have it
# First we use podman, else docker, else lilipod
case "${container_manager}" in
	autodetect)
		if command -v podman > /dev/null; then
			container_manager="podman"
		elif command -v podman-launcher > /dev/null; then
			container_manager="podman-launcher"
		elif command -v docker > /dev/null; then
			container_manager="docker"
		elif command -v lilipod > /dev/null; then
			container_manager="lilipod"
		fi
		;;
	podman)
		container_manager="podman"
		;;
	podman-launcher)
		container_manager="podman-launcher"
		;;
	lilipod)
		container_manager="lilipod"
		;;
	docker)
		container_manager="docker"
		;;
	*)
		printf >&2 "Invalid input %s.\n" "${container_manager}"
		printf >&2 "The available choices are: 'autodetect', 'podman', 'docker', 'lilipod'\n"
		;;
esac

# Be sure we have a container manager to work with.
if ! command -v "${container_manager}" > /dev/null && [ "${dryrun}" -eq 0 ]; then
	# Error: we need at least one between docker, podman or lilipod.
	printf >&2 "Missing dependency: we need a container manager.\n"
	printf >&2 "Please install one of podman,  docker or lilipod.\n"
	printf >&2 "You can follow the documentation on:\n"
	printf >&2 "\tman distrobox-compatibility\n"
	printf >&2 "or:\n"
	printf >&2 "\thttps://github.com/89luca89/distrobox/blob/main/docs/compatibility.md\n"
	exit 127
fi
# add  verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
	container_manager="${container_manager} --log-level debug"
fi

# prepend sudo (or the specified sudo program) if we want our container manager to be rootful
if [ "${rootful}" -ne 0 ]; then
	container_manager="${distrobox_sudo_program-} ${container_manager}"
fi

# if nopasswd, then let the init know via a mountpoint
if [ "${nopasswd}" -ne 0 ]; then
	container_manager_additional_flags="${container_manager_additional_flags}
			--volume /dev/null:/run/.nopasswd:ro"
fi

# Signal rootless mode explicitly so distrobox-init does not rely solely on
# the /etc/shadow heuristic, which gives false positives on Docker Desktop
# (macOS) where the container always has root access to the VM filesystem.
if [ "${rootful}" -eq 0 ]; then
	container_manager_additional_flags="${container_manager_additional_flags}
			--volume /dev/null:/run/.distrobox.rootless:ro"
fi

# inject additional volumes if specified
if [ -n "${container_additional_volumes}" ]; then
	for volume in ${container_additional_volumes}; do
		container_manager_additional_flags="${container_manager_additional_flags}
			--volume ${volume}"
	done
fi

# Check that we have a complete distrobox installation or
# entrypoint and export will not work.
if [ -z "${distrobox_entrypoint_path}" ] || [ -z "${distrobox_export_path}" ]; then
	printf >&2 "Error: no distrobox-init found in %s\n" "${PATH}"
	exit 127
fi

# get_clone_image will return the image name of a cloned existing container taken
# as input.
# Arguments:
#   None
# Expected global variables:
#   container_manager: string container manager to use
#   container_clone: string container name to clone
# Expected env variables:
#   None
# Outputs:
#   prints the image name of the newly cloned container
get_clone_image()
{
	# We need to clone a container.
	# to do this we will commit the container and create a new tag. Then use it
	# as image for the new container.
	#
	# to perform this we first ensure the source container exists and that the
	# source container is stopped, else the clone will not work,
	container_source_status="$(${container_manager} inspect --type container \
		--format '{{.State.Status}}' "${container_clone}")"
	# If the container is not already running, we need to start if first
	if [ "${container_source_status}" = "running" ]; then
		printf >&2 "Container %s is running.\nPlease stop it first.\n" "${container_clone}"
		printf >&2 "Cannot clone a running container.\n"
		return 1
	fi

	# Now we can extract the container ID and commit it to use as source image
	# for the new container.
	container_source_id="$(${container_manager} inspect --type container \
		--format '{{.ID}}' "${container_clone}")"
	container_commit_tag="$(echo "${container_clone}:$(date +%F)" | tr '[:upper:]' '[:lower:]')"

	# Commit current container state to a new image tag
	printf >&2 "Duplicating %s...\n" "${container_clone}"
	if ! ${container_manager} container commit \
		"${container_source_id}" "${container_commit_tag}" > /dev/null; then

		printf >&2 "Cannot clone container: %s\n" "${container_clone}"
		return 1
	fi

	# Return the image tag to use for the new container creation.
	printf "%s" "${container_commit_tag}"
	return 0
}

# generate_create_command will produce a Podman or Docker command to execute.
# Arguments:
#   None
# Expected global variables:
#   container_manager: string container manager to use
#   container_name: string container name
#   container_image: string container image
#   container_manager_additional_flags: string container manager additional flags to use
#   container_hostname: string container hostname
#   container_additional_packages: string additional packages
#   container_pre_init_hook: string pre init hooks
#   container_init_hook: string init hooks
#   container_user_home: string user's home path
#   container_user_name: string user's username
#   container_user_uid: string user's UID
#   container_user_gid: string user's GID
#   container_home_prefix: string container's custom home prefix
#   container_user_custom_home: string container's custom home path
#   init: bool initful
#   nvidia: bool nvidia integration
#   rootful: bool rootful
#   unshare_devsys: bool unshare devsys
#   unshare_groups: bool unshare groups
#   unshare_ipc: bool unshare ipc
#   unshare_netns: bool unshare netns
#   unshare_process: bool unshare proc
# Expected env variables:
#   None
# Outputs:
#   prints the podman, docker or lilipod command to create the distrobox container
generate_create_command()
{
	# On macOS, Docker Desktop mounts all paths as private in its Linux VM,
	# so rslave/rshared bind propagation is not supported.
	if [ "$(uname -s)" = "Darwin" ]; then
		rslave=""
	else
		rslave=":rslave"
	fi

	# Set the container hostname the same as the container name.
	result_command="${container_manager} create"
	result_command="${result_command} ${container_platform}"
	# use the host's namespace for ipc, network, pid, ulimit
	result_command="${result_command}
		--hostname \"${container_hostname}\"
		--name \"${container_name}\"
		--privileged
		--security-opt label=disable
		--security-opt apparmor=unconfined
		--pids-limit=-1
		--user root:root"

	if [ "${unshare_ipc}" -eq 0 ]; then
		result_command="${result_command}
			--ipc host"
	fi

	if [ "${unshare_netns}" -eq 0 ]; then
		result_command="${result_command}
			--network host"
	fi

	if [ "${unshare_process}" -eq 0 ]; then
		result_command="${result_command}
			--pid host"
	fi
	# Mount useful stuff inside the container.
	# We also mount host's root filesystem to /run/host, to be able to syphon
	# dynamic configurations from the host.
	#
	# Mount user home, dev and host's root inside container.
	# This grants access to external devices like usb webcams, disks and so on.
	#
	# Mount also the distrobox-init utility as the container entrypoint.
	# Also mount in the container the distrobox-export and distrobox-host-exec
	# utilities.
	result_command="${result_command}
		--label \"manager=distrobox\"
		--label \"distrobox.unshare_groups=${unshare_groups}\"
		--env \"SHELL=$(basename "${SHELL:-"/bin/bash"}")\"
		--env \"HOME=${container_user_home}\"
		--env \"container=${container_manager}\"
		--env \"TERMINFO_DIRS=/usr/share/terminfo:/run/host/usr/share/terminfo\"
		--env \"CONTAINER_ID=${container_name}\"
		--volume /tmp:/tmp${rslave}
		--volume \"${distrobox_entrypoint_path}\":/usr/bin/entrypoint:ro
		--volume \"${distrobox_export_path}\":/usr/bin/distrobox-export:ro
		--volume \"${distrobox_hostexec_path}\":/usr/bin/distrobox-host-exec:ro
		--volume \"${container_user_home}\":\"${container_user_home}\"${rslave}"

	# Due to breaking change in https://github.com/opencontainers/runc/commit/d4b670fca6d0ac606777376440ffe49686ce15f4
	# now we cannot mount /:/run/host as before, as it will try to mount RO partitions as RW thus breaking things.
	# This will ensure we will mount directories one-by-one thus avoiding this problem.
	#
	# This happens ONLY with podman+runc, docker and lilipod are unaffected, so let's do this only if we have podman AND runc.
	if echo "${container_manager}" | grep -q "podman" && ${container_manager} info 2> /dev/null | grep -q runc > /dev/null 2>&1; then
		for rootdir in /*; do

			# Skip symlinks
			if [ -L "${rootdir}" ]; then
				continue
			fi

			# Find if the directory belongs to a RO mount, if do, mount it as RO+Rslave
			if findmnt --notruncate --noheadings --list --output OPTIONS --target "${rootdir}" |
				tr ',' '\n' | grep -q "^ro$"; then

				result_command="${result_command}
				--volume ${rootdir}:/run/host${rootdir}:ro${rslave}"
				continue
			fi

			# Else we mount it RW+Rslave
			result_command="${result_command}
				--volume ${rootdir}:/run/host${rootdir}${rslave}"
		done
	else
		# We're either on podman+crun, docker or lilipod, let's keep old behaviour
		result_command="${result_command}
			--volume /:/run/host/${rslave}"

	fi

	if [ "${unshare_devsys}" -eq 0 ]; then
		result_command="${result_command}
			--volume /dev:/dev${rslave}
			--volume /sys:/sys${rslave}"
	fi

	# In case of initful containers, we implement a series of mountpoint in order
	# for systemd to work properly inside a container.
	# The following are a flag-based implementation of what podman's --systemd flag
	# does under the hood, as explained in their docs here:
	#   https://docs.podman.io/en/latest/markdown/options/systemd.html
	#
	# set the default stop signal to SIGRTMIN+3.
	# mount tmpfs file systems on the following directories
	#	/run
	#	/run/lock
	#	/tmp
	#	/var/lib/journal
	#	/sys/fs/cgroup/systemd <- this one is done by cgroupns=host
	if [ "${init}" -eq 1 ] && echo "${container_manager}" | grep -q "docker"; then
		# In case of docker we're actually rootful, so we need to use hosts cgroups
		result_command="${result_command}
			--cgroupns host"
	fi
	if [ "${init}" -eq 1 ] && echo "${container_manager}" | grep -vq "podman"; then
		# In case of all other non-podman container managers, we can do this
		result_command="${result_command}
			--stop-signal SIGRTMIN+3
			--mount type=tmpfs,destination=/run
			--mount type=tmpfs,destination=/run/lock
			--mount type=tmpfs,destination=/var/lib/journal"
	fi

	# This fix is needed so that the container can have a separate devpts instance
	# inside
	# This will mount an empty /dev/pts, and the init will take care of mounting
	# a new devpts with the proper flags set
	# Mounting an empty volume there, is needed in order to ensure that no package
	# manager tries to fiddle with /dev/pts/X that would not be writable by them
	#
	# This implementation is done this way in order to be compatible with both
	# docker and podman
	if [ "${unshare_devsys}" -eq 0 ]; then
		result_command="${result_command}
			--volume /dev/pts
			--volume /dev/null:/dev/ptmx"
	fi

	# This fix is needed as on Selinux systems, the host's selinux sysfs directory
	# will be mounted inside the rootless container.
	#
	# This works around this and allows the rootless container to work when selinux
	# policies are installed inside it.
	#
	# Ref. Podman issue 4452:
	#    https://github.com/containers/podman/issues/4452
	if [ -e "/sys/fs/selinux" ]; then
		result_command="${result_command}
			--volume /sys/fs/selinux"
	fi

	# This fix is needed as systemd (or journald) will try to set ACLs on this
	# path. For now overlayfs and fuse.overlayfs are not compatible with ACLs
	#
	# This works around this using an unnamed volume so that this path will be
	# mounted with a normal non-overlay FS, allowing ACLs and preventing errors.
	#
	# This work around works in conjunction with distrobox-init's package manager
	# setups.
	# So that we can use pre/post hooks for package managers to present to the
	# systemd install script a blank path to work with, and mount the host's
	# journal path afterwards.
	result_command="${result_command}
			--volume /var/log/journal"

	# In some systems, for example using sysvinit, /dev/shm is a symlink
	# to /run/shm, instead of the other way around.
	# Resolve this detecting if /dev/shm is a symlink and mount original
	# source also in the container.
	if [ -L "/dev/shm" ] && [ "${unshare_ipc}" -eq 0 ]; then
		result_command="${result_command}
			--volume $(realpath /dev/shm):$(realpath /dev/shm)"
	fi

	# Ensure that both all container managers (not only podman) support forwarding of RedHat subscription-manager
	# This is needed in order to have a working subscription forwarded into the container,
	# this will ensure that rhel-9-for-x86_64-appstream-rpms and rhel-9-for-x86_64-baseos-rpms repos
	# will be available in the container, so that distrobox-init will be able to
	# install properly all the dependencies like mesa drivers.
	#
	# /run/secrets is a standard location for RHEL containers, that is being pointed by
	# /etc/rhsm-host by default.
	RHEL_SUBSCRIPTION_FILES="
		/etc/pki/entitlement/:/run/secrets/etc-pki-entitlement:ro
		/etc/rhsm/:/run/secrets/rhsm:ro
		/etc/yum.repos.d/redhat.repo:/run/secrets/redhat.repo:ro
	"
	for rhel_file in ${RHEL_SUBSCRIPTION_FILES}; do
		if [ -e "$(echo "${rhel_file}" | cut -d':' -f1)" ]; then
			result_command="${result_command}
				--volume ${rhel_file}"
		fi
	done

	# If we have a home prefix to use, ano no custom home set, then we set
	# the custom home to be PREFIX/CONTAINER_NAME
	if [ -n "${container_home_prefix}" ] && [ -z "${container_user_custom_home}" ]; then
		container_user_custom_home="${container_home_prefix}/${container_name}"
	fi

	# If we have a custom home to use,
	#	1- override the HOME env variable
	#	2- export the DISTROBOX_HOST_HOME env variable pointing to original HOME
	# 	3- mount the custom home inside the container.
	if [ -n "${container_user_custom_home}" ]; then
		if [ ! -d "${container_user_custom_home}" ]; then
			if ! mkdir -p "${container_user_custom_home}"; then
				printf >&2 "Do you have permission to write to %s?\n" "${container_user_custom_home}"
				exit 1
			fi
		fi
		result_command="${result_command}
			--env \"HOME=${container_user_custom_home}\"
			--env \"DISTROBOX_HOST_HOME=${container_user_home}\"
			--volume \"${container_user_custom_home}:${container_user_custom_home}${rslave}\""
	fi

	# Mount also the /var/home dir on ostree based systems
	# do this only if $HOME was not already set to /var/home/username
	if [ "${container_user_home}" != "/var/home/${container_user_name}" ] &&
		[ -d "/var/home/${container_user_name}" ]; then

		result_command="${result_command}
			--volume \"/var/home/${container_user_name}\":\"/var/home/${container_user_name}\"${rslave}"
	fi

	# Mount also the XDG_RUNTIME_DIR to ensure functionality of the apps.
	# This is skipped in case of initful containers, so that a dedicated
	# systemd user session can be used.
	if [ -d "/run/user/${container_user_uid}" ] && [ "${init}" -eq 0 ]; then
		result_command="${result_command}
			--volume /run/user/${container_user_uid}:/run/user/${container_user_uid}${rslave}"
	fi

	# These are dynamic configs needed by the container to function properly
	# and integrate with the host
	#
	# We're doing this now instead of inside the init because some distros will
	# have symlinks places for these files that use absolute paths instead of
	# relative paths.
	# This is the bare minimum to ensure connectivity inside the container.
	# These files, will then be kept updated by the main loop every 15 seconds.
	if [ "${unshare_netns}" -eq 0 ]; then
		NET_FILES="
			/etc/hosts
			/etc/resolv.conf
		"

		# If container_hostname is custom, we skip mounting /etc/hostname, else
		# we want to keep it in sync
		if [ "${container_hostname}" = "$(uname -n)" ]; then
			NET_FILES="${NET_FILES} /etc/hostname"
		fi

		for net_file in ${NET_FILES}; do
			if [ -e "${net_file}" ]; then
				result_command="${result_command}
					--volume ${net_file}:${net_file}:ro"
			fi
		done
	fi

	# These flags are not supported by docker, so we use them only if our
	# container manager is podman.
	if echo "${container_manager}" | grep -q "podman"; then
		# If possible, always prefer crun, as it allows keeping original groups.
		# useful for rootless containers.
		if command -v crun > /dev/null 2>&1; then
			result_command="${result_command}
				--runtime=crun"
		fi
		result_command="${result_command}
			--annotation run.oci.keep_original_groups=1
			--ulimit host"

		if [ "${init}" -eq 1 ]; then
			result_command="${result_command}
				--systemd=always"
		fi

		# Use keep-id only if going rootless.
		if [ "${rootful}" -eq 0 ]; then
			result_command="${result_command}
				--userns keep-id"

			# Test if podman supports keep-id:size=
			if podman run --rm --userns=keep-id:size=65536 "${container_image}" /bin/true > /dev/null 2>&1 || [ "$?" -eq 127 ]; then
				has_keepid_size=1
			else
				has_keepid_size=0
			fi

			# Add :size=65536 if wanted
			if [ "${has_keepid_size}" -eq 1 ] && [ "${userns_nolimit}" -eq 0 ]; then
				result_command="${result_command}:size=65536"
			fi
		fi
	fi

	# Add additional flags
	result_command="${result_command}
		${container_manager_additional_flags}"

	# Now execute the entrypoint, refer to `distrobox-init -h` for instructions
	#
	# Be aware that entrypoint corresponds to distrobox-init, the copying of it
	# inside the container is moved to distrobox-enter, in the start phase.
	# This is done to make init, export and host-exec location independent from
	# the host, and easier to upgrade.
	#
	# We set the entrypoint _before_ running the container image so that
	# we can override any user provided entrypoint if need be
	result_command="${result_command}
	--entrypoint /usr/bin/entrypoint
	${container_image}
		--verbose
		--name \"${container_user_name}\"
		--user ${container_user_uid}
		--group ${container_user_gid}
		--home \"${container_user_custom_home:-"${container_user_home}"}\"
		--init \"${init}\"
		--nvidia \"${nvidia}\"
		--pre-init-hooks \"${container_pre_init_hook}\"
		--additional-packages \"${container_additional_packages}\"
		-- '${container_init_hook}'
	"
	# use container_user_custom_home if defined, else fallback to normal home.

	# Return generated command.
	printf "%s" "${result_command}"
}

# dry run mode, just generate the command and print it. No creation.
if [ "${dryrun}" -ne 0 ]; then
	if [ -n "${container_clone}" ]; then
		container_image="${container_clone}"
	fi
	cmd="$(generate_create_command)"
	cmd="$(echo "${cmd}" | sed 's/\t//g')"
	printf "%s\n" "${cmd}"
	exit 0
fi

# Check if the container already exists.
# If it does, notify the user and exit.
if ${container_manager} inspect --type container "${container_name}" > /dev/null 2>&1; then
	printf "Distrobox named '%s' already exists.\n" "${container_name}"
	printf "To enter, run:\n\n"
	# If it's a rootful container AND user is not logged as root.
	if [ "${rootful}" -eq 1 ] && [ "${container_user_uid}" -ne 0 ]; then
		printf "distrobox enter --root %s\n\n" "${container_name}"
	# If user is logged as root OR it's a rootless container.
	elif [ "${container_user_uid}" -eq 0 ] || [ "${rootful}" -eq 0 ]; then
		printf "distrobox enter %s\n\n" "${container_name}"
	fi
	exit 0
fi

# if we are using the clone flag, let's set the image variable
# to the output of container duplication
if [ -n "${container_clone}" ]; then
	if ! echo "${container_manager}" | grep -Eq "podman|docker"; then
		printf >&2 "ERROR: clone is only supported with docker and podman\n"
		exit 127
	fi
	container_image="$(get_clone_image)"
fi

# First, check if the image exists in the host or auto-pull is enabled
# If not prompt to download it.
if [ "${container_always_pull}" -eq 1 ] ||
	! ${container_manager} inspect --type image "${container_image}" > /dev/null 2>&1; then

	# If we do auto-pull, don't ask questions
	if [ "${non_interactive}" -eq 1 ] || [ "${container_always_pull}" -eq 1 ]; then
		response="yes"
	else
		# Prompt to download it.
		printf >&2 "Image %s not found.\n" "${container_image}"
		printf >&2 "Do you want to pull the image now? [Y/n]: "
		read -r response
		response="${response:-"Y"}"
	fi

	# Accept only y,Y,Yes,yes,n,N,No,no.
	case "${response}" in
		y | Y | Yes | yes | YES)
			# Pull the image
			# shellcheck disable=SC2086
			${container_manager} pull ${container_platform} "${container_image}"
			;;
		n | N | No | no | NO)
			printf >&2 "next time, run this command first:\n"
			printf >&2 "\t%s pull %s\n" "${container_manager}" "${container_image}"
			exit 0
			;;
		*) # Default case: If no more options then break out of the loop.
			printf >&2 "Invalid input.\n"
			printf >&2 "The available choices are: y,Y,Yes,yes,YES or n,N,No,no,NO.\nExiting.\n"
			exit 1
			;;
	esac
fi

# Generate the create command and run it
printf >&2 "Creating '%s' using image %s\t" "${container_name}" "${container_image}"
cmd="$(generate_create_command)"
# Eval the generated command. If successful display an helpful message.
# shellcheck disable=SC2086
if eval ${cmd} > /dev/null; then
	printf >&2 "\033[32m [ OK ]\n\033[0mDistrobox '%s' successfully created.\n" "${container_name}"
	printf >&2 "To enter, run:\n\n"
	# If it's a rootful container AND user is not logged as root.
	if [ "${rootful}" -eq 1 ] && [ "${container_user_uid}" -ne 0 ]; then
		printf "distrobox enter --root %s\n\n" "${container_name}"
	# If user is logged as root OR it's a rootless container.
	elif [ "${container_user_uid}" -eq 0 ] || [ "${rootful}" -eq 0 ]; then
		printf "distrobox enter %s\n\n" "${container_name}"
	fi

	# We've created the box, let's also create the entry
	if [ "${rootful}" -eq 0 ]; then
		if [ "${container_generate_entry}" -ne 0 ]; then
			"${distrobox_genentry_path}" "${container_name}"
		fi
	fi
else
	error="$?"
	printf >&2 "\033[31m [ ERR ]\033[0m failed to create container.\n"
	exit "${error}"
fi


================================================
FILE: distrobox-enter
================================================
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
#    https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# POSIX
# Expected env variables:
#	HOME
#	USER
# Optional env variables:
#	DBX_CONTAINER_ALWAYS_PULL
#	DBX_CONTAINER_CUSTOM_HOME
#	DBX_CONTAINER_GENERATE_ENTRY
#	DBX_CONTAINER_HOME_PREFIX
#	DBX_CONTAINER_HOSTNAME
#	DBX_CONTAINER_IMAGE
#	DBX_CONTAINER_MANAGER
#	DBX_CONTAINER_NAME
#	DBX_CONTAINER_CLEAN_PATH
#	DBX_NON_INTERACTIVE
#	DBX_VERBOSE
#	DBX_SKIP_WORKDIR
#	DBX_SUDO_PROGRAM

# Ensure we have our env variables correctly set
[ -z "${USER}" ] && USER="$(id -run)"
[ -z "${HOME}" ] && HOME="$(getent passwd "${USER}" | cut -d':' -f6)"
[ -z "${SHELL}" ] && SHELL="$(getent passwd "${USER}" | cut -d':' -f7)"

app_cache_dir=${XDG_CACHE_HOME:-"${HOME}/.cache"}/distrobox

trap cleanup TERM INT HUP EXIT

# cleanup will remove fifo and temp files, and print to stdout
# container's logs in case of error and verbose.
# Arguments:
#   None
# Expected global variables:
#   container_manager: string container manager to use
#   container_name: string container name
#   app_cache_dir: string cache dire to write file into
#   logs_pid: string pid of the podman/docker logs process
#   verbose: bool verbose
# Expected env variables:
#   None
# Outputs:
#   None
cleanup()
{
	rm -f "${app_cache_dir}/.${container_name}.fifo"
	if [ -n "${logs_pid:-}" ]; then
		kill "${logs_pid:-}" 2> /dev/null || :
	fi
	if [ "${verbose}" -eq 1 ]; then
		${container_manager} logs "${container_name}"
	fi
}

# Despite of running this script via SUDO/DOAS being not supported (the
# script itself will call the appropriate tool when necessary), we still want
# to allow people to run it as root, logged in in a shell, and create rootful
# containers.
#
# SUDO_USER is a variable set by SUDO and can be used to check whether the script was called by it. Same thing for DOAS_USER, set by DOAS.
if {
	[ -n "${SUDO_USER}" ] || [ -n "${DOAS_USER}" ]
} && [ "$(id -ru)" -eq 0 ]; then
	printf >&2 "Running %s via SUDO/DOAS is not supported. Instead, please try running:\n" "$(basename "${0}")"
	printf >&2 "  %s --root %s\n" "$(basename "${0}")" "$*"
	exit 1
fi

# Defaults
# by default we use getent to get the login shell of the user and use that
container_custom_command=0
container_command_user="$(echo "${USER}" | sed 's|\\|\\\\|g')"
container_image_default="registry.fedoraproject.org/fedora-toolbox:latest"
container_manager="autodetect"
container_manager_additional_flags=""
container_name=""
container_name_default="my-distrobox"
non_interactive=0

# Use cd + dirname + pwd so that we do not have relative paths in mount points
# We're not using "realpath" here so that symlinks are not resolved this way
# "realpath" would break situations like Nix or similar symlink based package
# management.
distrobox_enter_path="$(cd "$(dirname "$0")" && pwd)/distrobox-enter"
dryrun=0
headless=0
# If the user runs this script as root in a login shell, set rootful=1.
# There's no need for them to pass the --root flag option in such cases.
[ "$(id -ru)" -eq 0 ] && rootful=1 || rootful=0
skip_workdir=0
verbose=0
clean_path=0
version="1.8.2.4"

# Source configuration files, this is done in an hierarchy so local files have
# priority over system defaults
# leave priority to environment variables.
#
# On NixOS, for the distrobox derivation to pick up a static config file shipped
# by the package maintainer the path must be relative to the script itself.
self_dir="$(dirname "$(realpath "$0")")"
nix_config_file="${self_dir}/../share/distrobox/distrobox.conf"

config_files="
	${nix_config_file}
	/usr/share/distrobox/distrobox.conf
	/usr/share/defaults/distrobox/distrobox.conf
	/usr/etc/distrobox/distrobox.conf
	/usr/local/share/distrobox/distrobox.conf
	/etc/distrobox/distrobox.conf
	${XDG_CONFIG_HOME:-"${HOME}/.config"}/distrobox/distrobox.conf
	${HOME}/.distroboxrc
"
for config_file in ${config_files}; do
	# Shellcheck will give error for sourcing a variable file as it cannot follow
	# it. We don't care so let's disable this linting for now.
	# shellcheck disable=SC1090
	[ -e "${config_file}" ] && . "$(realpath "${config_file}")"
done
# If we're running this script as root -- as in, logged in in the shell as root
# user, and not via SUDO/DOAS --, we don't need to set distrobox_sudo_program
# as it's meaningless for this use case.
if [ "$(id -ru)" -ne 0 ]; then
	# If the DBX_SUDO_PROGRAM/distrobox_sudo_program variable was set by the
	# user, use its value instead of "sudo". But only if not running the script
	# as root (UID 0).
	distrobox_sudo_program=${DBX_SUDO_PROGRAM:-${distrobox_sudo_program:-"sudo"}}
fi

[ -n "${DBX_CONTAINER_MANAGER}" ] && container_manager="${DBX_CONTAINER_MANAGER}"
[ -n "${DBX_CONTAINER_NAME}" ] && container_name="${DBX_CONTAINER_NAME}"
[ -n "${DBX_CONTAINER_CLEAN_PATH}" ] && clean_path=1
[ -n "${DBX_SKIP_WORKDIR}" ] && skip_workdir="${DBX_SKIP_WORKDIR}"
[ -n "${DBX_NON_INTERACTIVE}" ] && non_interactive="${DBX_NON_INTERACTIVE}"
[ -n "${DBX_VERBOSE}" ] && verbose="${DBX_VERBOSE}"

# Fixup variable=[true|false], in case we find it in the config file(s)
[ "${non_interactive}" = "true" ] && non_interactive=1
[ "${non_interactive}" = "false" ] && non_interactive=0
[ "${verbose}" = "true" ] && verbose=1
[ "${verbose}" = "false" ] && verbose=0

# show_help will print usage to stdout.
# Arguments:
#   None
# Expected global variables:
#   version: distrobox version
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
distrobox version: ${version}

Usage:

	distrobox-enter --name fedora-39 -- bash -l
	distrobox-enter my-alpine-container -- sh -l
	distrobox-enter --additional-flags "--preserve-fds" --name test -- bash -l
	distrobox-enter --additional-flags "--env MY_VAR=value" --name test -- bash -l
	MY_VAR=value distrobox-enter --additional-flags "--preserve-fds" --name test -- bash -l

Options:

	--name/-n:		name for the distrobox						default: my-distrobox
	--/-e:			end arguments execute the rest as command to execute at login	default: default ${USER}'s shell
	--clean-path:		reset PATH inside container to FHS standard
	--no-tty/-T:		do not instantiate a tty
	--no-workdir/-nw:	always start the container from container's home directory
	--additional-flags/-a:	additional flags to pass to the container manager command
	--help/-h:		show this message
	--root/-r:		launch podman/docker/lilipod with root privileges. Note that if you need root this is the preferred
				way over "sudo distrobox" (note: if using a program other than 'sudo' for root privileges is necessary,
				specify it through the DBX_SUDO_PROGRAM env variable, or 'distrobox_sudo_program' config variable)
	--dry-run/-d:		only print the container manager command generated
	--verbose/-v:		show more verbosity
	--version/-V:		show version
EOF
}

# Parse arguments
while :; do
	case $1 in
		-h | --help)
			# Call a "show_help" function to display a synopsis, then exit.
			show_help
			exit 0
			;;
		-v | --verbose)
			shift
			verbose=1
			;;
		-T | -H | --no-tty)
			shift
			headless=1
			;;
		-r | --root)
			shift
			rootful=1
			;;
		-V | --version)
			printf "distrobox: %s\n" "${version}"
			exit 0
			;;
		-d | --dry-run)
			shift
			dryrun=1
			;;
		-nw | --no-workdir)
			shift
			skip_workdir=1
			;;
		-n | --name)
			if [ -n "$2" ]; then
				container_name="$2"
				shift
				shift
			fi
			;;
		-a | --additional-flags)
			if [ -n "$2" ]; then
				if [ -z "${container_manager_additional_flags=}" ]; then
					container_manager_additional_flags="$(echo "${2}" | sed -E "s/(--[a-zA-Z]+) ([^ ]+)/\1=\2/g" | sed 's/ --/\n--/g')"
				else
					container_manager_additional_flags="${container_manager_additional_flags}
					$(echo "${2}" | sed -E "s/(--[a-zA-Z]+) ([^ ]+)/\1=\2/g" | sed 's/ --/\n--/g')"
				fi
				shift
				shift
			fi
			;;
		-Y | --yes)
			non_interactive=1
			shift
			;;
		-e | --exec | --)
			container_custom_command=1
			shift
			# We pass the rest of arguments as $@ at the end
			break
			;;
		--clean-path)
			shift
			clean_path=1
			;;
		-*) # Invalid options.
			printf >&2 "ERROR: Invalid flag '%s'\n\n" "$1"
			show_help
			exit 1
			;;
		*) # Default case: If no more options then break out of the loop.
			# If we have a flagless option and container_name is not specified
			# then let's accept argument as container_name
			if [ -n "$1" ]; then
				container_name="$1"
				shift
			else
				break
			fi
			;;
	esac
done

set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
fi

if [ -z "${container_name}" ]; then
	container_name="${container_name_default}"
fi

if [ ! -t 0 ] || [ ! -t 1 ]; then
	headless=1
fi
# We depend on a container manager let's be sure we have it
# First we use podman, else docker, else lilipod
case "${container_manager}" in
	autodetect)
		if command -v podman > /dev/null; then
			container_manager="podman"
		elif command -v podman-launcher > /dev/null; then
			container_manager="podman-launcher"
		elif command -v docker > /dev/null; then
			container_manager="docker"
		elif command -v lilipod > /dev/null; then
			container_manager="lilipod"
		fi
		;;
	podman)
		container_manager="podman"
		;;
	podman-launcher)
		container_manager="podman-launcher"
		;;
	lilipod)
		container_manager="lilipod"
		;;
	docker)
		container_manager="docker"
		;;
	*)
		printf >&2 "Invalid input %s.\n" "${container_manager}"
		printf >&2 "The available choices are: 'autodetect', 'podman', 'docker', 'lilipod'\n"
		;;
esac

# Be sure we have a container manager to work with.
if ! command -v "${container_manager}" > /dev/null && [ "${dryrun}" -eq 0 ]; then
	# Error: we need at least one between docker, podman or lilipod.
	printf >&2 "Missing dependency: we need a container manager.\n"
	printf >&2 "Please install one of podman, docker or lilipod.\n"
	printf >&2 "You can follow the documentation on:\n"
	printf >&2 "\tman distrobox-compatibility\n"
	printf >&2 "or:\n"
	printf >&2 "\thttps://github.com/89luca89/distrobox/blob/main/docs/compatibility.md\n"
	exit 127
fi

# add  verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
	container_manager="${container_manager} --log-level debug"
fi

# prepend sudo (or the specified sudo program) if we want our container manager to be rootful
if [ "${rootful}" -ne 0 ]; then
	container_manager="${distrobox_sudo_program-} ${container_manager}"
fi

# generate_enter_command will produce a Podman, Docker or Lilipod command to execute to enter the container.
# Arguments:
#   None
# Expected global variables:
#   container_manager: string container manager to use
#   container_name: string container name
#   container_manager_additional_flags: string container manager additional flags to use
#   container_home: string container's home path
#   container_path: string container's default PATH variable
#   headless: bool headless mode
#   skip_workdir: bool skip workdir
#   verbose: bool verbose
#   unshare_groups
#   distrobox_enter_path
# Expected env variables:
#   PATH
#   USER
#   PWD
#   XDG_DATA_DIRS
#   XDG_CONFIG_DIRS
# Outputs:
#   prints the podman, docker or lilipod command to enter the distrobox container
generate_enter_command()
{
	result_command="exec"
	result_command="${result_command}
		--interactive"
	result_command="${result_command}
		--detach-keys="

	# In case of initful systems or unshared groups, we don't enter directly
	# as our user, but we instead enter as root, and then su $USER, in order
	# to trigger a proper login
	if [ "${unshare_groups:-0}" -eq 1 ]; then
		result_command="${result_command}
			--user=root"
	else
		result_command="${result_command}
			--user=${USER}"
	fi

	# For some usage, like use in service, or launched by non-terminal
	# eg. from desktop files, TTY can fail to instantiate, and fail to enter
	# the container.
	# To work around this, --headless let's you skip the --tty flag and make it
	# work in tty-less situations.
	# Disable tty also if we're NOT in a tty (test -t 0, test -t 1).
	if [ "${headless}" -eq 0 ]; then
		result_command="${result_command}
			--tty"
	fi

	# Entering container using our user and workdir.
	# Start container from working directory. Else default to home. Else do /.
	# Since we are entering from host, drop at workdir through '/run/host'
	# which represents host's root inside container. Any directory on host
	# even if not explicitly mounted is bound to exist under /run/host.
	# Since user $HOME is very likely present in container, enter there directly
	# to avoid confusing the user about shifted paths.
	# pass distrobox-enter path, it will be used in the distrobox-export tool.
	if [ "${skip_workdir}" -eq 0 ]; then
		workdir="${PWD:-${container_home:-"/"}}"
		if [ -n "${workdir##*"${container_home}"*}" ]; then
			workdir="/run/host${workdir}"
		fi
	else
		# Skipping workdir we just enter $HOME of the container.
		workdir="${container_home}"
	fi

	result_command="${result_command}
		--workdir=${workdir}"
	result_command="${result_command}
		--env=PWD=${workdir}"
	result_command="${result_command}
		--env=CONTAINER_ID=${container_name}"
	result_command="${result_command}
		--env=DISTROBOX_ENTER_PATH=${distrobox_enter_path}"

	# Loop through all the environment vars
	# and export them to the container.
	set +o xtrace
	# disable logging for this snippet, or it will be too talkative.
	# We filter the environment so that we do not have strange variables or
	# multiline.
	# We also NEED to ignore the HOME variable, as this is set at create time
	# and needs to stay that way to use custom home dirs. or it will be too talkative.
	result_command="${result_command}
		$(printenv | grep '=' | grep -Ev '"|`|\$' |
		grep -Ev '^(CONTAINER_ID|FPATH|HOST|HOSTNAME|HOME|PATH|PROFILEREAD|PWD|SHELL|XDG_SEAT|XDG_VTNR|XDG_.*_DIRS|^_)' |
		sed 's/ /\ /g' | sed 's/^\(.*\)$/--env=\1/g')"

	# Start with the $PATH set in the container's config
	container_paths="${container_path:-""}"
	# Ensure the standard FHS program paths are in PATH environment
	standard_paths="/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin"

	if [ "${clean_path}" -eq 1 ]; then
		# only add the standard paths
		for standard_path in ${standard_paths}; do
			if [ -z "${container_paths}" ]; then
				container_paths="${standard_path}"
			else
				container_paths="${container_paths}:${standard_path}"
			fi
		done
	else
		# collect standard paths not existing from host PATH
		for standard_path in ${standard_paths}; do
			pattern="(:|^)${standard_path}(:|$)"
			if ! echo "${PATH}" | grep -Eq "${pattern}"; then
				if [ -z "${container_paths}" ]; then
					container_paths="${standard_path}"
				else
					container_paths="${container_paths}:${standard_path}"
				fi
			fi
		done
		# append additional standard paths to host PATH to get final container_paths
		if [ -n "${container_paths}" ]; then
			container_paths="${PATH}:${container_paths}"
		else
			container_paths="${PATH}"
		fi

		# Ensure /usr/local/{s,}bin appears before /usr/{s,}bin in PATH.
		# This follows FHS conventions: /usr/local should override /usr,
		# and ensures distrobox wrappers in /usr/local/bin are found first.
		reordered_paths=""
		IFS_OLD="${IFS}"
		IFS=":"
		for p in ${container_paths}; do
			case "${p}" in
				/usr/local/bin | /usr/local/sbin)
					# skip, will be re-inserted before their /usr counterpart
					;;
				/usr/bin)
					# insert /usr/local/bin right before /usr/bin
					reordered_paths="${reordered_paths:+${reordered_paths}:}/usr/local/bin:${p}"
					;;
				/usr/sbin)
					# insert /usr/local/sbin right before /usr/sbin
					reordered_paths="${reordered_paths:+${reordered_paths}:}/usr/local/sbin:${p}"
					;;
				*)
					reordered_paths="${reordered_paths:+${reordered_paths}:}${p}"
					;;
			esac
		done
		IFS="${IFS_OLD}"

		# If /usr/bin or /usr/sbin were not in PATH, the corresponding
		# /usr/local paths were skipped above. Re-add them if missing.
		for lp in /usr/local/bin /usr/local/sbin; do
			pattern="(:|^)${lp}(:|$)"
			if ! echo "${reordered_paths}" | grep -Eq "${pattern}"; then
				reordered_paths="${lp}:${reordered_paths}"
			fi
		done
		container_paths="${reordered_paths}"
	fi

	result_command="${result_command}
		--env=PATH=${container_paths}"

	# Ensure the standard FHS program paths are in XDG_DATA_DIRS environment
	standard_paths="/usr/local/share /usr/share"
	container_paths="${XDG_DATA_DIRS:-}"
	# add to the XDG_DATA_DIRS only after the host's paths, and only if not already present.
	for standard_path in ${standard_paths}; do
		pattern="(:|^)${standard_path}(:|$)"
		if [ -z "${container_paths}" ]; then
			container_paths="${standard_path}"
		elif ! echo "${container_paths}" | grep -Eq "${pattern}"; then
			container_paths="${container_paths}:${standard_path}"
		fi
	done
	result_command="${result_command}
		--env=XDG_DATA_DIRS=${container_paths}"

	# This correctly sets the XDG_* dirs to the container_home
	# it will be $HOME if using regular home dirs
	# if will be $container_home if using a custom home during create
	result_command="${result_command}
		--env=XDG_CACHE_HOME=${container_home}/.cache
		--env=XDG_CONFIG_HOME=${container_home}/.config
		--env=XDG_DATA_HOME=${container_home}/.local/share
		--env=XDG_STATE_HOME=${container_home}/.local/state"

	# Ensure the standard FHS program paths are in XDG_CONFIG_DIRS environment
	standard_paths="/etc/xdg"
	container_paths="${XDG_CONFIG_DIRS:-}"
	# add to the XDG_CONFIG_DIRS only after the host's paths, and only if not already present.
	for standard_path in ${standard_paths}; do
		pattern="(:|^)${standard_path}(:|$)"
		if [ -z "${container_paths}" ]; then
			container_paths="${standard_path}"
		elif ! echo "${container_paths}" | grep -Eq "${pattern}"; then
			container_paths="${container_paths}:${standard_path}"
		fi
	done
	result_command="${result_command}
		--env=XDG_CONFIG_DIRS=${container_paths}"

	# re-enable logging if it was enabled previously.
	if [ "${verbose}" -ne 0 ]; then
		set -o xtrace
	fi

	# Add additional flags
	if [ -n "${container_manager_additional_flags}" ]; then
		result_command="${result_command}
			${container_manager_additional_flags}"
	fi

	# Run selected container with specified command.
	result_command="${result_command}
		${container_name}"

	# Return generated command.
	# here we remove tabs as an artifact of using indentation in code to improve
	# readability
	printf "%s\n" "${result_command}" | tr -d '\t'
}

container_home="${HOME}"
container_path="${PATH}"
unshare_groups=0

################################################################################
# In this section we will manipulate the positional parameters
# in order to generate our long docker/podman/lilipod command to execute.
#
# We use positional parameters in order to have the shell manage escaping and spaces
# so we remove the problem of we having to handle them.
#
# 1 - handle absence of custom command, we will need to add a getent command to
#     execute the right container's user's shell
# 2 - in case of unshared groups (or initful) we need to trigger a proper login
#     using `su`, so we will need to manipulate these arguments accorodingly
# 3 - prepend our generated command
#     to do this, we use `tac` so we reverse loop it and prepend each argument.
# 4 - now that we're done, we can prepend our container_command
#     we will need to use `rev` to reverse it as we reverse loop and prepend each
#     argument
################################################################################
#
# Setup default commands if none are specified
# execute a getent command using the /bin/sh shell
# to find out the default shell of the user, and
# do a login shell with it (eg: /bin/bash -l)
if [ "${container_custom_command}" -eq 0 ]; then
	set - "$@" "/bin/sh" "-c" "\$(getent passwd '${container_command_user}' | cut -f 7 -d :) -l"
fi

# If we have a command and we're unsharing groups, we need to execute those
# command using su $container_command_user
# if we're in a tty, also allocate one
if [ "${unshare_groups:-0}" -eq 1 ]; then
	# shellcheck disable=SC2089,SC2016
	set -- "-c" '"$0" "$@"' -- "$@"
	set -- "-s" "/bin/sh" "$@"
	if [ "${headless}" -eq 0 ]; then
		set -- "--pty" "$@"
	fi
	set -- "-m" "$@"
	set -- "${container_command_user}" "$@"
	set -- "su" "$@"
fi

################################################################################
# Execution section
################################################################################

# dry run mode, just generate the command and print it. No execution.
if [ "${dryrun}" -ne 0 ]; then
	cmd="$(generate_enter_command | sed 's/\t//g')"
	printf "%s %s\n" "${cmd}" "$*"
	exit 0
fi

# Now inspect the container we're working with.
container_status="unknown"
eval "$(${container_manager} inspect --type container --format \
	'container_status={{.State.Status}};
	unshare_groups={{ index .Config.Labels "distrobox.unshare_groups" }};
	{{range .Config.Env}}{{if and (ge (len .) 5) (eq (slice . 0 5) "HOME=")}}container_home={{slice . 5 | printf "%q"}}{{end}}{{end}};
	{{range .Config.Env}}{{if and (ge (len .) 5) (eq (slice . 0 5) "PATH=")}}container_path={{slice . 5 | printf "%q"}}{{end}}{{end}}' \
	"${container_name}")"

# Check if the container is even there
if [ "${container_status}" = "unknown" ]; then
	# If not, prompt to create it first
	# If we're not-interactive, just don't ask questions
	if [ "${non_interactive}" -eq 1 ]; then
		response="yes"
	else
		printf >&2 "Create it now, out of image %s? [Y/n]: " "${container_image_default}"
		read -r response
		response="${response:-"Y"}"
	fi

	# Accept only y,Y,Yes,yes,n,N,No,no.
	case "${response}" in
		y | Y | Yes | yes | YES)
			# Ok, let's create the container with just 'distrobox create $container_name
			create_command="$(dirname "${0}")/distrobox-create"
			if [ "${rootful}" -ne 0 ]; then
				create_command="${create_command} --root"
			fi

			create_command="${create_command} --yes -i ${container_image_default} -n ${container_name}"

			printf >&2 "Creating the container %s\n" "${container_name}"

			if [ "${dryrun}" -ne 1 ]; then
				${create_command}
			fi
			;;
		n | N | No | no | NO)
			printf >&2 "Ok. For creating it, run this command:\n"
			printf >&2 "\tdistrobox create <name-of-container> --image <remote>/<docker>:<tag>\n"
			exit 0
			;;
		*) # Default case: If no more options then break out of the loop.
			printf >&2 "Invalid input.\n"
			printf >&2 "The available choices are: y,Y,Yes,yes,YES or n,N,No,no,NO.\nExiting.\n"
			exit 1
			;;
	esac
fi

# If the container is not already running, we need to start if first
if [ "${container_status}" != "running" ]; then
	# If container is not running, start it first
	#
	# Here, we save the timestamp before launching the start command, so we can
	# be sure we're working with this very same session of logs later.
	log_timestamp="$(date -u +%FT%T).000000000+00:00"
	${container_manager} start "${container_name}" > /dev/null
	#
	# Check if the container is going in error status earlier than the
	# entrypoint
	if [ "$(${container_manager} inspect \
		--type container \
		--format "{{.State.Status}}" "${container_name}")" != "running" ]; then

		printf >&2 "\033[31m Error: could not start entrypoint.\n\033[0m"
		container_manager_log="$(${container_manager} logs "${container_name}")"
		printf >&2 "%s\n" "${container_manager_log}"
		exit 1
	fi

	printf >&2 "%-40s\t" "Starting container..."
	mkdir -p "${app_cache_dir}"
	rm -f "${app_cache_dir}/.${container_name}.fifo"
	mkfifo "${app_cache_dir}/.${container_name}.fifo"
	while true; do
		# Exit early in case of crashed/stopped container during setup
		if [ "$(${container_manager} inspect --type container --format '{{.State.Status}}' "${container_name}")" != "running" ]; then
			printf >&2 "\nContainer Setup Failure!\n"
			exit 1
		fi
		# save starting loop timestamp in temp variable, we'll use it
		# after to let logs command minimize possible holes
		${container_manager} logs --since "${log_timestamp}" -f "${container_name}" \
			> "${app_cache_dir}/.${container_name}.fifo" 2>&1 &
		logs_pid="$!"

		# read logs from log_timestamp to now, line by line
		while IFS= read -r line; do
			case "${line}" in
				"+"*)
					# Ignoring logging commands
					;;
				"Error:"*)
					printf >&2 "\033[31m %s\n\033[0m" "${line}"
					exit 1
					;;
				"Warning:"*)
					printf >&2 "\n\033[33m %s\033[0m" "${line}"
					;;
				"distrobox:"*)
					current_line="$(echo "${line}" | cut -d' ' -f2-)"
					# Save current line in the status, to avoid printing the same line multiple times
					printf >&2 "\033[32m [ OK ]\n\033[0m%-40s\t" "${current_line}"
					;;
				"container_setup_done"*)
					printf >&2 "\033[32m [ OK ]\n\033[0m"
					kill "${logs_pid}" > /dev/null 2>&1
					break 2
					;;
				*) ;;
			esac
		done < "${app_cache_dir}/.${container_name}.fifo"
	done
	# cleanup fifo
	rm -f "${app_cache_dir}/.${container_name}.fifo"
	printf >&2 "\nContainer Setup Complete!\n"
fi

# Generate the exec command and run it
cmd="$(generate_enter_command | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}')"
# Reverse it so we can reverse loop and prepend the command's arguments
# to our positional parameters
IFS='
'
for arg in ${cmd}; do
	set - "${arg}" "$@"
done

# Prepend the container manager command
# reverse it first, so we can loop backward as we're prepending not appending
IFS=' '
for arg in $(echo "${container_manager}" | rev); do
	arg="$(echo "${arg}" | rev)"
	set - "${arg}" "$@"
done

exec "$@"


================================================
FILE: distrobox-ephemeral
================================================
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
#    https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# POSIX
# Optional env variables:
#	DBX_CONTAINER_MANAGER
#	DBX_CONTAINER_NAME
#	DBX_VERBOSE
#	DBX_SUDO_PROGRAM

# Despite of running this script via SUDO/DOAS being not supported (the
# script itself will call the appropriate tool when necessary), we still want
# to allow people to run it as root, logged in in a shell, and create rootful
# containers.
#
# SUDO_USER is a variable set by SUDO and can be used to check whether the script was called by it. Same thing for DOAS_USER, set by DOAS.
if {
	[ -n "${SUDO_USER}" ] || [ -n "${DOAS_USER}" ]
} && [ "$(id -ru)" -eq 0 ]; then
	printf >&2 "Running %s via SUDO/DOAS is not supported. Instead, please try running:\n" "$(basename "${0}")"
	printf >&2 "  %s --root %s\n" "$(basename "${0}")" "$*"
	exit 1
fi

# Ensure we have our env variables correctly set
[ -z "${USER}" ] && USER="$(id -run)"
[ -z "${HOME}" ] && HOME="$(getent passwd "${USER}" | cut -d':' -f6)"
[ -z "${SHELL}" ] && SHELL="$(getent passwd "${USER}" | cut -d':' -f7)"

trap cleanup TERM INT HUP

name=$(mktemp -u distrobox-XXXXXXXXXX)
container_command=""
create_flags=""
distrobox_path="$(dirname "${0}")"
extra_flags=""
# If the user runs this script as root in a login shell, set rootful=1.
# There's no need for them to pass the --root flag option in such cases.
[ "$(id -ru)" -eq 0 ] && rootful=1 || rootful=0
verbose=0
version="1.8.2.4"
container_additional_packages=""
container_init_hook=" "
container_manager_additional_flags=""
container_pre_init_hook=" "

# Source configuration files, this is done in an hierarchy so local files have
# priority over system defaults
# leave priority to environment variables.
#
# On NixOS, for the distrobox derivation to pick up a static config file shipped
# by the package maintainer the path must be relative to the script itself.
self_dir="$(dirname "$(realpath "$0")")"
nix_config_file="${self_dir}/../share/distrobox/distrobox.conf"

config_files="
	${nix_config_file}
	/usr/share/distrobox/distrobox.conf
	/usr/share/defaults/distrobox/distrobox.conf
	/usr/etc/distrobox/distrobox.conf
	/usr/local/share/distrobox/distrobox.conf
	/etc/distrobox/distrobox.conf
	${XDG_CONFIG_HOME:-"${HOME}/.config"}/distrobox/distrobox.conf
	${HOME}/.distroboxrc
"
for config_file in ${config_files}; do
	# Shellcheck will give error for sourcing a variable file as it cannot follow
	# it. We don't care so let's disable this linting for now.
	# shellcheck disable=SC1090
	[ -e "${config_file}" ] && . "$(realpath "${config_file}")"
done

[ -n "${DBX_VERBOSE}" ] && verbose="${DBX_VERBOSE}"

# Fixup variable=[true|false], in case we find it in the config file(s)
[ "${verbose}" = "true" ] && verbose=1
[ "${verbose}" = "false" ] && verbose=0

# show_help will print usage to stdout.
# Arguments:
#   None
# Expected global variables:
#   version: distrobox version
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
distrobox version: ${version}

Usage:

	distrobox-ephemeral [--root/-r]

Options:

	--root/-r:		launch podman/docker/lilipod with root privileges. Note that if you need root this is the preferred
				way over "sudo distrobox" (note: if using a program other than 'sudo' for root privileges is necessary,
				specify it through the DBX_SUDO_PROGRAM env variable, or 'distrobox_sudo_program' config variable)
	--verbose/-v:		show more verbosity
	--help/-h:		show this message
	--/-e:			end arguments execute the rest as command to execute at login	default: default ${USER}'s shell
	--version/-V:		show version

See also:

	distrobox-ephemeral also inherits all the flags from distrobox-create:
EOF
}

# Parse arguments
while :; do
	case $1 in
		-h | --help)
			# Call a "show_help" function to display a synopsis, then exit.
			show_help
			"${distrobox_path}"/distrobox-create --help | tail -n +2
			exit 0
			;;
		-r | --root)
			shift
			rootful=1
			;;
		-v | --verbose)
			verbose=1
			shift
			;;
		-V | --version)
			printf "distrobox: %s\n" "${version}"
			exit 0
			;;
		-e | --exec | --)
			shift
			container_command="-- $*"
			break
			;;
		-n | --name)
			# Ignore --name on ephemeral
			if [ -n "$2" ]; then
				name="${2}"
				shift
				shift
			fi
			;;
		-a | --additional-flags)
			if [ -n "$2" ]; then
				container_manager_additional_flags="${container_manager_additional_flags} ${2}"
				shift
				shift
			fi
			;;
		-ap | --additional-packages)
			if [ -n "$2" ]; then
				container_additional_packages="${container_additional_packages} ${2}"
				shift
				shift
			fi
			;;
		--init-hooks)
			if [ -n "$2" ]; then
				container_init_hook="$2"
				shift
				shift
			fi
			;;
		--pre-init-hooks)
			if [ -n "$2" ]; then
				container_pre_init_hook="${2}"
				shift
				shift
			fi
			;;
		*) # Default case: If no more options then break out of the loop.
			# If we have a flagless option and container_name is not specified
			# then let's accept argument as container_name
			if [ -n "$1" ]; then
				create_flags="${create_flags} $1"
				shift
			else
				break
			fi
			;;
	esac
done

set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
	extra_flags="${extra_flags} --verbose"
fi

# prepend sudo (or the specified sudo program) if we want our container manager to be rootful
if [ "${rootful}" -ne 0 ]; then
	extra_flags="${extra_flags} --root"
fi

# generate_ephemeral_create_command will produce a distrobox-create command to execute.
# Arguments:
#   None
# Expected global variables:
#   distrobox_path = string distrobox path
#   name         = string container name
#   extra_flags  = string extra flags to inject
#   create_flags = string create extra flags to inject
# Expected env variables:
#   None
# Outputs:
#   prints the distrobox-create command handling special flags
generate_ephemeral_create_command()
{
	result_command="${distrobox_path}/distrobox-create"
	if [ -n "${container_manager_additional_flags}" ]; then
		result_command="${result_command} \
			--additional-flags \"${container_manager_additional_flags}\""
	fi
	if [ -n "${container_additional_packages}" ]; then
		result_command="${result_command} \
			--additional-packages \"${container_additional_packages}\""
	fi
	if [ -n "${container_init_hook}" ]; then
		result_command="${result_command} \
			--init-hooks \"${container_init_hook}\""
	fi
	if [ -n "${container_pre_init_hook}" ]; then
		result_command="${result_command} \
			--pre-init-hooks \"${container_pre_init_hook}\""
	fi
	result_command="${result_command} \
		${extra_flags} ${create_flags} --yes --name ${name}"

	# Return generated command.
	printf "%s" "${result_command}"
}

# cleanup will ensure we remove the ephemeral container
# Arguments:
#   None
# Expected global variables:
#   name: string the name of the container
#   extra_flags: string extra flags to append to the distrobox command
#   distrobox_path: string path to the distrobox script
# Expected env variables:
#   None
# Outputs:
#   None
cleanup()
{
	trap - TERM INT HUP
	# shellcheck disable=SC2086
	"${distrobox_path}"/distrobox-rm ${extra_flags} --force "${name}" --yes
}

cmd="$(generate_ephemeral_create_command)"
# shellcheck disable=SC2086
eval ${cmd}
# shellcheck disable=SC2086
"${distrobox_path}"/distrobox-enter ${extra_flags} "${name}" ${container_command}
exit_code="$?"

cleanup

exit "${exit_code}"


================================================
FILE: distrobox-export
================================================
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
#    https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# POSIX
# Expected env variables:
#	HOME
#	USER
#	DISTROBOX_ENTER_PATH
#	DISTROBOX_HOST_HOME

# Ensure we have our env variables correctly set
[ -z "${USER}" ] && USER="$(id -run)"
[ -z "${HOME}" ] && HOME="$(getent passwd "${USER}" | cut -d':' -f6)"
[ -z "${SHELL}" ] && SHELL="$(getent passwd "${USER}" | cut -d':' -f7)"

# Defaults
container_name="${CONTAINER_ID:-}"
[ -z "${container_name}" ] && container_name="$(grep "name=" /run/.containerenv | cut -d'=' -f2- | tr -d '"')"
export_action=""
exported_app=""
exported_app_label=""
exported_bin=""
exported_delete=0
extra_flags=""
enter_flags=""
# Use DBX_HOST_HOME if defined, else fallback to HOME
#	DBX_HOST_HOME is set in case container is created
#	with custom --home directory
host_home="${DISTROBOX_HOST_HOME:-"${HOME}"}"
dest_path="${DISTROBOX_EXPORT_PATH:-${host_home}/.local/bin}"
is_sudo=0
rootful=""
sudo_prefix=""
verbose=0
version="1.8.2.4"

sudo_askpass_path="${dest_path}/distrobox_sudo_askpass"
sudo_askpass_script="#!/bin/sh
if command -v zenity 2>&1 > /dev/null; then
	zenity --password
elif command -v kdialog 2>&1 > /dev/null; then
	kdialog --password 'A password is required...'
else
	exit 127
fi"

# We depend on some commands, let's be sure we have them
base_dependencies="basename find grep sed"
for dep in ${base_dependencies}; do
	if ! command -v "${dep}" > /dev/null; then
		printf >&2 "Missing dependency: %s\n" "${dep}"
		exit 127
	fi
done

# show_help will print usage to stdout.
# Arguments:
#   None
# Expected global variables:
#   version: distrobox version
# Expected env variables:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
distrobox version: ${version}

Usage:

	distrobox-export --app mpv [--extra-flags "flags"] [--enter-flags "flags"] [--delete] [--sudo]
	distrobox-export --bin /path/to/bin [--export-path ~/.local/bin] [--extra-flags "flags"] [--enter-flags "flags"] [--delete] [--sudo]

Options:

	--app/-a:		name of the application to export or absolute path to desktopfile to export
	--bin/-b:		absolute path of the binary to export
	--list-apps:		list applications exported from this container
	--list-binaries:	list binaries exported from this container, use -ep to specify custom paths to search
	--delete/-d:		delete exported application or binary
	--export-label/-el:	label to add to exported application name.
				Use "none" to disable.
				Defaults to (on \$container_name)
	--export-path/-ep:	path where to export the binary
	--extra-flags/-ef:	extra flags to add to the command
	--enter-flags/-nf:	flags to add to distrobox-enter
	--sudo/-S:		specify if the exported item should be run as sudo
	--help/-h:		show this message
	--verbose/-v:		show more verbosity
	--version/-V:		show version
EOF
}

# Parse arguments
while :; do
	case $1 in
		-h | --help)
			# Call a "show_help" function to display a synopsis, then exit.
			show_help
			exit 0
			;;
		-v | --verbose)
			shift
			verbose=1
			;;
		-V | --version)
			printf "distrobox: %s\n" "${version}"
			exit 0
			;;
		-a | --app)
			if [ -n "$2" ]; then
				export_action="app"
				exported_app="$2"
				shift
				shift
			fi
			;;
		-b | --bin)
			if [ -n "$2" ]; then
				export_action="bin"
				exported_bin="$2"
				shift
				shift
			fi
			;;
		--list-apps)
			export_action="list-apps"
			exported_bin="null"
			shift
			;;
		--list-binaries)
			export_action="list-binaries"
			exported_bin="null"
			shift
			;;
		-S | --sudo)
			is_sudo=1
			shift
			;;
		-el | --export-label)
			if [ -n "$2" ]; then
				exported_app_label="$2"
				shift
				shift
			fi
			;;
		-ep | --export-path)
			if [ -n "$2" ]; then
				dest_path="$2"
				shift
				shift
			fi
			;;
		-ef | --extra-flags)
			if [ -n "$2" ]; then
				extra_flags="$2"
				shift
				shift
			fi
			;;
		-nf | --enter-flags)
			if [ -n "$2" ]; then
				enter_flags="$2"
				shift
				shift
			fi
			;;
		-d | --delete)
			exported_delete=1
			shift
			;;
		-*) # Invalid options.
			printf >&2 "ERROR: Invalid flag '%s'\n\n" "$1"
			show_help
			exit 1
			;;
		*) # Default case: If no more options then break out of the loop.
			break ;;
	esac
done

set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
fi

# Ensure we can write stuff there
if [ ! -e "${dest_path}" ]; then
	mkdir -p "${dest_path}"
fi

# Check we're running inside a container and not on the host.
if [ ! -f /run/.containerenv ] && [ ! -f /.dockerenv ] && [ -z "${container:-}" ]; then
	printf >&2 "You must run %s inside a container!\n" " $(basename "$0")"
	exit 126
fi

# Check if we're in a rootful or rootless container.
if grep -q "rootless=0" /run/.containerenv 2> /dev/null; then
	rootful="--root"

	# We need an askpass script for SUDO_ASKPASS, to launch graphical apps
	# from the drawer
	if [ ! -e "${sudo_askpass_path}" ]; then
		echo "${sudo_askpass_script}" > "${sudo_askpass_path}"
		chmod +x "${sudo_askpass_path}"
	fi
fi

# We're working with HOME, so we must run as USER, not as root.
if [ "$(id -u)" -eq 0 ]; then
	printf >&2 "You must not run %s as root!\n" " $(basename "$0")"
	exit 1
fi

# Ensure we're not receiving more than one action at time.
if [ -n "${exported_app}" ] && [ -n "${exported_bin}" ]; then
	printf >&2 "Error: Invalid arguments, choose only one action below.\n"
	printf >&2 "Error: You can only export one thing at time.\n"
	exit 2
fi

# Filter enter_flags and remove redundant options
if [ -n "${enter_flags}" ]; then
	# shellcheck disable=SC2086
	set -- ${enter_flags}
	filtered_flags=""
	# Inform user that certain flags are redundant
	while [ $# -gt 0 ]; do
		case "$1" in
			--root | -r)
				printf >&2 "Warning: %s argument will be set automatically and should be removed.\n" "${1}"
				;;
			--name | -n)
				printf >&2 "Warning: %s argument will be set automatically and should be removed.\n" "${1}"
				shift
				;;
			*)
				filtered_flags="${filtered_flags} $1"
				;;
		esac
		shift
	done
	enter_flags="${filtered_flags}"
fi

# Command to execute
container_command_suffix="'${exported_bin}' ${extra_flags} \"\$@\""

# Check if exported application/binary should be run with sudo privileges
if [ "${is_sudo}" -ne 0 ]; then
	sudo_prefix="sudo -S"
	if ! ${sudo_prefix} test > /dev/null 2>&1; then
		sudo_prefix="sudo"
	fi

	# Edge case for systems with doas
	if command -v doas > /dev/null >&1; then
		sudo_prefix="doas"
		container_command_suffix="sh -l -c \"'${exported_bin}' ${extra_flags} \$@\""
	fi

	# Edge case for systems without sudo
	if command -v su-exec > /dev/null >&1; then
		sudo_prefix="su-exec root"
		container_command_suffix="sh -l -c \"'${exported_bin}' ${extra_flags} \$@\""
	fi
fi

# Prefix to add to an existing command to work through the container
container_command_prefix="${DISTROBOX_ENTER_PATH:-"distrobox-enter"} ${rootful} -n ${container_name} ${enter_flags} -- ${sudo_prefix} "

if [ -n "${rootful}" ]; then
	container_command_prefix="env SUDO_ASKPASS=\"${sudo_askpass_path}\" DBX_SUDO_PROGRAM=\"sudo --askpass\" ${container_command_prefix}"
fi

if [ -z "${exported_app_label}" ]; then
	exported_app_label=" (on ${container_name})"
elif [ "${exported_app_label}" = "none" ]; then
	exported_app_label=""
else
	# Add a leading space so that we can have "NAME LABEL" in the entry
	exported_app_label=" ${exported_app_label}"
fi

# generate_script will generate a script from template. This script will wrap the
# exported binary in order to ensure it will be executed in the right container.
# Arguments:
#	None
# Expected global variables:
#   CONTAINER_ID: id of the current container
#   container_command_suffix: string to postpone to the command to launch
#   container_name:  string name of this container
#   dest_path: string path where to export the binary
#   enter_flags:  string extra flags to append to the distrobox enter command
#   exported_bin: string path to the binary to export
#   exported_delete: bool delete the binary exported
#   extra_flags:  string extra flags to append to the exported app command
#   rootful: bool if this is a rootful container
#   sudo_prefix: string sudo command to prepend to the exported command
# Expected env variables:
#   None
# Outputs:
#	print generated script.
generate_script()
{
	cat << EOF
#!/bin/sh
# distrobox_binary
# name: ${container_name}
if [ -z "\${CONTAINER_ID}" ]; then
	exec "${DISTROBOX_ENTER_PATH:-"distrobox-enter"}" ${rootful} -n ${container_name} ${enter_flags} -- ${sudo_prefix} ${container_command_suffix}
elif [ -n "\${CONTAINER_ID}" ] && [ "\${CONTAINER_ID}" != "${container_name}" ]; then
	exec distrobox-host-exec '${dest_path}/$(basename "${exported_bin}")' "\$@"
else
	exec ${sudo_prefix} '${exported_bin}' "\$@"
fi
EOF
	return $?
}

# export_binary will export selected binary to destination directory.
# the following function will use generate_script to create a shell script in
# dest_path that will execute the exported binary in the selected distrobox.
#
# Arguments:
#	None
# Expected global variables:
#   CONTAINER_ID: id of the current container
#   container_name: string name of this container
#   dest_path: string path where to export the binary
#   exported_bin: string path to the binary to export
#   exported_delete: bool delete the binary exported
# Expected env variables:
#   None
# Outputs:
#	a generated_script in dest_path
#	or error code.
export_binary()
{
	# Ensure the binary we're exporting is installed
	if [ ! -f "${exported_bin}" ]; then
		printf >&2 "Error: cannot find %s.\n" "${exported_bin}"
		return 127
	fi
	# generate dest_file path
	dest_file="${dest_path}/$(basename "${exported_bin}")"

	# create the binary destination path if it doesn't exist
	mkdir -p "${dest_path}"

	# If we're deleting it, just do it and exit
	if [ "${exported_delete}" -ne 0 ]; then
		# ensure it's a distrobox exported binary
		if ! grep -q "distrobox_binary" "${dest_file}"; then
			printf >&2 "Error: %s is not exported.\n" "${exported_bin}"
			return 1
		fi

		if rm -f "${dest_file}"; then
			printf "%s from %s removed successfully from %s.\nOK!\n" \
				"${exported_bin}" "${container_name}" "${dest_path}"
			return 0
		fi
		return 1
	fi

	# test if we have writing rights on the file
	if ! touch "${dest_file}"; then
		printf >&2 "Error: cannot create destination file %s.\n" "${dest_file}"
		return 1
	fi

	# create the script from template and write to file
	if generate_script > "${dest_file}"; then
		chmod +x "${dest_file}"
		printf "%s from %s exported successfully in %s.\nOK!\n" \
			"${exported_bin}" "${container_name}" "${dest_path}"
		return 0
	fi
	# Unknown error.
	return 3
}

# export_application will export input graphical application to the host.
# the following function will scan the distrobox for desktop and icon files for
# the selected application. It will then put the needed icons in the host's icons
# directory and create a new .desktop file that will execute the selected application
# in the distrobox.
#
# Arguments:
#	None
# Expected global variables:
#   CONTAINER_ID: id of the current container
#   container_command_prefix: string to prepend to the command to launch
#   container_name:  string name of this container
#   exported_app:   string name of the app to export
#   exported_app_label: string label to use to mark the exported app
#   exported_delete:  bool if we want to delete or not
#   extra_flags:  string extra flags to append to the exported app command
#   host_home: home path ofr the host, where to search desktop files
# Expected env variables:
#   None
# Outputs:
#	needed icons in /run/host/$host_home/.local/share/icons
#	needed desktop files in /run/host/$host_home/.local/share/applications
#	or error code.
export_application()
{
	canon_dirs=""

	# In case we're explicitly going for a full desktopfile path
	if [ -e "${exported_app}" ]; then
		desktop_files="${exported_app}"
	else
		IFS=":"
		if [ -n "${XDG_DATA_DIRS}" ]; then
			for xdg_data_dir in ${XDG_DATA_DIRS}; do
				[ -d "${xdg_data_dir}/applications" ] && canon_dirs="${canon_dirs} ${xdg_data_dir}/applications"
			done
		else
			[ -d /usr/share/applications ] && canon_dirs="/usr/share/applications"
			[ -d /usr/local/share/applications ] && canon_dirs="${canon_dirs} /usr/local/share/applic
Download .txt
gitextract_321fe374/

├── .gitattributes
├── .github/
│   ├── ISSUE_TEMPLATE/
│   │   ├── bug_report.md
│   │   ├── discussion-issue-template.md
│   │   └── feature_request.md
│   ├── dependabot.yml
│   └── workflows/
│       ├── compatibility.yml
│       ├── main.yml
│       └── manpages.yml
├── .gitignore
├── .markdownlint.yaml
├── .shellcheckrc
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── COPYING.md
├── Containerfile
├── completions/
│   ├── bash/
│   │   ├── distrobox
│   │   ├── distrobox-assemble
│   │   ├── distrobox-create
│   │   ├── distrobox-enter
│   │   ├── distrobox-ephemeral
│   │   ├── distrobox-generate-entry
│   │   ├── distrobox-list
│   │   ├── distrobox-rm
│   │   ├── distrobox-stop
│   │   └── distrobox-upgrade
│   └── zsh/
│       ├── _distrobox
│       ├── _distrobox-assemble
│       ├── _distrobox-create
│       ├── _distrobox-enter
│       ├── _distrobox-ephemeral
│       ├── _distrobox-export
│       ├── _distrobox-generate-entry
│       ├── _distrobox-host-exec
│       ├── _distrobox-init
│       ├── _distrobox-list
│       ├── _distrobox-rm
│       ├── _distrobox-stop
│       ├── _distrobox-upgrade
│       ├── _distrobox_containers
│       ├── _distrobox_images
│       └── _distrobox_running_containers
├── distrobox
├── distrobox-assemble
├── distrobox-create
├── distrobox-enter
├── distrobox-ephemeral
├── distrobox-export
├── distrobox-generate-entry
├── distrobox-host-exec
├── distrobox-init
├── distrobox-list
├── distrobox-rm
├── distrobox-stop
├── distrobox-upgrade
├── docs/
│   ├── 404.md
│   ├── CNAME
│   ├── Gemfile
│   ├── README.md
│   ├── _config.yml
│   ├── _includes/
│   │   ├── footer.html
│   │   ├── head.html
│   │   └── header.html
│   ├── _layouts/
│   │   └── default.html
│   ├── assets/
│   │   ├── brand/
│   │   │   └── png/
│   │   │       └── distros/
│   │   │           └── base-distrobox-1.xcf
│   │   └── credits.md
│   ├── compatibility.md
│   ├── featured_articles.md
│   ├── posts/
│   │   ├── distrobox_custom.md
│   │   ├── execute_commands_on_host.md
│   │   ├── install_lilipod_static.md
│   │   ├── install_podman_static.md
│   │   ├── integrate_vscode_distrobox.md
│   │   ├── posts.md
│   │   ├── run_latest_gnome_kde_hyprland_on_distrobox.md
│   │   ├── run_libvirt_in_distrobox.md
│   │   └── steamdeck_guide.md
│   ├── style.css
│   ├── usage/
│   │   ├── distrobox-assemble.md
│   │   ├── distrobox-create.md
│   │   ├── distrobox-enter.md
│   │   ├── distrobox-ephemeral.md
│   │   ├── distrobox-export.md
│   │   ├── distrobox-generate-entry.md
│   │   ├── distrobox-host-exec.md
│   │   ├── distrobox-init.md
│   │   ├── distrobox-list.md
│   │   ├── distrobox-rm.md
│   │   ├── distrobox-stop.md
│   │   ├── distrobox-upgrade.md
│   │   └── usage.md
│   └── useful_tips.md
├── extras/
│   ├── distrobox-example-manifest.ini
│   ├── docker-host
│   ├── install-podman
│   ├── podman-host
│   └── vscode-distrobox
├── install
├── man/
│   ├── gen-man
│   └── man1/
│       ├── distrobox-assemble.1
│       ├── distrobox-compatibility.1
│       ├── distrobox-create.1
│       ├── distrobox-enter.1
│       ├── distrobox-ephemeral.1
│       ├── distrobox-export.1
│       ├── distrobox-generate-entry.1
│       ├── distrobox-host-exec.1
│       ├── distrobox-init.1
│       ├── distrobox-list.1
│       ├── distrobox-rm.1
│       ├── distrobox-stop.1
│       ├── distrobox-upgrade.1
│       └── distrobox.1
└── uninstall
Condensed preview — 113 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (692K chars).
[
  {
    "path": ".gitattributes",
    "chars": 19,
    "preview": "* text=auto eol=lf\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.md",
    "chars": 996,
    "preview": "---\nname: Bug report\nabout: Create a report to help us improve\ntitle: \"[Error]\"\nlabels: bug\nassignees: ''\n\n---\n\nPlease, "
  },
  {
    "path": ".github/ISSUE_TEMPLATE/discussion-issue-template.md",
    "chars": 619,
    "preview": "---\nname: Discussion issue template\nabout: Describe this issue template's purpose here.\ntitle: \"[Discussion]\"\nlabels: qu"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature_request.md",
    "chars": 616,
    "preview": "---\nname: Feature request\nabout: Suggest an idea for this project\ntitle: \"[Suggestion]\"\nlabels: enhancement\nassignees: '"
  },
  {
    "path": ".github/dependabot.yml",
    "chars": 119,
    "preview": "version: 2\nupdates:\n  - package-ecosystem: \"github-actions\"\n    directory: \"/\"\n    schedule:\n      interval: \"monthly\"\n"
  },
  {
    "path": ".github/workflows/compatibility.yml",
    "chars": 8185,
    "preview": "---\n# This is a basic workflow to help you get started with Actions\n\nname: CI\n\n# Controls when the workflow will run\non:"
  },
  {
    "path": ".github/workflows/main.yml",
    "chars": 4621,
    "preview": "# This is a basic workflow to help you get started with Actions\n\nname: Lint\n\n# Controls when the workflow will run\non:\n "
  },
  {
    "path": ".github/workflows/manpages.yml",
    "chars": 2274,
    "preview": "---\n# This is a basic workflow to help you get started with Actions\n\nname: Docs\n\n# Controls when the workflow will run\no"
  },
  {
    "path": ".gitignore",
    "chars": 20,
    "preview": "tags\n*.vim\nTODO.txt\n"
  },
  {
    "path": ".markdownlint.yaml",
    "chars": 167,
    "preview": "---\nMD013:\n  line_length: 120\n  code_blocks: false\n  tables: false\n  headings: false\n  headers: false\nMD025: false\nMD033"
  },
  {
    "path": ".shellcheckrc",
    "chars": 1041,
    "preview": "# Overrides the shell detected from the shebang. This is useful for files meant to be included (and thus lacking a sheba"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "chars": 247,
    "preview": "# Code of Conduct\n\nLet's just all be nice to each other and ourselves. Do we really need this?\n\nJust follow them:\n\n![who"
  },
  {
    "path": "CONTRIBUTING.md",
    "chars": 5362,
    "preview": "# Contributing to Distrobox\n\nWe greatly appreciate your input! We want to make contributing to this project\nas easy and "
  },
  {
    "path": "COPYING.md",
    "chars": 34865,
    "preview": "# GNU GENERAL PUBLIC LICENSE\n\nVersion 3, 29 June 2007\n\nCopyright (C) 2007 Free Software Foundation, Inc.\n<https://fsf.or"
  },
  {
    "path": "Containerfile",
    "chars": 70,
    "preview": "FROM  docker.io/mgoltzsche/podman:latest\n\nCOPY ./distrobox* /usr/bin/\n"
  },
  {
    "path": "completions/bash/distrobox",
    "chars": 1947,
    "preview": "# shellcheck disable=all\n\n_generate_from_help() {\n\tcommand=$1\n\t# if command does not exist, try with `distrobox subcomma"
  },
  {
    "path": "completions/bash/distrobox-assemble",
    "chars": 489,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-create",
    "chars": 487,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-enter",
    "chars": 486,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-ephemeral",
    "chars": 490,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-generate-entry",
    "chars": 495,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-list",
    "chars": 485,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-rm",
    "chars": 483,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-stop",
    "chars": 485,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/bash/distrobox-upgrade",
    "chars": 488,
    "preview": "# shellcheck disable=all\n\nif [ -e /usr/share/bash-completion/completions/distrobox ]; then\n\tsource /usr/share/bash-compl"
  },
  {
    "path": "completions/zsh/_distrobox",
    "chars": 1374,
    "preview": "#compdef distrobox\n\nlocal curcontext=\"$curcontext\" state line\ntypeset -A opt_args\n\n_distrobox_commands() {\n  local -a co"
  },
  {
    "path": "completions/zsh/_distrobox-assemble",
    "chars": 603,
    "preview": "#compdef distrobox-assemble\n\n_message -r \"Create or remove containers in batches, based on a manifest file.\"\n_arguments "
  },
  {
    "path": "completions/zsh/_distrobox-create",
    "chars": 2728,
    "preview": "#compdef distrobox-create\n\n_distrobox-create() {\n    local -a options\n    local expl\n    local state\n    # Array of opta"
  },
  {
    "path": "completions/zsh/_distrobox-enter",
    "chars": 1340,
    "preview": "#compdef distrobox-enter\n\n_distrobox-enter() {\n    local -a options\n    local expl\n    local state\n    local _db_cc\n\n   "
  },
  {
    "path": "completions/zsh/_distrobox-ephemeral",
    "chars": 2767,
    "preview": "#compdef distrobox-ephemeral\n\n_distrobox-ephemeral() {\n    local -a options\n    local expl\n    local state\n    # Array o"
  },
  {
    "path": "completions/zsh/_distrobox-export",
    "chars": 1182,
    "preview": "#compdef distrobox-export\n\n_message -r \"Export an app or a binary from the container to the host.\"\n_arguments \\\n  '(--ap"
  },
  {
    "path": "completions/zsh/_distrobox-generate-entry",
    "chars": 1397,
    "preview": "#compdef distrobox-generate-entry\n\n_distrobox-generate-entry() {\n    # expl is an internal zsh array that gets passed to"
  },
  {
    "path": "completions/zsh/_distrobox-host-exec",
    "chars": 420,
    "preview": "#compdef distrobox-host-exec\n\n_message -r \"Execute a command on the host while in a container\"\n_arguments \\\n  '*:command"
  },
  {
    "path": "completions/zsh/_distrobox-init",
    "chars": 914,
    "preview": "#compdef distrobox-init\n\n_message -r \"Init the distrobox (not to be launched manually)\"\n_arguments \\\n  '(--name -n)'{-n,"
  },
  {
    "path": "completions/zsh/_distrobox-list",
    "chars": 362,
    "preview": "#compdef distrobox-list\n\n_message -r \"List available distroboxes.\"\n_arguments \\\n  '(--help -h)'{-h,--help}'[show this me"
  },
  {
    "path": "completions/zsh/_distrobox-rm",
    "chars": 1352,
    "preview": "#compdef distrobox-rm\n\n_distrobox-rm() {\n    # expl is an internal zsh array that gets passed to _arguments bts\n    loca"
  },
  {
    "path": "completions/zsh/_distrobox-stop",
    "chars": 968,
    "preview": "#compdef distrobox-stop\n\n_distrobox-stop() {\n    local expl\n    local -a options\n    local _db_rcc\n\n     _db_rcc=($(dist"
  },
  {
    "path": "completions/zsh/_distrobox-upgrade",
    "chars": 910,
    "preview": "#compdef distrobox-upgrade\n\n_distrobox-upgrade() {\n    local expl\n    local -a options\n    local _db_cc\n\n    _db_cc=(\"${"
  },
  {
    "path": "completions/zsh/_distrobox_containers",
    "chars": 235,
    "preview": "#autoload\n\n_distrobox_containers() {\n    local -a containers\n    containers=(\"${(@f)$(distrobox list | sed 1d | awk -F'|"
  },
  {
    "path": "completions/zsh/_distrobox_images",
    "chars": 196,
    "preview": "#autoload\n\n_distrobox_images() {\n    local -a images\n    images=(\"${(@f)$(distrobox-create --compatibility | awk 'NF {pr"
  },
  {
    "path": "completions/zsh/_distrobox_running_containers",
    "chars": 291,
    "preview": "#autoload\n\n_distrobox_running_containers() {\n    local -a containers\n    containers=($(distrobox list | awk -F'|' '$3 ~ "
  },
  {
    "path": "distrobox",
    "chars": 2271,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-assemble",
    "chars": 22237,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-create",
    "chars": 39477,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-enter",
    "chars": 26095,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-ephemeral",
    "chars": 8055,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-export",
    "chars": 22370,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-generate-entry",
    "chars": 13404,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-host-exec",
    "chars": 6816,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-init",
    "chars": 88510,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-list",
    "chars": 8716,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-rm",
    "chars": 14350,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-stop",
    "chars": 9359,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "distrobox-upgrade",
    "chars": 8494,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project:\n#    https://github.co"
  },
  {
    "path": "docs/404.md",
    "chars": 357,
    "preview": "---\nlayout: default\npermalink: /404.html\n---\n\n![404]({{site.baseurl}}/assets/404.png){:.full.pixels}\n\n# Document Not Fou"
  },
  {
    "path": "docs/CNAME",
    "chars": 13,
    "preview": "distrobox.it\n"
  },
  {
    "path": "docs/Gemfile",
    "chars": 1128,
    "preview": "source \"https://rubygems.org\"\n\n# Hello! This is where you manage which Jekyll version is used to run.\n# When you want to"
  },
  {
    "path": "docs/README.md",
    "chars": 23098,
    "preview": "<img src=\"assets/splash.svg\" style=\"border-radius:12px\">\n\n# Distrobox\n\n<sub>previous logo credits [j4ckr3d](https://gith"
  },
  {
    "path": "docs/_config.yml",
    "chars": 703,
    "preview": "# Site settings\ntitle: Distrobox\nbaseurl: \"/\" # the subpath of your site, e.g. /blog/\n                                 #"
  },
  {
    "path": "docs/_includes/footer.html",
    "chars": 151,
    "preview": "<footer class=\"site-footer\">\n  <p>&copy; {{ site.title }}, 2021 &ndash; 2024</p>\n\n  <p><a href=\"{{ site.sourceurl }}\">We"
  },
  {
    "path": "docs/_includes/head.html",
    "chars": 1535,
    "preview": "<head>\n  <meta charset=\"utf-8\">\n  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n  <meta name=\"viewport\" content="
  },
  {
    "path": "docs/_includes/header.html",
    "chars": 125,
    "preview": "<header class=\"site-header\">\n  <h1 id=\"logo\"><a href=\"{{ \"/\" | prepend: site.baseurl }}\">{{ site.title }}</a></h1>\n</hea"
  },
  {
    "path": "docs/_layouts/default.html",
    "chars": 202,
    "preview": "<!DOCTYPE html>\n<html>\n\n  {% include head.html %}\n\n  <body>\n\n    {% include header.html %}\n\n    <div class=\"container\">\n"
  },
  {
    "path": "docs/assets/credits.md",
    "chars": 1677,
    "preview": "# Assets credits\n\n## Logo\n\nPrevious logo was created by [j4ckr3d](https://github.com/j4ckr3d)\n\nCurrent logo was created "
  },
  {
    "path": "docs/compatibility.md",
    "chars": 17777,
    "preview": "- [Distrobox](README.md)\n  - [Compatibility](#compatibility)\n    - [Supported container managers](#supported-container-m"
  },
  {
    "path": "docs/featured_articles.md",
    "chars": 2989,
    "preview": "- [Distrobox](README.md)\n  - [Featured articles](#articles)\n  - [Talks](#talks)\n  - [Podcasts](#podcasts)\n\n---\n\n## Artic"
  },
  {
    "path": "docs/posts/distrobox_custom.md",
    "chars": 1663,
    "preview": "- [Distrobox](README.md)\n\n---\n\n# Create a dedicated distrobox container\n\nDistrobox wants to be as generic as possible in"
  },
  {
    "path": "docs/posts/execute_commands_on_host.md",
    "chars": 3594,
    "preview": "- [Distrobox](../README.md)\n  - [Execute a command on the host](#execute-a-command-on-the-host)\n    - [With distrobox-ho"
  },
  {
    "path": "docs/posts/install_lilipod_static.md",
    "chars": 1292,
    "preview": "# Install Lilipod in a static manner\n\nIf on your distribution (eg. SteamOS) can be difficult to install something and ke"
  },
  {
    "path": "docs/posts/install_podman_static.md",
    "chars": 1098,
    "preview": "# Install Podman in a static manner\n\nIf on your distribution (eg. SteamOS) can be difficult to install something and kee"
  },
  {
    "path": "docs/posts/integrate_vscode_distrobox.md",
    "chars": 6432,
    "preview": "- [Distrobox](../README.md)\n  - [Integrate VSCode and Distrobox](#integrate-vscode-and-distrobox)\n    - [From distrobox]"
  },
  {
    "path": "docs/posts/posts.md",
    "chars": 484,
    "preview": "- [Distrobox](../README.md)\n\n---\n\n## Latest posts\n\n- [Execute a command on the Host](execute_commands_on_host.md)\n- [Ins"
  },
  {
    "path": "docs/posts/run_latest_gnome_kde_hyprland_on_distrobox.md",
    "chars": 7240,
    "preview": "- [Distrobox](../README.md)\n  - [Run latest GNOME, KDE Plasma, and Hyprland using distrobox](run_latest_gnome_kde_on_dis"
  },
  {
    "path": "docs/posts/run_libvirt_in_distrobox.md",
    "chars": 3605,
    "preview": "- [Distrobox](../README.md)\n  - [Run Libvirt using distrobox](run_libvirt_in_distrobox.md)\n    - [Prepare the container]"
  },
  {
    "path": "docs/posts/steamdeck_guide.md",
    "chars": 2809,
    "preview": "### Install Distrobox and Podman PERMANENT on Steam Deck >= 3.5\n\n**1 - Modify $PATH for binaries**\nFirst, verify if ~/.b"
  },
  {
    "path": "docs/style.css",
    "chars": 8452,
    "preview": "/* \n\n    OS Component Website\n    ====================\n    \n    shamelessly stolen CSS from systemd\n    https://github.c"
  },
  {
    "path": "docs/usage/distrobox-assemble.md",
    "chars": 9010,
    "preview": "<!-- markdownlint-disable MD010 MD036 MD046 -->\n# NAME\n\n\tdistrobox assemble\n\tdistrobox-assemble\n\n# DESCRIPTION\n\ndistrobo"
  },
  {
    "path": "docs/usage/distrobox-create.md",
    "chars": 11126,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox create\n\tdistrobox-create\n\n# DESCRIPTION\n\ndistrobox-create t"
  },
  {
    "path": "docs/usage/distrobox-enter.md",
    "chars": 3317,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox enter\n\tdistrobox-enter\n\n# DESCRIPTION\n\ndistrobox-enter take"
  },
  {
    "path": "docs/usage/distrobox-ephemeral.md",
    "chars": 1276,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox ephemeral\n\tdistrobox-ephemeral\n\n# DESCRIPTION\n\ndistrobox-ep"
  },
  {
    "path": "docs/usage/distrobox-export.md",
    "chars": 3876,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n\n# NAME\n\n\tdistrobox-export\n\n# DESCRIPTION\n\n**Application and binary exporting*"
  },
  {
    "path": "docs/usage/distrobox-generate-entry.md",
    "chars": 908,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox generate-entry\n\n# DESCRIPTION\n\ndistrobox-generate-entry wil"
  },
  {
    "path": "docs/usage/distrobox-host-exec.md",
    "chars": 2091,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox-host-exec\n\n# DESCRIPTION\n\ndistrobox-host-exec lets one exec"
  },
  {
    "path": "docs/usage/distrobox-init.md",
    "chars": 1276,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox-init\n\n# DESCRIPTION\n\n**Init the distrobox (not to be launch"
  },
  {
    "path": "docs/usage/distrobox-list.md",
    "chars": 1036,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox list\n\tdistrobox-list\n\n# DESCRIPTION\n\ndistrobox-list lists a"
  },
  {
    "path": "docs/usage/distrobox-rm.md",
    "chars": 1063,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox rm\n\tdistrobox-rm\n\n# DESCRIPTION\n\ndistrobox-rm delete one of"
  },
  {
    "path": "docs/usage/distrobox-stop.md",
    "chars": 1194,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox stop\n\tdistrobox-stop\n\n# DESCRIPTION\n\ndistrobox-stop stop a "
  },
  {
    "path": "docs/usage/distrobox-upgrade.md",
    "chars": 1691,
    "preview": "<!-- markdownlint-disable MD010 MD036 -->\n# NAME\n\n\tdistrobox-upgrade\n\n# DESCRIPTION\n\ndistrobox-upgrade will enter the sp"
  },
  {
    "path": "docs/usage/usage.md",
    "chars": 736,
    "preview": "<!-- markdownlint-disable MD010 MD051 -->\n- [Distrobox](../README.md)\n  - [Outside the distrobox](#outside-the-distrobox"
  },
  {
    "path": "docs/useful_tips.md",
    "chars": 33641,
    "preview": "- [Distrobox](README.md)\n  - [Launch a distrobox from your applications list](#launch-a-distrobox-from-your-applications"
  },
  {
    "path": "extras/distrobox-example-manifest.ini",
    "chars": 3468,
    "preview": "# This is an example assemble file to show how options are laid out\n# You generally have a section header, followed by o"
  },
  {
    "path": "extras/docker-host",
    "chars": 2058,
    "preview": "#!/bin/sh\n\nid=\"$(echo \"$@\" | grep -Eo ' [a-zA-Z0-9]{64} ' | tr -d ' ')\"\nDOCKER_COMMAND=\"$(command -v docker 2> /dev/null"
  },
  {
    "path": "extras/install-podman",
    "chars": 1106,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project: https://github.com/89l"
  },
  {
    "path": "extras/podman-host",
    "chars": 2058,
    "preview": "#!/bin/sh\n\nid=\"$(echo \"$@\" | grep -Eo ' [a-zA-Z0-9]{64} ' | tr -d ' ')\"\nPODMAN_COMMAND=\"$(command -v podman 2> /dev/null"
  },
  {
    "path": "extras/vscode-distrobox",
    "chars": 429,
    "preview": "#!/bin/sh\n\ncontainer_name=\"$(printf '{\"containerName\":\"%s\"}' \"$1\" | od -A n -t x1 | tr -d \"\\n\\t \")\"\n\nif command -v code "
  },
  {
    "path": "install",
    "chars": 7713,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project: https://github.com/89l"
  },
  {
    "path": "man/gen-man",
    "chars": 1824,
    "preview": "#!/bin/sh\n\nif ! command -v pandoc; then\n\techo '\nPlease install \"pandoc\". This tool is needed to convert markdown to man "
  },
  {
    "path": "man/man1/distrobox-assemble.1",
    "chars": 10045,
    "preview": "'\\\" t\n.\\\n.\\\"\n.TH \"DISTROBOX\\-ASSEMBLE\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox assemble\ndist"
  },
  {
    "path": "man/man1/distrobox-compatibility.1",
    "chars": 10857,
    "preview": "'\\\" t\n.\\\n.\\\"\n.TH \"DISTROBOX\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH COMPATIBILITY\nThis project \\f[B]does not need "
  },
  {
    "path": "man/man1/distrobox-create.1",
    "chars": 12616,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-CREATE\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox create\ndistrobox\\-cre"
  },
  {
    "path": "man/man1/distrobox-enter.1",
    "chars": 3823,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-ENTER\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox enter\ndistrobox\\-enter"
  },
  {
    "path": "man/man1/distrobox-ephemeral.1",
    "chars": 1459,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-EPHEMERAL\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox ephemeral\ndistrobo"
  },
  {
    "path": "man/man1/distrobox-export.1",
    "chars": 4532,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-EXPORT\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox\\-export\n.EE\n.SH DESCR"
  },
  {
    "path": "man/man1/distrobox-generate-entry.1",
    "chars": 1091,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-GENERATE\\-ENTRY\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox generate\\-en"
  },
  {
    "path": "man/man1/distrobox-host-exec.1",
    "chars": 2329,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-HOST\\-EXEC\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox\\-host\\-exec\n.EE\n."
  },
  {
    "path": "man/man1/distrobox-init.1",
    "chars": 1468,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-INIT\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox\\-init\n.EE\n.SH DESCRIPTI"
  },
  {
    "path": "man/man1/distrobox-list.1",
    "chars": 1234,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-LIST\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox list\ndistrobox\\-list\n.E"
  },
  {
    "path": "man/man1/distrobox-rm.1",
    "chars": 1255,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-RM\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox rm\ndistrobox\\-rm\n.EE\n.SH "
  },
  {
    "path": "man/man1/distrobox-stop.1",
    "chars": 1384,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-STOP\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox stop\ndistrobox\\-stop\n.E"
  },
  {
    "path": "man/man1/distrobox-upgrade.1",
    "chars": 1960,
    "preview": ".\\\n.\\\"\n.TH \"DISTROBOX\\-UPGRADE\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox\\-upgrade\n.EE\n.SH DES"
  },
  {
    "path": "man/man1/distrobox.1",
    "chars": 54053,
    "preview": "'\\\" t\n.\\\n.\\\"\n.TH \"DISTROBOX\\-ASSEMBLE\" \"1\" \"Mar 2026\" \"Distrobox\" \"User Manual\"\n.SH NAME\n.IP\n.EX\ndistrobox assemble\ndist"
  },
  {
    "path": "uninstall",
    "chars": 3329,
    "preview": "#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-only\n#\n# This file is part of the distrobox project: https://github.com/89l"
  }
]

// ... and 1 more files (download for full content)

About this extraction

This page contains the full source code of the 89luca89/distrobox GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 113 files (626.5 KB), approximately 186.8k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!