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

VERSION=${VERSION:-2.5}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG

# @#$%...  We don't want grep to depend on a shared PCRE:
if [ -r /usr/lib/libpcre.la ]; then #$%@
  mv /usr/lib/libpcre.so $TMP
  mv /usr/lib/libpcre.la $TMP
fi

cd $TMP
rm -rf grep-$VERSION
tar xjvf $CWD/grep-$VERSION.tar.bz2
cd grep-$VERSION

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --bindir=/bin \
  --mandir=/usr/man \
  --infodir=/usr/info

make -j3 || exit 1

# Install the binary:
mkdir -p $PKG/bin
cat src/grep > $PKG/bin/grep
chmod 755 $PKG/bin/grep

# Strip binary:
( 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
)

# Make symlinks in /bin:
( cd $PKG/bin
  ln -sf grep egrep
  ln -sf grep fgrep
)

# Make symlinks in /usr/bin:
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin
  ln -sf ../../bin/grep .
  ln -sf ../../bin/egrep .
  ln -sf ../../bin/fgrep .
)

# Install man and info pages:
mkdir -p $PKG/usr/man/man1
( cd doc
  cat grep.1 | gzip -9c > $PKG/usr/man/man1/grep.1.gz
  cat egrep.1 | gzip -9c > $PKG/usr/man/man1/egrep.1.gz
  cat fgrep.1 | gzip -9c > $PKG/usr/man/man1/fgrep.1.gz
  mkdir -p $PKG/usr/info
  for file in *.info* ; do
    cat $file | gzip -9c > $PKG/usr/info/$file.gz
  done
)

# Install NLS files:
( cd po
  for file in *.gmo ; do
    LOC=`basename $file .gmo`
    mkdir -p $PKG/usr/share/locale/$LOC/LC_MESSAGES
    cat $file > $PKG/usr/share/locale/$LOC/LC_MESSAGES/grep.mo
  done
)

mkdir -p $PKG/usr/doc/grep-$VERSION
cp -a \
  ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README README-alpha README.DOS THANKS TODO \
  $PKG/usr/doc/grep-$VERSION

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

# @#$%...  Put back the shared PCRE:
if [ -r $TMP/libpcre.la ]; then
  mv $TMP/libpcre.so /usr/lib
  mv $TMP/libpcre.la /usr/lib
fi

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

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