#!/bin/sh
#
# Dialog box to provide package infos and actions
#

XTERM_OPTS="-geometry 80x16+120+120"

PKG=`cat /tmp/tazpkgbox/pkg | sed s/" "/""/g`

if [ "$1" = "list_files" ]; then
	tazpkg list-files $2 | awk '
BEGIN { ls=0 } {
  if (/^===/) ls=1-ls;
  else if (ls) system("stat -c \"%A|%U|%G|%s|%n\" " $0);
}'
	exit 0
fi

if [ "$1" = "list_files_mirror" ]; then
	unlzma -c /var/lib/tazpkg/files.list.lzma | grep -- "^$2:" | \
		awk '{ print substr($0,index($0,":")+2) }'
	exit 0
fi

export LIST_FILES="
<window title=\"$PKG files\" icon-name=\"system-file-manager\">
  <vbox>
	<tree>
		<width>600</width><height>160</height>
		<label>Access | User | Group | Size | Name</label>
		<input> $0 list_files $PKG </input>
	</tree>
	<hbox>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>
	</hbox>
  </vbox>
</window>
"

export LIST_FILES_MIRROR="
<window title=\"$PKG files\" icon-name=\"system-file-manager\">
  <vbox>
	<tree>
		<width>300</width><height>160</height>
		<label>File Name</label>
		<input> $0 list_files_mirror $PKG </input>
	</tree>
	<hbox>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>
	</hbox>
  </vbox>
</window>
"

# Installed or not installed, that the question.
if [ -d /var/lib/tazpkg/installed/$PKG ]; then
	PACKED_SIZE=""
	DEPENDS=""
	. /var/lib/tazpkg/installed/$PKG/receipt
	PACKAGE_INFOS="
<window title=\"Package: $PKG\" icon-name=\"package-x-generic\">
<vbox>

	<tree>
		<width>460</width><height>160</height>
		<label>Package|$PKG</label>
		<item icon=\"tazpkg\">Version: | $VERSION</item>
		<item icon=\"tazpkg\">Category: | $CATEGORY</item>"
[ -n "$DEPENDS" ] && PACKAGE_INFOS="$PACKAGE_INFOS
		<item icon=\"tazpkg\">Depends: | $DEPENDS</item>"
[ -n "$PACKED_SIZE" ] && PACKAGE_INFOS="$PACKAGE_INFOS
		<item icon=\"tazpkg\">Size: | $PACKED_SIZE ($UNPACKED_SIZE installed)</item>"
PACKAGE_INFOS="$PACKAGE_INFOS
		<item icon=\"system-users\">Maintainer: | $MAINTAINER</item>
		<item icon=\"applications-internet\">Web site: | $WEB_SITE</item>
	</tree>

	<hbox>"
grep -q post_install /var/lib/tazpkg/installed/$PKG/receipt && PACKAGE_INFOS="$PACKAGE_INFOS
		<button>
			<label>Reconfigure</label>
			<input file icon=\"reload\"></input>
			<action>xterm -T \"Reconfigure $PACKAGE\" $XTERM_OPTS -e \"\
			tazpkg reconfigure $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>"
if grep -q ^$PKG$ /var/lib/tazpkg/blocked-packages.list; then
	PACKAGE_INFOS="$PACKAGE_INFOS
		<button>
			<label>Unblock</label>
			<input file icon=\"up\"></input>
			<action>xterm -T \"Unblock $PACKAGE\" $XTERM_OPTS -e \"\
			tazpkg unblock $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>"
else
	PACKAGE_INFOS="$PACKAGE_INFOS
		<button>
			<label>Block</label>
			<input file icon=\"down\"></input>
			<action>xterm -T \"Block $PACKAGE\" $XTERM_OPTS -e \"\
			tazpkg block $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>"
fi
PACKAGE_INFOS="$PACKAGE_INFOS
		<button>
			<label>Remove</label>
			<input file icon=\"edit-delete\"></input>
			<action>xterm -T \"Remove $PACKAGE\" $XTERM_OPTS -e \"\
			tazpkg remove $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>
		<button>
			<label>Repack</label>
			<input file icon=\"edit-redo\"></input>
			<action>xterm -T \"Repack $PACKAGE\" $XTERM_OPTS -e \"\
			cd /var/cache/tazpkg; \
			tazpkg repack $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>
		<button>
			<label>Files</label>
			<input file icon=\"tazpkg\"></input>
			<action type=\"launch\">LIST_FILES</action>
		</button>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>
	</hbox>

</vbox>
</window>
"
	export PACKAGE_INFOS
else
	RES=`grep "^$PKG" /var/lib/tazpkg/packages.desc | head -n 1`
	PACKAGE=`echo "$RES" | cut -d "|" -f 1`
	VERSION=`echo "$RES" | cut -d "|" -f 2`
	CATEGORY=`echo "$RES" | cut -d "|" -f 4`
	WEB_SITE=`echo "$RES" | cut -d "|" -f 5`
	SIZES=`grep -A 3 "^$(echo $PACKAGE)$" /var/lib/tazpkg/packages.txt | tail -1`
	PACKAGE_INFOS="
<window title=\"Package: $PACKAGE\" icon-name=\"package-x-generic\">
<vbox>

	<tree>
		<width>460</width><height>140</height>
		<label>Package|$PKG</label>
		<item icon=\"tazpkg\">Name: | $PACKAGE</item>
		<item icon=\"tazpkg\">Version: | $VERSION</item>
		<item icon=\"tazpkg\">Category: | $CATEGORY</item>"
	[ -n "$SIZES" ] && PACKAGE_INFOS="$PACKAGE_INFOS
		<item icon=\"tazpkg\">Size: | $SIZES</item>"
	PACKAGE_INFOS="$PACKAGE_INFOS
		<item icon=\"applications-internet\">Web site: | $WEB_SITE</item>
	</tree>

	<hbox>
		<button>
			<label>Get-install</label>
			<input file icon=\"go-next\"></input>
			<action>xterm -T \"Install $PACKAGE\" $XTERM_OPTS -e \"\
			tazpkg get-install $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">MIRRORED_PACKAGE_ACTIONS</action>
		</button>
		<button>
			<label>Get</label>
			<input file icon=\"go-next\"></input>
			<action>xterm -T \"Get $PACKAGE\" $XTERM_OPTS -e \"\
			cd /var/cache/tazpkg; tazpkg get $PACKAGE; sleep 2\"</action>
			<action type=\"closewindow\">MIRRORED_PACKAGE_ACTIONS</action>
		</button>
		<button>
			<label>Files</label>
			<input file icon=\"tazpkg\"></input>
			<action type=\"launch\">LIST_FILES_MIRROR</action>
		</button>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">MIRRORED_PACKAGE_ACTIONS</action>
		</button>
	</hbox>

</vbox>
</window>
"
	export PACKAGE_INFOS
fi

gtkdialog --center --program=PACKAGE_INFOS

exit 0
