#!/bin/bash
#
########################################################################
# Configuration:
#-----------------------------------------------------------------------
myVers=3.0.6				# this program's version number.
#=======================================================================
pkgMgr='pacman'				# package manager to use.
pkgMgr="echo $pkgMgr"
#=======================================================================
orgDir=$(pwd)				# original starting directory.
myName=${0##*/}				# this program's name.
#=======================================================================
########################################################################
# Subroutines:
#-----------------------------------------------------------------------
# Give a usage message and exit.
Usage() { # $1 is the desired exit status, $Act is the usage type.
    local myNM="$myName"
    local M_pn="<pkgName>"
    local M_re="<regexp>"
    local M_dd=' ... '
    local actn="${Act:0:1}"

    [ "$1" == "4" ] && actn=''
    echo
    echo "$myName  $myVers"
    echo
    case $actn in
	c)  # clean:
	  echo "Usage:	$myNM c[lean]"
	  echo 
	  echo "When $myNM downloads packages, it saves them in a cache directory"
	  echo " on disk.  '$myNM clean' removes those that are old, that is, those"
	  echo " in which a newer version exists in the cache."
	  echo "This may be used to save some disk space."
	    ;;
	C)  # Clean:
	  echo "Usage:	$myNM C[lean]"
	  echo 
	  echo "When $myNM downloads packages, it saves them in a cache directory"
	  echo " on disk.  '$myNM Clean' removes _all_ those packages from the"
	  echo " cache.  This may be used to save some disk space."
	    ;;
	q)  # query:
	  echo "Usage:	$myNM q[uery]"
	  echo 
	  echo "Show the old, out of date packages and what would be upgraded"
	  echo " by an '$myNM Upgrade' execution."
	    ;;
	U)  # Upgrade:
	  echo "Usage:	$myNM Upgrade"
	  echo 
	  echo "'$myNM Upgrade' upgrades/updates all packages that are out of date."
	  echo " Each currently installed package will be examined and upgraded"
	  echo " if a newer package exists.  A report of all packages to upgrade"
	  echo " will be presented and the operation will not proceed without user"
	  echo " confirmation.  Dependencies are automatically resolved."
	    ;;
	v)  # version:
	  echo "Usage:	$myNM v[ersion]"
	  echo 
	  echo "List all installed packages and their version."
	    ;;
	d)  # db:
	  echo "Usage:	$myNM db"
	  echo 
	  echo "Download a fresh copy of the master package database from the server."
	  echo " This should typically be used each time before a '$myNM update' or"
	  echo " a '$myNM add' to get the latest."
	    ;;
	o)  # owns:
	  echo "Usage:	$myNM o[wns] <path/filename>"
	  echo 
	  echo "Search for the installed package that owns or supplied"
	  echo " the given path/filename."
	    ;;
	s)  # search:
	  echo "Usage:	$myNM s[earch] $M_re"
	  echo 
	  echo "Search for names or descriptions that match $M_re."
	    ;;
	i)  # info:
	  echo "Usage:	$myNM i[nfo] $M_pn"
	  echo 
	  echo "Display information about the given package."
	    ;;
	p)  # purge:
	  echo "Usage:	$myNM p[urge] $M_pn"
	  echo 
	  echo "Remove a named package from the system.  Files belonging to the"
	  echo " package will be deleted and the database will be marked to show"
	  echo " that the package is nolonger installed."
	  echo "Do not save the configuration files but remove them also."
	    ;;
	r)  # remove:
	  echo "Usage:	$myNM r[emove] $M_pn"
	  echo 
	  echo "Remove a named package from the system.  Files belonging to the"
	  echo " package will be deleted and the database will be marked to show"
	  echo " that the package is nolonger installed."
	  echo "Most configuration files will be saved."
	    ;;
	a)  # add:
	  echo "Usage:	$myNM a[dd] $M_pn $M_dd"
	  echo 
	  echo "Download and install one or more named package(s) as well as"
	  echo " all the required dependencies."
	    ;;
	l)  # list:
	  echo "Usage:	$myNM l[ist] $M_pn $M_dd"
	  echo 
	  echo "List all files owned by one or more installed package(s)."
	    ;;
	*)  # anything else:
	  echo "Usage:	$myNM -h		= provides this help message,"
	  echo "   or	$myNM <Action> -h	= provides help on <action>,"
	  echo "   or	$myNM <Action> <args>	= see the above line."
	  echo 
	  echo "    Where <action> is one of:"
	  echo "	   add     = add/install one or more package(s) by name,"
	  echo "	   clean   = clean out stale packages from the cache,"
	  echo "	   Clean   = clean out _all_ packages from the cache,"
	  echo "	   db      = synchronize the package database information,"
	  echo "	   info    = give information about a named package,"
	  echo "	   list    = list all files installed by a given package,"
	  echo "	   owns    = show what package supplied a given filename,"
	  echo "	   purge   = purge one or more package(s) by name,"
	  echo "	   query   = show what would be installed by '$myNM Upgrade',"
	  echo "	   remove  = remove one or more package(s) by name,"
	  echo "	   search  = search for packages matching a given regexp,"
	  echo "	   Upgrade = upgrade _all_ out of date installed packages,"
	  echo "	   version = list all installed packages and their version."
	  echo 
	  echo "Execpt for the 'db' action and the 'Upgrade' action, all actions"
	  echo " may be specified by any combination of its initial letters, such"
	  echo " as 'cl' for clean.  But, the part provided must be spelled"
	  echo " correctly, 'clen' for example, will not work."
	  echo " The use of 'db' must be specific as in '$myNM db'."
	  echo " The use of 'Upgrade' must be specific as in '$myNM Upgrade'."
	    ;;
    esac
    echo
    exit $1
}
#=======================================================================
try() { # $1 is a flag to tell how many args are valid
    [ "$1" == "-h" ] && Usage 0
    case $1 in
	0) [ "$2" -eq 0 ] || Usage 1	;;
	1) [ "$2" -eq 1 ] || Usage 1	;;
	M) [ "$2" -gt 0 ] || Usage 1	;;
    esac
    Usage 2
}
#=======================================================================
# Top-level Function subroutines:
#-----------------------------------------------------------------------
# add: add/nstall one or more packages given by name.		!
do_a() { # arguments are one or more package name(s)
    try M $#
    $pkgMgr -S $@
}
#-----------------------------------------------------------------------
# clean: remove stale packages from the package cache.		!
do_c() { # no arguments
    try 0 $#
    $pkgMgr -Sc
}
#-----------------------------------------------------------------------
# Clean: remove _all_ packages from the package cache.		!
do_C() { # no arguments
    try 0 $#
    $pkgMgr -Scc
}
#-----------------------------------------------------------------------
# sync the database:						!
do_d() { # no arguments
    try 0 $#
    $pkgMgr -Sy
}
#-----------------------------------------------------------------------
# info: give information about a named package.			Colorize.
do_i() { # $1 is a package name
    try 1 $#
    $pkgMgr -Qi \& -Si $1		### See more/paci/paci for a start.
}
#-----------------------------------------------------------------------
# list: list files within one or more installed package name(s).  Colorize.
#	<pkgName> <path/filename>
#	error: package "<pkgName>" not found
do_l() { # $1 is a package name
    try M $#
    $pkgMgr -Ql $@
}
#-----------------------------------------------------------------------
# owns: show what package supplied a given file.		Colorize.
#	<path/filename> is owned by <pkgName> <pkgVers>
#	error: No package owns <path/filename>
do_o() { # $1 is a path/filename
    try 1 $#
    $pkgMgr -Qo $1
}
#-----------------------------------------------------------------------
# purge: delete a given package by name with configuration.	!
do_p() { # $1 is an installed package name
    try 1 $#
    $pkgMgr -Rn $1
}
#-----------------------------------------------------------------------
# query: list all out of date installed packages.		!
do_q() { # no arguments
    try 0 $#
    $pkgMgr -Qu
}
#-----------------------------------------------------------------------
# remove: delete a given package by name.			!
do_r() { # $1 is an installed package name
    try 1 $#
    $pkgMgr -R $1
}
#-----------------------------------------------------------------------
# search: search for packages given regexp.			Colorize.
do_s() { # $1 is a search regexp 
    try 1 $#
    $pkgMgr -Qs \& -Ss $1		### See ../../projects.archlinux.org/pacman/contrib/pacsearch
}
#-----------------------------------------------------------------------
# Upgrade: upgrade all out of date installed packages.		!
#		(maybe do $pkgMgr -Sy first)
do_U() { # no arguments
    try 0 $#
    $pkgMgr -Su
}
#-----------------------------------------------------------------------
# version: list installed packages and their version.		Colorize.
do_v() { # no arguments
    try 0 $#
$pkgMgr -Q
return
    $pkgMgr -Q | while read pkg ver ; do
	printf "${P}%30s ${V}%s${N}\n" "$pkg" "$ver"
    done
}
########################################################################
# Main:
#-----------------------------------------------------------------------
Act="$1"					# number of
shift						#  remaining
case $Act in					#  arguments:
    a|ad|add)				do_a "$@" ;; # >0
    c|cl|cle|clea|clean)		do_c "$@" ;; #  0
    C|Cl|Cle|Clea|Clean)		do_C "$@" ;; #  0
    db)					do_d "$@" ;; #  0
    i|in|inf|info)			do_i "$@" ;; #  1
    l|li|lis|list)			do_l "$@" ;; # >0
    o|ow|own|owns)			do_o "$@" ;; #  1
    p|pu|pur|purg|purge)		do_p "$@" ;; #  1
    q|qu|que|quer|query)		do_q "$@" ;; #  0
    r|re|rem|remo|remov|remove)		do_r "$@" ;; #  1
    s|se|sea|sear|searc|search)		do_s "$@" ;; #  1
    Upgrade)				do_U "$@" ;; #  0
    v|ve|ver|vers|versi|versio|version)	do_v "$@" ;; #  0
    -h|--help)				Usage 0   ;;
    *)					Usage 4   ;;
esac
########################################################################
# vim: ts=8 sw=4
# End
