#!/bin/sh

[ -n "$COMFORT_DIR" ] || COMFORT_DIR=/usr/share/comfort
. "$COMFORT_DIR/functions"

msg_usage()
{
	apt-cache -v
	cat <<EOF
Usage: $PROG [options] command
       $PROG [options] install|remove pkg1 [pkg2 ...]
       $PROG [options] source pkg1 [pkg2 ...]
       $PROG [options] add file1 [file2 ...]
       $PROG [options] showpkg pkg1 [pkg2 ...]
       $PROG [options] showsrc pkg1 [pkg2 ...]
       $PROG [options] kernel [flavour]

apt is a unified command line interface tool for all apt utilities,
including apt-get, apt-cache and apt-cdrom. Basically, it understands
all the command these utilities use and redirect them to appropriate
one. Abbreviated versions of commands documented in [] could be used.

Commands:
   update [u] - Retrieve new lists of packages
   upgrade - Perform an upgrade
   install [i] - Install new packages (pkg is libc6 not libc6.rpm)
   remove [r] - Remove packages
   source [so] - Download source archives
   build-dep [bd] - Configure build-dependencies for source packages
   dist-upgrade [du] - Distribution upgrade, see apt-get(8)
   kernel [k] - Update kernel
   clean - Erase downloaded archive files
   autoclean [ac] - Erase old downloaded archive files
   check [ch] - Verify that there are no broken dependencies
   add - Add a package file to the source cache
   gencaches - Build both the package and source cache
   showpkg - Show some general information for a single package
   showsrc - Show source records
   stats - Show some basic statistics
   dump - Show the entire file in a terse form
   dumpavail - Print an available file to stdout
   unmet - Show unmet dependencies
   search [se] - Search the package list for a regex pattern
   show [sh] - Show a readable record for the package
   depends - Show raw dependency information for a package
   whatdepends - Show raw dependency information on a package
   pkgnames - List the names of all packages
   dotty - Generate package graphs for GraphVis
   policy - Show policy settings

Options:
  -h   This help text.
  -q   Loggable output - no progress indicator
  -qq  No output except for errors
  -d   Download only - do NOT install or unpack archives
  -s   No-act. Perform ordering simulation
  -y   Assume Yes to all queries and do not prompt
  -f   Attempt to continue if the integrity check fails
  -m   Attempt to continue if archives are unlocatable
  -u   Show a list of upgraded packages as well
  -b   Build the source package after fetching it
  -D   When removing packages, remove dependencies as possible
  -p=? The package cache.
  -s=? The source cache.
  -i   Show only important deps for the unmet command.
  -c=? Read this configuration file
  -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp

See the apt-get(8) and apt-cache(8) manual pages for more information.
EOF
}

while getopts ":hq:dsyfmubDip:s:c:o:" option
do
	case $option in
	h)
		msg_usage
		exit 1
		;;
	d | s | y | f | m | u | b | D | i)
		opts="$opts -$option"
		;;
	q | p | s | c | o)
		opts="$opts -$option$OPTARG"
		;;
	h | *)
		msg_usage
		exit 1
	esac
done

shift $(($OPTIND - 1))

cmd=$1
shift
case "$cmd" in
	update | upgrade | install | remove | source | build-dep | dist-upgrade | clean | autoclean | check)
		apt-get $opts $cmd $@
		;;
	add | gencaches | showpkg | showsrc | stats | dump | dumpavail | unmet | search | show | depends | whatdepends | pkgnames | dotty | policy)
		apt-cache $opts $cmd $@
		;;
	u)
		apt-get $opts update $@
		;;
	i)
		apt-get $opts install $@
		;;
	r)
		apt-get $opts remove $@
		;;
	so)
		apt-get $opts source $@
		;;
	bd)
		apt-get $opts build-dep $@
		;;
	du)
		apt-get $opts dist-upgrade $@
		;;
	ac)
		apt-get $opts autoclean $@
		;;
	ch)
		apt-get $opts check $@
		;;
	se)
		apt-cache $opts search $@
		;;
	sh)
		apt-cache $opts show $@
		;;	
	k|kernel)
		$COMFORT_DIR/update-kernel $@
		;;
	*)
		msg_usage
		exit 1
		;;
esac
