#!/bin/bash

# ppp.SlackBuild
# based on the original Slackware build scripts,
# Extensively modified by Stuart Winter <mozes@slackware.com>
# 30-Sep-2004

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

# Update this with the current version of radius client:
RADIUSCLIENTVER=0.3.2
# Version of ppp setup:
PPPSETUPVER=1.98

# 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=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

################################ Build radiusclient ##################################

# Use newer version of radiusclient rather than the old crufy version included with
# ppp's source:
tar xvvf $PORTCWD/sources/radiusclient*orig*.gz
cd radiusclient* || exit 1
slackhousekeeping

# Apply Debian's diff. It's probably not really necessary but the Debian Changelog
# suggests that it fixes some bugs I'd rather not see:
zcat $PORTCWD/sources/radiusclient*diff*.gz | patch -p1 --verbose 

# Slackware build script uses this, but I've never needed to:
#cp -favv /usr/share/libtool/config/config.{guess,sub} .       # libtool-2.x

printf "\t\t\t\t\t\t\t[***] Building radiusclient [***]\n"
# Configure:
./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --enable-shadow || failconfig

# Nasty old libtool version, off you go:
rm -vf libtool
cp -fav /usr/bin/libtool .

# Build:
make $NUMJOBS CC="gcc $SLKCFLAGS" || failmake 

# and finally install into the package:
make install DESTDIR=$PKG  || failinstall 
zcat $CWD/radius.msdict.gz > $PKG/etc/radiusclient/dictionary.microsoft
zcat $CWD/realms.gz > $PKG/etc/radiusclient/realms
zcat $CWD/servers.gz > $PKG/etc/radiusclient/servers

# Prevent clobber of config files:
( cd $PKG/etc/radiusclient
  chmod 600 realms servers
  mv issue issue.new
  mv radiusclient.conf radiusclient.conf.new
  mv realms realms.new
  mv servers servers.new )

# Copy docs:
mkdir -pm755 $PKG/usr/doc/radiusclient-$RADIUSCLIENTVER
cp -fav BUGS CHANGES COPYRIGHT README README.radexample doc/instop.html \
        $PKG/usr/doc/radiusclient-RADIUSCLIENTVER

################################ Build ppp ############################################

printf "\t\t\t\t\t\t\t[***] Building ppp [***]\n"

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
#tar xvvf $PORTCWD/sources/$PKGNAM-cvs-20040427.tar.gz
cd $PKGNAM* || exit 1
slackhousekeeping

# Switch /usr/local with /usr.  This saves having to pass a number of
# variables to 'make'.
fgrep -lr -- '/usr/local' . | xargs sed -i 's?/usr/local?/usr?g'

# Apply patches (these are in Slackware):
zcat $CWD/ppp.slack.diff.gz | patch -p1 --verbose || failpatch

# Fix net/bpf.h problem (this header has been renamed in versions of libpcap >0.7)
# I took this patch from Debian's diff:
#zcat $PORTCWD/sources/ppp-2.4.2-libpcap.diff.gz | patch -p1

# Add the radius client plugin to the build list.
# No, don't do this because we need to specify some additional configuration
# bits to radiusclient which we can't do if we let the main build handle it.
#perl -p -i -e "s?# SUBDIRS \+= radius?SUBDIRS += radius?g" pppd/plugins/Makefile.linux

# Configure:
CFLAGS="$SLKCFLAGS -D_GNU_SOURCE" \
./configure \
   --prefix=/usr || failconfig

# Build:
make clean
make $NUMJOBS CC="gcc $SLKCFLAGS -D_GNU_SOURCE" \
     BINDIR=/usr/sbin \
     LIBDIR=/usr/lib/pppd/$VERSION \
     MANDIR=/usr/man \
     INSTALL="/usr/bin/install -oroot " || failmake
# This is legacy from before I replaced the radiusclient stuff -- we don't need this anymore.
#make -C pppd/plugins -f Makefile.linux || failmake

# Install into package:
make install \
     BINDIR=$PKG/usr/sbin \
     MANDIR=$PKG/usr/man \
     ETCDIR=$PKG/etc/ppp \
     LIBDIR=$PKG/usr/lib/pppd/$VERSION \
     INSTALL="/usr/bin/install -oroot" \
     DESTDIR=$PKG || failinstall
mv -fv $PKG/usr/share/man $PKG/usr
rm -rfv $PKG/usr/share
# These gets misplaced:
mkdir -pm755 $PKG/usr/man/man8
mv -fv $PKG/usr/man/*.8 $PKG/usr/man/man8/

# For some reason pppd/Makefile has put the pppd include files into
# the package's root directory.  From a quick glance I cannot see why it's
# done this.
mkdir -pm755 $PKG/usr/include
mv -f $PKG/include/pppd $PKG/usr/include
rm -rvf $PKG/include

# Install PPP config files:
mkdir -pm755 $PKG/etc/ppp
cp -fav etc.ppp/* $PKG/etc/ppp
chmod 600 $PKG/etc/ppp/*secrets
( cd $PKG/etc/ppp
  mv chap-secrets chap-secrets.new
  mv options options.new
  mv pap-secrets pap-secrets.new )
zcat $CWD/options.new.gz > $PKG/etc/ppp/options.new

# Fix what seems like an insecure default setting.
# Feel free to "chmod 4750 pppoatm.so rp-pppoe.so" at your own risk.
# Since they are only runnable by group root, the risk really isn't much...
chmod 755 $PKG/usr/lib/pppd/*/*.so

# Install docs:
mkdir -pm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -a FAQ PLUGINS README* SETUP scripts \
      $PKG/usr/doc/$PKGNAM-$VERSION

########################### Build pppsetup #############################################

printf "\t\t\t\t\t\t\t[***] Building pppsetup [***]\n"

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/pppsetup-$PPPSETUPVER.tar.gz
cd pppsetup-*
slackhousekeeping

# Apply patches:
zcat $CWD/pppsetup-1.98.slack.diff.gz       | patch -p1 --backup
zcat $CWD/pppsetup-1.98.pppoff.diff.gz      | patch -p0 --backup
zcat $CWD/pppsetup-1.98.moredevs.diff.gz    | patch -p1 --backup
zcat $CWD/pppsetup-1.98.backupfiles.diff.gz | patch -p1 --backup

# Install:
install -m755 ppp-off pppsetup $PKG/usr/sbin

# Install docs:
mkdir -p $PKG/usr/doc/pppsetup
cp -a README.pppsetup ppp-compile.txt pppsetup-${PPPSETUPVER}README pppsetup-${PPPSETUPVER}.lsm \
      $PKG/usr/doc/pppsetup

#########################################################################################

# 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

# Quick hack: these two files no longer exist in the newer version of radiusclient
# so I'll remove them from the install script.
# 29-Jul-2005: Puh, well, they're in Slackware's which now includes the same version of
#              the radiusclient, so back in they go..
#zcat $CWD/doinst.sh.gz | egrep -v "realms.new|servers.new" > $PKG/install/doinst.sh

slackmp         # run makepkg -l y -c n

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