#!/bin/sh
#----------------------------------------------------------------------------
# /usr/bin/unrpm - unrpm
#
# Creation   :  2013-02-23 holbru
# Last update:  $Id$
#
# Based on unrpm from Kent Robotti 1999-12-03
# Copyright (c) 2013-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net
#
# 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.
#----------------------------------------------------------------------------
MSG() {
cat <<EOF

# This script 'unrpm' extracts a 'xxx.rpm' package.
#
# Based on unrpm from Kent Robotti 1999-12-03
#
# USAGE: unrpm -x -d directory package.rpm
# USAGE: unrpm -X -d directory package.src.rpm
# USAGE: unrpm -l package.rpm   <List contents of 'xxx.rpm' package>
#
# Extract 'xxx.rpm' package to current directory.
# Example: unrpm -x -d . package.rpm
#
# Extract 'xxx.rpm' package to a directory called fooboo, create
# the directory if it doesn't exist: # mkdir fooboo
# Example: unrpm -x -d fooboo package.rpm
#
# Extract 'xxx.rpm' package to /mnt/linux directory.
# Example: unrpm -x -d /mnt/linux package.rpm
#
# If you want to extract a 'xxx.src.rpm' package but not the
# tar.? package inside, use the -X option.
#
# Extract 'xxx.src.rpm' package to directory but not the tar.? inside.
# Example: unrpm -X -d directory package.src.rpm

EOF
}


case "${1}" in
""|--help|-h)
    MSG
    exit 0
    ;;
esac

if [ "${1}" = "-X" -a "${2}" = "-d" ]
then
    _path="${3}"

    if [ "${_path}" = "/" ]
    then
        echo "No extract to '/' "
        echo "extract to /tmp/$(basename "${4}")"
        _path="/tmp/$(basename "${4}")"
    fi

    if [ ! -d "${_path}" ]
    then
        mkdir -pv -m 1777 "${_path}"
    fi
    pwd=${PWD}
    echo
    cd ${_path} && rpm2cpio ${pwd}/${4} | cpio -iumvd --quiet || exit 1
    cd ${pwd}
    echo
    echo "${4} extracted to ${_path}"
    echo
    exit

elif [ "${1}" = "-x" -a "${2}" = "-d" ]
then
    _path="${3}"

    if [ "${_path}" = "/" ]
    then
        echo "No extract to '/' "
        echo "extract to /tmp/$(basename "${4}")"
        _path="/tmp/$(basename "${4}")"
    fi
    if [ ! -d "${_path}" ]
    then
        mkdir -pv -m 1777 "${_path}"
    fi
    pwd=${PWD}
    echo
    cd ${_path} && rpm2cpio ${pwd}/${4} | cpio -iumvd --quiet || exit 1
    for tarball in $(ls *.tar.*)
    do
        tar xfv ${tarball} 2>/dev/null || exit 1
    done

    cd ${pwd}
    echo
    echo "${4} extracted to ${_path}"
    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
    rpm2cpio ${2} | cpio -tvf --quiet | ${pager}
fi