#!/bin/bash

# xinetd.SlackBuild
# by Stuart Winter <mozes@armedslack.org>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source:
export PORTCWD=$PWD CWD=$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" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar zxvvf $CWD/sources/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# Configure:
./configure \
   --prefix=/usr \
   --with-loadavg \
   --with-inet6 \
   --with-libwrap \
   --build=$ARCH-slackware-linux \
   --host=$ARCH-slackware-linux \
   --target=$ARCH-slackware-linux || failconfig

# Install into package:
mkdir -pm755 $PKG/etc/rc.d
mkdir -pm755 $PKG/usr/{sbin,man/man{5,8}}
make install DAEMONDIR=$PKG/usr/sbin MANDIR=$PKG/usr/man
# Install run control script:
install -m755 $CWD/rc.xinetd $PKG/etc/rc.d/
# Install a default sample config:
install -m600 $CWD/xinetd.conf-distrib $PKG/etc/xinetd.conf.new

# Remove the inetd.conf > xinetd.conf converter programs cos they're
# not much use really since this xinetd package is meant to be installed
# on to a inetd-less Slackware 8.1 box (as you can not opt to not install inetd
# in the installer)
( cd $PKG
  rm -f usr/sbin/{itox,xconv.pl} usr/man/man8/{itox*,xconv*} 
  # Remove some section 3 man pages which weren't installed in the
  # correct place.
  rm -f usr/man/*.3 )

# Copy the docs into our 'local' build dir.
# We also copy the sample config into here.  I'm not including this 
# sample in /etc because it's too open.
mkdir -p $PKG/usr/doc/${PACKAGE}-${VERSION}
cp -a AUDIT INSTALL README TODO CHANGELOG COPYRIGHT xinetd/sample.conf \
      $PKG/usr/doc/${PACKAGE}-${VERSION}/

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
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, slack644docs
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
