#!/bin/bash

# slrn.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 13-Hul-2004

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

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Create package framework:
mkdir -p $PKG/{etc,usr/doc,/var/spool/slrnpull/{data,news,out.going/rejects}}

# Apply patches:
zcat $CWD/slrn.slrnfeat.diff.gz | patch -p1

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --with-docdir=/usr/doc/$PACKAGE-$VERSION \
   --sysconfdir=/etc \
   --with-ssl \
   --with-slrnpull \
   --enable-setgid-code \
   --with-server-file=/etc/nntpserver || failconfig

# Build:
make || failmake

# Start fake root server if necessary:
start_fakeroot

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

# Docs:
rm -f $PKG/usr/doc/$PACKAGE-$VERSION/changes

# Permissions & ownerships:
chown root.news $PKG/usr/bin/slrnpull
chmod 2750 $PKG/usr/bin/slrnpull
chown -R news.news $PKG/var/spool/slrnpull
chmod 3777 $PKG/var/spool/slrnpull/out.going $PKG/var/spool/slrnpull/out.going/rejects

# Config files:
install -m644 $PKG/usr/doc/slrn-$VERSION/slrn.rc $PKG/etc/slrn.rc
cp -a $PKG/usr/doc/$PACKAGE-$VERSION/slrnpull/slrnpull.conf $PKG/var/spool/slrnpull

# Slackware policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
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
