#!/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, 2009, 2010, 2013, 2015, 2018, 2020  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:
XSB=$CWD/$PKGNAM.SlackBuild
[ ! -x $XSB ] && { echo "ERROR: Cannot find x86 SlackBuild for ${PKGNAM}!" ; exit 1; }
for upstreamvar in \
  RADVER \
  PPPVER \
  ; do
  eval $( egrep "^${upstreamvar}=" $XSB  )
  echo "Upstream variable: ${upstreamvar} version $( eval $( echo "echo \$${upstreamvar}" ) )"
done

# 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 xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
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 --backup --suffix=.orig || exit 1
sed -i -e "s#lib/pppd#lib${LIBDIRSUFFIX}/pppd#g" $(grep -lr 'lib/pppd' *)
# This conflicts with the header in 3.5+ kernels:
rm -f include/linux/if_pppol2tp.h
zcat $CWD/ppp.CVE-2015-3310.diff.gz | patch -p1 --verbose || exit 1

# Choose correct options depending on whether PAM is installed:
if [ -L /lib${LIBDIRSUFFIX}/libpam.so.? ]; then
   PAM_OPTIONS="USE_PAM=y"
else
   unset PAM_OPTIONS
fi

# Configure:
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} || failconfig

# Build:
make $PAM_OPTIONS $NUMJOBS || make $PAM_OPTIONS || exit 1

# Install into package:
make install DESTDIR=$PKG/usr || exit 1

if [ ! -z "$PAM_OPTIONS" ]; then
   mkdir -p $PKG/etc/pam.d
   install -vpm644 pppd/ppp.pam $PKG/etc/pam.d/ppp.new
fi

# Install PPP config files:
mkdir -p $PKG/etc/ppp
cp -a 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${LIBDIRSUFFIX}/pppd/*/*.so

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

# Copy docs:
mkdir -p $PKG/usr/doc/ppp-$VERSION
cp -fav \
  FAQ PLUGINS README* SETUP scripts \
  $PKG/usr/doc/ppp-$VERSION

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

tar xf $CWD/freeradius-client-$RADVER.tar.!(*sign|*asc|*sig) || exit 1
cd freeradius-client-* || exit 1
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 xvvf $CWD/pppsetup-$PPPVER.tar.!(*sign|*asc|*sig)
cd pppsetup-*/ || exit 1
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
slackmp         # run makepkg -l y -c n

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