#!/bin/bash

###########################################################################
#
# $Id: OO2_unpack.sh (v-0.2)$ 
# Contact: USM Bish  $
# Licence: GPL-2 [http://www.gnu.org/licenses/gpl.html]
# 
# This  script  is  meant  for  installing  OpenOffice-2.0.x  on
# non-rpm systems.  Unlike older verions  of which used  to come
# with  an installer,  Ver-2.0.x has  been bundled  in a  rather
# strange fashion - there are 28 binary rpm files all tar zipped
# in a huge tarball (126 mb  or so) for download. For my non-rpm
# box (an LFS box really), this needed extra effort. This script
# is meant to help people initially stumped, like me.
#
# The Principle: The default binary distribution of OpenOffice 2
# is meant for running from /opt. If we can  un-rpm the contents
# of the 28 rpms into the constituent elements and move the tree
# to /opt, then things would be as expected, and it would work.
# 
# The  process:  Essentially an  rpm  file  consists of  a  cpio
# archive, with  some header information.  Step one would  be to
# change the  rpm file  to a  cpio. For  this an  utility called
# rpm2cpio  is needed.  This utility  is  a portion  of the  rpm
# package. Install the  rpm package for your  distro, or compile
# it  from  source  (www.rpm.org).  Yes, you  also  need  "cpio"
# itself for processing. This would be there in most distros for 
# processing.
#
# Sequentially, these are the steps I followed:
#
# o Downloaded: OOo_2.0.4rc3_060927_LinuxIntel_install.tar.gz
#   [ I did a bittorrent down load ]
#
# o $ tar -xzvf OOo_2.0.4rc3_060927_LinuxIntel_install.tar.gz
#   [ This created : OOD680_m5_native_packed-1_en-US.9073 and
#   there were 28 rpm files in that directory].
#
# o $ cd OOD680_m5_native_packed-1_en-US.9073
#   I wrote this script within that directory (You may like to
#   copy the script there)
#
# o Then after making the script executable, ran it.
#   $ chmod +x $PROGNAME
#   $ ./$PROGNAME
#   [ All files were unpacked in : opt/openoffice.org2.0 under
#   the existing directory].
#
# o The rest is simple:
#   $ su
#   # cd opt  (within existing directory)
#   # mv openoffice.org2.0 /opt
#
# o The full openoffice.org2.0 tree is presently under /opt and
#   you may test it by running from there:
#   $ cd /opt/openoffice.org2.0/program/
#   $ soffice
#
# o It worked like a charm ! You can do the rest ...
#
# Ciao. Hope this helps !
#
# PS : This system can ofcourse be used for any rpm file.
#
###########################################################################

###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.2"

###########################################################################
#	Functions
###########################################################################

function get_prefix
{
    echo $PACKAGE > $PROGNAME.1
    rev $PROGNAME.1 > $PROGNAME.2
    cat $PROGNAME.2 | cut -b 5- > $PROGNAME.1
    rev $PROGNAME.1 > $PROGNAME.2
    PREFIX=`cat $PROGNAME.2`
}

###########################################################################
#	Program starts here
###########################################################################

# Check for rpm2cpio and cpio binaries on path

ERR=0
HAVE_R2C=`which rpm2cpio`
HAVE_CPIO=`which cpio`

if [ -z $HAVE_R2C ]; then
   echo "ERR : rpm2cpio not on path"
   ERR=1
fi

if [ -z $HAVE_CPIO ]; then
   echo "ERR : cpio not on path"
   ERR=1
fi

if [ $ERR -eq 0 ]; then
   echo
   echo "Found: $HAVE_R2C $HAVE_CPIO"
   echo
else
   echo "Install above utilities to proceed"
   exit 1
fi

# Now begin the process

CNT=1
for PACKAGE in `ls *.rpm`; do
    echo -en "$CNT]\t $PACKAGE \n"   
    get_prefix

    echo "o Creating $PREFIX.cpio"
    rpm2cpio $PACKAGE > $PREFIX.cpio

    echo "o Extracting $PREFIX.cpio"
    cpio --extract --preserve-modification-time --make-directories < $PREFIX.cpio 1> /dev/null 2> /dev/null

    echo 
    echo -en "Delete $PREFIX.cpio ? [y/n] : "
    read YN

    if [ $YN = "y" ]; then
       rm -f $PREFIX.cpio
    fi   

    CNT=$((CNT+1))

    echo 
    echo "-----------------------------------"
    echo
done

rm -f $PROGNAME.1
rm -f $PROGNAME.2

echo "Cheers ... all over !"
   
exit
        
#############################################################