#!/bin/sh
#
# inst2rpm: take output of "installwatch make install" and create/install
# a binary rpm, so that the info is in the rpm data base.
#
# This file may be distributed under the terms of the GNU GPL.
#
# Jon A. Christopher (jac8792@tamu.edu)
# Oct 2, 1998
#

if [ x$1 = x ]
then
	echo "usage: inst2rpm installwatch_log_file"
	exit
fi

if [ x$2 != x ]
then
    PACKAGE=$2
else
    PACKAGE=$1
fi

VERSION=1.0.0
SUMMARY="Program installed via installwatch"
RELEASE=1
COPYRIGHT=GPL
GROUP=unknown
DESCRIPTION="Program installed by installwatch"


echo -n "New package name: ($PACKAGE): "
read tmp
if [ x"$tmp" != x ]
then 
     PACKAGE="$tmp"
fi

echo -n "Version: ($VERSION): "
read tmp
if [ x"$tmp" != x ]
then 
     VERSION="$tmp"
fi

echo -n "Release: ($RELEASE): "
read tmp
if [ x"$tmp" != x ]
then 
     RELEASE="$tmp"
fi

echo -n "Summary: ($SUMMARY): "
read tmp
if [ x"$tmp" != x ]
then 
     SUMMARY="$tmp"
fi

echo -n "Group: ($GROUP): "
read tmp
if [ x"$tmp" != x ]
then 
     GROUP="$tmp"
fi

echo -n "Copyright: ($COPYRIGHT): "
read tmp
if [ x"$tmp" != x ]
then 
     COPYRIGHT="$tmp"
fi

echo -n "Description: ($DESCRIPTION): "
read tmp
if [ x"$tmp" != x ]
then 
     DESCRIPTION="$tmp"
fi

cat > /tmp/$PACKAGE.spec << EOF
Summary:   $SUMMARY
Name:      $PACKAGE
Version:   $VERSION
Release:   $RELEASE
Copyright: $COPYRIGHT
Packager:  installwatch2rpm
Group:     $GROUP
Source:    $PACKAGE-$VERSION.tar.gz
BuildRoot: /tmp/buildroot

%description 
$DESCRIPTION

%prep
%setup 
%build
%install
%post
%postun 
%clean
%files
EOF

#
# Get the file list from installwatch log
# Note, this could be improved to get the file attributes as well, and put
# them in in a format RPM could understand
#
echo "Getting file list..."

awk '$2=="open"||$2=="link" {print $3} ; $2=="rename" {print $4}' < $1 | grep ^/ | \
   egrep -v '/dev/null|$HOME' | sort | uniq > /tmp/filelist.$$

for each in `cat /tmp/filelist.$$`
do
   if [ -e $each ] 
   then
      echo $each >> /tmp/$PACKAGE.spec
   fi
done
/bin/rm /tmp/filelist.$$

#
# Create a fake source directory for rpm
#
echo "Making fake source tree..."
mkdir /usr/src/redhat/SOURCES/$PACKAGE-$VERSION
cd /usr/src/redhat/SOURCES/
tar -czvf $PACKAGE-$VERSION.tar.gz $PACKAGE-$VERSION

#
# create symlink to root from /tmp/buildroot to make rpm happy
#
cd /tmp
ln -sf / /tmp/buildroot 

#
# now run rpm -bb to build the binary
#
echo "Building binary rpm..."
rpm -bb /tmp/$PACKAGE.spec

#
# now "install" the rpm to let the rpm data base know about the new files
#
echo "Installing binary rpm..."
rpm -i --nodeps /usr/src/redhat/RPMS/i386/$PACKAGE-$VERSION-$RELEASE.i386.rpm

#
# Clean up
#

/bin/rm /tmp/buildroot #/tmp/$PACKAGE.spec
/bin/rm -rf /usr/src/redhat/SOURCES/$PACKAGE-$VERSION
/bin/rm -rf /usr/src/redhat/SOURCES/$PACKAGE-$VERSION.tar.gz

echo "The binary rpm package has been installed, and is now in"
echo "/usr/src/redhat/RPMS/i386/$PACKAGE-$VERSION-$RELEASE.i386.rpm"
echo "If you ever wish to undo the installation of this package,"
echo "it can be done with a simple 'rpm -e $PACKAGE' command."








