#!/bin/sh

init_variables() {
  progname=${0##*/}

  ZSH_VERSION=$(zsh --version | cut -d ' ' -f 2)
  ZSH_HELPDIR="/usr/share/zsh/${ZSH_VERSION}/help"
  MAN_PATH="${MANPATH:-/usr/share/man}"

  topic=
  section=
}

parse_command_line() {
  topic="$1"
  section="$2"
}

validate_command_line() {
  if [ -z "${topic}" ]; then
    exit_error 'missing topic argument'
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${progname}: $1"
  exit 1
} >&2

select_section() {
  sections=

  # zsh section
  if [ -f "${ZSH_HELPDIR}/${topic}" ]; then
    sections="${sections}:zsh"
  fi

  # man sections
  sections="${sections}:$(
    echo "${MAN_PATH}" \
      | tr ':' '\n' \
      | xargs -I{} find -L {} -type f -name "${topic}.[0-9]*" \
      | sed -e '/.*\.gz$/s/.*\.\([^.]*\).gz/\1/' \
        -e '/.*\.[0-9][^.]*$/s/.*\.\([^.]*\)$/\1/' \
      | sort -u \
      | tr '\n' ':'
  )"

  # don't show menu if there is not at least 2 sections
  # shellcheck disable=SC2312
  if [ "$(echo "${sections}" | tr ':' '\n' | grep -c '.')" -lt 2 ]; then
    return 0
  fi

  # manual menu building
  # order of displayed sections is the order of documentation lookup
  # for example: zsh / 1 / 1p / 3
  echo 'Available documentation sections:'
  echo "${sections}" \
    | tr ':' '\n' \
    | grep '.' \
    | while read -r ITEM; do
      echo "${ITEM})"
      section_name "${ITEM}"
    done \
    | pr -l1 -s -2 \
    | expand -t 8 \
    | sort -n
  printf 'Select section (empty for default): '

  read -r section
}

section_name() {
  # shellcheck disable=SC2249
  case "$1" in
    zsh) echo 'ZSH help' ;;
    0p) echo 'Header files (POSIX)' ;;
    0*) echo 'Header files' ;;
    1p) echo 'General commands (POSIX)' ;;
    1*) echo 'General commands' ;;
    2*) echo 'System functions' ;;
    3*) echo 'Library functions' ;;
    4*) echo 'Special files' ;;
    5*) echo 'File formats and conventions' ;;
    6*) echo 'Games' ;;
    7*) echo 'Miscellaneous' ;;
    8*) echo 'System administration commands' ;;
    9*) echo 'Kernel routines' ;;
  esac
}

search_documentation() {
  if [ -z "${section}" ] || [ "${section}" = "zsh" ]; then
    if [ -f "${ZSH_HELPDIR}/${topic}" ]; then
      "${PAGER:-less}" "${ZSH_HELPDIR}/${topic}"
      return 0
    fi
  fi

  if [ -z "${section}" ]; then
    # shellcheck disable=SC2312
    if [ "$(echo "${MAN_PATH}" | tr ':' '\n' \
      | xargs -I{} find -L {} -type f -name "${topic}.[0-9]*" \
      | wc -l)" -ge 1 ]; then
      man "${topic}"
      return 0
    fi

    echo "no documentation available for '${topic}'"

  else
    # shellcheck disable=SC2312
    if [ "$(echo "${MAN_PATH}" | tr ':' '\n' \
      | xargs -I{} find -L {} -type f \
        -name "${topic}.${section}" -o -name "${topic}.${section}.*" \
      | wc -l)" -ge 1 ]; then
      man "${topic}.${section}"
      return 0
    fi

    echo "no documentation available for '${topic}' section '${section}'"
  fi

  return 1
}

init_variables
parse_command_line "$@"
validate_command_line

if [ -z "${section}" ]; then
  select_section
fi

search_documentation
