#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# ppp.SlackBuild
# based on the original Slackware build scripts,
# Extensively modified by Stuart Winter <mozes@slackware.com>
# 30-Sep-2004
#
# Copyright 2008-2025  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull the version numbers from the x86 trunk:
slack_setvarfromupstream RADVER PPPVER

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

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

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

# Extract source:
cd $TMPBUILD
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM*/ || failextract
slackhousekeeping

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

# Apply patches:
sed -i -e "s#lib/pppd#lib${LIBDIRSUFFIX}/pppd#g" $(grep -lr 'lib/pppd' *)

# Configure:
slack_autotoolsprep
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --disable-static \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# Install PPP config files:
rm -f $PKG/etc/ppp/*
cp -fav etc.ppp/* $PKG/etc/ppp
for file in $PKG/etc/ppp/*.example ; do
  mv -fv $file $(dirname $file)/$(basename $file .example)
done
( cd $PKG/etc/ppp
  mv -fv chap-secrets chap-secrets.new
  mv -fv options options.new
  mv -fv pap-secrets pap-secrets.new )

zcat $CWD/options.new.gz > $PKG/etc/ppp/options.new
mkdir -p $PKG/etc/pam.d
install -vpm644 pppd/ppp.pam > $PKG/etc/pam.d/ppp.new

# The Makefile doesn't error out if building pppoe.so fails, so check for it:
if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/pppd/${VERSION}/pppoe.so ]; then
  echo "FATAL: /usr/lib${LIBDIRSUFFIX}/pppd/${VERSION}/pppoe.so failed to build."
  exit 1
fi

# Copy docs:
mkdir -p $PKG/usr/doc/ppp-$VERSION
cp -fav \
  AUTHORS* COPYING* ChangeLog* FAQ* LICENSE* NEWS* PLUGINS* README* SECURITY* SETUP* Submitting-patches* \
  scripts \
  $PKG/usr/doc/ppp-$VERSION
rm -f $PKG/usr/doc/ppp-$VERSION/scripts/Makefile*

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

tar xf $CWD/freeradius-client-$RADVER.tar.!(*sign|*asc|*sig)
cd freeradius-client-*/ || failextract
slackhousekeeping

printf "\t\t\t\t\t\t\t[***] Building radiusclient [***]\n"
# Configure:
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --enable-shadow \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS CC="gcc $SLKCFLAGS" || make 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

# 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 -p $PKG/usr/doc/freeradius-client-$RADVER
cp -fav \
  BUGS CHANGES COPYRIGHT README README.radexample doc/instop.html \
  $PKG/usr/doc/freeradius-client-$RADVER

# Don't ship the static library:
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/libfreeradius-client.a

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

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

# Extract source:
cd $TMPBUILD
tar xf $CWD/pppsetup-$PPPVER.tar.!(*sign|*asc|*sig)
cd pppsetup-*/ || failextract
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 -fav \
  README.pppsetup ppp-compile.txt pppsetup-$PPPVER.README pppsetup-$PPPVER.lsm \
  $PKG/usr/doc/pppsetup

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

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
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

# Special permissions:
chmod 600 $PKG/etc/ppp/*secrets

slackmp         # run makepkg -l y -c n

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