#!/bin/sh

# rpmls -- List contents of rpm packages
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

set -u

all=
owner=

usage() {
	cat <<-EOF
	rpmls -- List contents of rpm packages

	Usage: rpmls [-a] [-l] PKG...
	       rpmls -?

	Options:
	   -a  list all packages matching a PKG
	   -l  use a long listing format
	   -?  print this help and exit

	The PKG arguments may either be package
	name expressions or file names.
	EOF
	exit
}

while getopts 'al?' opt
do
	case $opt in
	a) all=a ;;
	l) owner='%-8{fileusername} %-8{filegroupname} ' ;;
	?) usage ;;
	esac
done

shift $((OPTIND - 1))

qf="[%-11{filemodes:perms} $owner%{filenames}\\n]"

for arg in "$@" ; do
	a=$all
	p=
	if [ -f "$arg" ]
	then
		a=
		p=p
	fi
	rpm -q$a$p --qf="$qf" --nodigest --nosignature "$arg"
done
