#!/bin/sh
# Copyright 2011  Patrick J. Volkerding, Sebeka, MN, USA
# Licensed under the GPLv2 or later (your choice).

# This is a silly little script to display already
# processed (and bzip2ed) man pages.  And yes, after lots of
# testing bzip2 beats all for text so we don't support anything
# else.

if [ -z $1 ]; then
  echo "Usage:  man [section] name"
fi

unset USE_SECTION FOUNDPAGE
if [ ! -z $2 ]; then
  USE_SECTION=$1
  shift
  if [ ! -r /usr/man/man${USE_SECTION}/${1}.${USE_SECTION}.bz2 ]; then
    FOUNDPAGE=/usr/man/man${USE_SECTION}/${1}.${USE_SECTION}.bz2
  else
    echo "No entry for $1 in section $USE_SECTION of the manual"
    exit 1
  fi
fi

if [ -z $FOUNDPAGE ]; then
  for USE_SECTION in 1 8 2 3 4 5 6 7 9 ; do
    if [ -r /usr/man/man${USE_SECTION}/${1}.${USE_SECTION}.bz2 ]; then
      FOUNDPAGE=/usr/man/man${USE_SECTION}/${1}.${USE_SECTION}.bz2
      break
    fi
  done
fi

if [ ! -z $FOUNDPAGE ]; then
  bunzip2 -c $FOUNDPAGE | less
else
  echo "No manual entry for $1"
fi

