#!/bin/bash -
# deb--wrapper/reminder for various apt* and dpkg command

# $Id: deb 1627 2009-09-14 18:09:14Z root $

PROGRAM=$(basename $0)             # What's our name?
#PROGRAM=${0##*/}
VERSION='$Id: deb 1627 2009-09-14 18:09:14Z root $'
COPYRIGHT='Copyright 2007-2008 JP Vossen (http://www.jpsdomain.org/)'
LICENSE='GNU GENERAL PUBLIC LICENSE v2'

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Help
# Called like:  Usage
function Usage {
    # Note use of <<- 'here document' style which strips leading TABs but
    # not leading space, allowing for more or less proper indenting.

    # Follow the classic man page format
    less -n <<-EoN
	NAME
	    $PROGRAM--wrapper/reminder for various apt* and dpkg command

	SYNOPSIS
	    $PROGRAM [command] [arguments]

	COMMANDS
	* The | in upd|ate means you only need to type the "upd" part.
	* <names> means one or more space delimited package names
	* <egrep> means a single egrep regular expression (e.g. 'zip|arj')

	    upd|ate              # Update the package cache (do this at least weekly)
	    che|ck,chk           # Check for available upgrades
	    upche|ck,upchk       # Update, then check for available upgrades
	    upg|rade             # Upgrade all installed packages
	    dist|-upgrade        # Really upgrade all installed packages
	    se|arch;find <regex> # Search for <regex> in the package cache
	    sh|ow,info <names>   # Show details about package <names>
	    showpkg <names>      # Show name and various depends info for <names>
	    in|stall <names>     # Install package <names>
	    rem|ove <names>      # Remove package <names>
	                         # (--purge <names> removes config files too!)
	    what <egrep>         # Show the name and version of <egrep> if installed
	    which,installed,list <egrep> # Same as previous
	    files <names>        # List the files installed by package <names>
	    dep|ends <names>     # Show packages <names> depends on
	    rdep|ends <names>    # Show other packages that depend on <names>
	    pro|vides <names>    # Show the capability that <names> provides
	    whatpro|vides <file> # Show the package that provides <file>
	    pol|icy <file>       # Show the install policy (source repo, ver) for <file>
	    clean                # Remove downloaded package files from the cache
	    autoclean            # Remove obsolete packages from the cache
	    purge                # Purge the local cache, forces complete re-download
	    stats                # Show package cache stats
	    changelog            # Show package change log

	    Examples:
	        $PROGRAM update
	        $PROGRAM find zip
	        $PROGRAM install zip

	DESCRIPTION ($VERSION)
	    The Debian Advanced Package Tool (apt) is a very powerful and flexible
	    packaging tool.  But its functions are scattered among a number of
	    tools and it's hard to keep track of what's where, especially coming
	    from yum, which is very simple.  Just as rpm is the basis for yum,
	    dpkg is the basis for apt, but like yum apt has a package cache and 
	    various ways to manipulate it.  This is just a wrapper to unify the
	    tools.

	AUTHOR / BUG REPORTS
	    JP Vossen (jp {at} jpsdomain {dot} org)
	    http://www.jpsdomain.org/

	COPYRIGHT & LICENSE
	    $COPYRIGHT
	    $LICENSE

	SEE ALSO
	    * apt-file
	    * http://maketecheasier.com/become-an-apt-guru/2009/02/24
	    * http://www.debian.org/doc/manuals/apt-howto/index.en.html
	    * http://nakedape.cc/wiki/PackageManagerCheatsheet
	    * http://www.pthree.org/2007/08/12/aptitude-vs-apt-get/
	    * http://www.togaware.com/linux/survivor/Wajig_Overview.shtml
	      Wajig is similar to this script and is far more comprehensive, but
	      also much larger and more complicated.
	    * aptitude is more powerful than apt-get, and includes some features of
	      apt-cache as well (but it likes to install "recommended" packages by
	      default (http://people.debian.org/~dburrows/aptitude-doc/en/ch02s04s05.html#configRecommends-Important)).
	    * aptitude is more powerful searching than apt-cache but it's also
	      tricker to use.  See http://algebraicthunk.net/~dburrows/projects/aptitude/doc/en/ch02s03s05.html#tableSearchTermQuickGuide
	    * Use of aptitude instead of apt-get is recommended:
	      http://www.debian.org/doc/manuals/reference/ch-package.en.html
	      http://www.debian.org/doc/FAQ/ch-pkgtools.en.html#s-aptitude
	    * /var/log/aptitude
	    * man pages for apt-get, aptitude, apt-cache, apt-query, dpkg,
	      and even rpm and yum
	    * localepurge and deborphan are great too
	EoN
} # end of function Usage


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Show the command to run, then run it
# Called like:  Run foo
function Run {
    echo "$*"
    eval $*
}


###########################################################
# Main

# These are the programs we need
apt_get="$(type -P aptitude)"     # Use aptitude if available
# If aptitude is available, do *not* treat recommendations as dependencies
# when installing new packages!  If it's not there, fall back to 'apt-get'.
[ $apt_get ] && apt_get="$apt_get --without-recommends" || apt_get='apt-get'
apt_cache='apt-cache'  # See also http://algebraicthunk.net/~dburrows/projects/aptitude/doc/en/ch02s03s05.html#tableSearchTermQuickGuide
dpkg='dpkg'
dpkg_query='dpkg-query'
egrep='egrep'
sort='sort'
# Use less if we can, else use more
less=$(type -P less)
[ $less ] && less="$less --quit-if-one-screen --no-init" || less='more'

# Figure out what to do and go do it
case "$1" in

    # upd|ate              # Update the package cache (do this at least weekly)
    upd* )
        Run "$apt_get update"
        ;;
    # che|ck,chk           # Check for available upgrades
    che*|chk )
        # apt_get -s, --simulate, --just-print, --dry-run, --recon, --no-act
        # $apt_get --simulate dist-upgrade
        Run "$apt_get --simulate upgrade"
        ;;
    # upche|ck,upchk       # Update, then check for available upgrades
    upche*|upchk )
        $0 update && $0 check
        ;;
    # upg|rade             # Upgrade all installed packages
    upg* )
        Run "$apt_get upgrade $*"
        ;;
    # dist|-upgrade        # Really upgrade all installed packages
    dist* )
        Run "$apt_get dist-upgrade"
        ;;
    # se|arch;find <regex> # Search for <regex> in the package cache
    se*|find )
        # aptitude search
        shift
        Run "$apt_cache search $* | $sort | $less"
        ;;
    # sh|ow,info <names>   # Show details about package <names>
    sh*|info )
        shift
        Run "$apt_cache show $* | $less"
        ;;
    # showpkg <names>      # Show name and various depends info for <names>
    showpkg )
        shift
        Run "$apt_cache showpkg $* | $less"
        ;;
    # in|stall <names>     # Install package <names>
    in* )
        shift
        Run "$apt_get install $*"
        ;;
    # rem|ove <names>      # Remove package <names>
    rem* )
        shift
        Run "$apt_get remove $*"
        ;;
    # what <egrep>         # Show the name and version of <egrep> if installed
    # which,installed,list <egrep> # Same as previous
    what|which|installed|list )
        shift
        [ "$1" ] && and_maybe_grep="| $egrep -i $1"
        # dpkg -l | --list package-name-pattern ... = List packages matching given pattern.
        # dpkg-query -W | --show
        Run "$dpkg_query --show | perl -pe 's/^(.*?)\t(?:\d:)?(.*)\$/\$1\t\$2/;' \
          | sort $and_maybe_grep"
        ;;
    # files <names>        # List the files installed by package <names>
    files )
        shift
        # dpkg -L | --listfiles
        Run "$dpkg -L $* | $less"
        ;;
    # dep|ends <names>     # Show packages <names> depends on
    dep* )
        shift
        Run "$apt_cache depends $* | $less"
        ;;
    # rdep|ends <names>    # Show other packages that depend on <names>
    rdep* )
        shift
        Run "$apt_cache rdepends $* | $less"
        ;;
    # pro|vides <names>    # Show the capability that <names> provides
    pro* )
        echo 'Not implemented yet'
        exit 2
        ;;
    # whatpro|vides <file> # Show the package that provides <file>
    whatpro* )
        # dpkg_query -S | --search
        Run "$dpkg_query --search $2"
        ;;
    # pol|icy <file>       # Show the install policy (source repo, ver) for <file>
    pol* )
        Run "$apt_cache policy $2"
        ;;
    # clean                # Remove downloaded package files from the cache
    clean )
        Run "$apt_get clean"
        ;;
    # autoclean            # Remove obsolete packages from the cache
    autoclean )
        Run "$apt_get autoclean"
        ;;
    # purge                # Purge the local cache, forces complete re-download
    purge )
        Run "$apt_get purge"
        ;;
    # stats                # Show package cache stats
    stats )
        Run "$apt_cache stats"
        ;;
    changelog )   # Show package changes
        #echo "Not implemented--try aptitude changelog <package> or apt-listchanges"
        # lynx http://master.debian.org/cgi-bin/get-changelog\?package=$1
        Run "aptitude changelog $*"
        ;;

    verify )      # Verify all installed packages
        # Use debsums
        echo "Like rpm -Va, but not implemented yet--use debsums"
        ;;

    -h|--help|help|'?' )
        Usage
        exit 0
        ;;
    * )
        echo -e "\nBad command:  try $PROGRAM -h"
        exit 1;;
esac

