#!/bin/bash

#################################################################
# Program: slacktrack.SlackBuild
# Purpose: Build a Slackware Package of slacktrack
# Author : Stuart Winter <mozes@slackware.com>
# Version: 1.04
# Date...: 08-Jul-2004
#################################################################

PKGNAME=slacktrack
VERSION=1.32
BUILD=${BUILD:=1}
ARCH=${ARCH:=sparc} 
SLACKPACKAGE=$PKGNAME-$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 .
chmod -Rf a-s,a+rX,u+w,g-w,o-w .

# 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 --verbose -p0 < $CWD/sources/installwatch1.patch || exit 1
patch --verbose -p2 < $CWD/sources/installwatch-portable.diff || exit 1

# Build installwatch:
# The CFLAGS doesn't work, as the makefile has the opts hardcoded.
# It will build okay on your system, but don't expect it to work everywhere.
# FIXME. It needs a patch to accept CFLAGS from outside.
make clean
make PREFIX=/usr CFLAGS="gcc -m32 -mcpu=v8 -mtune=v9"

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

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

# slacktrack's docs:
cp -fav $CWD/docs/* $PKG/usr/doc/$PKGNAME-${VERSION}_${BUILD}
rm -fv $PKG/usr/doc/$PKGNAME-${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/installwatch-slacktrack.so

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

# Install package description:
install -pm644 $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 $PKGNAME-${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
