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

# util-linux.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for ARMedslack.
#
# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2017  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

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

# Versions of bundled utils:
ADJTIMEXVERS=1.29
SETSERIALVERS=2.17
ZIPTOOLVERS=1.4.0

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Choose correct options depending on whether PAM is installed:
if [ -L /lib${LIBDIRSUFFIX}/libpam.so.? ]; then
   LOGIN_OPTIONS=""
   # By default, don't use the util-linux version of /bin/su:
   SHIP_SU=${SHIP_SU:-NO}
   # Add /etc/pam.d config files:
   rm -rf $PKG/etc/pam.d
   mkdir -p $PKG/etc/pam.d
   for file in $CWD/pam.d/* ; do
     cp -a ${file} $PKG/etc/pam.d/
   done
   if [ "$SHIP_SU" = "YES" ]; then
      # Add su default file to ensure using ENV_SUPATH with 'su':
      mkdir -p $PKG/etc/default
      cp -a $CWD/su.default $PKG/etc/default/su.new
      chown root:root $PKG/etc/default/su.new
      chmod 644 $PKG/etc/default/su.new
      # Add su /etc/pam.d files:
      cp -a $CWD/pam.d-su/* $PKG/etc/pam.d/
   fi
   # Ensure correct perms/ownership on files in /etc/pam.d/:
   chown root:root $PKG/etc/pam.d/*
   chmod 644 $PKG/etc/pam.d/*
   # Don't clobber existing config files:
   find $PKG/etc/pam.d -type f -exec mv {} {}.new \;
 else
   # Don't use the util-linux version of /bin/su.
   # It can't be built without PAM anyway.
   SHIP_SU=NO
   LOGIN_OPTIONS="--disable-login"
fi

# Changing the fdisk -l output (this was done prior to util-linux) broke
# our installation scripts, so we have changed the name of partition type
# 83 back to "Linux swap":
zcat $CWD/util-linux.fdisk-no-solaris.diff.gz | patch -p1 || exit 1

# Revert to the behavior of earlier versions of fdisk which did not list the
# /dev/ram* devices in 'fdisk -l' output:
zcat $CWD/util-linux.do.not.list.ram.devices.diff.gz | patch -p1 --verbose || exit 1

# Regenerate, since patch 0001 hits mount/Makefile.am:
#./autogen.sh

# This just call the configure script with $1 either being 2 for python2
# or 3 for python3.
#
# The releasenotes say to use --runstatedir, but it doesn't work and the
# configure script fails. So we'll leave things using localstatedir, but
# check again later:
#
#   --runstatedir=/run
configure_with_python(){
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --docdir=/usr/doc/util-linux-$VERSION \
   --disable-static \
   --enable-agetty \
   --enable-fdformat \
   --enable-kill \
   --disable-last \
   --enable-libmount-support-mtab \
   --enable-line \
   --enable-libmount \
   --enable-mesg \
   --disable-raw \
   --enable-rename \
   --enable-partx \
   --enable-pg \
   $LOGIN_OPTIONS \
   --enable-schedutils \
   --enable-wall \
   --enable-write \
   --enable-use-tty-group \
   --enable-libblkid \
   --enable-libuuid \
   --disable-uuidd \
   --with-python=$1 \
   --build=${SLK_ARCH_BUILD} \
   --host=${SLK_ARCH_HOST} || failconfig
}

# Configure, with Python2:
CFLAGS="$SLKCFLAGS" \
  configure_with_python 2 || exit 1

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

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

# Configure, with Python3 bindings for libmount:
make clean
  configure_with_python 3 || exit 1
make $NUMJOBS \
  install-pylibmountexecLTLIBRARIES \
  install-pylibmountexecPYTHON \
  DESTDIR=$PKG || failmake

# Remove all overlap with the shadow package, since --disable-login doesn't quite do that:
rm -fv $PKG/sbin/nologin $PKG/usr/man/man8/nologin.8 $PKG/sbin/sulogin $PKG/usr/man/man8/sulogin.8

# If we aren't shipping su, remove the files:
if [ "$SHIP_SU" = "NO" ]; then
   rm -fv $PKG/bin/su
   rm -fv $PKG/usr/man/man1/su.1
   rm -fv $PKG/usr/share/bash-completion/completions/su
   rm -fv $PKG/usr/share/bash-completion/completions/runuser
fi

# Move the libblkid, libfdisk, libmount, libsmartcols, and
# libuuid libraries to /lib${LIBSUFFIX}:
mkdir -p $PKG/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  mv libblkid.so.1* $PKG/lib${LIBDIRSUFFIX}
  mv libsmartcols.so.1* $PKG/lib${LIBDIRSUFFIX}
  mv libfdisk.so.1* $PKG/lib${LIBDIRSUFFIX}
  mv libmount.so.1* $PKG/lib${LIBDIRSUFFIX}
  mv libuuid.so.1* $PKG/lib${LIBDIRSUFFIX}
  rm -f libblkid.so libfdisk.so libmount.so libsmartcols.so libuuid.so
  ln -sf ../../lib${LIBDIRSUFFIX}/libblkid.so.1 libblkid.so
  ln -sf ../../lib${LIBDIRSUFFIX}/libfdisk.so.1 libfdisk.so
  ln -sf ../../lib${LIBDIRSUFFIX}/libmount.so.1 libmount.so
  ln -sf ../../lib${LIBDIRSUFFIX}/libsmartcols.so.1 libsmartcols.so
  ln -sf ../../lib${LIBDIRSUFFIX}/libuuid.so.1 libuuid.so
)

# Moving things around that have been in the same place
# for 15 years is, IMHO, not a wise idea AT ALL.
# If this had to be moved, some place out of /usr might
# have shown a grain of insight...
if [ -r $PKG/usr/sbin/fdformat ]; then
  mkdir -pm755 $PKG/usr/bin
  mv -fv $PKG/usr/sbin/fdformat $PKG/usr/bin/fdformat
  # Now since stuff will start looking in this new place,
  # we have no choice but to link these:
  ( cd $PKG/usr/sbin
    ln -vsf ../bin/fdformat .
  )
fi

# Add the rc.setterm init script to set default terminal attributes:
mkdir -p $PKG/etc/rc.d
install -vpm755 $CWD/rc.setterm $PKG/etc/rc.d/rc.setterm.new

# ddate.{1,c} taken from https://github.com/bo0ts/ddate:
cc -O -o ddate $CWD/ddate.c
install -vpm755 ddate $PKG/usr/bin/
install -vpm644 $CWD/ddate.1 $PKG/usr/man/man1/

# Copy docs:
mkdir -pm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -favv \
   AUTHORS COPYING* DEPRECATED NEWS README* \
   Documentation/v${VERSION}-ReleaseNotes \
  Documentation/licenses/* Documentation/{TODO,*.txt} \
  $PKG/usr/doc/$PKGNAM-$VERSION

###########################
# Build ziptool
cd $TMPBUILD
tar xvvf $CWD/ziptool-$ZIPTOOLVERS.tar.!(*sign|*asc|*sig) || exit 1
cd ziptool-*/ || exit 1
zcat $CWD/ziptool-fix_build.patch.gz | patch -p1 || exit 1
mkdir scsi
install -vpm644 $CWD/scsi_ioctl.h scsi/
slackhousekeeping
make clean
make || exit 1
install -vpm755 ziptool $PKG/sbin/jaztool
install -vpm644 ziptool.1.gz $PKG/usr/man/man1/jaztool.1.gz
mkdir -vpm755 $PKG/usr/doc/ziptool-$ZIPTOOLVERS
cp -a README $PKG/usr/doc/ziptool-$ZIPTOOLVERS

#####################################################
# Build bsdstrings
#####################################################

cd $TMPBUILD
tar xvvf $CWD/bsdstrings.tar.!(*sign|*asc|*sig)
cd bsdstrings || exit 1
zcat $CWD/bsdstrings-util-linux_overflow.diff.gz | patch -p1 --verbose || exit 1

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

# Install into package:
install -vpm755 strings $PKG/usr/bin/strings-BSD
install -vpm644 strings.1 $PKG/usr/man/man1/strings-BSD.1

#####################################################
# Build adjtimex
#####################################################

cd $TMPBUILD
tar xvvf $CWD/adjtimex_${ADJTIMEXVERS}.orig.tar.!(*sign|*asc|*sig) || failextract
cd adjtimex-$ADJTIMEXVERS || exit 1

# Apply patches:
zcat $CWD/adjtimex_1.29-2.2.diff.gz | patch -p1 || exit 1

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

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

# Install into package:
install -vpm755 adjtimex $PKG/sbin/
install -vpm644 adjtimex.8 $PKG/usr/man/man8/

# Copy docs:
mkdir -p $PKG/usr/doc/adjtimex-$ADJTIMEXVERS
cp -a COPYING COPYRIGHT README README.ru adjtimex.lsm \
      $PKG/usr/doc/adjtimex-$ADJTIMEXVERS

#####################################################
# Build setserial
#####################################################
cd $TMPBUILD
tar xvvf $CWD/setserial-$SETSERIALVERS.tar.!(*sign|*asc|*sig) || failextract
cd setserial-$SETSERIALVERS
zcat $CWD/setserial-rc.serial.diff.gz | patch -E -p1 --verbose || failpatch
zcat $CWD/setserial-undef_TIOCGHAYESESP.diff.gz | patch -E -p1 --verbose || exit 1

# The original config.{guess,sub} do not work on x86_64
cp -favv /usr/share/libtool/config/config.{guess,sub} .

# Configure:
./configure \
   --prefix=/usr || failconfig

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

# Install into package:
install -vpm755 setserial $PKG/sbin
mkdir -p $PKG/etc/rc.d
cat rc.serial > $PKG/etc/rc.d/rc.serial.new
cat serial.conf > $PKG/etc/serial.conf.new
install -vpm644 setserial.8 $PKG/usr/man/man8/

# These have always traditionally been available before /usr
# might be mounted:
( cd $PKG/usr/bin
  mv -fv getopt setterm $PKG/bin
  cd $PKG/usr/bin
  ln -vfs ../../bin/getopt .
  ln -vfs ../../bin/setterm .
)

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

# Now let's add some important symlinks :)
( cd $PKG/sbin
  ln -vfs ../bin/mount .
  ln -vfs ../bin/umount .
  #  ln -vfs ziptool jaztool
  ln -vfs hwclock clock
  cd $PKG/usr/sbin
  ln -vfs ../../sbin/cfdisk .
  ln -vfs ../../sbin/ctrlaltdel .
  ln -vfs ../../sbin/sfdisk .
  cd $PKG/usr/bin
  ln -vfs ../sbin/readprofile .
  # tunelp was removed by upstream:
  #ln -s ../sbin/tunelp .
  ln -vfs ../../bin/more .
  cd $PKG/usr/man/man1
  # ln -vfs ziptool.1 jaztool.1
  cd $PKG/usr/man/man8
  ln -vfs hwclock.8 clock.8
)

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

# These need to be setuid root to work properly (only built for PAM):
if [ -r $PKG/usr/bin/chfn ]; then
  chmod 4711 $PKG/usr/bin/chfn
fi
if [ -r $PKG/usr/bin/chsh ]; then
  chmod 4711 $PKG/usr/bin/chsh
fi

slackmp         # run makepkg -l y -c n

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