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

VERSION=3.1.1
ARCH=i386
BUILD=2

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

echo "+============+"
echo "| gawk-$VERSION |"
echo "+============+"
cd $TMP
tar xzvf $CWD/gawk-$VERSION.tar.gz
cd gawk-$VERSION
CFLAGS="-O2 -march=i386 -mcpu=i686" ./configure --prefix=/usr i386-slackware-linux
make
mkdir -p $PKG/usr $PKG/bin
make install prefix=$PKG/usr
( cd $PKG/usr/bin
  ln -sf gawk-3.1.1 gawk
  ln -sf pgawk-3.1.1 pgawk
  strip * )
mv $PKG/usr/bin/gawk* $PKG/bin
mv $PKG/usr/bin/awk $PKG/bin
( cd $PKG/usr/bin
  ln -sf ../../bin/awk .
  ln -sf ../../bin/gawk . )
chown -R root.bin $PKG/bin $PKG/usr/bin
( cd $PKG/usr/info
  rm dir
  gzip -9 * )
strip $PKG/usr/libexec/awk/*
gzip -9 $PKG/usr/man/man1/*
( cd $PKG/usr/man/man1
  ln -sf gawk.1.gz awk.1.gz )
mkdir -p $PKG/usr/doc/gawk-$VERSION
cp -a \
  ABOUT-NLS AUTHORS COPYING FUTURES INSTALL LIMITATIONS NEWS POSIX.STD PROBLEMS README README_d \
  $PKG/usr/doc/gawk-$VERSION
chown -R root.root $PKG/usr/doc/gawk-$VERSION
find $PKG/usr/doc/gawk-$VERSION -type d -exec chmod 755 {} \;
find $PKG/usr/doc/gawk-$VERSION -type f -exec chmod 644 {} \;
# dunno why this gets inserted here, but it's wrong
rm -f $PKG/usr/share/locale/locale.alias
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/gawk-$VERSION
  rm -rf $PKG
fi
