#!/bin/sh
#
# Spk - The SliTaz Packages toolset. Read the README before adding or
# modifying any code in spk!
#
# Copyright (C) SliTaz GNU/Linux - BSD License
# Author: See AUTHORS files
#
. /usr/lib/slitaz/libspk.sh
#. lib/libspk.sh

#
# Functions
#

VERSION=1.0

# Help and usage
usage() {
	name=$(basename $0)
	cat << EOT

$(boldify $(gettext "Usage:")) $name [packages|--options]

$(gettext "SliTaz Packages toolset v$VERSION")

$(boldify $(gettext "Commands:"))
  info        $(gettext "Display path, mirror and other stats")
  activity    $(gettext "Display packages activities")
  clean       $(gettext "Clean cache and temporary files")

$(boldify $(gettext "Options:"))
  --add       $(gettext "Install packages if mirrored")
  --rm        $(gettext "Remove installed packages")
  --get       $(gettext "Download packages specified on cmdline")
  --block     $(gettext "Add packages to the blocked list")
  --unblock   $(gettext "Remove packages from the blocked list")
  --log       $(gettext "Show package install and upgrade log")
  --cache     $(gettext "Used with --get to download in cache")
  --forced    $(gettext "Force packages reinstall or download")
  --root=     $(gettext "Set the root file system path")
  --debug     $(gettext "Display some useful debug information")

$(boldify $(gettext "Examples:"))
  $name package1 package2 packageN
  $name package package2 --block

EOT
	exit 0
}

#
# Commands and exit
#

case "$1" in
	""|*usage|*help) usage ;;
	info)
		cache="$(du -sh $CACHE_DIR | awk '{print $1 " " $2}')"
		extra=$(ls $extradb | wc -l)
		newline
		boldify "Spk info"
		separator
		gettext "Architecture  :"; echo " $SLITAZ_ARCH"
		gettext "Database      :"; echo " $installed"
		gettext "Cache info    :"; echo " $cache"
		gettext "Mirror URL    :"; echo " $(cat $mirrorurl)"
		gettext "Extra mirrors :"; echo " $extra"
		count_installed
		count_mirrored
		separator && newline
		exit 0 ;;
	activity)
		# --lines=NB
		: ${lines=18}
		newline
		boldify "Spk Activity"
		separator
		cat $activity | tail -n $lines
		separator && newline
		exit 0 ;;
	clean)
		newline
		boldify "Spk Clean"
		separator
		size=$(du -sh $CACHE_DIR | awk '{print $1}')
		gettext "Cleaning cache:"; echo -n " $CACHE_DIR ($size)"
		rm -rf $CACHE_DIR/* && status
		gettext "Cleaning tmp  :"; echo -n " $(dirname $tmpdir)"
		rm -rf $(dirname $tmpdir) && status
		separator && newline
		exit 0 ;;
esac

#
# Handle packages: spk package1 ... packageN
#

#debug "cmdline: $@"
count=0

for pkg in $@
do
	# Handle general: --options
	case " $@ " in
		*\ --get\ *)
			# We want: package-version.tazpkg
			package_full=$(full_package $pkg)
			mirrored_pkg $pkg
			[ "$mirrored" ] || continue
			[ "$count" == 0 ] && newline
			count=$(($count + 1))
			download $package_full $mirror
			unset_mirrored
			if [ ! "$cache" ]; then
				gettext "Moving package to current directory..."
				mv -f "$CACHE_DIR/$package_full" .
				status
			fi
			newline && continue ;;
	esac
	# Installed ?
	if [ -d "$installed/$pkg" ]; then
		# Handle: --options
		case " $@ " in
			*\ --block\ *)
				check_root
				[ -d "$installed/$pkg" ] || continue
				if grep -qs ^${pkg}$ $blocked; then
					echo $(boldify $pkg) $(gettext "is already blocked")
				else
					gettext "Blocking package:"; echo -n " $pkg"
					echo $pkg >> $blocked
					log "Blocked package: $pkg" && status
				fi
				continue ;;
			*\ --unblock\ *)
				check_root
				[ -d "$installed/$pkg" ] || continue
				if grep -qs ^${pkg}$ $blocked; then
					gettext "Unblocking package:"; echo -n " $pkg"
					sed -i /"^${pkg}$"/d $blocked
					log "Unblocked package: $pkg" && status
				else
					echo $(boldify $pkg) $(gettext "is not blocked")
				fi
				continue ;;
			*\ --rm\ *)
				is_package_installed $pkg || continue
				spk-rm $pkg --count=$count
				count=$(($count + 1))
				continue ;;
			*\ --log\ *)
				# Display package's log
				if [ -f "$logdir/$pkg/install.log" ]; then
					count=$(($count + 1))
					[ "$count" == "1" ] && newline
					colorize 36 $(gettext "Install log for:"; echo " $pkg")
					separator
					cat $logdir/$pkg/install.log
				else
					gettext "Any install log for:"; boldify " $pkg"
				fi
				if [ -f "$logdir/$pkg/up.log" ]; then
					colorize 36 $(gettext "Upgrade log for:"; echo " $pkg")
					separator
					cat $logdir/$pkg/up.log
				else
					colorize 36 $(gettext "Any upgrade log for:"; echo " $pkg")
					newline
				fi
				continue ;;
			*\ --extract\ *)
					newline
					echo $(boldify $(gettext "Extracting:")) $pkg
					separator ;;
			*\ --forced\ *)
				spk-add --forced $pkg --count=$count
				count=$(($count + 1))
				continue ;;
		esac
		count=$(($count + 1))
		[ "$count" == 1 ] && newline
		unset_receipt
		source_receipt $installed/$pkg/receipt
		boldify $(gettext "Package") $pkg
		separator
		gettext "Status     :"; colorize 32 " installed"
		receipt_info
		separator && newline
		continue
	fi
	# Mirrored ?
	mirrored_pkg $pkg
	if [ "$mirrored" ]; then
		# Handle mirrored: --options
		case " $@ " in
			*\ --add\ *)
				spk-add $pkg --count=$count
				count=$(($count + 1))
				continue ;;
		esac
		count=$(($count + 1))
		[ "$count" == 1 ] && newline
		boldify $(gettext "Package") $pkg
		separator
		gettext "Status     :"; colorize 31 " not installed"
		gettext "Version    :"; echo "$mirrored" | cut -d '|' -f 2
		gettext "Short desc :"; echo "$mirrored" | cut -d '|' -f 3
		gettext "Category   :"; echo "$mirrored" | cut -d '|' -f 4
		separator && newline
		continue
	fi
	unset mirrored
	# Skip options such as --confirm or unknown package
	case "$pkg" in
		--*) continue ;;
		*) gettext "WARNING: Unknown package"; echo ": $pkg"
	esac
done
exit 0
