#!/bin/bash

# telnet.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/netkit-$PKGNAM-$VERSION.tar.gz
cd netkit-$PKGNAM*
slackhousekeeping

# Apply patches:
zcat $CWD/netkit-telnet-0.17.diff.gz | patch -p1 --verbose --backup --suffix=.orig      || failpatch
zcat $CWD/netkit-telnet-0.17-ayt.patch.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch
./configure \
   --prefix=/usr || failconfig
cd telnetd
make $NUMJOBS || failmake
mkdir -p $PKG/usr/sbin
install -m755 telnetd $PKG/usr/sbin/in.telnetd
mkdir -p $PKG/usr/man/man8
install -m644 telnetd.8 $PKG/usr/man/man8/
cd ..

# The OpenBSD version of the telnet client is superior.
rm -rf telnet
tar zxvvf $CWD/telnet-OpenBSD-20020321.tar.gz
zcat $CWD/telnet-OpenBSD-20020321.diff.gz | patch -p1 --verbose || failpatch
zcat $CWD/telnet-OpenBSD-014_telnet.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch
cd telnet
make $NUMJOBS || failmake
mkdir -p $PKG/bin
install -m755 telnet $PKG/bin/
mkdir -p $PKG/usr/man/man1
install -m644 telnet.1 $PKG/usr/man/man1/
cd ..
mkdir -p $PKG/usr/doc/netkit-telnet-0.17
cp -a README $PKG/usr/doc/netkit-telnet-0.17

# 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
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
