#!/bin/sh -ef
export LC_ALL=C

fakeroot_save='fakeroot -s .fakedata --'
fakeroot_load='fakeroot -i .fakedata --'
while getopts hn opt; do
	case "$opt" in
		n) fakeroot_save= fakeroot_load= ;;
		h) pod2usage --exit=0 "$0"; exit 0 ;;
		*) pod2usage --exit=2 "$0"; exit 2 ;;
	esac
done
shift "$((OPTIND-1))"

if [ $# -lt 1 ]; then
	echo "${0##*/}: not enough arguments" >&2
	pod2usage --exit=2 "$0"; exit 2
fi

. tmpdir.sh
if [ $# -lt 2 ]; then
	# Make it load aliases and other things, but with our prompt.
	[ -e "$HOME/.bashrc" ] && cat "$HOME/.bashrc" > "$TMPDIR/.bashrc"
	PS1="@\[\033[1;36m\]$1\[\033[m\]> "
	echo "PS1=\"$PS1\"" >> "$TMPDIR/.bashrc"
	rcfile="$TMPDIR/.bashrc"
	TMPDIR="$TMPDIR/root"
	mkdir -- "$TMPDIR"
fi
rpm2cpio "$1" |
	(cd "$TMPDIR";
	 $fakeroot_save cpio -idmu --quiet --no-absolute-filenames;
	 ${fakeroot_save:-chmod -Rf u+rwX .}
	) || exit 2
shift
cd "$TMPDIR"
export RPM_BUILD_ROOT="$TMPDIR"
if [ $# -lt 1 ]; then
	set -- bash --rcfile "$rcfile"
fi
$fakeroot_load "$@"

: <<'__EOF__'

=head1	NAME

rpmpeek - execute command within RPM contents

=head1	SYNOPSIS

B<rpmpeek> [B<-n>] [B<-h>] I<RPM> [I<command> I<args>...]

=head1	DESCRIPTION

B<rpmpeek> unpacks I<RPM> contents into a temporary directory and
executes a I<command> under that directory.  The directory is purged
upon exit. If no I<command> is specified it's bash(1).

=head1  OPTIONS

=over

=item   B<-n>

Do not preserve attributes of unpacked files.

=item   B<-h>

Display the usage and exit.

=back

=head1	AUTHOR

Written by Alexey Tourbin <at@altlinux.org>.

=head1	COPYING

Copyright (c) 2005 Alexey Tourbin, ALT Linux Team.

This 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.

=head1	SEE ALSO

rpm(8),
rpm2cpio(8),
fakeroot(1).

=cut

__EOF__
