#!/bin/bash

# dpkg.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# 03-Jul-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$PWD
export PORTCWD=$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 ARCH_CFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export ARCH_CFLAGS="-O2" ;;
   sparc)   export ARCH_CFLAGS="-O2" ;;
   alpha)   export ARCH_CFLAGS="-O2" ;;
   x86_64)  export ARCH_CFLAGS="-O2" ;;
   i486)    export ARCH_CFLAGS="-O2" ;;
   mips)    export ARCH_CFLAGS="-O2" ;;
   *)       export ARCH_CFLAGS="-O2" ;;
esac

# Extract source:
tar zxvvf $CWD/sources/${PACKAGE}_$VERSION.tar.gz
cd ${PACKAGE}*
slackhousekeeping

# Stop dpkg-architecture from warning that 'armv3-slackware-linux' != 'arm-linux'
( cd scripts && zcat $CWD/dpkg-architecture.warn.patch.gz | patch -p0 )

# Configure (options ripped from debian/rules):
CFLAGS="$ARCH_CFLAGS" \
CPPFLAGS="$ARCH_CFLAGS" \
./configure \
   --prefix=/usr \
   --datadir=/usr/share \
   --sysconfdir=/etc \
   --sharedstatedir=/var/lib \
   --localstatedir=/var/lib \
   --with-admindir=/var/lib/dpkg \
   --with-zlib=static \
   --with-static-progs \
   arm-linux || failconfig

# Make. We ignore some of the errors when producing docs and other stuff
# because we don't need them anyway.
make -i 

# Install into package framework:
make -i install DESTDIR=$PKG 

# Enter the package's root:
cd $PKG

# Move docs into the Slackware location:
mv -f usr/share/doc usr
mv -f usr/doc/$PACKAGE usr/doc/$PACKAGE-$VERSION

# Remove the md5sum stuff.  This is included within a/coreutils-*.tgz
find . -iname 'md5sum*' -print0 | xargs -0 rm -f
# I don't know what the purpose of this md5sum-static is (apart from the obvious):
# it may be needed if you're doing some Debian OS upgrade.  I don't know and 
# I don't really care.  I'm just going to make a symlink to the real version
# from coreutils.  I'm sure whatever it is will manifest itself at some stage.
# But since all I use dpkg for (and all I suggest anybody else uses it for) is
# to build l/glibc and d/gcc.
( cd usr/bin && ln -fs md5sum md5sum-static )

# I thought about deleting a lot of the stuff we will never need (bearing in mind
# this package only exists to enable us to do 'make -f debian/rules patch' for
# native gcc and glibc builds.  Then I thought that perhaps I should leave everything
# in incase somebody wants to install a Debian package (not that I'd recommend that).
# I may change my mind later though.
# 29-Dec-04: after looking at the tonnes of stuff included within this package, I 
# am starting to develop a serious aversion to this package.  I'm sure there is
# *tonnes* of stuff that can be removed.  I may look into this again later when
# I upgrade again.  If I become independent of Debian (for glibc and gcc) then I will
# dump this package.

# Slackware policies:
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
   . $PORTCWD/arm/pkger
 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
