#!/bin/sh
#
# TazPKG - Tiny autonomous zone packages manager.
#
# This is a lightwight packages manager for *.tazpkg files written in SHell
# script. It works well with Busybox ash shell and bash. TazPKG lets you
# list, install, remove, download or get information about a package. You
# can use 'tazpkg usage' to get a list of commands with short descriptions.
# TazPKG also resolves dependencies and can upgrade packages from a mirror.
#
# (C) 2007-2014 SliTaz - GNU General Public License v3.
#
# Authors: See the AUTHORS files
#

####################
# Script variables #
####################

# TazPKG version
VERSION=5.2

. /etc/slitaz/slitaz.conf
. /etc/slitaz/tazpkg.conf

. /lib/libtaz.sh

# Internationalization.
. /usr/bin/gettext.sh
TEXTDOMAIN='tazpkg'
export TEXTDOMAIN

#
# Functions set for translate categories
#

# No operations, only for xgettext collect
gettext_noop() {
	gettext "base-system"; gettext "x-window"; gettext "utilities";
	gettext "network"; gettext "graphics"; gettext "multimedia";
	gettext "office"; gettext "development"; gettext "system-tools";
	gettext "security"; gettext "games"; gettext "misc"; gettext "meta";
	gettext "non-free"
}
# Make array of pre-translated categories
cat_i18n=""
for c in "base-system" "x-window" "utilities" "network" "graphics" "multimedia" "office" "development" "system-tools" "security" "games" "misc" "meta" "non-free"; do
	cat_i18n="$cat_i18n
$(gettext "$c")	$c"
done
# If category is not one of those translated in native language, keep it
# untranslated. This allows both native and english language support.
# This also supports custom categories.
# And now we support spaces in translated categories
reverse_translate_category()
{
	echo "$cat_i18n" | awk "BEGIN{FS=\"	\"}{if (/^$@	/) a=\$2}END{if (a==\"\") a=\"$@\"; print a}"
}

#
# TazPKG output functions
#
# Print localized title
title() { newline; boldify "$(eval_gettext "$1")"; separator; }
# Print footer
footer() { separator; echo "$1"; newline; }
# Print current action in brown color (separate from any other msgs)
action() {
	case $output in
		raw|gtk|html) eval_gettext "$@" ;;
		*) echo -ne "\033[0;33m"$(eval_gettext "$@")"\033[0m" ;;
	esac
}

# Initialize some variables to use words rather than numbers for functions
# and actions.
COMMAND=$1
PACKAGE=${2%/}
PACKAGE_DIR="$(cd $(dirname $PACKAGE 2>/dev/null) 2>/dev/null; pwd)"
[ -n "$PACKAGE" ] &&
PACKAGE_FILE="$PACKAGE_DIR/${PACKAGE##*/}"
if [ -f "$PACKAGE" ]; then
	# Set pkg basename for install, extract
	PACKAGE=$(basename ${PACKAGE%.tazpkg} 2>/dev/null)
else
	# Pkg name for remove, search and all other cmds
	PACKAGE=${PACKAGE%.tazpkg}
fi
TARGET_DIR=$3
TOP_DIR=`pwd`
TMP_DIR=/tmp/$RANDOM
INSTALL_LIST=""
SAVE_CACHE_DIR="$CACHE_DIR"

# Path to tazpkg used dir and configuration files
MIRROR=$LOCALSTATE/mirror
BLOCKED=$LOCALSTATE/blocked-packages.list
UP_LIST=$LOCALSTATE/packages.up
DEFAULT_MIRROR="$ONLINE_PKGS"

# Need by check_depends
TMPLOCALSTATE=

####################
# Script functions #
####################

# Print the usage.
usage () {
	cat << EOT

$(eval_gettext 'SliTaz package manager - Version:') $(colorize 34 $VERSION)

$(boldify "$(gettext 'Usage:')")
  $(gettext 'tazpkg [command] [package|dir|pattern|list|cat|--opt] [dir|--opt]')

$(boldify "$(gettext 'SHell:')") tazpkg shell

$(boldify "$(gettext 'Commands:')")
  usage            $(gettext 'Print this short usage')
  bugs             $(gettext 'Show known bugs in packages')
  activity|-a      $(gettext 'Show TazPKG activity log')
  list|-l          $(gettext 'List installed packages on the system by category or all')
  list-mirror|-lm  $(gettext 'List all available packages on the mirror (--diff for new)')
  info             $(gettext 'Print information about a package')
  desc             $(gettext 'Print description of a package (if it exists)')
  list-files|-lf   $(gettext 'List the files installed with a package')
  list-config      $(gettext 'List the configuration files')
  search|-s        $(gettext 'Search for a package by pattern or name (options: -i|-l|-m)')
  search-pkgname   $(gettext 'Search on mirror for package having a particular file')
  search-file|-sf  $(gettext 'Search for file(s) in all installed packages files')
  install|-i       $(gettext 'Install a local (*.tazpkg) package (--forced to force)')
  install-list     $(gettext 'Install all packages from a list of packages.')
  remove|-r        $(gettext 'Remove the specified package and all installed files')
  extract|-e       $(gettext 'Extract a (*.tazpkg) package into a directory')
  pack             $(gettext 'Pack an unpacked or prepared package tree')
  recharge         $(gettext 'Recharge your packages.list from the mirror')
  up|help-up       $(eval_gettext 'Check packages $CHECKSUM to list and install latest upgrades')
  repack           $(gettext 'Create a package archive from an installed package')
  repack-config    $(gettext 'Create a package archive with configuration files')
  recompress       $(gettext 'Rebuild a package with a better compression ratio')
  block|unblock    $(gettext 'Block an installed package version or unblock it for upgrade')
  get              $(gettext 'Download a package into the current directory')
  get-install|-gi  $(gettext 'Download and install a package from the mirror')
  get-install-list $(gettext 'Download and install a list of packages from the mirror')
  check            $(gettext 'Verify consistency of installed packages')
  add-flavor       $(gettext 'Install the flavor list of packages')
  install-flavor   $(gettext 'Install the flavor list of packages and remove other ones')
  set-release      $(gettext 'Change release and update packages')
  clean-cache|-cc  $(gettext 'Clean all packages downloaded in cache directory')
  depends          $(gettext 'Display dependencies tree')
  rdepends         $(gettext 'Display reverse dependencies tree')
  convert          $(gettext 'Convert deb/rpm/tgz/pet/sfs/sb/arch/ipk package to tazpkg)')
  link             $(gettext 'Link a package from another slitaz installation')
  setup-mirror|-sm $(gettext 'Change the mirror url configuration')
  list-undigest    $(gettext 'List undigest mirrors')
  remove-undigest  $(gettext 'Remove an undigest mirror')
  add-undigest     $(gettext 'Add an undigest mirror')
  setup-undigest   $(gettext 'Update an undigest mirror')
  reconfigure      $(gettext 'Replay post install script from package')

EOT
}

usage_up() {
	cat << EOT
$(emsg "<b>$(gettext 'Usage for command up:')</b>") tazpkg up [$(gettext 'option')]

 * $(gettext 'Without options run in interactive mode and ask before install')

$(boldify "$(gettext 'Where options are:')")
  --check    |-c   $(gettext 'Check only for available upgrades')
  --recharge |-r   $(gettext 'Force recharge of packages list and check')
  --install  |-i   $(gettext 'Check for upgrades and install them all')

$(boldify "$(gettext 'Example:')")
  tazpkg up --recharge --install
  tazpkg up -c -r
EOT
}

# Check if dir exist
check_dir()
{
	if ! [ -d "$1" ]; then
		FOLDER=$1
		action 'Creating $FOLDER...'
		mkdir -p "$FOLDER"
		status
		return 1
	fi
}

# Check if the directories and files used by TazPKG
# exist. If not and user is root we create them.
check_base_dir()
{
	if [ "$(id -u)" = "0" ]; then
		check_dir $1$CACHE_DIR
		check_dir $1$INSTALLED
		check_dir $1$SLITAZ_LOGS
		if [ ! -f "$1$LOCALSTATE/mirror" ]; then
			echo "$DEFAULT_MIRROR" > $1$LOCALSTATE/mirror
			[ -n "$1" ] && cp $LOCALSTATE/packages.* $1$LOCALSTATE/
		fi
	fi
}
check_base_dir

# Check for a package name on cmdline.
check_for_package_on_cmdline()
{
	if [ -z "$PACKAGE" ]; then
		newline
		gettext "Please specify a package name on the command line."; echo
		newline
		exit 0
	fi
}

# Check if the package (*.tazpkg) exists before installing or extracting.
check_for_package_file()
{
	if [ ! -f "$PACKAGE_FILE" ]; then
		newline
		eval_gettext "Unable to find: \$PACKAGE_FILE"; echo
		newline
		exit 0
	fi
}

# Check for the receipt of an installed package.
check_for_receipt()
{
	receipt_path="$1$INSTALLED/$PACKAGE/receipt"
	if [ ! -f $receipt_path ]; then
		newline
		eval_gettext 'Unable to find the receipt: $receipt_path'; echo
		newline
		exit 0
	fi
}

# Get repositories priority using $LOCALSTATE/priority.
# In this files, undigest are called by their name and main mirror
# by main. Sort order: priority
look_for_priority()
{
[ -s $LOCALSTATE/priority ] && priority=$(cat $LOCALSTATE/priority)
for rep in main $(ls $LOCALSTATE/undigest 2>/dev/null); do
	if [ ! -s $LOCALSTATE/priority ] || \
		! grep -q ^$rep$ $LOCALSTATE/priority; then
		priority=$(echo -e "$priority\n$rep")
	fi
done
priority=$(echo "$priority" | sed '/^$/d' | \
	while read line; do
	if [ "$line" = main ]; then
		echo $LOCALSTATE
	else
		echo $LOCALSTATE/undigest/$line
	fi
done)
}

# Get package name in a directory
package_fullname_in_dir()
{
	[ -f $1/receipt ] || return
	EXTRAVERSION=""
	. $1/receipt
	echo $PACKAGE-$VERSION$EXTRAVERSION
}

# Get package name that is already installed.
get_installed_package_pathname()
{
	for i in $2$INSTALLED/${1%%-*}*; do
		[ -d $i ] || continue
		if [ "$1" = "$(package_fullname_in_dir $i)" ]; then
			echo $i
			return
		fi
	done
}

# Check if a package is already installed.
check_for_installed_package()
{
	if [ -n "$(get_installed_package_pathname $PACKAGE $1)" ]; then
		newline
		cat << EOT
$(colorize 34 $PACKAGE) $(gettext "package is already installed.
You can use the --forced option to force installation.")
EOT
		newline
		exit 0
	fi
}

# Check for packages.list to download and install packages.
check_for_packages_list()
{
	list_path="$LOCALSTATE/packages.list"
	if [ ! -f "$list_path" ]; then
		if test $(id -u) = 0 ; then
			tazpkg recharge
		else
			newline
			eval_gettext 'Unable to find the list: $list_path'; echo
			gettext \
"You must probably run 'tazpkg recharge' as root to get the latest list of
packages available on the mirror."; echo
			newline
			exit 0
		fi
	fi
}

get_cache_dir()
{
	echo $rep > $tmp/rep
	if [ "$rep" = "$LOCALSTATE" ]; then
		CACHE_DIR="$SAVE_CACHE_DIR/$SLITAZ_RELEASE/packages"
	elif [ "${rep%-incoming}" = "$rep" ]; then
		CACHE_DIR="$SAVE_CACHE_DIR/${rep##*/}/packages"
	else
		rep="${rep%-incoming}"
		CACHE_DIR="$SAVE_CACHE_DIR/${rep##*/}/packages-incoming"
	fi
	[ -d "$CACHE_DIR" ] || mkdir -p $CACHE_DIR
	echo $CACHE_DIR > $tmp/cachedir
}

# get an already installed package from packages.equiv
equivalent_pkg()
{
	for i in $(grep -hs "^$1=" $LOCALSTATE/packages.equiv \
		   $LOCALSTATE/undigest/*/packages.equiv | sed "s/^$1=//"); do
		if echo $i | fgrep -q : ; then
			# format 'alternative:newname'
			# if alternative is installed then substitute newname
			if [ -f $2$INSTALLED/${i%:*}/receipt ]; then
				# substitute package dependancy
				echo ${i#*:}
				return
			fi
		else
			# if alternative is installed then nothing to install
			if [ -f $2$INSTALLED/$i/receipt ]; then
				# substitute installed package
				echo $i
				return
			fi
		fi
	done
	# if not found in packages.equiv then no substitution
	echo $1
}

# get a virtual package from packages.equiv
virtual_pkg()
{
	for i in $(for rep in $priority; do
		grep -hs "^$1=" $rep/packages.equiv
		done | sed "s/^$1=//"); do
		if echo $i | fgrep -q : ; then
			# format 'alternative:newname'
			# if alternative is installed then substitute newname
			if [ -f $2$INSTALLED/${i%:*}/receipt ]; then
				# substitute package dependancy
				echo ${i#*:}
				return
			fi
		else
			# unconditional substitution
			echo $i
			return
		fi
	done
}

# Get package filename available on the mirror
get_package_filename()
{
	local pkg
	for rep in $priority; do
		pkg=$(grep -A 1 -sh "^$1$" $rep/packages.txt | tail -1 | \
			sed 's/^ *//')
		[ "$pkg" ] && pkg=$(grep -sh "^$1-$pkg" \
			$rep/packages.list | head -1)

		# Allow user to call a package with his version number.
		[ "$pkg" ] || pkg=$(grep -sh "^$1$" $rep/packages.list | head -1)

		[ "$pkg" ] || pkg=$(grep -sh "^$1-[0-9]" \
			$rep/packages.list | head -1)
		[ "$pkg" ] || pkg=$(grep -sh "^$1-.[\.0-9]" \
		$rep/packages.list | head -1)
		[ "$pkg" ] && get_cache_dir && break
	done
	if [ -z "$pkg" ]; then
		# Check for vitual package
		local equiv
		equiv=$(virtual_pkg $1)
		if [ "$equiv" != "$1" ]; then
			PACKAGE=$equiv
			get_package_filename $PACKAGE
			return
		fi
	fi
	echo $pkg
}

# Check for a package in packages.list. Used by get and get-install to grep
# package basename.
check_for_package_in_list()
{
	local filename
	local check_only
	check_only="$1"
	filename=$(get_package_filename $PACKAGE)
	if [ "$filename" ]; then
		PACKAGE=$filename
		CACHE_DIR=$(cat $tmp/cachedir)
		rep=$(cat $tmp/rep)
		rm -f $tmp/rep $tmp/cachedir
	else
		newline
		eval_gettext "Unable to find: \$PACKAGE in the mirrored packages list."; echo
		newline
		[ -n "$check_only" ] && return 1
		exit 0
	fi
}

# Log this activity
# (there log_pkg because we have log() in libtaz.sh)
log_pkg()
{
	local extra
	[ "$1" = "Installed" ] && \
	extra=" - $(fgrep $PACKAGE-$VERSION $LOCALSTATE/installed.$SUM | awk '{ print $1 }')"
	[ -e $LOG ] || touch $LOG
	DATE=`date +'%F %T'`
	[ -w $LOG ] &&
	echo "$DATE - $1 - $PACKAGE ($VERSION$EXTRAVERSION)$extra" >> $LOG
}

# Download a get-package script from this mirror
download_get_script()
{
	local p
	for p in $priority ; do
		local i
		for i in $(cat $p/mirror) ; do
			case "$i" in
			http://*|ftp://*)
				wget -O $2 ${i%packages/*}packages/get/$1 &&
				return 0 ;;
			esac
		done
	done
	return 1
}

# Download a file from this mirror
download_from()
{
	local i
	local mirrors
	mirrors="$1"
	shift
	for i in $mirrors; do
		case "$i" in
			# Mirror URL can have a trailing slash or not.
			http://*|ftp://*) busybox wget -c ${i%/}/$@ && break ;;
			*) ln -sf $i/$1 . && break ;;
		esac
	done
}

# Download a file trying all mirrors
download()
{
	local i
	case "$1" in
	*.tazpkg)
		for i in $priority ; do
			grep -q "^${1%.tazpkg}$" $i/packages.list 2>/dev/null || continue
			download_from "$(cat $i/mirror)" "$@" && return
		done
	esac
	for i in $(cat `for rep in $priority; do echo $rep/mirror; done` \
		2> /dev/null); do
		download_from "$i" "$@" && break
	done
}

# Extract a package with cpio and gzip/lzma.
extract_package()
{
	action "Extracting \$PACKAGE..."
	cpio -idm --quiet < ${PACKAGE_FILE##*/} && rm -f ${PACKAGE_FILE##*/}
	status
	if [ -f fs.cpio.lzma ]; then
		unlzma -c fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
	elif [ -f fs.cpio.gz ]; then
		zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
	fi
}

remove_with_path()
{
	# Avoid dirname errors by checking for argument.
	[ "$1" ] || return

	local dir
	rm -f $1 2>/dev/null
	dir="$1"
	while [ "$dir" != "/" ]; do
		dir="$(dirname $dir)"
		rmdir $dir 2> /dev/null || break
	done
}

grepesc()
{
	sed 's/\[/\\[/g'
}

# This function installs a package in the rootfs.
install_package()
{
	ROOT=$1
	if [ -n "$ROOT" ]; then
		 # Get absolute path
		 ROOT=$(cd $ROOT; pwd)
	fi
	{
		# Create package path early to avoid dependencies loop
		mkdir -p $TMP_DIR
		{ cd $TMP_DIR ; cpio --quiet -i receipt > /dev/null 2>&1; } < $PACKAGE_FILE
		. $TMP_DIR/receipt
		if grep -q ^pre_depends $TMP_DIR/receipt; then
			pre_depends $ROOT
		fi
		# Keep modifers and file list on upgrade
		cp $ROOT$INSTALLED/$PACKAGE/modifiers \
		   $ROOT$INSTALLED/$PACKAGE/files.list $TMP_DIR 2> /dev/null
		rm -rf $ROOT$INSTALLED/$PACKAGE 2> /dev/null
		# Make the installed package data dir to store
		# the receipt and the files list.
		mkdir -p $ROOT$INSTALLED/$PACKAGE
		cp $TMP_DIR/modifiers $ROOT$INSTALLED/$PACKAGE 2> /dev/null
		cp $TMP_DIR/files.list $ROOT$INSTALLED/$PACKAGE 2> /dev/null
		rm -rf $TMP_DIR 2> /dev/null
		sed -i "/ $(basename $PACKAGE_FILE)$/d" \
			$ROOT$LOCALSTATE/installed.$SUM 2> /dev/null
		cd $(dirname $PACKAGE_FILE)
		$CHECKSUM $(basename $PACKAGE_FILE) >> $ROOT$LOCALSTATE/installed.$SUM
	}
	# Resolve package deps.
	check_for_deps $ROOT
	if [ ! "$MISSING_PACKAGE" = "" ]; then
		install_deps $ROOT
	fi
	mkdir -p $TMP_DIR
	[ -n "$INSTALL_LIST" ] && echo "$PACKAGE_FILE" >> $ROOT$LOCALSTATE/$INSTALL_LIST-processed
	title 'Installation of: $PACKAGE'
	action "Copying \$PACKAGE..."
	cp $PACKAGE_FILE $TMP_DIR
	status
	cd $TMP_DIR
	extract_package
	SELF_INSTALL=0
	EXTRAVERSION=""
	CONFIG_FILES=""
	# Include temporary receipt to get the right variables.
	. $PWD/receipt
	cd $ROOT$INSTALLED
	if [ $SELF_INSTALL -ne 0 -a -n "$ROOT" ]; then
		action "Checking post install dependencies..."
		[ -f $INSTALLED/$PACKAGE/receipt ]
		if ! status; then
			command="tazpkg install $PACKAGE_FILE"
			eval_gettext "Please run '\$command' in / and retry."; echo
			rm -rf $TMP_DIR
			exit 1
		fi
	fi
	# Get files to remove if upgrading
	if [ -f $PACKAGE/files.list ]; then
		while read file; do
			grep -q "^$(echo $file | grepesc)$" $TMP_DIR/files.list && continue
			for i in $(cat $PACKAGE/modifiers 2> /dev/null ;
			    fgrep -sl $PACKAGE */modifiers | cut -d/ -f1 ); do
				grep -qs "^$(echo $file | grepesc)$" $i/files.list && continue 2
			done
			echo $file
		done < $PACKAGE/files.list > $TMP_DIR/files2remove.list
	fi
	# Remember modified packages
	{ check=false
	  for i in $(fgrep -v [ $TMP_DIR/files.list); do
		[ -e "$ROOT$i" ] || continue
		[ -d "$ROOT$i" ] && continue
		echo "- $i"
		check=true
	  done ;
	  $check && for i in *; do
	  	[ "$i" == "$PACKAGE" ] && continue
	  	[ -s $i/files.list ] || continue
	  	awk "{ printf \"$i %s\\n\",\$1 }" < $i/files.list
	  done; } | awk '
{
	if ($1 == "-" || file[$2] != "") {
		file[$2] = file[$2] " " $1
		if ($1 != "-") {
			if (pkg[$1] == "") all = all " " $1
			pkg[$1] = pkg[$1] " " $2
		}
	}
}
END {
	for (i = split(all, p, " "); i > 0; i--)
		for (j = split(pkg[p[i]], f, " "); j > 0; j--)
			printf "%s %s\n",p[i],f[j];
}
		' | while read dir file; do
		if grep -qs ^$dir$ $PACKAGE/modifiers; then
			# Do not overload an overloaded file !
			rm $TMP_DIR$file 2> /dev/null
			continue
		fi
		grep -qs ^$PACKAGE$ $dir/modifiers && continue
		if [ -s "$dir/volatile.cpio.gz" ]; then
			# We can modify backed up files without notice
			zcat $dir/volatile.cpio.gz | cpio -t --quiet | \
				grep -q "^${file#/}$" && continue
		fi
		echo "$PACKAGE" >> $dir/modifiers
	done

	cd $TMP_DIR
	cp receipt files.list $ROOT$INSTALLED/$PACKAGE
	# Copy the description if found.
	if [ -f "description.txt" ]; then
		cp description.txt $ROOT$INSTALLED/$PACKAGE
	fi
	# Copy the md5sum if found.
	if [ -f "$CHECKSUM" ]; then
		cp $CHECKSUM $ROOT$INSTALLED/$PACKAGE
	fi
	# Pre install commands.
	if grep -q ^pre_install $ROOT$INSTALLED/$PACKAGE/receipt; then
		pre_install $ROOT
	fi
	if [ -n "$CONFIG_FILES" ]; then
		# save 'official' configuration files
		action "Saving configuration files for \$PACKAGE..."
		for i in $CONFIG_FILES; do
			{ cd fs ; find ${i#/} -type f 2> /dev/null; cd ..; }
		done | { cd fs ; cpio -o -H newc --quiet | gzip -9; cd ..; } > \
			$ROOT$INSTALLED/$PACKAGE/volatile.cpio.gz
		# keep user configuration files
		for i in $CONFIG_FILES; do
			{ cd fs ; find ${i#/} -type f 2> /dev/null; cd ..; }
		done | while read i; do
			[ -e $ROOT/$i ] || continue
			cp -a $ROOT/$i fs/$i
		done
		status
	fi
	action "Installing \$PACKAGE..."
	cp -a fs/* $ROOT/
	status
	if [ -s files2remove.list ]; then
		action "Removing old \$PACKAGE..."
		while read file; do
			remove_with_path $ROOT$file
		done < files2remove.list
		true
		status
	fi
	# Remove the temporary random directory.
	action "Removing all tmp files..."
	cd .. && rm -rf $TMP_DIR
	status
	# Post install commands.
	if grep -q ^post_install $ROOT$INSTALLED/$PACKAGE/receipt; then
		post_install $ROOT
	fi
 	# Update-desktop-database if needed.
	if [ "$(fgrep .desktop $ROOT$INSTALLED/$PACKAGE/files.list | fgrep /usr/share/applications/)" ]; then
		updatedesktopdb=yes
	fi
	# Update-mime-database if needed.
	if [ "$(fgrep /usr/share/mime $ROOT$INSTALLED/$PACKAGE/files.list)" ]; then
		updatemimedb=yes
	fi
	# Update-icon-database
	if [ "$(fgrep /usr/share/icon/hicolor $ROOT$INSTALLED/$PACKAGE/files.list)" ]; then
		updateicondb=yes
	fi
	# Compile glib schemas if needed.
	if [ "$(fgrep /usr/share/glib-2.0/schemas $ROOT$INSTALLED/$PACKAGE/files.list)" ]; then
		compile_schemas=yes
	fi
	# Update depmod list
	if [ "$(fgrep /lib/modules $ROOT$INSTALLED/$PACKAGE/files.list)" ]; then
		updatedepmod=yes
	fi
	cd $TOP_DIR
	pkg_name="$PACKAGE ($VERSION$EXTRAVERSION)"
	footer "$(eval_gettext '$pkg_name is installed.')"
	# Log this activity
	[ -n "$ROOT" ] || log_pkg Installed
}

# This function may be called by a get script.
abort_package()
{
	cd $CUR_DIR
	rm -rf $TMP_DIR
	echo "${1:-Abort $PACKAGE.}"
	exit 1
}

# This function installs a package form a get script in the rootfs.
install_package_from_get_script()
{
	SCRIPT="$1"
	ROOT="$2"
	[ -d $ROOT$INSTALLED/$PACKAGE ] && exit 1

	grep -q no-check-certificate $SCRIPT &&
	[ ! -d $INSTALLED/wget ] && tazpkg get-install wget

	mkdir -p $TMP_DIR && cd $TMP_DIR

	unset VERSION CATEGORY WEB_SITE SHORT_DESC MAINTAINER LICENSE \
	      TARBALL WGET_URL CONFIG_FILES SUGGESTED PROVIDE DEPENDS
	set -e
	. $SCRIPT
	
	if [ ! -s $PACKAGE-$VERSION/receipt ]; then
		cat > $PACKAGE-$VERSION/receipt <<EOT
# SliTaz package receipt.

PACKAGE="$PACKAGE"
VERSION="$VERSION"
CATEGORY="${CATEGORY:-non-free}"
WEB_SITE="$WEB_SITE"
SHORT_DESC="${SHORT_DESC:-$PACKAGE}"
MAINTAINER="${MAINTAINER:-nobody@slitaz.org}"
EOT
		for i in LICENSE TARBALL WGET_URL CONFIG_FILES SUGGESTED \
			 PROVIDE DEPENDS ; do
			eval "[ -n \"\$$i\" ] && echo \"$i=\\\"\$$i\\\"\""
		done >> $PACKAGE-$VERSION/receipt
	fi

	tazpkg pack $PACKAGE-$VERSION

	# Clean to save RAM memory before installation
	rm -rf $PACKAGE-$VERSION

	# Install pseudo package
	tazpkg install $PACKAGE-$VERSION.tazpkg --root=$ROOT
	mv $PACKAGE-$VERSION.tazpkg $CACHE_DIR

	# Clean
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# Check for loop in deps tree.
check_for_deps_loop()
{
	local list
	local pkg
	local deps
	pkg=$1
	shift
	[ -n "$1" ] || return
	list=""
	# Filter out already processed deps
	for i in $@; do
		case " $ALL_DEPS" in
		*\ $i\ *);;
		*) list="$list $i";;
		esac
	done
	ALL_DEPS="$ALL_DEPS$list "
	for i in $list; do
		[ -f $i/receipt ] || continue
		deps="$(DEPENDS=""; . $i/receipt; echo $DEPENDS)"
		case " $deps " in
		*\ $pkg\ *) echo -e "$MSG  $i"; MSG="";;
		*) check_for_deps_loop $pkg $deps;;
		esac
	done
}

# Check for missing deps listed in a receipt packages.
check_for_deps()
{
	local saved;
	saved=$PACKAGE
	mkdir -p $TMP_DIR
	{ cd $TMP_DIR ; cpio --quiet -i receipt > /dev/null 2>&1; } < $PACKAGE_FILE
	. $TMP_DIR/receipt
	PACKAGE=$saved
	rm -rf $TMP_DIR

	num=0
	for pkgorg in $DEPENDS
	do
		i=$(equivalent_pkg $pkgorg $1)
		if [ ! -d "$1$INSTALLED/$i" ]; then
			MISSING_PACKAGE=$i
			num=$(($num+1))
		elif [ ! -f "$1$INSTALLED/$i/receipt" ]; then
			eval_gettext "WARNING Dependency loop between \$PACKAGE and \$i."; echo
		fi
	done

	if [ ! "$MISSING_PACKAGE" = "" ]; then
		title "$(eval_gettext 'Tracking dependencies for: $PACKAGE')"
		for pkgorg in $DEPENDS
		do
			i=$(equivalent_pkg $pkgorg $1)
			if [ ! -d "$1$INSTALLED/$i" ]; then
				MISSING_PACKAGE=$i
				eval_gettext "Missing: \$MISSING_PACKAGE"; echo
			fi
		done
		separator
		eval_ngettext \
			"\$num missing package to install." \
			"\$num missing packages to install." $num; echo
	fi
}

# Install all missing deps. Auto install or ask user then install all missing
# deps from local dir, cdrom, media or from the mirror. In case we want to
# install packages from local, we need a packages.list to find the version.
install_deps()
{
	local root
	root=""
	[ -n "$1" ] && root="--root=$1"
	if [ "$AUTO_INSTALL_DEPS" == "yes" ]; then
		answer=0
	else
		newline
		gettext "Install all missing dependencies" && confirm
		answer=$?
		newline
	fi
	if [ $answer = 0 ]; then
		for pkgorg in $DEPENDS
		do
			pkg=$(equivalent_pkg $pkgorg $1)
			if [ ! -d "$1$INSTALLED/$pkg" ]; then
				local list
				list="$INSTALL_LIST"
				[ -n "$list" ] || list="$TOP_DIR/packages.list"
				# We can install packages from a local dir by greping
				# the TAZPKG_BASENAME in the local packages.list.
				found=0
				if [ -f "$list" ]; then
					eval_gettext "Checking if \$pkg exists in local list..."; echo
					mkdir $TMP_DIR
					for i in $pkg-*.tazpkg; do
						[ -f $i ] || continue
						{ cd $TMP_DIR ; cpio --quiet -i receipt > /dev/null 2>&1; } < $i
						[ "$(. $TMP_DIR/receipt; echo $PACKAGE)" = "$pkg" ] || continue
						if grep -q ^$(package_fullname_in_dir $TMP_DIR).tazpkg$ $list
						then
							found=1
							tazpkg install $i $root --list=$list
							break
						fi
					done
					rm -rf $TMP_DIR
				fi
				# Install deps from the mirror.
				if [ $found -eq 0 ]; then
					if [ ! -f "$LOCALSTATE/packages.list" ]; then
						tazpkg recharge
					fi
					tazpkg get-install $pkg $root
				fi
			fi
		done
	else
		newline
		eval_gettext \
"Leaving dependencies for \$PACKAGE unresolved.
The package is installed but will probably not work."; echo
		newline
	fi
}

# Search pattern in installed packages.
search_in_installed_packages()
{
	gettext "Installed packages"; echo
	separator
	list=`ls -1 $INSTALLED | grep -i "$PATTERN"`
	num=0
	for pkg in $list
	do
		EXTRAVERSION=""
		[ -f $INSTALLED/$pkg/receipt ] || continue
		. $INSTALLED/$pkg/receipt
		emsg "$PACKAGE<i 24> $VERSION$EXTRAVERSION<i 42> $(gettext $CATEGORY)"
		num=$(($num+1))
	done
	# Set correct ending messages.
	if [ x$num == x ]; then
		eval_gettext "No installed packages found for: \$PATTERN"; echo
		newline
	else
		footer "$(eval_ngettext \
			'$num installed package found for: $PATTERN' \
			'$num installed packages found for: $PATTERN' $num)"
	fi
}

# Search in packages.list for available pkgs.
search_in_packages_list()
{
	gettext "Available packages name-version"; echo
	separator
	num=0
	for i in $LOCALSTATE/packages.list $LOCALSTATE/undigest/*/packages.list; do
		grep -is "$PATTERN" $i
		num=$(($num + `grep -is "$PATTERN" $i | wc -l`))
	done
	if [ ! -f "$LOCALSTATE/packages.list" ]; then
		newline
		gettext \
"No 'packages.list' found to check for mirrored packages. For more results,
please run 'tazpkg recharge' once as root before searching."; echo
		newline
	fi
	if [ "$num" = "0" ]; then
		eval_gettext "No available packages found for: \$PATTERN"; echo
		newline
	else
		footer "$(eval_ngettext \
			'$num available package found for: $PATTERN' \
			'$num available packages found for: $PATTERN' $packages)"
	fi
}

# search --mirror: Search in packages.txt for available pkgs and give more
# info than --list or default.
search_in_packages_txt()
{
	gettext "Matching packages name with version and desc"; echo
	separator
	num=0
	for i in $LOCALSTATE/packages.txt $LOCALSTATE/undigest/*/packages.txt; do
		grep -is -A 2 "^$PATTERN" $i
		num=$(($num + `grep -is "^$PATTERN" $i | wc -l`))
	done
	if [ ! -f "$LOCALSTATE/packages.txt" ]; then
		newline
		gettext \
"No 'packages.txt' found to check for mirrored packages. For more results,
please run 'tazpkg recharge' once as root before searching."; echo
		newline
	fi
	if [ "$num" = "0" ]; then
		eval_gettext "No available packages found for: \$PATTERN"; echo
		newline
	else
		footer "$(eval_ngettext \
			'$num available package found for: $PATTERN' \
			'$num available packages found for: $PATTERN' $packages)"
	fi
}

# Install package-list from a flavor
install_flavor()
{
	check_root $@

	# Get repositories priority list.
	look_for_priority

	FLAVOR=$1
	ARG=$2
	mkdir -p $TMP_DIR
	[ -f $FLAVOR.flavor ] && cp $FLAVOR.flavor $TMP_DIR
	cd $TMP_DIR
	if [ -f $FLAVOR.flavor ] || download $FLAVOR.flavor; then
		zcat < $FLAVOR.flavor | cpio --quiet -i >/dev/null
		while read file; do
			for pkg in $(ls -d $INSTALLED/${file%%-*}*); do
				[ -f $pkg/receipt ] || continue
				EXTRAVERSION=""
				. $pkg/receipt
				[ "$PACKAGE-$VERSION$EXTRAVERSION" = "$file" ] && break
			done
			[ "$PACKAGE-$VERSION$EXTRAVERSION" = "$file" ] && continue
			cd $CACHE_DIR
			download $file.tazpkg
			cd $TMP_DIR
		        tazpkg install $CACHE_DIR/$file.tazpkg --forced
		done < $FLAVOR.pkglist
		[ -f $FLAVOR.nonfree ] && while read pkg; do
			[ -d $INSTALLED/$pkg ] || continue
			[ -d $INSTALLED/get-$pkg ] && tazpkg get-install get-$pkg
			get-$pkg
		done < $FLAVOR.nonfree
		[ "$ARG" == "--purge" ] && for pkg in $(ls $INSTALLED); do
			[ -f $INSTALLED/$pkg/receipt ] || continue
			EXTRAVERSION=""
			. $INSTALLED/$pkg/receipt
			grep -q ^$PACKAGE-$VERSION$EXTRAVERSION$ $FLAVOR.pkglist && continue
			grep -qs ^$PACKAGE$ $FLAVOR.nonfree && continue
			tazpkg remove $PACKAGE
		done
	else
		eval_gettext "Can't find flavor \$FLAVOR. Abort."; echo
	fi
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# Update mirror urls
setup_mirror()
{
	# Backup old list.
	if [ -f "$1/mirror" ]; then
		cp -f $1/mirror $1/mirror.bak
	fi
	title 'Current mirror(s)'
	echo "  `cat $1/mirror 2> /dev/null`"
	gettext \
"Please enter URL of the new mirror (http, ftp or local path). You must specify
the complete address to the directory of the packages and packages.list file."; echo
	newline
	gettext "New mirror(s) URL: "
	NEW_MIRROR_URL=$2
	if [ -n "$NEW_MIRROR_URL" ]; then
		echo $NEW_MIRROR_URL
	else
		read NEW_MIRROR_URL
	fi
	if [ "$NEW_MIRROR_URL" = "" ]; then
		gettext "Nothing has been changed."; echo
	else
		eval_gettext "Setting mirror(s) to: \$NEW_MIRROR_URL"; echo
		rm -f $1/mirror
		for i in $NEW_MIRROR_URL; do
			echo "$i" >> $1/mirror
		done
	fi
	newline
}

# recursive dependencies scan
dep_scan()
{
for i in $1; do
	case " $ALL_DEPS " in
	*\ $i\ *) continue;;
	esac
	ALL_DEPS="$ALL_DEPS $i"
	[ -n "$2" ] && echo "$2$i ($(fgrep -A 3 $i $LOCALSTATE/packages.txt \
				     | tail -1 | sed 's/.*(\([^ ]*\).*/\1/'))"
	[ -f $i/receipt ] || continue
	DEPENDS=""
	. $i/receipt
	[ -n "$DEPENDS" ] && dep_scan "$DEPENDS" "$2  "
done
}

# recursive reverse dependencies scan
rdep_scan()
{
SEARCH=$1

for i in * ; do
	DEPENDS=""
	. $i/receipt
	echo "$i $(echo $DEPENDS)"
done | busybox awk -v search=$SEARCH '
function show_deps(deps, all_deps, pkg, space)
{
	if (all_deps[pkg] == 1) return
	all_deps[pkg] = 1
	if (space != "") printf "%s %s\n",space,pkg
	for (i = 1, n = split(deps[pkg], mydeps, " "); i <= n; i++) {
		show_deps(deps, all_deps, mydeps[i],"==" space)
	}
}

{
	all_deps[$1] = 0
	for (i = 2; i <= NF; i++)
		deps[$i] = deps[$i] " " $1
}

END {
	show_deps(deps, all_deps, search, "")
}
' | while read spc pkg; do
	echo -n $spc | sed 's/=/ /g'
	echo -n $pkg
	echo -n ' ('
	fgrep -A 3 $pkg $LOCALSTATE/packages.txt  | tail -1 | \
				    sed 's/.*(\([^ ]*\).*/\1)/'
done
}

# Check for ELF file
is_elf()
{
	[ "$(dd if=$1 bs=1 skip=1 count=3 2> /dev/null)" = "ELF" ]
}

# Print shared library dependencies
ldd()
{
	LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $1 2> /dev/null
}

# search dependencies for files in $TMP_DIR/$file/fs
find_depends()
{
	DEFAULT_DEPENDS="glibc-base gcc-lib-base"

	[ -n "$TMPLOCALSTATE" ] || TMPLOCALSTATE=$LOCALSTATE
	[ -f $TMPLOCALSTATE/files.list.lzma ] || tazpkg recharge > /dev/null
	for i in $TMPLOCALSTATE/files.list.lzma \
 		$TMPLOCALSTATE/undigest/*/files.list.lzma ; do
 		[ -f $i ] && lzma d $i -so >> $TMP_DIR/files.list
	done
	find $TMP_DIR/$file/fs -type f | while read chkfile ; do
		is_elf $chkfile || continue
		case "$chkfile" in
		*.o|*.ko|*.ko.gz) continue;;
		esac
		ldd $chkfile | while read lib rem; do
			case "$lib" in
			statically|linux-gate.so*|ld-*.so|*/ld-*.so)
				continue;;
			esac
			find $TMP_DIR/$file/fs | grep -q /$lib$ && continue
			for dep in $(fgrep $lib files.list | cut -d: -f1); do
				case " $DEFAULT_DEPENDS " in
				*\ $dep\ *) continue 2;;
				esac
				grep -qs "^$dep$" $TMP_DIR/depends && continue 2
			done
			if [ -n "$dep" ]; then
				echo "$dep" >> $TMP_DIR/depends
			else
				grep -qs ^$lib$ $TMP_DIR/unresolved ||
				echo "$lib" >> $TMP_DIR/unresolved
			fi
		done
	done
	spc=""
	sort < $TMP_DIR/depends 2> /dev/null | uniq | while read file; do
		echo -n "$spc$file"
		spc=" "
	done
}

show_unresolved_lib()
{
	if [ -s $TMP_DIR/unresolved ]; then
		echo -e "BUGS=\"`gettext \"No dependency for\"`" >> $1
		sort < $TMP_DIR/unresolved | uniq | while read file; do
			eval_gettext "WARNING: unknown dependency for \$lib"; echo
			echo -n " $file" >> $1
		done
		echo "\"" >> $1
	fi
}

# convert a .ipk package to .tazpkg
convert_ipk()
{
	mkdir -p $TMP_DIR
	tar xOzf $PACKAGE_FILE ./control.tar.gz | tar xzf - -C $TMP_DIR
	package="$(grep ^Package $TMP_DIR/control | sed 's/.*: //')"
	version="$(grep ^Version $TMP_DIR/control | sed 's/.*: //')"
	maintainer="$(grep ^Maintainer $TMP_DIR/control | sed 's/.*: //')"
	target="$(grep ^Architecture $TMP_DIR/control | sed 's/.*: //')"
	descrip="$(grep ^Description $TMP_DIR/control | sed 's/.*: //')"
	url="http://openwrt.org/"
	case "$target" in
	i386|all)
		file=$package-$version
		mkdir -p $TMP_DIR/$file/fs
		tar xOzf $PACKAGE_FILE ./data.tar.gz | \
			tar xzf - -C $TMP_DIR/$file/fs
		cd $TMP_DIR
		cat > $file/receipt <<EOT
# SliTaz package receipt.
# generated by tazpkg from package $(basename $PACKAGE_FILE)
PACKAGE="$package"
VERSION="$version"
CATEGORY="misc"
SHORT_DESC="$descrip"
WEB_SITE="$url"
MAINTAINER="$maintainer"
DEPENDS="$(find_depends)"
EOT
		[ -s conffiles ] && cat >> $file/receipt <<EOT
CONFIG_FILES="$(cat conffiles)"
EOT
		show_unresolved_lib $file/receipt
		while read script func; do
			[ -s $script ] && cat >> $file/receipt <<EOT

$func()
{
$(cat $script)
}
EOT
		done <<EOT
preinst		pre_install
postinst	post_install
prerm		pre_remove
postrm		post_remove
EOT
		awk '
{
   if (/^ / && show) print substr($0,2);
   else show=0;
   if (/^Description/) show=1;
}' < $TMP_DIR/control > $file/description.txt
		sed -i 's/^\.$//' $file/description.txt
		[ -s $file/description.txt ] || rm -f $file/description.txt
		tazpkg pack $file
		cd $TOP_DIR
		mv $TMP_DIR/$file.tazpkg .
		;;
	*)
		eval_gettext "Invalid target: \$target (expected i386)"; echo
		;;
	esac
	rm -rf $TMP_DIR
}

# convert a .pkg.tar.gz/.pkg.tar.xz/.apk package to .tazpkg
convert_arch()
{
	mkdir -p $TMP_DIR/fs
	busybox tar xf $PACKAGE_FILE -C $TMP_DIR/fs
	if [ -f $TMP_DIR/fs/.PKGINFO ]; then
		cd $TMP_DIR
		package="$(grep ^pkgname fs/.PKGINFO | sed 's/.*= //')"
		version="$(grep ^pkgver  fs/.PKGINFO | sed 's/.*= //')"
		descrip="$(grep ^pkgdesc fs/.PKGINFO | sed 's/.*= //')"
		url="$(grep ^url fs/.PKGINFO | sed 's/.*= //')"
		maintainer="$(grep ^packager fs/.PKGINFO | sed 's/.*= //')"
		file=$package-$version
		mkdir $file
		mv fs $file
		cat > $file/receipt <<EOT
# SliTaz package receipt.
# generated by tazpkg from Archlinux package $(basename $PACKAGE_FILE)
PACKAGE="$package"
VERSION="$version"
CATEGORY="misc"
SHORT_DESC="$descrip"
WEB_SITE="$url"
MAINTAINER="$maintainer"
DEPENDS="$(find_depends)"
EOT
		show_unresolved_lib $file/receipt
		rm -f $file/fs/.[A-Z]*
		tazpkg pack $file
		mv $file.tazpkg $TOP_DIR
	else
		eval_gettext "\$PACKAGE_FILE does not look like an Archlinux/Alpine package!"; echo
	fi
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# get package and version from PACKAGE_FILE
parse_pkgname()
{
	package=$(basename ${2:-$PACKAGE_FILE} $1)
	IFS='-'
	set -- $package
	unset IFS
	package=$1
	version=$2
	if [ -z "$version" ]; then
		package=${1%%[0-9\.]*}
		version=${1#$package}
	fi
	file="$package-$version"
	mkdir $file
}

finish_convert_puppy()
{
	rm -f fs/*.specs
	mv fs $file
	cat > $file/receipt <<EOT
# SliTaz package receipt.
# generated by tazpkg from puppy package $(basename $PACKAGE_FILE)
PACKAGE="$package"
VERSION="$version"
CATEGORY="misc"
SHORT_DESC="$desc"
WEB_SITE="http://puppylinux.org/"
MAINTAINER="nobody@slitaz.org"
DEPENDS="$(find_depends)"
EOT
	show_unresolved_lib $file/receipt
	for i in install uninstall ; do
		[ -f $file/fs/p$i.sh ] && cat >> $file/receipt <<EOM

post_$i()
{
chroot \$1/ sh - << EOT
cd /
$(sed -e 's/\\/\\\\/g' -e 's/\$/\\$/g' < $file/fs/p$i.sh ; rm -f $file/fs/p$i.sh)
EOT
}
EOM
	done
	sed -i 's/post_uninstall/post_remove/' $file/receipt
	tazpkg pack $file
	mv $file.tazpkg $TOP_DIR
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# convert a .sfs package to .tazpkg
convert_sfs()
{
	[ -n "$(which unsquashfs)" ] || tazpkg get-install squashfs 
	if ! unsquashfs -l $PACKAGE_FILE | grep -q squashfs-root/pet.specs$ ; then
		eval_gettext "\$PACKAGE_FILE does not look like a Puppy package!"; echo
		return 1
	fi
	mkdir -p $TMP_DIR 
	cd $TMP_DIR
	parse_pkgname .sfs
	unsquashfs $PACKAGE_FILE
	mv squashfs-root fs
	set -- $(cat fs/pet.specs)
	desc="$10"
	finish_convert_puppy
}

# convert a .pet package to .tazpkg
convert_pet()
{
	mkdir -p $TMP_DIR 
	cd $TMP_DIR
	parse_pkgname .pet
	tar xzf $PACKAGE_FILE 2> /dev/null
	. $package*/*.specs
	desc="$PETMENUDESCR"
	mv $package*/ fs
	finish_convert_puppy
}

# convert a .sb package to .tazpkg
convert_sb()
{
	[ -n "$(which unsquashfs)" ] || tazpkg get-install squashfs 
	if ! unsquashfs -l $PACKAGE_FILE | grep -q squashfs-root/var/log/removed_scripts ; then
		eval_gettext "\$PACKAGE_FILE does not look like a Slax package!"; echo
		return 1
	fi
	mkdir -p $TMP_DIR 
	cd $TMP_DIR
	unsquashfs $PACKAGE_FILE
	mv squashfs-root fs
	parse_pkgname '' $(cat fs/var/log/packages/* | sed '/PACKAGE NAME:/!d;s/.*: *//')
	desc="$(cat fs/var/log/packages/* | sed '/^PACKAGE DESCRIPTION:/,$!d;N;s/.*: //;q')"
	cat fs/var/log/packages/* | sed '/^PACKAGE DESCRIPTION:/,/FILE LIST/!d;s/.*://;/^$/d' > $file/description.txt
	mv fs $file
	cat > $file/receipt <<EOT
# SliTaz package receipt.
# generated by tazpkg from slax package $(basename $PACKAGE_FILE)
PACKAGE="$package"
VERSION="$version"
CATEGORY="misc"
SHORT_DESC="$desc"
WEB_SITE="http://www.slax.org/"
MAINTAINER="nobody@slitaz.org"
DEPENDS="$(find_depends)"
EOT
	show_unresolved_lib $file/receipt
	[ -f $file/fs/var/log/scripts/$package* ] && cat >> $file/receipt <<EOM

post_install()
{
chroot \$1/ sh - << EOT
cd /
$(cat $file/fs/var/log/scripts/$package* | sed -e 's/\\/\\\\/g' | sed -e 's/\$/\\$/g')
EOT
}
EOM
	tazpkg pack $file
	mv $file.tazpkg $TOP_DIR
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# convert a .tgz package to .tazpkg
convert_tgz()
{
	parse_pkgname
	mkdir -p $TMP_DIR/$file/fs
	tar xzf $PACKAGE_FILE -C $TMP_DIR/$file/fs
	cd $TMP_DIR
	if [ -d $file/fs/install ]; then
		descrip=$(grep ^$package $file/fs/install/slack-desc | \
			  head -1 | sed 's/.*(\(.*\)).*/\1/')
		cat > $file/receipt <<EOT
# SliTaz package receipt.
# generated by tazpkg from slackware package $(basename $PACKAGE_FILE)
PACKAGE="$package"
VERSION="$version"
CATEGORY="misc"
SHORT_DESC="$descrip"
WEB_SITE="http://www.slackware.com/packages/"
MAINTAINER="nobody@slitaz.org"
DEPENDS="$(find_depends)"
EOT
		show_unresolved_lib $file/receipt
		[ -f $file/fs/install/doinst.sh ] && cat >> $file/receipt <<EOM

post_install()
{
chroot \$1/ sh - << EOT
cd /
$(sed -e 's/\\/\\\\/g' -e 's/\$/\\$/g' < $file/fs/install/doinst.sh)
EOT
}
EOM
		grep ^$package $file/fs/install/slack-desc | \
			sed "s/^$package://" > $file/description.txt
		[ -s $file/description.txt ] || rm -f $file/description.txt
		rm -rf $file/fs/install
		tazpkg pack $file
		mv $file.tazpkg $TOP_DIR
	else
		eval_gettext "\$PACKAGE_FILE does not look like a Slackware package!"; echo
	fi
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# convert a .deb package to .tazpkg
convert_deb()
{
	mkdir -p $TMP_DIR
	dpkg-deb -e $PACKAGE_FILE $TMP_DIR
	package=$(grep '^ *Package:' $TMP_DIR/control)
	package=$(echo ${package##*:})
	version=$(grep '^ *Version:' $TMP_DIR/control)
	version=$(echo ${version##*:})
	descrip=$(grep '^ *Description:' $TMP_DIR/control)
	descrip=$(echo ${descrip##*:})
	target="$(grep ^Architecture $TMP_DIR/control | sed 's/.*: //')"
	case "$target" in
	i386|all)
		file="$package-$version"
		mkdir -p $TMP_DIR/$file/fs/
		dpkg-deb -x $PACKAGE_FILE $TMP_DIR/$file/fs
		cd $TMP_DIR
		cat > $file/receipt <<EOT
# SliTaz package receipt.
# generated by tazpkg from debian package $(basename $PACKAGE_FILE)
PACKAGE="$package"
VERSION="$version"
CATEGORY="misc"
SHORT_DESC="$descrip"
WEB_SITE="http://packages.debian.org/search?keywords=$package"
MAINTAINER="nobody@slitaz.org"
EOT
		LICENSE="$(grep /usr/share/common-licenses/ \
		  $PACKAGE_FILE $TMP_DIR/$file/fs/usr/share/doc/*/copyright | \
		  sed 's|.*common-licenses/\([-\.A-Za-z0-9]*\).*|\1|;s|[-\.]$||q')"
		[ -n "$LICENSE" ] && echo "LICENSE=\"$LICENSE\"" >> $file/receipt
		echo "DEPENDS=\"$(find_depends)\"" >> $file/receipt
		[ -s conffiles ] && cat >> $file/receipt <<EOT
CONFIG_FILES="$(cat conffiles)"
EOT
		show_unresolved_lib $file/receipt
		awk '
{
   if (/^ / && show) print substr($0,2);
   else show=0;
   if (/^Description/) show=1;
}' < $TMP_DIR/control > $file/description.txt
		sed -i 's/^\.$//' $file/description.txt
		[ -s $file/description.txt ] || rm -f $file/description.txt
		tazpkg pack $file
		mv $file.tazpkg $TOP_DIR
		;;
	*)
		eval_gettext "Invalid target: \$target (expected i386)"; echo
		;;
	esac
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

# convert a .rpm package to .tazpkg
convert_rpm()
{
	mkdir -p $TMP_DIR
	cp $PACKAGE_FILE $TMP_DIR
	PACKAGE_FILE=$TMP_DIR/$(basename $PACKAGE_FILE)
	rpm -qip $PACKAGE_FILE | awk -v pkg=$(basename $PACKAGE_FILE) '
BEGIN {
	goturl=0;
	printf "# SliTaz package receipt.\n";
	printf "# generated by tazpkg from rpm package %s\n",pkg;
}
{
	if (/^Name/) { name=$3; printf "PACKAGE=\"%s\"\n",$3; }
	if (/^Version/) printf "VERSION=\"%s-",$3;
	if (/^Release/) printf "%s\"\n",$3;
	if (/^Summary/) printf "SHORT_DESC=\"%s\"\n",substr($0,15);
	if (/^URL/) { goturl=1; printf "WEB_SITE=\"%s\"\n",$3; }
}
END {
	if (goturl == 0)
		printf "WEB_SITE=\"http://rpmfind.net/linux/rpm2html/search.php?query=%s\"\n",name;
	printf "CATEGORY=\"misc\"\n";
	printf "MAINTAINER=\"nobody@slitaz.org\"\n";
}
' > $TMP_DIR/receipt
	. $TMP_DIR/receipt
	file=$PACKAGE-$VERSION
	mkdir -p $TMP_DIR/$file/fs/
	mv $TMP_DIR/receipt $TMP_DIR/$file
	rpm -qip $PACKAGE_FILE | awk '
BEGIN { show=0 }
{
  if (show) print;
  if (/^Description/) show=1;
}
' > $TMP_DIR/$file/description.txt
	cd $TMP_DIR/$file/fs/
	rpm2cpio $PACKAGE_FILE | cpio -idm --quiet
	# rpm2cpio can't extract some new RPMs
	if [ $? == 1 ]; then
		if [ ! -e $INSTALLED/p7zip-full/receipt ]; then
			boldify $(gettext "Unable to extract the RPM using standard tools (rpm2cpio).")
			gettext "Do you want to install 'p7zip-full' package" && confirm
			if [ $? = 0 ]; then
				tazpkg -gi p7zip-full
			else
				rm -rf $TMP_DIR
				exit 1
			fi
		fi
		# we may already have 7z or may have just installed it in the step above
		if [ -e $INSTALLED/p7zip-full/receipt ]; then
			7z x -so $PACKAGE_FILE | cpio -idm --quiet
		fi
	fi
	cd ../..
	echo "DEPENDS=\"$(find_depends)\"" >> $TMP_DIR/$file/receipt
	show_unresolved_lib $TMP_DIR/$file/receipt
	tazpkg pack $file
	mv $file.tazpkg $TOP_DIR
	cd $TOP_DIR
	rm -rf $TMP_DIR
}

update_desktop_database()
{
	if [ -f $1/usr/bin/update-desktop-database ] && [ -n "$updatedesktopdb" ]; then
		chroot "$1/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
	fi
}

update_mime_database()
{
	if [ -f $1/usr/bin/update-mime-database ] && [ -n "$updatemimedb" ]; then
		chroot "$1/" /usr/bin/update-mime-database /usr/share/mime
	fi
}

update_icon_database()
{
	if [ -f $1/usr/bin/gtk-update-icon-cache ] && [ -n "$updateicondb" ]; then
		chroot "$1/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
	fi
}

compile_glib_schemas()
{
	if [ -f $1/usr/bin/glib-compile-schemas ] && [ -n "$compile_schemas" ]; then
		chroot "$1/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
	fi
}

update_kernel_modules()
{
	if [ -f $1/sbin/depmod ] && [ -n "$updatedepmod" ]; then
		chroot "$1/" /sbin/depmod -a
	fi
}

###################
# TazPKG commands #
###################

case "$COMMAND" in
	list|-l)
		shift
		# List all installed packages or a specific category.
		if [ "$1" = "blocked" ]; then
			title 'Blocked packages'
			if [ -s "$BLOCKED" ];then
				cat $BLOCKED
			else
				gettext "No blocked packages found."; echo
			fi
			newline && exit 0
		fi
		# Display the list of categories.
		if [ "$1" = "cat" -o "$1" = "categories" ]; then
			title 'Packages categories'
			num=0
			for i in $PKGS_CATEGORIES
			do
				gettext $i; echo
				num=$(($num+1))
			done
			footer "$(eval_ngettext '$num category' '$num categories' $num)"; echo
			exit 0
		fi
		# Check for an asked category.
		ASKED_CATEGORY_I18N="$@"
		if [ -n "$ASKED_CATEGORY_I18N" ]; then
			ASKED_CATEGORY=$(reverse_translate_category "$ASKED_CATEGORY_I18N")
			title 'Installed packages of category: $ASKED_CATEGORY_I18N'
			for pkg in $INSTALLED/*
			do
				[ -f $pkg/receipt ] || continue
				EXTRAVERSION=""
				. $pkg/receipt
				if [ "$CATEGORY" == "$ASKED_CATEGORY" ]; then
					echo -e "$PACKAGE\033[24G $VERSION$EXTRAVERSION"
					packages=$(($packages+1))
				fi
			done
			num="<c 32>$packages</c>"; cat_name="<c 34>$ASKED_CATEGORY_I18N</c>"
			footer "$(emsg $(eval_ngettext \
				'$num package installed of category $cat_name.' \
				'$num packages installed of category $cat_name.' \
				$packages))"; echo
		else
			# By default list all packages and versions.
			title 'List of all installed packages'
			for pkg in $INSTALLED/*
			do
				[ -f $pkg/receipt ] || continue
				EXTRAVERSION=""
				. $pkg/receipt
				echo -e "$PACKAGE\033[35G $VERSION$EXTRAVERSION\033[53G $(gettext $CATEGORY)"
				packages=$(($packages+1))
			done
			num="<c 32>$packages</c>"
			footer "$(emsg $(eval_ngettext \
				'$num package installed.' \
				'$num packages installed.' $packages))"
		fi ;;
	list-mirror|-lm)
		# List all available packages on the mirror. Option --diff displays
		# last mirrored packages diff (see recharge).
		check_for_packages_list
		case $2 in
			--diff)
				if [ -f "$LOCALSTATE/packages.diff" ]; then
					title 'Mirrored packages diff'
					cat $LOCALSTATE/packages.diff
					num=$(wc -l < $LOCALSTATE/packages.diff)
					footer "$(eval_ngettext \
						'$num new package listed on the mirror.' \
						'$num new packages listed on the mirror.' $num)"
				else
					newline
					gettext "Unable to list anything, no packages.diff found."; echo
					gettext "Recharge your current list to create a first diff."; echo
					newline
				fi && exit 0 ;;
			--text|--txt|--raw|*)
				title 'List of available packages on the mirror'
				cat $LOCALSTATE/packages.txt ;;
		esac
		pkgs=$(wc -l < $LOCALSTATE/packages.list)
		num=$(emsg "<c 32>$pkgs</c>")
		footer "$(eval_ngettext \
			'$num package in the last recharged list.' \
			'$num packages in the last recharged list.' $pkgs)"
		;;
	list-files|-lf)
		# List files installed with the package.
		check_for_package_on_cmdline
		check_for_receipt
		title 'Installed files by: $PACKAGE'
		sort < $INSTALLED/$PACKAGE/files.list
		files=$(wc -l < INSTALLED/$PACKAGE/files.list)
		num=$(emsg "<c 32>$files</c>")
		footer "$(eval_ngettext \
			'$num file installed with $PACKAGE' \
			'$num files installed with $PACKAGE' $files)"
		;;
	info)
		# Information about package.
		check_for_package_on_cmdline
		check_for_receipt
		EXTRAVERSION=""
		. $INSTALLED/$PACKAGE/receipt
		title 'TazPKG information'
		# Display localized short description
		if [ -e "$LOCALSTATE/packages-desc.$LANG" ]; then
			LOCDESC=$(grep -e "^$PACKAGE	" $LOCALSTATE/packages-desc.$LANG | cut -d'	' -f2)
			[ "x$LOCDESC" != "x" ] && SHORT_DESC="$LOCDESC"
		fi
		emsg "\
<b>$(gettext 'Package    :')</b> $PACKAGE
<b>$(gettext 'Version    :')</b> $VERSION$EXTRAVERSION
<b>$(gettext 'Category   :')</b> $(gettext $CATEGORY)
<b>$(gettext 'Short desc :')</b> $SHORT_DESC
<b>$(gettext 'Maintainer :')</b> $MAINTAINER"
		[ "$LICENSE" ] && emsg "<b>$(gettext 'License    :')</b> $LICENSE"
		[ "$DEPENDS" ] && emsg "<b>$(gettext 'Depends    :')</b> $DEPENDS"
		[ "$SUGGESTED" ] &&	emsg "<b>$(gettext 'Suggested  :')</b> $SUGGESTED"
		[ "$BUILD_DEPENDS" ] &&	emsg "<b>$(gettext 'Build deps :')</b> $BUILD_DEPENDS"
		[ "$WANTED" ] && emsg "<b>$(gettext 'Wanted src :')</b> $WANTED"
		[ "$WEB_SITE" ] && emsg "<b>$(gettext 'Web site   :')</b> $WEB_SITE"
		footer ;;
	
	desc)
		# Display package description.txt if available.
		if [ -f "$INSTALLED/$PACKAGE/description.txt" ]; then
			title 'Description of: $PACKAGE'
			cat $INSTALLED/$PACKAGE/description.txt
			footer
		else
			newline
			gettext "Sorry, no description available for this package."; echo
			newline
		fi ;;
	
	activity|log|-a)
		# Show activity log
		[ "$nb" ] || nb=18
		title 'TazPKG Activity'
		IFS=" "
		tail -n ${nb} ${LOG} | while read date hour none action none pkg vers none
		do
			case "$action" in
				Installed)
					echo "$date $hour : $(colorize 32 $action) $pkg $vers" ;;
				Removed)
					echo "$date $hour : $(colorize 31 $action) $pkg $vers" ;;
				*)
					echo "$date $hour : $(boldify $action) $pkg $vers" ;;
			esac
		done 
		unset IFS 
		separator && newline ;;
	
	search|-s)
		# Search for a package by pattern or name.
		PATTERN="$2"
		if [ -z "$PATTERN" ]; then
			newline
			gettext "Please specify a pattern or package name to search for."; echo
			echo "$(gettext 'Example:') 'tazpkg search paint'"
			newline
			exit 0
		fi
		title 'Search result for: $PATTERN'
		# Default is to search in installed pkgs and the raw list.
		case "$3" in
			-i|--installed)
				search_in_installed_packages ;;
			-l|--list)
				search_in_packages_list ;;
			-m|--mirror)
				search_in_packages_txt ;;
			*)
				search_in_installed_packages
				search_in_packages_list ;;
		esac ;;
	
	search-file|-sf)
		# Search for a file by pattern or name in all files.list.
		if [ -z "$2" ]; then
			newline
			gettext "Please specify a pattern or file name to search for."; echo
			echo "$(gettext 'Example:') 'tazpkg search-file libnss'"
			newline
			exit 0
		fi
		s_file="$2"
		title 'Search result for file $s_file'

		if [ "$3" == "--mirror" ]; then

			match=0
			for i in $LOCALSTATE/files.list.lzma \
				 $LOCALSTATE/undigest/*/files.list.lzma; do
				 [ -f $i ] || continue
				unlzma -c $i | grep -- ".*:.*$2" | awk '
					BEGIN { last="" }
					{
						pkg=substr($0,0,index($0,":")-1);
						file=substr($0,index($0,":")+2);
						if (last != pkg) {
							last = pkg;
							printf("\n%c[1mPackage %s:%c[0m\n",27,pkg,27);
						}
						printf("%s\n",file);
					}'
				match=$(($match + `unlzma -c $i | grep -- ".*:.*$2" | wc -l`))
			done

		else

			# Check all pkg files.list in search match which specify the package
			# name and the full path to the file(s).
			for pkg in $INSTALLED/*
			do
				if grep -qs "$2" $pkg/files.list; then
					. $pkg/receipt
					newline
					boldify "$(eval_gettext 'Package $PACKAGE:')"
					grep "$2" $pkg/files.list
					files=`grep $2 $pkg/files.list | wc -l`
					match=$(($match+$files))
				fi
			done

		fi
		pkg=$2
		if [ "$match" = "" ]; then
			eval_gettext "0 file found for: \$pkg"; echo
		else
			num=$(emsg "<c 32>$files</c>")
			footer "$(eval_ngettext \
				'$num file found for: $pkg' \
				'$num files found for: $pkg' $match)"
		fi
		;;
	search-pkgname)
		# Search for a package name
		if [ -z "$2" ]; then
			newline
			gettext "Please specify a pattern or file name to search for."; echo
			echo "$(gettext 'Example:') 'tazpkg search-pkgname libnss'"
			newline
			exit 0
		fi
		s_pkg="$2"
		title 'Search result for package $s_pkg'

		# Search for a file on mirror and output only the package name
		match=0
		for i in $LOCALSTATE/files.list.lzma \
				 $LOCALSTATE/undigest/*/files.list.lzma; do
				 [ -f $i ] || continue
				unlzma -c $i | grep -- ".*:.*$2" | cut -d: -f1 | uniq | awk '{ print $1 }'
				match=$(($match + `unlzma -c $i | grep -- ".*:.*$2" | cut -d: -f1 | uniq | wc -l`))
		done
		file=$2
		if [ "$match" = "0" ]; then
			eval_gettext "No file found for: \$file"; echo
			newline
		else
			num=$(emsg "<c 32>$files</c>")
			footer "$(eval_ngettext \
				'$num package found with file: $file' \
				'$num packages found with file: $file' $match)"
		fi
		;;
	install|-i)
		# Install .tazpkg packages.
		check_root $@
		check_for_package_on_cmdline
		check_for_package_file

		[ "$root" ] && ROOT="$root" && check_base_dir "$root"
		[ "$list" ] && INSTALL_LIST="$list"
		if [ "$rootconfig" ]; then
			if [ "$root" ]; then
				CACHE_DIR=$root/$CACHE_DIR
				SAVE_CACHE_DIR=$CACHE_DIR
				LOCALSTATE=$root/$LOCALSTATE
			else
				echo "rootconfig needs --root= option used." >&2
				exit 1
			fi
		fi

		# Get repositories priority list.
		look_for_priority

		# Check if forced install.
		if ! [ "$forced" ]; then
			check_for_installed_package $ROOT
		fi
		install_package $ROOT
		update_desktop_database $ROOT
		update_mime_database $ROOT
		update_icon_database $ROOT
		compile_glib_schemas $ROOT ;;
	install-list|get-install-list)
		# Install a set of packages from a list.
		check_root $@
		if [ -z "$2" ]; then
			newline
			gettext \
"Please change directory (cd) to the packages repository and specify the
list of packages to install. Example: tazpkg install-list packages.list"
			newline && exit 0
		fi
		# Check if the packages list exist.
		list_file=$2
		if [ ! -f "$list_file" ]; then
			eval_gettext "Unable to find: \$list_file"; echo
			exit 0
		else
			LIST=`cat $2`
		fi

		# Remember processed list
		export INSTALL_LIST="$2"

		# Set $COMMAND and install all packages.
		if [ "$1" = "get-install-list" ]; then
			COMMAND=get-install
		else
			COMMAND=install
		fi
		touch $2-processed

		# Upgrade tazpkg first. It may handle new features/formats...
		# then upgrade essential packages early
		for pkg in busybox-pam busybox gcc-lib-base glibc-base \
			   slitaz-base-files tazpkg ; do
			pkg=$(egrep $pkg-[0-9] $INSTALL_LIST)
			[ -n "$pkg" ] || continue
			eval_gettext "Adding implicit depends \$pkg..."; echo
			LIST="$pkg
$LIST"
		done

		for pkg in $LIST
		do
			grep -qs ^$pkg$ $2-processed && continue
			tazpkg $COMMAND $pkg --list=$2 "$3" "$4" "$5"
		done
		rm -f $2-processed ;;
	add-flavor)
		# Install a set of packages from a flavor.
		install_flavor $2 ;;
	install-flavor)
		# Install a set of packages from a flavor and purge other ones.
		install_flavor $2 --purge ;;
	set-release)
		# Change curent release and upgrade packages.
		RELEASE=$2
		if [ -z "$RELEASE" ]; then
			newline
			gettext "Please specify the release you want on the command line."; echo
			echo "$(gettext 'Example:') tazpkg set-release cooking"
			newline
			exit 0
		fi
		rm $LOCALSTATE/mirror
		echo "$RELEASE" > /etc/slitaz-release
		tazpkg recharge && tazpkg upgrade

		# Install missing depends
		cd $INSTALLED
		for i in * ; do
			DEPENDS=""
			. $i/receipt
			for j in $DEPENDS ; do
				[ -d $j ] || tazpkg get-install $j
			done
		done ;;
	remove|-r)
		# Remove packages.
		check_root $@
		check_for_package_on_cmdline

		[ "$root" ] && ROOT="$root"
		if [ ! -f "$ROOT$INSTALLED/$PACKAGE/receipt" ]; then
			newline
			eval_gettext "\$PACKAGE is not installed."; echo
			exit 0
		else
			ALTERED=""
			THE_PACKAGE=$PACKAGE	# altered by receipt
			for i in $(cd $ROOT$INSTALLED ; ls); do
				[ -f $ROOT$INSTALLED/$i/receipt ] || continue
				DEPENDS=""
				. $ROOT$INSTALLED/$i/receipt
				case " $(echo $DEPENDS) " in
				*\ $THE_PACKAGE\ *) ALTERED="$ALTERED $i";;
				esac
			done
			EXTRAVERSION=""
			. $ROOT$INSTALLED/$THE_PACKAGE/receipt
		fi
		newline
		if [ -n "$ALTERED" ]; then
			eval_gettext "The following packages depend on \$PACKAGE:"; echo
			for i in $ALTERED; do
				echo "  $i"
			done
		fi
		REFRESH=$(cd $ROOT$INSTALLED ; grep -sl ^$PACKAGE$ */modifiers)
		if [ -n "$REFRESH" ]; then
			eval_gettext "The following packages have been modified by \$PACKAGE:"; echo
			for i in $REFRESH; do
				echo "  ${i%/modifiers}"
			done
		fi
		if [ "$auto" ]; then
			answer=0
		else
			eval_gettext "Remove \$PACKAGE (\$VERSION\$EXTRAVERSION)" && confirm
			answer=$?
		fi
		if [ $answer = 0 ]; then
			title 'Removing: $PACKAGE'
			# Pre remove commands.
			if grep -q ^pre_remove $ROOT$INSTALLED/$PACKAGE/receipt; then
				pre_remove $ROOT
			fi
			action "Removing all files installed..."
			if [ -f $ROOT$INSTALLED/$PACKAGE/modifiers ]; then
				for file in `cat $ROOT$INSTALLED/$PACKAGE/files.list`
				do
					for mod in `cat $ROOT$INSTALLED/$PACKAGE/modifiers`
					do
						[ -f $ROOT$INSTALLED/$mod/files.list ] && [ $(grep "^$(echo $file | grepesc)$" $ROOT$INSTALLED/$mod/files.list | wc -l) -gt 1 ] && continue 2
					done
					remove_with_path $ROOT$file
				done
			else
				for file in `cat $ROOT$INSTALLED/$PACKAGE/files.list`
				do
					remove_with_path $ROOT$file
				done
			fi
			status
			if grep -q ^post_remove $ROOT$INSTALLED/$PACKAGE/receipt; then
				post_remove $ROOT
			fi
			# Remove package receipt.
			action "Removing package receipt..."
			rm -rf $ROOT$INSTALLED/$PACKAGE
			status
			sed -i "/ $PACKAGE-$VERSION$EXTRAVERSION$/d" \
				$LOCALSTATE/installed.$SUM 2> /dev/null
			# Log this activity
			log_pkg Removed
			if [ "$ALTERED" ]; then
				if [ "$auto" ]; then
					answer=0
				else
					eval_gettext "Remove packages depending on \$PACKAGE" && confirm
					answer=$?
				fi
				if [ $answer = 0 ]; then
					for i in $ALTERED; do
						if [ -d "$ROOT$INSTALLED/$i" ]; then
							tazpkg remove $i $ROOTOPTS
						fi
					done
				fi
			fi
			if [ "$REFRESH" ]; then
				if [ "$auto" ]; then
					answer=0
				else
					eval_gettext "Reinstall packages modified by \$PACKAGE" && confirm
					answer=$?
				fi
				if [ $answer = 0 ]; then
					for i in $REFRESH; do
						if [ $(wc -l < $ROOT$INSTALLED/$i) -gt 1 ]; then
							eval_gettext "Check \$INSTALLED/\$i for reinstallation"; echo
							continue
						fi
						rm -r $ROOT$INSTALLED/$i
						tazpkg get-install ${i%/modifiers} $ROOTOPTS --forced
					done
				fi
			fi
		else
			newline
			eval_gettext "Uninstallation of \$PACKAGE cancelled."; echo
		fi
		newline ;;
	extract|-e)
		# Extract .tazpkg cpio archive into a directory.
		check_for_package_on_cmdline
		check_for_package_file
		title 'Extracting: $PACKAGE'
		# If no directory destination is found on the cmdline
		# we create one in the current dir using the package name.
		if [ -n "$TARGET_DIR" ]; then
			DESTDIR=$TARGET_DIR/$PACKAGE
		else
			DESTDIR=$PACKAGE
		fi
		mkdir -p $DESTDIR
		action "Copying original package..."
		cp $PACKAGE_FILE $DESTDIR
		status
		cd $DESTDIR
		extract_package
		[ -e "receipt" ] && footer "$(eval_gettext '$PACKAGE is extracted to: $DESTDIR')"
		;;
	recompress)
		# Recompress .tazpkg cpio archive with lzma.
		check_for_package_on_cmdline
		check_for_package_file
		title 'Recompressing: $PACKAGE'
		mkdir -p $TMP_DIR
		action "Copying original package..."
		cp $PACKAGE_FILE $TMP_DIR
		status
		cd $TMP_DIR
		extract_package
		action "Recompressing the fs..."
		find fs | cpio -o -H newc --quiet | lzma e fs.cpio.lzma -si
		rm -rf fs
		status
		action "Creating new package..."
		find . -print | cpio -o -H newc --quiet > \
			$TOP_DIR/$(basename $PACKAGE_FILE).$$ && mv -f \
			$TOP_DIR/$(basename $PACKAGE_FILE).$$ \
			$TOP_DIR/$(basename $PACKAGE_FILE)
		status
		cd $TOP_DIR
		rm -rf $TMP_DIR
		separator; newline ;;
	list-config)
		# List configuration files installed.
		if [ "$2" = "--box" ]; then
			mkdir -p $TMP_DIR && cd $TMP_DIR
			FILES="$INSTALLED/*/volatile.cpio.gz"
			[ -n "$3" ] && FILES="$INSTALLED/$3/volatile.cpio.gz"
			for i in $FILES; do
				zcat $i | cpio -idm --quiet > /dev/null
				find * -type f 2>/dev/null | while read file; do
					if [ ! -e /$file ]; then
					echo -en "----------|----|----|`gettext \"File lost\"`"
					else
					echo -n "$(stat -c "%A|%U|%G|%s|" /$file)"
					cmp $file /$file > /dev/null 2>&1 || \
						echo -n "$(stat -c "%.16y" /$file)"
					fi
					echo "|/$file"
				done
				rm -rf *
			done
			cd $TOP_DIR
			rm -rf $TMP_DIR
		else
			title 'Configuration files'
			for i in $INSTALLED/*/volatile.cpio.gz; do
				[ -n "$2" -a "$i" != "$INSTALLED/$2/volatile.cpio.gz" ] && continue
				[ -f "$i" ] || continue
				zcat $i | cpio -t --quiet
			done | sed 's|^|/|' | sort
			separator
			newline
		fi ;;
	repack-config)
		# Create SliTaz package archive from configuration files.
		mkdir -p $TMP_DIR && cd $TMP_DIR
		CONFIG_VERSION=1.0
		mkdir config-$CONFIG_VERSION
		cd config-$CONFIG_VERSION
		for i in $INSTALLED/*/volatile.cpio.gz; do
			zcat $i | cpio -t --quiet
		done > files.list
		mkdir fs
		cd fs
		( cd / ; cpio -o -H newc --quiet ) < ../files.list | cpio -idm --quiet > /dev/null
		mkdir -p etc/tazlito
		for i in $INSTALLED/*/receipt; do
			EXTRAVERSION=""
			. $i
			echo "$PACKAGE-$VERSION$EXTRAVERSION"
		done > etc/tazlito/config-packages.list
		cd ..
		echo "etc/tazlito/config-packages.list" >> files.list
		pkg_date=$(date +"%x %X")
		cat > receipt <<EOT
# SliTaz package receipt.

PACKAGE="config"
VERSION="$CONFIG_VERSION"
CATEGORY="base-system"
SHORT_DESC="$(eval_gettext 'User configuration backup on $pkg_date')"
DEPENDS="$(ls $INSTALLED)"
EOT
		cd ..
		tazpkg pack config-$CONFIG_VERSION
		cp config-$CONFIG_VERSION.tazpkg $TOP_DIR
		cd $TOP_DIR
		rm -rf $TMP_DIR
		;;
	repack)
		# Create SliTaz package archive from an installed package.
		check_for_package_on_cmdline
		check_for_receipt
		EXTRAVERSION=""
		. $INSTALLED/$PACKAGE/receipt
		title 'Repacking: $PACKAGE-$VERSION$EXTRAVERSION.tazpkg'
		if grep -qs ^NO_REPACK= $INSTALLED/$PACKAGE/receipt; then
			eval_gettext "Can't repack \$PACKAGE"; echo
			exit 1
		fi
		if [ -s $INSTALLED/$PACKAGE/modifiers ]; then
			eval_gettext "Can't repack, \$PACKAGE files have been modified by:"; echo
			for i in $(cat $INSTALLED/$PACKAGE/modifiers); do
				echo "  $i"
			done
			exit 1
		fi
		MISSING=""
		while read i; do
			[ -e "$i" ] && continue
			[ -L "$i" ] || MISSING="$MISSING\n  $i"
		done < $INSTALLED/$PACKAGE/files.list
		if [ -n "$MISSING" ]; then
			gettext "Can't repack, the following files are lost:"
			echo -e "$MISSING"
			exit 1
		fi
		mkdir -p $TMP_DIR && cd $TMP_DIR
		FILES="fs.cpio.lzma\n"
		for i in $(ls $INSTALLED/$PACKAGE) ; do
			[ "$i" = "volatile.cpio.gz" ] && continue
			[ "$i" = "modifiers" ] && continue
			cp $INSTALLED/$PACKAGE/$i . && FILES="$FILES$i\n"
		done
		ln -s / rootfs
		mkdir tmp
		sed 's/^/rootfs/' < files.list | cpio -o -H newc --quiet |\
		      { cd tmp ; cpio -idm --quiet >/dev/null; cd ..; }
		mv tmp/rootfs fs
		if [ -f $INSTALLED/$PACKAGE/volatile.cpio.gz ]; then
			zcat $INSTALLED/$PACKAGE/volatile.cpio.gz | \
				{ cd fs; cpio -idm --quiet; cd ..; }
		fi
		if fgrep -q repack_cleanup $INSTALLED/$PACKAGE/receipt; then
			. $INSTALLED/$PACKAGE/receipt
			repack_cleanup fs
		fi
		if [ -f $INSTALLED/$PACKAGE/$CHECKSUM ]; then
			sed 's,  ,  fs,' < $INSTALLED/$PACKAGE/$CHECKSUM | \
			$CHECKSUM -s -c || {
				eval_gettext "Can't repack, \$CHECKSUM error."; echo
				cd $TOP_DIR
				rm -rf $TMP_DIR
				exit 1
			}
		fi
		find fs | cpio -o -H newc --quiet | lzma e fs.cpio.lzma -si
		echo -e "$FILES" | cpio -o -H newc --quiet > \
			$TOP_DIR/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg
		cd $TOP_DIR
		\rm -R $TMP_DIR
		eval_gettext "Package \$PACKAGE repacked successfully."; echo
		pkg_size=$(du -sh $PACKAGE-$VERSION$EXTRAVERSION.tazpkg)
		eval_gettext "Size: \$pkg_size"; echo
		newline ;;
	pack)
		# Create SliTaz package archive using cpio and lzma.
		# TODO: Cook also pack packages, we should share code in libpkg.sh
		check_for_package_on_cmdline
		cd $PACKAGE
		if [ ! -f "receipt" ]; then
			gettext "Receipt is missing. Please read the documentation."; echo
			exit 0
		else
			title 'Packing: $PACKAGE'
			# Create files.list with redirecting find outpout.
			action "Creating the list of files..."
			cd fs
			find . -type f -print > ../files.list
			find . -type l -print >> ../files.list
			cd .. && sed -i s/'^.'/''/ files.list
			status
			action "Creating \$CHECKSUM of files..."
			while read file; do
				[ -L "fs$file" ] && continue
				[ -f "fs$file" ] || continue
				case "$file" in
				/lib/modules/*/modules.*|*.pyc) continue;;
				esac
				$CHECKSUM "fs$file" | sed 's/  fs/  /'
			done < files.list > $CHECKSUM
			status
			UNPACKED_SIZE=$(du -chs fs receipt files.list $CHECKSUM \
				description.txt 2> /dev/null | awk \
				'{ sz=$1 } END { print sz }')
			# Build cpio archives.
			action "Compressing the fs..."
			find fs | cpio -o -H newc --quiet | lzma e fs.cpio.lzma -si
			rm -rf fs
			status
			PACKED_SIZE=$(du -chs fs.cpio.lzma receipt files.list \
				$CHECKSUM description.txt 2> /dev/null | awk \
				'{ sz=$1 } END { print sz }')
			action "Updating receipt sizes..."
			sed -i s/^PACKED_SIZE.*$// receipt
			sed -i s/^UNPACKED_SIZE.*$// receipt
			sed -i "s/^PACKAGE=/PACKED_SIZE=\"$PACKED_SIZE\"\nUNPACKED_SIZE=\"$UNPACKED_SIZE\"\nPACKAGE=/" receipt
			status
			action "Creating full cpio archive..."
			find . -print | cpio -o -H newc --quiet > ../$PACKAGE.tazpkg
			status
			action "Restoring original package tree..."
			unlzma -c fs.cpio.lzma | cpio -idm --quiet
			status
			rm fs.cpio.lzma && cd ..
			footer "$(eval_gettext 'Package $PACKAGE compressed successfully.')"
			pkg_size=$(du -sh $PACKAGE.tazpkg)
			eval_gettext "Size: \$pkg_size"; echo
			newline
		fi ;;
	recharge)
		# Recharge packages.list from a mirror.
		#
		# WARNING: The mirrors file has all SliTaz mirrors but mirror
		# must have only the chosen main mirror.
		#
		check_root $@

		ARG=$2
		if [ "$root" ]; then
			LOCALSTATE=$root$LOCALSTATE
			[ "${2#--}" != "$2" ] && ARG=$3
		fi
		if [ "$ARG" = main ]; then
			repository_to_recharge=$LOCALSTATE
		elif [ "$ARG" ]; then
			if [ -d "$LOCALSTATE/undigest/$ARG" ]; then
				repository_to_recharge=$LOCALSTATE/undigest/$ARG
			else
				repo="$LOCALSTATE/undigest/$ARG"
				eval_gettext "\$repo doesn't exist." >&2
				echo >&2
				exit 1
			fi
		else
			repository_to_recharge="$LOCALSTATE $LOCALSTATE/undigest/*"
		fi
		for path in $repository_to_recharge; do
			[ -f $path/mirror ] || continue
			cd $path

			# Quietly check if recharging is needed.
			[ -f ID ] && mv ID ID.bak
			download_from "$(cat mirror)" ID >/dev/null 2>/dev/null
			if [ -f ID ] && fgrep -q `cat ID.bak 2>/dev/null || echo "null"` ID; then
				if [ "$path" = "$LOCALSTATE" ]; then
					repository_name=Main
				else
					base_path="$(basename $path)"
					repository_name="$(eval_gettext 'Undigest $base_path')"
				fi
				eval_gettext "\$repository_name is up to date."; echo
				rm ID.bak
				continue
			fi

			# Don't let ID be a symlink when using local repository.
			if [ -f ID ]; then
				mv -f ID ID.bak
				cat ID.bak > ID
				rm ID.bak
			fi

			newline
			if [ "$path" != "$LOCALSTATE" ]; then
				base_path="$(basename $path)"
				eval_gettext "Recharging undigest \$base_path:"; echo
			fi

			if [ -f "packages.list" ]; then
				action "Creating backup of the last packages list..."
				for i in packages.desc packages.$SUM packages.txt \
					packages.list packages.equiv files.list.lzma \
					mirrors
				do
					mv -f $i $i.bak 2>/dev/null
				done
				status
			fi

			for i in desc md5 txt list equiv; do
				download_from "$(cat mirror)" packages.$i
			done
			download_from "$(cat mirror)" files.list.lzma
			download_from "$(sed 's|packages/.*||' < mirror)" mirrors

			if [ -f "packages.list.bak" ]; then
				diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
				sed -i s/+// packages.diff
				title 'Mirrored packages diff'
				cat packages.diff
				new_pkgs=$(wc -l < packages.diff)
				if [ "$new_pkgs" != 0 ]; then
					num=$(emsg "<c 32>$new_pkgs</c>")
					footer "$(eval_ngettext \
						'$num new package on the mirror.' \
						'$num new packages on the mirror.' $new_pkgs)"
				else
					gettext "No new packages on the mirror."; echo
					newline
				fi
			else
				footer "$(gettext \
'Last packages.list is ready to use. Note that next time you recharge the
list, a list of differences will be displayed to show new and upgradeable
packages.')"
			fi
		done ;;
	help-up)
		# Options available for the command: up
		newline; usage_up; newline
		exit 1 ;;
	up|upgrade)
		#
		# This is the new way to upgrade packages making 'upgrade' and
		# upgradeable out-of-date. This new way is much, much more faster!
		# Look into installed packages and get data from receipt, it is fast
		# and easy to handle vars after using only md5sum to compare packages
		#
		for opt in $@
		do
			case "$opt" in
				--recharge|-r)
					tazpkg recharge ;;
				--install|-i)
					install="y" ;;
				--check|-c)
					install="n" ;;
			esac
		done
		time=$(date +%s)
		installed_sum=$LOCALSTATE/installed.$SUM
		look_for_priority
		for repo in $priority; do
			pkg_list=$repo/packages.list
			mtime=`find $pkg_list -mtime +7`
			if [ "$mtime" ]; then
				if [ "$repo" = "$LOCALSTATE" ]; then
					repo_name=main
				else
					repo_name="${repo##*/}"
				fi
				eval_gettext "\$pkg_list is older than one week... recharging"; echo
				tazpkg recharge $repo_name
			fi
		done
		emsg "<n><b>$(gettext 'Package')</b><i 28> $(gettext 'Version')<i 48> $(gettext 'Status')<->"
		cd $INSTALLED
		newline > $UP_LIST
		blocked_count=0
		for pkg in *
		do
			unset VERSION EXTRAVERSION
			. $pkg/receipt
			md5=$(fgrep "  $PACKAGE-${VERSION}$EXTRAVERSION.tazpkg" \
				$installed_sum | awk '{print $1}')
			for repo in $priority; do
				pkg_desc=$repo/packages.desc
				pkg_list=$repo/packages.list
				pkg_sum=$repo/packages.$SUM

				if ! fgrep -q "$md5  $PACKAGE-" $pkg_sum; then
					# Jump to next repository in priority if pkg doesn't exists
					# in this one.
					grep -q ^$PACKAGE- $pkg_list || continue

					emsg -n "$PACKAGE<i 28> $VERSION"

					# Skip pkgs listed in $LOCALSTATE/blocked-packages.list
					if $(grep -qs "^$PACKAGE" $BLOCKED); then
						blocked_count=$(($blocked_count + 1))
						emsg "<i 48><c 31> $(gettext 'Blocked')</c>"
						break
					fi

					new=$(grep "^$PACKAGE |" $pkg_desc | awk '{print $3}')

					if [ "$VERSION" == "$new" ]; then
						emsg "<i 48><c 34> $(gettext 'New build')</c>"
					else
						emsg "<i 48><c 32> $(eval_gettext 'New version $new')</c>"
					fi
					echo "$PACKAGE" >> $UP_LIST
					break
				fi
			done
		done
		sed -i /^$/d $UP_LIST
		upnb=$(wc -l < $UP_LIST)
		pkgs=$(ls | wc -l)
		time=$(($(date +%s) - $time))
		if [ "$upnb" = 0 ]; then
			install="n"
			gettext "System is up-to-date..."; echo
		fi
		num=$(emsg "<c 32>$pkgs</c>")
		footer "$(eval_ngettext \
			'$num installed package scanned in ${time}s' \
			'$num installed packages scanned in ${time}s' $pkgs)"
		if [ "$upnb" != 0 ]; then
			if [ "$blocked_count" -gt 0 ]; then
				num=$blocked_count
				blocked=$(eval_ngettext '$num blocked' '$num blocked' $num)
			else
				blocked="$(gettext '0 blocked')"
			fi
			num=$upnb
			boldify "$(eval_ngettext \
				'You have $num available upgrade ($blocked)' \
				'You have $num available upgrades ($blocked)' $num)"
			newline
		fi
		# Pkgs to upgrade ? Skip, let install them all or ask user
		[ "$install" == "n" ] && exit 0
		if [ "$upnb" -gt 0 ]; then
			if [ "$install" == "y" ]; then
				continue
			else
				gettext "Do you wish to install them now: y/n ? "
				read install
			fi
			case "$install" in
				y|Y|yes|YES|Yes)
					for pkg in $(cat $UP_LIST)
					do
						echo 'y' | tazpkg get-install $pkg --forced
					done
					# List is generated each time and must be cleaned so
					# tazpkg-notify don't find upgrade anymore.
					rm $UP_LIST && touch $UP_LIST ;;
				*)
					gettext "Leaving without any upgrades installed."; echo
					newline
					exit 0 ;;
			esac
		fi
		newline ;;
	bugs)
		# Show known bugs in package(s)
		cd $INSTALLED
		shift
		LIST=$@
		[ -n "$LIST" ] || LIST=`ls`
		MSG=$(gettext "No known bugs.")
		for PACKAGE in $LIST; do
			BUGS=""
			EXTRAVERSION=""
			. $PACKAGE/receipt
			if [ -n "$BUGS" ]; then
				MSG=$(gettext "Bug list completed")
				newline
				eval_gettext "Bugs in package \$PACKAGE version \$VERSION\$EXTRAVERSION:"; echo
				cat <<EOT
  $BUGS
EOT
			fi
		done
		echo "$MSG" ;;
	check)
		# Check installed packages set.
		check_root $@

		# Get repositories priority list.
		look_for_priority

		cd $INSTALLED
		for PACKAGE in `ls`; do
			if [ ! -f $PACKAGE/receipt ]; then
				eval_gettext "The package \$PACKAGE installation has not completed"; echo
				continue
			fi
			DEPENDS=""
			EXTRAVERSION=""
			. $PACKAGE/receipt
			if [ -s $PACKAGE/modifiers ]; then
				eval_gettext \
					"The package \$PACKAGE-\$VERSION\$EXTRAVERSION has been modified by:"; echo
				for i in $(cat $PACKAGE/modifiers); do
					echo "  $i"
				done
			fi
			MSG="$(eval_gettext 'Files lost from $PACKAGE-$VERSION$EXTRAVERSION:')\n"
			while read file; do
				[ -e "$file" ] && continue
				if [ -L "$file" ]; then
					MSG="$MSG  $(gettext 'target of symlink')"
				fi
				echo -e "$MSG  $file"
				MSG=""
			done < $PACKAGE/files.list
			MSG="$(eval_gettext 'Missing dependencies for $PACKAGE-$VERSION$EXTRAVERSION:')\n"
			for i in $DEPENDS; do
				[ -d $i ] && continue
				[ -d $(equivalent_pkg $i) ] && continue
				echo -e "$MSG  $i"
				MSG=""
			done
			MSG="$(eval_gettext 'Dependencies loop between $PACKAGE and:')\n"
			ALL_DEPS=""
			check_for_deps_loop $PACKAGE $DEPENDS
		done
		gettext "Looking for known bugs..."; echo
		tazpkg bugs
		if [ "$PACKAGE_FILE" = "--full" ]; then
			for file in */$CHECKSUM; do
				CONFIG_FILES=""
				. $(dirname "$file")/receipt
				[ -s "$file" ] || continue
				while read md5 f; do
					[ -f $f ] || continue
					for i in $CONFIG_FILES; do
						case "$f" in
						$i|$i/*) continue 2;;
						esac
					done
					echo "$md5  $f"
				done < "$file" | $CHECKSUM -c - 2> /dev/null | \
				  grep -v OK$ | sed 's/FAILED$/$CHECKSUM MISMATCH/'
			done
			FILES=" "
			for file in $(cat */files.list); do
				[ -d "$file" ] && continue
				case "$FILES" in *\ $file\ *) continue;; esac
				[ $(grep "^$(echo $file | grepesc)$" */files.list 2> /dev/null | \
					wc -l) -gt 1 ] || continue
				FILES="$FILES$file "
				eval_gettext "The following packages provide \$file:"; echo
				grep -l "^$(echo $file | grepesc)$" */files.list | while read f
				do
					pkg=${f%/files.list}
					if [ -f $pkg/modifiers ]; then
						pkg_list="$(cat $pkg/modifiers)"
						overriders=$(eval_gettext '(overridden by $pkg_list)')
					else
						overriders=''
					fi
					echo -n "  $pkg $overriders"
					newline
				done
			done
			MSG="$(gettext 'No package has installed the following files:')\n"
			find /etc /bin /sbin /lib /usr /var/www \
				-not -type d 2> /dev/null | while read file; do
				case "$file" in *\[*) continue;; esac
				grep -q "^$(echo $file | grepesc)$" */files.list && continue
				echo -e "$MSG  $file"
				MSG=""
			done
		fi
		gettext "Check completed."; echo ;;
	block)
		# Add a pkg name to the list of blocked packages.
		check_root $@
		check_for_package_on_cmdline
		newline
		if grep -qs "^$PACKAGE" $BLOCKED; then
			eval_gettext "\$PACKAGE is already in the blocked packages list."; echo
			newline
			exit 0
		else
			action "Add \$PACKAGE to: \$BLOCKED..."
			echo $PACKAGE >> $BLOCKED
			status
			# Log this activity
			. $INSTALLED/$PACKAGE/receipt
			log_pkg Blocked
		fi
		newline ;;
	unblock)
		# Remove a pkg name from the list of blocked packages.
		check_root $@
		check_for_package_on_cmdline
		newline
		if grep -qs "^$PACKAGE" $BLOCKED; then
			action "Removing \$PACKAGE from: \$BLOCKED..."
			sed -i s/$PACKAGE/''/ $BLOCKED
			sed -i '/^$/d' $BLOCKED
			status
			# Log this activity
			. $INSTALLED/$PACKAGE/receipt
			log_pkg Unblocked
		else
			eval_gettext "\$PACKAGE is not in the blocked packages list."; echo
			newline
			exit 0
		fi
		newline ;;
	get)
		# Download a package with wget.
		check_root $@
		check_for_package_on_cmdline
		check_for_packages_list

		[ "$root" ] && ROOT="$root" && check_base_dir "$root"
		if [ "$rootconfig" ]; then
			if [ "$root" ]; then
				CACHE_DIR=$root/$CACHE_DIR
				SAVE_CACHE_DIR=$CACHE_DIR
				LOCALSTATE=$root/$LOCALSTATE
			else
				gettext "rootconfig needs --root= option used." >&2
				echo >&2
				exit 1
			fi
		fi

		# Get repositories priority list.
		look_for_priority

		CURRENT_DIR=$PWD
		check_for_package_in_list
		cd $CACHE_DIR
		if [ -f "$PACKAGE.tazpkg" ]; then
			eval_gettext "\$PACKAGE already in the cache: \$CACHE_DIR"; echo
			# Check package download was finished
			tail -c 2k $PACKAGE.tazpkg | fgrep -q 00000000TRAILER || {
				eval_gettext "Continuing \$PACKAGE download"; echo
				download $PACKAGE.tazpkg
			}
			if [ "$($CHECKSUM $PACKAGE.tazpkg)" != "$(fgrep "  $PACKAGE.tazpkg" $rep/packages.$SUM)" ]; then
				rm -f $PACKAGE.tazpkg
				download $PACKAGE.tazpkg
			fi
		else
			download $PACKAGE.tazpkg
		fi
		PACKAGE_FILE=$CACHE_DIR/$PACKAGE.tazpkg
		cp -a $PACKAGE_FILE $CURRENT_DIR
		;;
	get-install|-gi)
		# Download and install a package.
		check_root $@
		check_for_package_on_cmdline
		check_for_packages_list

		DO_CHECK=""
		[ "$forced" ] && DO_CHECK=no
		[ "$root" ] && ROOT="$root" && check_base_dir "$root"
		[ "$list" ] && INSTALL_LIST="$list"
		if [ "$rootconfig" ]; then
			if [ "$root" ]; then
				CACHE_DIR=$root/$CACHE_DIR
				SAVE_CACHE_DIR=$CACHE_DIR
				LOCALSTATE=$root/$LOCALSTATE
			else
				gettext "rootconfig needs --root= option used." >&2
				echo >&2
				exit 1
			fi
		fi

		# Get repositories priority list.
		look_for_priority

		AUTOEXEC="no"
		if ! check_for_package_in_list check; then
			CACHE_DIR="${CACHE_DIR%/*}/get"
			[ -d "$CACHE_DIR" ] || mkdir -p $CACHE_DIR
			if download_get_script $PACKAGE /tmp/$PACKAGE.$$ ; then
				install_package_from_get_script /tmp/$PACKAGE.$$ $ROOT
				exit 0
			else
				PACKAGE=get-$PACKAGE
				AUTOEXEC=$PACKAGE
				check_for_package_in_list
				if [ -n "$(get_installed_package_pathname $PACKAGE $ROOT)" ]; then
					$AUTOEXEC $ROOT
					exit 0
				fi
			fi
		fi
		# Check if forced install.
		if ! [ "$forced" ]; then
			check_for_installed_package $ROOT
		fi
		cd $CACHE_DIR
		if [ -f "$PACKAGE.tazpkg" ]; then
			eval_gettext "\$PACKAGE already in the cache: \$CACHE_DIR"; echo
			# Check package download was finished
			tail -c 2k $PACKAGE.tazpkg | fgrep -q 00000000TRAILER || {
				eval_gettext "Continuing \$PACKAGE download"; echo
				download $PACKAGE.tazpkg
			}
			if [ "$($CHECKSUM $PACKAGE.tazpkg)" != "$(fgrep "  $PACKAGE.tazpkg" $rep/packages.$SUM)" ]; then
				rm -f $PACKAGE.tazpkg
				download $PACKAGE.tazpkg
			fi
		else
			newline
			download $PACKAGE.tazpkg
		fi
		PACKAGE_FILE=$CACHE_DIR/$PACKAGE.tazpkg
		[ "$rootconfig" ] && LOCALSTATE=${LOCALSTATE#$root}
		install_package $ROOT
		[ "$AUTOEXEC" != "no" ] && $PACKAGE $ROOT
		update_desktop_database $ROOT
		update_mime_database $ROOT ;;
	
	clean-cache|-cc)
		# Remove all downloaded packages.
		check_root $@
		files=$(find $CACHE_DIR -name *.tazpkg | wc -l)
		title 'Path: $CACHE_DIR'
		action "Cleaning cache directory..."
		rm -rf $CACHE_DIR/*
		status
		num=$(colorize 32 "$files")
		if [ "$files" == 0 ]; then
			footer "$(eval_gettext '$num file removed from cache.')"
		else
			footer "$(eval_gettext '$num files removed from cache.')"
		fi ;;
	
	list-undigest)
		# list undigest URLs.
		if [ "$2" = "--box" ]; then
			for i in $LOCALSTATE/undigest/*/mirror; do
				[ -f $i ] || continue
				echo "$(basename $(dirname $i))|$(cat $i)"
			done
		else
			title 'Current undigest(s)'
			for i in $LOCALSTATE/undigest/*/mirror; do
				if [ ! -f $i ]; then
					gettext "No undigest mirror found."; echo
					exit 1
				fi
				echo "$(basename $(dirname $i))	$(cat $i)"
			done
			newline
		fi ;;
	remove-undigest)
		# remove undigest URL.
		check_root $@
		undigest="$2"
		if [ -d $LOCALSTATE/undigest/$2 ]; then
			eval_gettext "Remove \$undigest undigest" && confirm
			if [ $? = 0 ]; then
				action "Removing \$undigest undigest..."
				rm -rf $LOCALSTATE/undigest/$2
				status
				rmdir $LOCALSTATE/undigest 2> /dev/null
			fi
		else
			eval_gettext "Undigest \$undigest not found"; echo
		fi ;;
	add-undigest|setup-undigest)
		# Add undigest URL.
		check_root $@
		undigest=$2
		[ -d $LOCALSTATE/undigest ] || mkdir $LOCALSTATE/undigest
		if [ -z "$undigest" ]; then
			i=1
			while [ -d $LOCALSTATE/undigest/$i ]; do
				i=$(($i+1))
			done
			undigest=$i
		fi
		if [ ! -d $LOCALSTATE/undigest/$undigest ]; then
			eval_gettext "Creating new undigest \$undigest."; echo
			mkdir $LOCALSTATE/undigest/$undigest
		fi
		setup_mirror $LOCALSTATE/undigest/$undigest $3 ;;
	setup-mirror|-sm)
		# Change mirror URL.
		check_root $@
		setup_mirror $LOCALSTATE $2 ;;
	reconfigure)
		# Replay post_install from receipt
		check_for_package_on_cmdline
		check_root $@
		ROOT=""
		while [ -n "$3" ]; do
			case "$3" in
			--root=*)
				ROOT="${3#--root=}/" ;;
			*)	shift 2
				u_opt="$*"
				echo -e "\n$(gettext 'Unknown option $u_opt.')\n" >&2
				exit 1 ;;
			esac
			shift
		done
		if [ -d "$ROOT$INSTALLED/$PACKAGE" ]; then
			check_for_receipt $ROOT
			# Check for post_install
			if grep -q ^post_install $ROOT$INSTALLED/$PACKAGE/receipt; then
				. $ROOT$INSTALLED/$PACKAGE/receipt
				post_install $ROOT
				# Log this activity
				[ -n "$ROOT" ] || log_pkg Reconfigured
			else
				newline
				eval_gettext "Nothing to do for \$PACKAGE."; echo
			fi
		else
			newline
			eval_gettext "Package \$PACKAGE is not installed."; echo
			gettext "Install package with 'tazpkg install' or 'tazpkg get-install'"; echo
			newline
		fi ;;
	shell)
		# TazPKG SHell
		if test $(id -u) = 0 ; then
			PROMPT="\\033[1;33mtazpkg\\033[0;39m# "
		else
			PROMPT="\\033[1;33mtazpkg\\033[0;39m> "
		fi
		if [ ! "$2" = "--noheader" ]; then
			clear
			title 'TazPKG SHell.'
			gettext "Type 'usage' to list all available commands or 'quit' or 'q' to exit."; echo
			newline
		fi
		while true
		do
			echo -en "$PROMPT"; read cmd
			case $cmd in
				q|quit)
					break ;;
				shell)
					gettext "You are already running a TazPKG SHell."; echo ;;
				su)
					su -c 'exec tazpkg shell --noheader' && break ;;
				"")
					continue ;;
				*)
					tazpkg $cmd ;;
			esac
		done ;;
	depends)
		# Display dependencies tree
		cd $INSTALLED
		ALL_DEPS=""
		if [ -f $2/receipt ]; then
			dep_scan $2 ""
		fi ;;
	rdepends)
		# Display reverse dependencies tree
		cd $INSTALLED
		ALL_DEPS=""
		if [ -f $2/receipt ]; then
			rdep_scan $2
		fi ;;
	convert|-c)
		# convert misc package format to .tazpkg
		check_for_package_file
		[ -n "$TARGET_DIR" -a -s "$TARGET_DIR/files.list.lzma" ] &&
			TMPLOCALSTATE="$TARGET_DIR"
		if [ "$(dd if=$PACKAGE_FILE bs=8 count=1 skip=1 2> /dev/null)" \
		      == "debian-b" ]; then
			convert_deb
		else
		case "$PACKAGE_FILE" in
		*.deb|*.udeb)
			convert_deb;;
		*.rpm)
			convert_rpm;;
		*.sb)
			convert_sb;;
		*.sfs)
			convert_sfs;;
		*.pet)
			convert_pet;;
		*.tgz)
			convert_tgz;;
		*.apk|*.pkg.tar.gz|*.pkg.tar.xz)
			convert_arch;;
		*.ipk|*.opk)
			convert_ipk;;
		*)
			gettext "Unsupported format"; echo ;;
		esac
		fi ;;
	link)
		# link a package from another slitaz installation
		PACKAGE=$2
		if [ ! -d "$TARGET_DIR" -o \
		     ! -d "$TARGET_DIR$INSTALLED/$PACKAGE" ]; then
			gettext "
usage:   tazpkg link package_name slitaz_root
example: 'tazpkg link openoffice /mnt' will use less than 100k in
         your running system ram.
"
			exit 1
		fi
		if [ -e "$INSTALLED/$PACKAGE" ]; then
			eval_gettext "\$PACKAGE is already installed."; echo
			exit 1
		fi
		ln -s $TARGET_DIR$INSTALLED/$PACKAGE $INSTALLED
		DEPENDS="$(. $INSTALLED/$PACKAGE/receipt ; echo $DEPENDS)"
		MISSING=""
		for i in $DEPENDS; do
			[ -e $INSTALLED/$i ] && continue
			MISSING="$MISSING$i "
			eval_gettext "Missing: \$i"; echo
		done
		if [ -n "$MISSING" ]; then
			newline
			gettext "Link all missing dependencies" && confirm
			answer=$?
			newline
			if [ $answer = 0 ]; then
				for i in $MISSING; do
					tazpkg link $i $TARGET_DIR
				done
			else
				newline
				eval_gettext "Leaving dependencies unresolved for: \$PACKAGE"; echo
				gettext "The package is installed but probably will not work."; echo
				newline
			fi
		fi
		. $INSTALLED/$PACKAGE/receipt
		if grep -q ^pre_install $INSTALLED/$PACKAGE/receipt; then
			pre_install
		fi
		while read path; do
			[ -e $path ] && continue
			while true; do
				dir=$(dirname $path)
				[ -e $dir ] && break
				path=$dir
			done
			ln -s $TARGET_DIR$path $dir
		done < $INSTALLED/$PACKAGE/files.list
		if grep -q ^post_install $INSTALLED/$PACKAGE/receipt; then
			post_install
		fi ;;
	usage|*)
		# Print a short help or give usage for an unknown or empty command.
		usage ;;
esac

exit 0
