#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-patch

VERSION=2.5.4
ARCH=${ARCH:-i486}
BUILD=1

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf patch-2.5.4
tar xzvf $CWD/patch-2.5.4.tar.gz
cd patch-2.5.4
chown -R root:root .
mkdir -p $PKG/usr/doc/patch-2.5.4
cp -a AUTHORS COPYING NEWS README $PKG/usr/doc/patch-2.5.4
chmod 644 $PKG/usr/doc/patch-2.5.4/*
./configure \
  --prefix=/usr \
  $ARCH-slackware-linux
make CFLAGS=-O2 || exit 1
mkdir -p $PKG/usr/bin
cat patch > $PKG/usr/bin/patch
chmod 755 $PKG/usr/bin/patch
mkdir -p $PKG/usr/man/man1
cat patch.man  | gzip -9c > $PKG/usr/man/man1/patch.1.gz

# Strip everything for good measure:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/patch-$VERSION-$ARCH-$BUILD.tgz