#!/bin/sh -e
#
# ex - archive extractor
# adapted and improved from the commonly circulating version
# detects whether unpacking into a subfolder is sensible
# and shows progress indications for some operations
# optdepends: rewrite(part of my dotfiles, for unzip line rewriting) 7z p7zip unzip
# TODO(2025-09-11): auto-delete archive, auto-extract in current dir, trim .tar.gz fully

if test "$#" -eq 0; then
  file -- * | grep -e '(*compress*\|*archive*)' | cut -d: -f1 | xargs ex
  exit $?
fi

for arg; do
  # shellcheck disable=SC2003
  case ${arg} in
    -d)
      del=$(expr "${del:-0}" + 1)
      ;;
    -*)
      param="${arg}"
      ;;
    *)
      if test -r "${arg}"; then
        fullpath="$(realpath "${arg}")"
        name="$(basename "${fullpath%.*}")"
        ls --color -dlhF "${name}" >&2 2>/dev/null \
          && echo "Target directory exists - archive already extracted?" >&2 \
          && continue
        namepart="${name}.part"
        (
          # create temporary subfolder if current folder contains stuff
          # shellcheck disable=SC2012,SC2312
          if test "$(ls -U | wc -l)" -gt 2; then
            mkdir -p "${namepart}"
            cd "${namepart}"
          fi
          echo "Extracting ${arg} into ${PWD}"
          case "${arg}" in
            *.tar.* | *.tar)
              tar --extract --file "${fullpath}"
              ;;
            *.tbz2)
              tar xjf "${fullpath}"
              ;;
            *.tgz)
              tar xzf "${fullpath}"
              ;;
            *.7z)
              7z"$(command -v 7z >/dev/null || echo r)" x "${param}" "${fullpath}"
              ;;
            *.z01 | *.zip | *.jar)
              if command -v 7z >/dev/null; then
                7z x "${param}" "${fullpath}"
              else
                unzip "${fullpath}" | rewrite
              fi
              ;;
            *.gz)
              gunzip "${fullpath}"
              ;;
            *.bz2)
              bunzip2 "${fullpath}"
              ;;
            *.rar)
              unrar x "${fullpath}"
              ;;
            *.deb)
              ar x "${fullpath}"
              ;;
            *.zst)
              unzstd "${fullpath}"
              ;;
            *.Z)
              uncompress "${fullpath}"
              ;;
            *)
              command -v 7z >/dev/null \
                && 7z x "${param}" "${fullpath}" \
                || echo "'${arg}' cannot be extracted by ex" >&2
              ;;
          esac
          # if we created a temporary subfolder, check if it can be eliminated
          if test "$(basename "${PWD}")" = "${namepart}"; then
            # shellcheck disable=SC2012,SC2312
            if test $# -lt 2 -a "$(ls -U | wc -l)" -lt 3; then
              test "$(ls)" && mv -iv ./* ..
              cd .. && rm -d "${namepart}"
            else
              cd .. && mv -ivT "${namepart}" "${name}"
            fi
          fi
          # shellcheck disable=SC2249
          case ${del} in
            2) rm "${arg}" ;;
            1) mv -v "${arg}" "/tmp" ;;
          esac
        )
      else
        echo "'$1' is not a readable file"
      fi
      ;;
  esac
done
