#!/bin/sh
# TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
# help - TazPkg module
# TazPkg help system

. /lib/libtaz.sh
BASE='/usr/share/doc/tazpkg'
for i in $(echo $LANGUAGE | tr ':' ' ') ${LANG%.*} ${LANG%_*} en; do
if [ -f "$BASE/tazpkg.$i.html" ]; then
DOC="$BASE/tazpkg.$i.html"
break
fi
done
usage () {
cat <<EOT
 
$(_ 'SliTaz package manager - Version: %s' $(colorize 34 $VERSION))
 
$(boldify "$(_ 'Usage:')")
  $(_ 'tazpkg [command] [package|dir|pattern|list|cat|--opt] [dir|--opt]')
 
$(boldify "$(_ 'SHell:')") tazpkg shell
 
$(boldify "$(_ 'Commands:')")
$(optlist "\
     usage				$(_ 'Print this short usage')
-h   help				$(_ 'Show help on the TazPkg commands')
-a   activity			$(_ 'Show TazPkg activity log')
-cc  clean-cache		$(_ 'Clean all packages downloaded in cache directory')
     list-cache			$(_ 'List *.tazpkg packages downloaded to the cache')
     shell				$(_ 'Run interactive TazPkg shell')
 
-l   list				$(_ 'List installed packages on the system')
-lm  list-mirror		$(_ 'List all available packages on the mirror')
     list-config		$(_ 'List the configuration files')
 
-s   search				$(_ 'Search for a package by pattern or name')
-sp  search-pkgname		$(_ 'Search on mirror for package having a particular file')
-sf  search-file		$(_ 'Search for file in all installed packages files')
 
-g   get				$(_ 'Download a package into the current directory')
-i   install			$(_ 'Install a local package')
-gi  get-install		$(_ 'Download and install a package from the mirror')
     install-list		$(_ 'Install all packages from a list of packages')
     get-install-list	$(_ 'Download and install a list of packages from the mirror')
-r   remove				$(_ 'Remove the specified package and all installed files')
     reconfigure		$(_ 'Replay post install script from package')
     link				$(_ 'Link a package from another SliTaz installation')
     set-release		$(_ 'Change release and update packages')
     add-flavor			$(_ 'Install the flavor list of packages')
     install-flavor		$(_ 'Install the flavor list of packages and remove other ones')
 
     info				$(_ 'Print information about a package')
-d   desc				$(_ 'Print description of a package')
-lf  list-files			$(_ 'List the files installed with a package')
-b|u block|unblock		$(_ 'Block an installed package version or unblock it for upgrade')
     check				$(_ 'Verify consistency of installed packages')
     bugs				$(_ 'Show known bugs in packages')
     depends			$(_ 'Display dependencies tree')
     rdepends			$(_ 'Display reverse dependencies tree')
-e   extract			$(_ 'Extract a (*.tazpkg) package into a directory')
     pack				$(_ 'Pack an unpacked or prepared package tree')
     repack				$(_ 'Create a package archive from an installed package')
     repack-config		$(_ 'Create a package archive with configuration files')
     recompress			$(_ 'Rebuild a package with a better compression ratio')
     convert			$(_ 'Convert alien package to tazpkg')
     list-suggested		$(_ 'Print list of suggested packages')
 
     recharge			$(_ 'Recharge your packages.list from the mirror')
     up					$(_ 'Check packages %s to list and install latest upgrades' $CHECKSUM)
-sm  setup-mirror		$(_ 'Change the mirror URL configuration')
     setup-undigest		$(_ 'Update an undigest mirror')
     list-undigest		$(_ 'List undigest mirrors')
     add-undigest		$(_ 'Add an undigest mirror')
     remove-undigest	$(_ 'Remove an undigest mirror')
     mkdb				$(_ 'Make a TazPkg database for a folder with *.tazpkg packages')
")
EOT
}
T="${1#-}"
QUERY="$T"
if [ -z "$T" ]; then
usage; exit 0
fi
commands="\
:usage::help,h::activity,log,a::clean-cache,cc::shell::list,l::list-mirror,lm:\
:list-config::search,s::search-pkgname,sp::search-file,sf::get,g::install,i:\
:get-install,gi::install-list,get-install-list::remove,r::reconfigure::link:\
:set-release::add-flavor,install-flavor::info::desc,d::list-files,lf:\
:block,b,unblock,u,chblock::check::bugs::depends,rdepends::extract,e::pack:\
:repack::repack-config::recompress::convert,c::list-suggested::recharge:\
:upgrade,up::setup-mirror,sm::add-undigest,setup-undigest::list-undigest:\
:remove-undigest:"
if [ ! $(echo $commands | grep -e "[:,]$T[:,]") ]; then
try1=$(echo $commands | grep -oe ":[^:]*$T[^:]*:")
if [ -z "$try1" ]; then
_ 'Sorry, no help for "%s"' "$QUERY"; newline; exit 0
fi
try2=$(echo "$try1" | tr -d : )
trynum=$(echo "$try2" | wc -l)
if [ "$trynum" -ne 1 ]; then
_p '%d help topic available:' '%d help topics available:' "$trynum" \
"$trynum"; newline
echo "$try2" | sed 's|^|  * &|; s|  ,|  |; s|,|, |g'
exit 0
else
T="${try2%%,*}"
fi
else
try3=$(echo $commands | grep -oe "[:,]$T[:,]")
case $try3 in
:*)	try4="$try3";;
,*)	try4=$(echo $commands | grep -oe ":[^:]*$try3" | cut -d, -f1);;
esac
T=$(echo $try4 | tr -d ':,')
fi
title=$(echo $commands | grep -oe ":$T\(,[^:]*\)*:" | tr -d : | sed 's|,|, |g')
HLP=$(sed "/article id=\"$T\"/,/<\/article/!d; s|</*article[^>]*>||; s|<h3>.*</h3>||" $DOC)
if [ -z "$HLP" ]; then
_ 'Sorry, no help for "%s"' "$QUERY"
exit 0
fi
PRE=$(echo "$HLP" | sed "/^$/d; /<pre>/,/<\/pre\>/{s|.*|  &|; s| |·|g}; \
s|^  </*pre>$||; s|<pre>||; s|</pre>||; s|  ·#|  #|;" | tr '\n' ' ' | \
sed 's|[ 	][ 	]*| |g;' | \
sed 's|[ 	]*<dl>|O\n|g; s|[ 	]*</dl>|L\n|g; s|[ 	]*</*dt>||g;' | \
sed 's|[ 	]*<dd>|	|g; s|</dd>|\n|g;' | \
sed 's|[ 	]*<ul>|O\n|g; s|[ 	]*</ul>|L\n|g; s|[ 	]*<li>|*	|g; s|</li>|\n|g;' | \
sed 's|<h4>|<b>|g; s|</h4>|</b>\n|g' | \
sed 's|[ 	]*<p>[ 	]*||g; s|[ 	]*</p>|\n \n|g; s|  |\n  |g' | \
sed 's|<a [^>]*>||g; s|</a>||g; s|·| |g' | \
sed 's|</*nobr>||g; s|&shy;||g; s|^[ 	]*||' | \
sed '/^$/d' | \
sed 's|<tt>|A|g; s|<code>|A|g; s|<em>|B|g; s|<strong>|B|g; \
s|</tt>|D|g; s|</code>|D|g; s|</em>|D|g; s|</strong>|C|g; \
s|DD|D|g;')
OPT=$(echo "$PRE" | sed '/O/,/L/!d; /[OL]/d')
OPTLIST=$(optlist "$OPT")
OPT0=$(echo "$PRE" | tr '\n' '@')
OPTA=$(echo "$OPT" | tr '\n' '@' | sed 's|\*|\\*|g;')
OPTB=$(echo "$OPTLIST" | tr '\n' '@' | sed 's|\*|\\*|g;')
PRE2=$(echo "$OPT0" | sed "s|$OPTA|$OPTB|" | tr '@' '\n' | sed '/O/d; s|L||')
[ ! -z "$TA" ] && T="$T, $TA"
title '%s' "$title"
emsg "$(longline "$PRE2" | sed 's|A|<c 33>|g; s|B|<c 36>|g; s|C|</b>|g; s|D|</c>|g; ')"
newline
