#!/bin/sh
#
# inst2deb: take output of "installwatch make install" and create
# a binary debian package.
#
# This file may be distributed under the terms of the GNU GPL.
# Modified from inst2rpm by Jon A. Christopher <jac8792@tamu.edu>
#
# Radovan Garabik <garabik@melkor.dnp.fmph.uniba.sk>
#

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

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

Version=1.0.0
Section=installwatch
Priority=extra
Architecture=i386
#Depends="libc6"
Maintainer="`whoami` <`whoami`@`hostname --fqdn`>"
Description="Installed via 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 "Section: ($Section): "
read tmp
if [ x"$tmp" != x ]
then 
     Section="$tmp"
fi


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

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


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


p=`basename $0`
BUILDDIR=`mktemp -u -q /tmp/$p.XXXXXX`
if [ $? -ne 0 ]; then
  echo "$0: Can't create build directory, exiting..."
  exit 1
fi

 

mkdir $BUILDDIR
mkdir $BUILDDIR/DEBIAN
cat > $BUILDDIR/DEBIAN/control << EOF
Package: $Package
Version: $Version
Section: $Section
Priority: $Priority
Architecture: $Architecture
Maintainer: $Maintainer
Description: $Description
  $Description
EOF

#
# Get the file list from installwatch log
#
echo "Getting file list..."

p=`basename $0`
TMPFILE=`mktemp -q /tmp/$p.XXXXXX`
if [ $? -ne 0 ]; then
  echo "$0: Can't create temp file, exiting..."
  exit 1
fi


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

for each in `cat $TMPFILE`
do
   if [ -e $each ] 
   then
      mkdir -p $BUILDDIR/`dirname $each` || true
      cp -a $each $BUILDDIR/`dirname $each`
   fi
done
/bin/rm -f $TMPFILE

#importtant: remove /dev because /dev/tty (and maybe others) would be in 
#debian package

#sanity check
if [ "$BUILDDIR/dev" != "/dev" ]; then
  /bin/rm -fr $BUILDDIR/dev
fi

#
# now run dpkg-deb to build the binary
#
echo "Building binary deb..."
dpkg-deb -b $BUILDDIR $Package\_$Version\_$Architecture.deb && \
echo $Package\_$Version\_$Architecture.deb built successfully.



#
# Clean up
#
echo "Cleaning up..."
rm -rf $BUILDDIR




