#!/usr/bin/env sh

## Quit if anything goes wrong
set -e

## Location to use: /usr/share/example (default to /)
location="${1-/}"
## Package name: example
package="$2"
## Depth: 0-9 (default to 1)
depth="${3:-1}"
## List only folders: true/false (default to true)
folders="${4:-true}"

## Package description ~ $ awk -F ': ' '/^Description: / {print $2}' ./example/debian/control
[ -n "${package}" ] \
  && description="$(dpkg-query -f'${binary:Synopsis}\n' -W "${package}" 2>/dev/null)"

## Feedback - banner
[ -n "${description}" ] \
  && printf '%b' "\n> \033[1;4m${package}\e[0m ~ \033[3m${description}\e[0m\n\n"

## Move to location
cd "${location}"

## List output
if command -v tree >/dev/null 2>&1; then
  ## Check to see if folders only
  [ "${folders}" = "true" ] \
    && folder="-d" \
    || folder=""

  # Add colors for tree
  if [ -x /usr/bin/dircolors ]; then
    if test -r ~/.dircolors; then
      # shellcheck disable=SC2312
      eval "$(dircolors -b ~/.dircolors)"
    else
      # shellcheck disable=SC2312
      eval "$(dircolors -b)"
    fi
  fi

  ## Using tree - display output
  tree "${folder}" -L "${depth}" --noreport "${location}" -I __pycache__
else
  ## Feedback - location
  echo "${location}"

  ## Check to see if folders only
  [ "${folders}" = "true" ] \
    && folder="-type d" \
    || folder=""

  ## Using find/sed - display output
  find "${location}" -maxdepth "${depth}" -mindepth 1 "${folder}" \
    | sort \
    | sed -e 's/[^-][^\/]*\//-/g; s/^/  /; s/-/|/' \
    | grep -v __pycache__
fi

## Check to see if last location is where we want to end up
## If its not, spawn a new shell (sub shell), so to "move" with "cd"
## This isn't ideal, but best option for the time being (rather keep everything in parent shell)
if [ "${OLDPWD}" != "${location}" ]; then
  ## From kali-menu: exec-in-shell
  USER=${USER:-$(whoami)}
  SHELL=${SHELL:-$(getent passwd "${LOGNAME:-${USER}}" | cut -d: -f7)}
  ${SHELL:-bash} -i
fi
