#!/bin/bash

#################################################################
# Program: slacktrack.SlackBuild
# Purpose: Build a Slackware Package of slacktrack
# Author : Stuart Winter <stuart@polplex.co.uk>
# Version: 1.04
# Date...: 08-Jul-2004
#################################################################

PACKAGE=slacktrack
VERSION=1.25
BUILD=${BUILD:=0}
ARCH=${ARCH:=sparc} 
SLACKPACKAGE=$PACKAGE-$VERSION-$ARCH-$BUILD.tgz

# Resting place for the package .tgz:
PKGSTORE=${PKGSTORE:=/tmp}

# Temporary unarchive, compile & package-root directory:
TMP=/tmp/build-slacktrack
PKG=/tmp/package-slacktrack

# Work out where we are now so we can untar our source ball from it:
CWD=$PWD

# Delete previous build dirs
rm -rf $PKG $TMP
mkdir -pm755 $PKG $TMP

# Extract source:
cd $TMP
tar zxvvf $CWD/sources/checkinstall-*.tgz
cd checkinstall*
chown -R root:root .

# Get version of installwatch (required for the docs directory):
IWVERSION=$( ls -d installwatch* )
cd $IWVERSION

# Patch from Alexander Kern on the checkinstall mailing list:
# It adds simply "utimes" and "setxattr" to the list of
# intercepted functions.
patch -p0 < $CWD/sources/installwatch1.patch

# Build installwatch:
make clean
make PREFIX=/usr

# Create package framework:
mkdir -p $PKG/{install,usr/{lib,bin,"doc/$PACKAGE-${VERSION}_${BUILD}/$IWVERSION",man/man8}}
chown root:bin $PKG/usr/bin

# Installwatch's docs:
cp -a BUGS COPYING TODO CHANGELOG README VERSION \
      $PKG/usr/doc/$PACKAGE-${VERSION}_${BUILD}/$IWVERSION

# slacktrack's docs:
cp -a $CWD/docs/* $PKG/usr/doc/$PACKAGE-${VERSION}_${BUILD}
rm -f $PKG/usr/doc/$PACKAGE-${VERSION}_${BUILD}/INSTALL

# Fix any wonky permissions the docs may have attracted:
find $PKG/usr/doc -type f -print0 | xargs -0 chmod 644
find $PKG/usr/doc -type f -print0 | xargs -0 chown root:root

# Install man pages:
( cd $CWD/man
  ./man.build 
  gzip -9c slacktrack.8 > $PKG/usr/man/man8/slacktrack.8.gz
  gzip -9c slackdtxt.8  > $PKG/usr/man/man8/slackdtxt.8.gz
  rm -f *.8 ) 

# Install installwatch:
strip --strip-unneeded installwatch.so
install -m755 installwatch.so $PKG/usr/lib

# Install slacktrack & friends:
install -m755 -oroot -gbin $CWD/scripts/{altertrack,slacktrack,slackdtxt} \
                           $PKG/usr/bin

# Install package description:
install -m644 $CWD/slack-desc $PKG/install

# Build package:
cd $PKG
chmod -R og-w .
makepkg -l y -c n $PKGSTORE/$SLACKPACKAGE

# Create the corresponding .txt description file:
( cd $PKGSTORE && $CWD/scripts/slackdtxt $SLACKPACKAGE )

# Package maintainer stuff:
PARAMS="$( getopt -qn "$( basename $0 )" -o iz -- "$@" )"
if [ $? -eq 0 ]; then 
    eval set -- "$PARAMS"
    for option in $* ; do
    case "${option}" in
         -i) 
           removepkg slacktrack
           installpkg $PKGSTORE/$SLACKPACKAGE
           shift ;;

         -z)
           echo -n "Making a distributable source archive"
           ( cd $CWD/..
             # Don't worry, you're not missing much!
             tar --exclude slacktrack/2bourbon \
                 --exclude slacktrack/old_stuff \
                 -zcf $PACKAGE-${VERSION}_${BUILD}-source.tar.gz slacktrack )
             echo " ... done"
             shift ;;
    esac done
fi

# Delete temporary build dir and package-root.
# I don't like doing this anymore - I prefer to be able to refer back to
# the compiled source tree after the build.
#rm -rf $TMP $PKG
