#!/bin/sh
#
# Modified by John Eriksson <johne@rootlinux.org>, March 2003.
# Modified by Per Liden <per@fukt.bth.se>, November 2000.
#
# Copyright 1997, 1998 Patrick Volkerding, Moorhead, Minnesota USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ]; then
  echo "Usage: rpm2targz [FILENAME]..."
  echo "Convert a package in rpm format to a gzipped tar file."
  echo
  echo "  -h, --help		print this help and exit"
  echo "  -v, --version		output version information and exit"
  echo
  echo "Report bugs to <johne@rootlinux.org>"
  exit 1
fi

if [ "$1" = "-v" -o "$1" = "--version" ]; then
  echo "rpm2targz (rltools) #VERSION#"
  exit
fi

for i in $* ; do
  if [ ! "$1" = "$*" ]; then
    echo "Processing file: $i"
  fi
  rm -rf /tmp/rpm2targz$$
  mkdir /tmp/rpm2targz$$
  ofn=`basename $i .rpm`.cpio
  dd ibs=`/usr/lib/rpm2targz/rpmoffset < $i` skip=1 if=$i 2> /dev/null | gzip -dc > /tmp/rpm2targz$$/$ofn
  (cd /tmp/rpm2targz$$
   cpio --extract --preserve-modification-time --make-directories < $ofn 1> /dev/null 2> /dev/null
   rm -f $ofn
   find . -type d -perm 700 -exec chmod 755 {} \;
   tar cf - . ) > `basename $i .rpm`.tar
   gzip `basename $i .rpm`.tar
  (cd /tmp; rm -rf rpm2targz$$)
done
