#!/bin/sh

MSG() {
cat <<EOF

# This script 'undeb' extracts a Debian.deb package.
#
# Kent Robotti 3-12-99
#
# USAGE: undeb -x -d directory package.deb  
# USAGE: undeb -l package.deb   <List contents of debian package>
#
# Extract debian package to current directory.
# Example: undeb -x -d . package.deb  
# Extract debian package to a directory called fooboo, create 
# the directory if it doesn't exist: # mkdir fooboo
# Example: undeb -x -d fooboo package.deb  
# Extract debian package to /mnt/linux directory.
# Example: undeb -x -d /mnt/linux package.deb  

EOF
}

if [ "$1" = "" ]; then
MSG
exit
fi

if [ "$1" = "-x" -a "$2" = "-d" ]; then
if [ ! -d "$3" ]; then
echo "No such directory: $3"
exit 1
fi
dpkg-deb -X $4 $3 || exit 1
echo
echo "$4 extracted to $3"
echo
exit
fi

if [ "$1" = "-l" -a -s "$2" ]; then
if [ ! "$PAGER" = "" ]; then
pager="$PAGER"
elif type -all less >/dev/null 2>&1 ; then
pager=less
elif type -all more >/dev/null 2>&1 ; then
pager=more
else
pager="echo No PAGER found, no more or less."
exit 1
fi
dpkg-deb -I $2 > /tmp/deb.tmp
echo >> /tmp/deb.tmp
dpkg-deb -c $2 >> /tmp/deb.tmp
tmp=/tmp/deb.tmp
$pager $tmp   
rm -f /tmp/deb.tmp  
fi
