#!/bin/sh -
# This is a simple script to install an RPM on a non-Red Hat Linux system
# (or pre-RPM 4.0 Red Hat system).

VERS=6.1
RELEASE=1
ARCH=i386
DDIR=lv61
BINPATH="`pwd`"
PACKAGE=labview61-app
DESTPATH=/usr/local
CPIOOPTS="--quiet --no-preserve-owner -i -d -u"

BINPATH="`dirname $0`"
case "$BINPATH" in
    /*) ;;
     .) BINPATH="`pwd`" ;;
     *) BINPATH="`pwd`/$BINPATH" ;;
esac

if [ "$1" != "" ]; then
    case "$1" in
        *.*) RPM=$1 ;;
        *) PACKAGE=$1 ;;
    esac
else
    echo "Usage: $0 package-base-name [install-dir]"
    echo "e.g. $0 labview61-app"
    exit 1
fi
if [ "$RPM" = "" ]; then
    RPM="$PACKAGE-$VERS-$RELEASE.$ARCH.rpm"
fi

if [ "$2" != "" ]; then
    DESTPATH=$2
fi
RPMPATH="`dirname $BINPATH`"
RPMPATH="$RPMPATH/$RPM"

cd $DESTPATH
if [ ! -f "$RPMPATH" ]; then
   echo "Cannot find $RPM"
   exit 1
fi

install_rpm () {
  $BINPATH/rpm2cpio "$1" | $BINPATH/cpio $CPIOOPTS
  # Run RPM post-install script
  $BINPATH/rpmq -qp --qf "RPM_INSTALL_PREFIX=$DESTPATH\n%|POSTIN?{%{POSTIN}}|" "$1" > /tmp/rpmpostun.$$
  /bin/sh /tmp/rpmpostun.$$ 
  rm -f /tmp/rpmpostun.$$ 
}

echo "Installing $RPM into $DESTPATH..."
# RPM files are all relative to / 
if [ -d usr/local ]; then
   # Attempts to install in / will go to /usr/local anyway
   install_rpm "$RPMPATH"
else
   if [ -d usr ]; then
       ln -s .. usr/local
       install_rpm "$RPMPATH"
       rm -f usr/local
   else
       mkdir usr
       ln -s .. usr/local
       install_rpm "$RPMPATH"
       rm -f usr/local
       rmdir usr 2>/dev/null
   fi
fi

exit 0
