#!/bin/sh
#
# GTKdialog interface to SliTaz Packages Manager aka Tazpkg. Notes:
# Notebook tab are vbox, tabs are used to indent and functions are
# split and found in $LIB.
#
# (C) GNU gpl v3 - SliTaz GNU/Linux 2010.
#
VERSION=3.2

# Functions path.
export LIB=/usr/lib/slitaz/tazpkgbox

# Tazpkgbox is only for root.
if test $(id -u) != 0 ; then
	exec subox tazpkgbox
	exit 0
fi

# We need at least a mirror URL.
if [ ! -f /var/lib/tazpkg/mirror ]; then
	tazpkg >/dev/null
fi

RELEASE=$(cat /etc/slitaz-release)
case "$1" in
dliso)
	echo "Downloading packages-$RELEASE.iso into $2..."
	cd $2
	for i in $(cat /var/lib/tazpkg/mirror) ; do
		wget ${i%packages*}/iso/$RELEASE/packages-$RELEASE.iso && break
	done
	exit 0;;
instiso)
	PKGSIGN="LABEL=\"packages-$RELEASE\" TYPE=\"iso9660\""
	PKGDEV=$(blkid $2 | grep "$PKGSIGN" | cut -d: -f1)
	[ -z "$PKGDEV$2" -a -L /dev/cdrom ] && PKGDEV=$(blkid /dev/cdrom | grep "$PKGSIGN" | cut -d: -f1)
	if [ -n "$PKGDEV" ]; then
		mkdir /packages 2> /dev/null
		mount -t iso9660 -o ro $PKGDEV /packages
		/packages/install.sh
	fi
	exit 0;;
esac
# Tmp dir and files used to get pkgs info on the fly. Clean also
# lists and searches for files to load less data and have a faster start.
mkdir -p /tmp/tazpkgbox
echo "all" > /tmp/tazpkgbox/status
echo "all" > /tmp/tazpkgbox/category
echo "all" > /tmp/tazpkgbox/undigest-category
echo "" > /tmp/tazpkgbox/search

# English/French help dialog.
export HELP='
<window title="Tazpkgbox Help" icon-name="help">
<vbox>
	<text use-markup="true" width-chars="54">
		<label>"
<b>Tazpkgbox - Packages Manager Help</b>"
		</label>
	</text>

	<frame English>
		<text wrap="false">
			<label>"
Tazpkgbox is a simple and easy to use interface to the SliTaz
package manager - aka Tazpkg. You can install, remove, repack or
get packages by double-clicking on the package item. A Search engine
lets you find and install new applications in a few mouse clicks."
			</label>
		</text>
	</frame>

	<frame Français>
		<text wrap="false">
			<label>"
Tazpkgbox est une interface graphique au gestionnaire de paquet
de Slitaz aka Tazpkg. Simple et facile à utiliser, vous pouvez
installer, télécharger ou supprimer des paquets en double-cliquant
sur sa ligne. La fonction de recherche permet de facilement trouver
et installer des logiciels ou fichiers."
			</label>
		</text>
	</frame>

	<hbox>
		<button ok>
			<action type="closewindow">HELP</action>
		</button>
	</hbox>
</vbox>
</window>
'

FILES="$2"
[ "$1" = "list_config" ] || FILES=""

export SETUP_DVD="
<window title=\"Packages archive\" icon-name=\"media-cdrom\">
<vbox>
	<text use-markup=\"true\" width-chars=\"54\">
		<label>\"
<b>Tazpkgbox - Packages Archive Manager</b>\"
		</label>
	</text>
	<frame English>
		<text wrap=\"false\">
			<label>\"
An ISO image with all $(cat /etc/slitaz-release) packages is available on Slitaz mirrors.
You can burn it onto a DVD or install it on an USB key and each
package becomes installable without an Internet connection.\"
			</label>
		</text>
	</frame>

	<frame Français>
		<text wrap=\"false\">
			<label>\"
Une image ISO installable sur clé USB ou gravable sur DVD est
disponible sur les mirroirs Slitaz. Elle contient tous les paquets
de la version $(cat /etc/slitaz-release) pour pouvoir installer des paquets sans
connexion Internet.\"
			</label>
		</text>
	</frame>
	<frame>
	<hbox>
		<text use-markup=\"true\">
			<label>\"<b>ISO image : </b>\"</label>
		</text>
		<entry>
			<variable>PKGISO</variable>
		</entry>
		<button>
			<input file stock=\"gtk-open\"></input>
			<action type=\"fileselect\">PKGISO</action>
		</button>
		<button>
			<label>Install ISO</label>
			<input file icon=\"go-jump\"></input>
			<action>xterm -T \"Install ISO\" -geometry 80x16+120+120 -e \"tazpkgbox instiso \$PKGISO; sleep 2\" 2>/dev/null</action>
			<action>refresh:PKG</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</button>
	</hbox>
	</frame>
	<hbox>
		<button>
			<label>Download ISO</label>
			<input file icon=\"system-software-update\"></input>
			<action>xterm -T \"Download ISO\" -geometry 80x16+120+120 -e \"tazpkgbox dliso /tmp; sleep 2\" 2>/dev/null</action>
		</button>
		<button>
			<input file icon=\"media-cdrom\"></input>
			<label>Burn DVD</label>
			<action>burnbox</action>
		</button>
		<button>
			<label>Install DVD/USB key</label>
			<input file icon=\"go-jump\"></input>
			<action>xterm -T \"Install DVD/USB key\" -geometry 80x16+120+120 -e \"tazpkgbox instiso; sleep 2\" 2>/dev/null</action>
			<action>refresh:PKG</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</button>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">SETUP_DVD</action>
		</button>
	</hbox>
</vbox>
</window>
"

export LIST_CONFIG_FILES="
<window title=\"configuration files\" icon-name=\"system-file-manager\">
  <vbox>
	<tree exported_column=\"5\">
		<width>560</width><height>160</height>
		<variable>CONF_FILE</variable>
		<label>Access | User | Group | Size | Update Date | Name</label>
		<input>tazpkg list-config --box $FILES</input>
		<action>leafpad \$CONF_FILE</action>
	</tree>
	<hbox>
		<text wrap=\"false\">
			<label>
\"Only files with Update Date have been modified after package installation\"
			</label>
		</text>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">INSTALLED_PACKAGE_ACTIONS</action>
		</button>
	</hbox>
  </vbox>
</window>
"
if [ "$1" = "list_config" ]; then
	gtkdialog --center --program=LIST_CONFIG_FILES >/dev/null
	exit 0
fi

export SETUP_UNDIGEST='
<window title="undigest mirrors" icon-name="tazpkg">
  <vbox>
	<tree>
		<width>560</width><height>160</height>
		<variable>UNDIGEST_LIST</variable>
		<label>Name | URL</label>
		<input>tazpkg list-undigest --box</input>
		<action>xterm -T "Remove undigest" -geometry 80x16+120+120 -e "tazpkg remove-undigest $UNDIGEST_LIST; sleep 2"</action>
	</tree>
	<hbox>
		<text>
			<label>URL:</label>
		</text>
		<entry>
			<variable>UNDIGEST_URL</variable>
		</entry>
	</hbox>
	<hbox>
		<text>
			<label>Name:</label>
		</text>
		<entry>
			<variable>UNDIGEST_NAME</variable>
		</entry>
		<button>
			<label>Add</label>
			<input file icon="gtk-add"></input>
			<action>tazpkg add-undigest "$UNDIGEST_NAME" $UNDIGEST_URL</action>
			<action>refresh:UNDIGEST_LIST</action>
		</button>
		<button>
			<label>Remove</label>
			<input file icon="gtk-remove"></input>
			<action>xterm -T "Remove undigest" -geometry 80x16+120+120 -e "tazpkg remove-undigest $UNDIGEST_NAME; sleep 2"</action>
			<action>refresh:UNDIGEST_LIST</action>
		</button>
		<button>
			<label>Update</label>
			<input file icon="reload"></input>
			<action>tazpkg setup-undigest "$UNDIGEST_NAME" $UNDIGEST_URL</action>
			<action>refresh:UNDIGEST_LIST</action>
		</button>
		<button>
			<input file icon="gtk-close"></input>
			<action type="closewindow">SETUP_UNDIGEST</action>
		</button>
	</hbox>
  </vbox>
</window>
'

# Scan notification
desktopbox notify "Scanning packages database" 6 &

# Main dialog
export TAZPKG_DIALOG='
<window title="SliTaz Packages Manager" icon-name="package-x-generic">
<vbox>

	<hbox>
		<text use-markup="true">
			<label>"<b>Packages Manager</b>"</label>
		</text>
		<pixmap>
			<input file>/usr/share/pixmaps/tazpkg.png</input>
		</pixmap>
	</hbox>

	<notebook labels="Packages|Search|Undigest|Configuration">

	<vbox>
		<tree>
			<width>620</width><height>240</height>
			<variable>PKG</variable>
			<label>Name|Version|Description</label>
			<input icon_column="0">$LIB/list all</input>
			<action>echo "$PKG" > /tmp/tazpkgbox/pkg</action>
			<action>$LIB/package_infos</action>
			<action>refresh:PKG</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</tree>
		<hbox>
			<text>
				<label>"Status:"</label>
			</text>
			<combobox>
				<variable>STATUS</variable>
				<item>all</item>
				<item>installed</item>
				<item>installable</item>
				<item>blocked</item>
			</combobox>
			<text>
				<label>"Category:"</label>
			</text>
			<combobox>
				<variable>CAT</variable>
				<item>all</item>
				<item>base-system</item>
				<item>x-window</item>
				<item>utilities</item>
				<item>network</item>
				<item>graphics</item>
				<item>multimedia</item>
				<item>office</item>
				<item>development</item>
				<item>system-tools</item>
				<item>security</item>
				<item>games</item>
				<item>misc</item>
				<item>meta</item>
				<item>non-free</item>
			</combobox>
			<button>
				<label>List</label>
				<input file icon="reload"></input>
				<action>echo "$CAT" > /tmp/tazpkgbox/category</action>
				<action>echo "$STATUS" > /tmp/tazpkgbox/status</action>
				<action>refresh:PKG</action>
			</button>
		</hbox>
	</vbox>

	<vbox>
		<tree>
			<width>620</width><height>120</height>
			<variable>RESULT</variable>
			<label>Packages Name|Version|Description / File</label>
			<input icon_column="0">cat /tmp/tazpkgbox/search</input>
			<action>echo "$RESULT" > /tmp/tazpkgbox/pkg</action>
			<action>$LIB/package_infos</action>
			<action>$LIB/search packages</action>
			<action>refresh:RESULT</action>
			<action>refresh:PKG</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</tree>
		<hbox>
			<entry>
				<variable>SEARCH</variable>
			</entry>
			<button>
				<label>Packages</label>
				<input file icon="system-search"></input>
				<action>$LIB/search packages</action>
				<action>refresh:RESULT</action>
			</button>
			<button>
				<label>Files</label>
				<input file icon="system-search"></input>
				<action>$LIB/search files</action>
				<action>refresh:RESULT</action>
			</button>
'
tmp='
			<button>
				<label>Tags</label>
				<input file icon="system-search"></input>
				<action>$LIB/search tags</action>
				<action>refresh:RESULT</action>
			</button>
'
[ -d /home/slitaz/wok ] && TAZPKG_DIALOG="$TAZPKG_DIALOG $tmp"
tmp='
		</hbox>
	</vbox>

	<vbox>
		<tree>
			<width>620</width><height>240</height>
			<variable>DEV</variable>
			<label>Name|Version|Description</label>
			<input icon_column="0">$LIB/list undigest</input>
			<action>echo "$DEV" > /tmp/tazpkgbox/pkg</action>
			<action>$LIB/package_infos</action>
			<action>refresh:PKG</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</tree>
		<hbox>
			<button>
				<label>Setup undigest mirrors</label>
				<input file icon="go-next"></input>
				<action type="launch">SETUP_UNDIGEST</action>
			</button>
		</hbox>
		<hbox>
			<text>
				<label>"Undigest:"</label>
			</text>
			<combobox>
				<variable>UNDIGEST_ENTRY</variable>
'
TAZPKG_DIALOG="$TAZPKG_DIALOG $tmp"
for i in all $(ls /var/lib/tazpkg/undigest 2> /dev/null); do
	TAZPKG_DIALOG="$TAZPKG_DIALOG <item>$i</item> "
done
tmp='			</combobox>
			<text>
				<label>"Category:"</label>
			</text>
			<combobox>
				<variable>UNDIGEST_CAT</variable>
				<item>all</item>
				<item>base-system</item>
				<item>x-window</item>
				<item>utilities</item>
				<item>network</item>
				<item>graphics</item>
				<item>multimedia</item>
				<item>office</item>
				<item>development</item>
				<item>system-tools</item>
				<item>security</item>
				<item>games</item>
				<item>misc</item>
				<item>meta</item>
				<item>non-free</item>
			</combobox>
			<button>
				<label>List</label>
				<input file icon="reload"></input>
				<action>echo "$UNDIGEST_CAT $UNDIGEST_ENTRY" > /tmp/tazpkgbox/undigest-category</action>
				<action>refresh:DEV</action>
			</button>
		</hbox>
	</vbox>

	<vbox>
		<frame Files and Cache directory>
			<text use-markup="true" width-chars="60" wrap="false">
				<label>"
Lists, mirror URL and installed packages: <b>/var/lib/tazpkg</b>
The cache directory is used to store downloaded or repacked packages.
				"</label>
			</text>
			<hbox>
				<entry editable="false">
					<input>cd /var/cache/tazpkg; echo "Packages: `ls | wc -l`, size: `du -sh $PWD`"</input>
					<variable>CACHE_STATS</variable>
				</entry>
				<button>
					<label>Clean cache</label>
					<input file icon="go-next"></input>
					<action>rm -rf /var/cache/tazpkg/*</action>
					<action>refresh:CACHE_STATS</action>
				</button>
			</hbox>
		</frame>
		<frame Mirror URL>
			<hbox>
				<entry>
					<input>head -n 1 /var/lib/tazpkg/mirror</input>
					<variable>MIRROR</variable>
				</entry>
				<button>
					<label>Setup</label>
					<input file icon="go-next"></input>
					<action>echo "$MIRROR" > /var/lib/tazpkg/mirror</action>
					<action>clear:MIRROR</action>
					<action>refresh:MIRROR</action>
				</button>
				<button>
					<label>Add</label>
					<input file icon="gtk-add"></input>
					<action>echo "$MIRROR" >> /var/lib/tazpkg/mirror</action>
					<action>clear:MIRROR</action>
					<action>refresh:MIRROR</action>
				</button>
				<button>
					<input file icon="media-cdrom"></input>
					<label>DVD</label>
					<action type="launch">SETUP_DVD</action>
				</button>
			</hbox>
		</frame>
		<hbox>
			<frame Journal>
				<hbox>
				<button>
					<label>Show journal</label>
					<input file icon="find"></input>
					<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -T "$(ls -l /var/log/tazpkg.log | while read mod links user grep remain ; do echo $remain ; done) (q to quit)" -geometry 80x25+120+120 -e "cat /var/log/tazpkg.log | less"</action>
				</button>
				</hbox>
			</frame>
			<frame Configuration files>
				<hbox>
				<button>
					<label>Repack config</label>
					<input file icon="edit-redo"></input>
					<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -T "Repack configs" -geometry 80x25+120+120 -e "tazpkg repack-config ; sleep 5" 2> /dev/null</action>
				</button>
				<button>
					<label>Config Files</label>
					<input file icon="tazpkg"></input>
					<action type="launch">LIST_CONFIG_FILES</action>
				</button>
				</hbox>
			</frame>
			<frame Packages check>
			<hbox>
				<button>
					<label>Quick check</label>
					<input file icon="go-next"></input>
					<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -T "Check packages (q to quit)" -geometry 80x25+120+120 -e "tazpkg check | less " 2> /dev/null</action>
				</button>
				<button>
					<label>Full check</label>
					<input file icon="go-next"></input>
					<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -T "Check packages (q to quit)" -geometry 80x25+120+120 -e "tazpkg check --full | less " 2> /dev/null</action>
				</button>
				</hbox>
			</frame>
		</hbox>
	</vbox>

	</notebook>

	<hbox>
		<text wrap="false" width-chars="82">
			<input>installed=`ls /var/lib/tazpkg/installed | wc -l`; mirrored=`cat /var/lib/tazpkg/packages.list | wc -l`; undigest=`cat /var/lib/tazpkg/undigest/*/packages.list 2> /dev/null | wc -l`; blocked=`cat /var/lib/tazpkg/blocked-packages.list 2> /dev/null | wc -l`; installable=$(($mirrored + $undigest - $installed)); [ $installable -lt 0 ] && installable=0; echo -n "Packages statistics: $installed installed, $blocked blocked, $installable installable, $undigest undigest, $mirrored mirrored, `cat /var/lib/tazpkg/upgradable-packages.list 2> /dev/null | wc -l` to upgrade "</input>
			<variable>PKG_STATS</variable>
		</text>
	</hbox>

	<hbox>
		<button>
			<label>Recharge lists</label>
			<input file icon="system-software-update"></input>
			<action>xterm -T "Recharge" -geometry 80x16+120+120 -e "tazpkg recharge; sleep 2" 2>/dev/null</action>
			<action>refresh:DEV</action>
			<action>refresh:PKG</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</button>
		<button>
			<label>Upgrade all</label>
			<input file icon="system-software-update"></input>
			<action>xterm -T "Package upgrade" -geometry 80x16+120+120 -e "tazpkg upgrade; sleep 2" 2>/dev/null</action>
			<action>refresh:PKG</action>
			<action>refresh:DEV</action>
			<action>refresh:PKG_STATS</action>
			<action>refresh:CACHE_STATS</action>
		</button>
		<button>
			<label>Wok</label>
			<input file icon="gtk-open"></input>
			<action>firefox http://hg.slitaz.org/wok/file/</action>
		</button>
		<button help>
			<label>Help</label>
			<action type="launch">HELP</action>
		</button>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>
			<action type="exit">Exit</action>
		</button>
	</hbox>

</vbox>

</window>
'
TAZPKG_DIALOG="$TAZPKG_DIALOG$tmp"

gtkdialog --center --program=TAZPKG_DIALOG >/dev/null

exit 0
