#!/bin/bash

# quilt.SlackBuild
# by Stuart Winter <stuart@armedslack.org>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export PORTCWD=$PWD CWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Version information:
DIFFSTATVER=1.41

######################### Build diffstat ###################################
# quit requires diffstat.

# Extract source:
tar zxvvf $PORTCWD/sources/diffstat_$DIFFSTATVER.orig.tar.gz
cd diffstat*
slackhousekeeping

# Configure:
./configure \
   --prefix=/usr \
   --target=$ARCH-slackware-linux \
   --build=$ARCH-slackware-linux \
   --host=$ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# Copy docs:
mkdir -pm755 $PKG/usr/doc/diffstat-$DIFFSTATVER
cp -fa README CHANGES \
       $PKG/usr/doc/diffstat-$DIFFSTATVER

# Install diffstat binary onto the filesystem so that it's picked up during
# the build of quilt:
install -m755 -oroot -gbin diffstat /usr/bin/

######################### Build quilt ###################################

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/sources/quilt_*.orig.tar.gz
cd quilt*
zcat $PORTCWD/sources/quilt_*.diff.gz | patch -p1
slackhousekeeping

# Configure:
./configure \
   --prefix=/usr \
   --infodir=/usr/info \
   --mandir=/usr/man \
   --with-awk=/usr/bin/gawk \
   --with-mta=/usr/sbin/sendmail \
   --target=$ARCH-slackware-linux \
   --build=$ARCH-slackware-linux \
   --host=$ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Install into package framework:
make LC_ALL=C BUILD_ROOT=$PKG install || failinstall

# Even though --infodir and --mandir were specified in ./configure
# to go in /usr, they don't.  Let's move those to the Slackware standard
# dirs:
mv -f $PKG/usr/share/{doc,info} $PKG/usr
cp -fa $PKG/usr/share/man $PKG/usr
rm -rf $PKG/usr/share/man

# I was considering removing the bash completion stuff from this package
# but Slackware does have bash_completion in extra/ so I'll leave it.
# I still don't like it though :)

###########################################################################

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown (root:bin), slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
