#!/bin/sh
#Barry Kauler 2006. written for puppy, www.puppylinux.com
#license gpl2 (ref: /usr/share/doc/legal)
#a script to replace the man command.
#w464 updated for PKGS_HOMEPAGES homepages db.
#w482 changed die.net/man/1 url.
# 24aug09 i18n by Shinobar
# 27aug09 page1-8 for JM search
# 28aug09 run *.sh if found
#  2sep09 look up japanese puppy wiki
#  5sep09 read man pages with devx
# 12sep09 remove extentions such as .txt, .html, excute .sh
#v431 fix looking up linux.die.net
# v431JP suppress some locale if no X
# dev431JPbeta2: man [section] name 
#120317 L18L: enable correct Character Encoding: UTF-8
# jlst: groff support

CREDIT="man for puppy linux"
if [ -x /usr/local/bin/man ];then
  CREDIT="${CREDIT}\nIf you like to use original version of man, try '/usr/local/bin/man'.\n"
fi

SECTION=""
NAME="$1"
[ $# -gt 1 ] && SECTION="$1" && NAME="$2"
case "$1" in
  *-h*|'') echo -e "${CREDIT}\nusage: $(basename $0) [section] name" ; exit;;
esac

BBAPPLETS="|addgroup|adduser|adjtimex|ar|arping|ash|awk|basename|bunzip2|busybox|bzcat|cal|cat|chgrp|chmod|chown|chroot|chvt|clear|cmp|cp|cpio|crond|crontab|cut|date|dc|dd|deallocvt|delgroup|deluser|devfsd|df|dirname|dmesg|dos2unix|dpkg|dpkg-deb|du|dumpkmap|dumpleases|echo|egrep|env|expr|false|fbset|fdflush|fdformat|fdisk|fgrep|find|fold|free|freeramdisk|fsck.minix|ftpget|ftpput|getopt|getty|grep|gunzip|gzip|halt|hdparm|head|hexdump|hostid|hostname|httpd|hush|hwclock|id|ifconfig|ifdown|ifup|inetd|init|insmod|install|ip|ipaddr|ipcalc|iplink|iproute|iptunnel|kill|killall|klogd|lash|last|length|linuxrc|ln|loadfont|loadkmap|logger|login|logname|logread|losetup|ls|lsmod|makedevs|md5sum|mesg|mkdir|mkfifo|mkfs.minix|mknod|mkswap|mktemp|modprobe|more|mount|msh|mt|mv|nameif|nc|netstat|nslookup|od|openvt|passwd|patch|pidof|ping|ping6|pipe_progress|pivot_root|poweroff|printf|ps|pwd|rdate|readlink|realpath|reboot|renice|reset|rm|rmdir|rmmod|route|rpm|rpm2cpio|run-parts|rx|sed|seq|setkeycodes|sha1sum|sleep|sort|start-stop-daemon|strings|stty|su|sulogin|swapoff|swapon|sync|sysctl|syslogd|tail|tar|tee|telnet|telnetd|test|tftp|time|top|touch|tr|traceroute|true|tty|udhcpc|udhcpd|umount|uname|uncompress|uniq|unix2dos|unzip|uptime|usleep|uudecode|uuencode|vconfig|vi|vlock|watch|watchdog|wc|wget|which|who|whoami|xargs|yes|zcat|"

# set locale
for lng in C $(echo $LANGUAGE|cut -d':' -f1) $LC_ALL $LANG;do :;done   # ex.    ja_JP.UTF-8
lng1=$(echo $lng|cut -d'.' -f1)      # ex.   ja_JP
lng2=$(echo $lng|cut -d'_' -f1)   # ex.   ja
LANGS="$lng,$lng1,$lng2,C"

# handlers
HTMLVIEWERS="$BROWSER defaulthtmlviewer defaultbrowser dillo seamonkey mozilla firefox opera"
TEXTVIEWERS="defaulttextviewer leafpad nicoedit geany gedit"
MANHANDLERS="groff man2html nroff"

if [ "$DISPLAY" = "" ]; then
 HTMLVIEWERS="w3m lynx"
 TEXTVIEWERS="more less"
 MANHANDLERS="groff nroff"
 echo $lng2 | grep -qE 'ja|ko|th|zh' && export LANG=C && LANGS=C
fi

HTMLVIEWER=""
for PROG in $HTMLVIEWERS;do
  which $PROG >/dev/null && HTMLVIEWER=$PROG && break
done

TEXTVIEWER=""
for PROG in $TEXTVIEWERS;do
  which $PROG >/dev/null && TEXTVIEWER=$PROG && break
done

# look up man page
MANHANDLER=""
for PROG in $MANHANDLERS; do
  which $PROG >/dev/null && MANHANDLER=$PROG && break
done

if [ "$DISPLAY" ]; then
  htmlvieweverprog=$HTMLVIEWER
  if [[ $HTMLVIEWER == default* ]] ; then
    #determine what program is run by defaulthtmlviewer or defaultbrowser
    htmlvieweverprog=$(sed -e '/#.*/d' -e 's/.*exec //' -e 's/^ //g' -e 's/^\t//' -e '/^$/d' -e 's/ .*//' $(which $HTMLVIEWER) | head -1 )
  fi
  echo $HTMLVIEWER
  echo $htmlvieweverprog
  case $htmlvieweverprog in
    palemoon|firefox|seamonkey|opera|google-chrome|qtweb)
      echo "DONTDELETE"
      DONTDELETE=1
      ;;
    "") echo "ERROR reading $HTMLVIEWER" ; exit 1 ;;
  esac
fi

if [ "$MANHANDLER" != "" ]; then
 [ "$MANPATH" = "" ] && MANPATH="/usr/man:/usr/share/man:/usr/local/man:/usr/local/share/man"
 MANDIRS=$(echo $MANPATH| tr ':' ',')
 MANLANGS="$LANGS"
 [ "$MANHANDLER" != "man2html" ] && echo $lng2 | grep -qE 'ja|ko|th|zh' && export MANLANGS=C
 [ "$SECTION" != "" ] && SEC="$SECTION" || SEC='*'
 #[ "$MANDIRS" != "" ] && \
 FNDMAN="$(eval find -L {$MANDIRS}/{{$MANLANGS}/man${SEC},man${SEC}} -maxdepth 1 -iname ${NAME}.[0-9]* -not -type d 2>/dev/null|head -n 1)"
 if [ "$FNDMAN" != "" ]; then
   case $MANHANDLER in
   nroff)
     MORE="less -FRis"
     which more >/dev/null && MORE="more"
     if echo "$FNDMAN" | grep -q '.gz$' ;then
       zcat "$FNDMAN" | nroff -mandoc | $MORE
     else
       nroff -mandoc "$FNDMAN" | $MORE
     fi
     exit
    ;;
   man2html)
     TMPFILE=/tmp/$(basename "$FNDMAN" .gz | tr '.' '_').html
     [ "$DONTDELETE" ] && TMPFILE=/tmp/pman_tmp.html
     if echo "$FNDMAN" | grep -q '.gz$' ;then
       zcat "$FNDMAN" | man2html > $TMPFILE
     else
       man2html "$FNDMAN" > $TMPFILE
     fi
     #120317 L18L: enable correct Character Encoding: UTF-8
     sed 's/<\/HEAD><BODY>/<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8" \/><\/HEAD><BODY>/' $TMPFILE > ${TMPFILE}1
     mv -f ${TMPFILE}1 $TMPFILE 
     $HTMLVIEWER $TMPFILE
     [ "$DONTDELETE" ] && exit
     rm -f $TMPFILE
     exit
     ;;
   groff)
     if [ "$DISPLAY" ] ; then
       TMPFILE=/tmp/$(basename "$FNDMAN" .gz | tr '.' '_').html
       [ "$DONTDELETE" ] && TMPFILE=/tmp/pman_tmp.html
       if echo "$FNDMAN" | grep -q '.gz$' ;then
         zcat "$FNDMAN" | groff -mandoc -Thtml > $TMPFILE
       else
         groff -mandoc -Thtml "$FNDMAN" > $TMPFILE
       fi
       #120317 L18L: enable correct Character Encoding: UTF-8
       sed 's/<\/HEAD><BODY>/<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8" \/><\/HEAD><BODY>/' $TMPFILE > ${TMPFILE}1
       mv -f ${TMPFILE}1 $TMPFILE
       rm -f grohtml-*
       rm -f /tmp/grohtml-*
       $HTMLVIEWER $TMPFILE
       [ "$DONTDELETE" ] && exit
       rm -f $TMPFILE
     else
       MORE="less -FRis"
       which more >/dev/null && MORE="more"
       if echo "$FNDMAN" | grep -q '.gz$' ;then
         zcat "$FNDMAN" | groff -mandoc | $MORE
       else
         groff -mandoc "$FNDMAN" | $MORE
       fi
     fi
     exit
     ;;
   esac
 fi
fi

# look up help file
HELPDIR=/usr/share/doc/help
BASE=$(basename $NAME |cut -d'.' -f1)
FNDTXT="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.txt -not -type d 2>/dev/null| head -n 1)"
[ "$FNDTXT" = "" ] && \
FNDHTM="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.htm* -not -type d 2>/dev/null| head -n 1)"
SYMLNKS="`find /usr/share/doc -maxdepth 1 -type l 2>/dev/null | tr "\n" " "`"
[ "$FNDTXT" = "" -a "$FNDHTM" = "" ] && \
 FNDTXT="`find /usr/share/doc -maxdepth 3 -mount -xtype f -iname ${NAME}.txt 2>/dev/null`"
 #find does not follow symlinks in paths unless followed by at least a "/", need this crap...
if [ "$FNDTXT" = "" -a "$FNDHTM" = "" ];then
 FNDSH="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.sh -not -type d 2>/dev/null| head -n 1)"
 [ -f "$FNDSH" ] && exec "$FNDSH"
 for ONELNK in $SYMLNKS
  do
   [ ! "`echo -n "$ONELNK" | grep -i "${NAME}.txt$"`" = "" ] && FNDTXT="$ONELNK"
   [ ! "$FNDTXT" = "" ] && break
   FNDTXT="`find ${ONELNK}/  -mount -maxdepth 3 -xtype f -iname ${NAME}.txt 2>/dev/null`"
   [ ! "$FNDTXT" = "" ] && break
  done
fi

if [ "$FNDTXT" != "" ];then
 FNDTXT="$(echo "$FNDTXT" | head -n 1)"
 exec $TEXTVIEWER "$FNDTXT"
else
 [ "$HTMLVIEWER" = "" ] && exit
 [ "$FNDHTM" = "" ] && \
  FNDHTM="`find /usr/share/doc -maxdepth 9 -mount -xtype f -iname ${NAME}.htm* 2>/dev/null`"
 #v2.12 improved find code contributed by Dougal...
 if [ "$FNDHTM" = "" ];then
   FNDDIR="`find /usr/share/doc -maxdepth 9 -mount -type d -iname ${NAME}* 2>/dev/null`"
   [ $? -eq 0 ] && [ -f $FNDDIR/index.html ] && FNDHTM=$FNDDIR/index.html
 fi
 #find does not follow symlinks in paths unless followed by at least a "/", need this crap...
 if [ "$FNDHTM" = "" ];then
  for ONELNK in $SYMLNKS
  do
   [ ! "`echo -n "$ONELNK" | grep -i "${NAME}.htm"`" = "" ] && FNDHTM="$ONELNK"
   [ ! "$FNDHTM" = "" ] && break
   FNDHTM="`find ${ONELNK}/ -mount -maxdepth 3 -xtype f -iname ${NAME}.htm* 2>/dev/null`"
   [ ! "$FNDHTM" = "" ] && break
  done
 fi

 if [ ! "$FNDHTM" = "" ];then
  FNDHTM="$(echo "$FNDHTM"| head -n 1)"
  [ "`echo -n "$FNDHTM"| cut -b 1`" = "/" ] && FNDHTM=file://$FNDHTM
  exec $HTMLVIEWER "$FNDHTM"
 fi
fi

#w464 search pkg homepages db...
pPATTERN='^'"${NAME}"' '
HOMESITE="`grep -i "$pPATTERN" /root/.packages/PKGS_HOMEPAGES | head -n 1 | cut -f 2 -d ' '`"
if [ "$SECTION" = "" -a "$HOMESITE" != "" ];then
 exec $HTMLVIEWER $HOMESITE
fi
if [ "$lng2" = "ja" ]; then
  # Japanese Puppy Wiki
  url="http://puppylinux.jp/w/index.php?${NAME}"
  wget -t 1 -T 5 -O - "$url" 2>/dev/null | fgrep -q "<title>${NAME} -" && \
  exec $HTMLVIEWER "$url"
  # JP project
  SECS="Sec$SECTION=on"
  [ "$SECTION" = "" ] && SECS="Sec1=on&Sec2=on&Sec3=on&Sec4=on&Sec5=on&Sec6=on&Sec7=on&Sec8=on"
  url="http://search.linux.or.jp/cgi-bin/JM/man.cgi?Pagename=${NAME}&${SECS}"
  if REPLY=$(LANG=C wget -t 1 -T 5 --spider "$url" 2>&1);then
    echo "$REPLY" | fgrep -q 'nosuchpage.html' || \
    exec $HTMLVIEWER "$url"
  fi
fi
#exec defaulthtmlviewer http://en.wikipedia.org/wiki/${NAME}
#exec defaulthtmlviewer http://unixhelp.ed.ac.uk/CGI/man-cgi?${NAME}
#exec defaulthtmlviewer http://threads.seas.gwu.edu/cgi-bin/man2web?program=${NAME}
#v431 fix thanks to technosaurus...
[ "${SECTION}" != "" ] && exec $HTMLVIEWER "http://linux.die.net/man/${SECTION}/${NAME}"
exec $HTMLVIEWER "http://www.duckduckgo.com/?q=man+\"${NAME}\"+site:linux.die.net"
 
###END###
