#!/bin/sh
# TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
# description - TazPkg module
# Display package description


# Connect function libraries
. /lib/libtaz.sh

# Get TazPkg working environment
. @@MODULES@@/getenv




unset desc

# 1) Localized description
for lang in $LANG ${LANG%_*}; do
	[ -e "$PKGS_DB/descriptions.$lang.txt" ] || continue
	desc="$(awk -vRS='' -vFS='\n' -vOFS='\n' -vp="$1" '
		$1 == p { $1 = ""; print $0; exit; }
		' "$PKGS_DB/descriptions.$lang.txt" | sed '1d')"
done

# 2) Installed description
if [ -z "$desc" -a -s "$INSTALLED/$1/description.txt" ]; then
	desc="$(cat "$INSTALLED/$1/description.txt")"
fi

# 3) Mirrored description
if [ -z "$desc" -a -s "$PKGS_DB/descriptions.txt" ]; then
	desc="$(awk -vRS='' -vFS='\n' -vOFS='\n' -vp="$1" '
		$1==p {$1 = ""; print $0; exit}
		' "$PKGS_DB/descriptions.txt" | sed '1d')"
fi

# 4) Short description only in interactive terminal
if [ -z "$desc" ] && im; then
	for lang in $LANG ${LANG%_*}; do
		[ -e "$PKGS_DB/packages-desc.$lang" ] || continue
		desc=$(awk -F$'\t' -vp="$1" '$1==p {print $2; exit}' "$PKGS_DB/packages-desc.$lang")
	done

	[ -z "$desc" -a -s "$PKGS_DB/packages.info" ] &&
		desc="$(awk -F$'\t' -vp="$1" '$1==p {print $4; exit}' "$PKGS_DB/packages.info")"
fi

if [ -n "$desc" ]; then
	case $output in
		html)
			# Description for TazPanel in html format
			if [ -n "$(which sundown)" ]; then
				# Parse description as markdown
				echo "$desc" | sundown
			else
				# Dump description within <pre> tag
				echo '<pre class="pre-wrap">'
				echo "$desc" | sed -e 's|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
				echo '</pre>'
			fi
			;;
		*)
			# Description for terminal tazpkg in plain text
			# Title and footer only in interactive terminal
			im && title 'Description of package "%s"' "$1"
			echo "$desc"
			im && footer
			;;
	esac

else
	im && _ 'Description absent.'
fi

exit 0
