#!/usr/bin/env sh

# This is based from the `mansplain` script by Luke Smith. 
# Video reference is at https://www.youtube.com/watch?v=8E8sUNHdzG8.
# There's not much room for customizability so you'll have to edit the script itself if you want something different. 

# Minimum requirements as of writing this script at 2019-12-18:
# * man - v2.9.0
# * xargs - v4.7.0
# * rofi - v1.5.4
# * awk - v5.0.1

# Optional dependencies:
# * zathura - v0.4.4
# * girara - v0.3.3
# * pdf-mupdf - v0.3.5

help_section="Provides an interface for viewing all available manual pages. 

Usage:
  $0 [-h, --help] [--viewpdf] [--viewer <PROGRAM>]

Options:
  -h, --help            - View the help section.
  --viewpdf             - View the manual as a PDF. 
"

VIEW_PDF=0;

while [[ $# -gt 0 ]]
do
    case $1 in 
        -h|--help)
            echo "$help_section"
            exit 0;;
        --viewpdf)
            VIEW_PDF=1
            shift;;
        *)
            shift;;
    esac
done

COMMAND_STRING="man -k . | rofi -dmenu -p | awk '{print \$1}' | xargs -r man"

if [[ $VIEW_PDF -eq 1 ]]; then
    COMMAND_STRING+=" -Tpdf | zathura - "
fi

eval "$COMMAND_STRING"

exit $?
