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

# etc.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-2004
#
# Copyright 2005-2018  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 and re-create temporary directories

# Explode the package framework:
cd $PKG
explodepkg $CWD/_etc.tar.!(*sign|*asc|*sig)
mkdir -p $PKG/usr/etc # there is a printcap symlink in here

zcat $CWD/nsswitch.conf.gz > $PKG/etc/nsswitch.conf.new

# Check to make sure that shadow has the same number of entries
# as passwd:
if [ ! "$(cat $CWD/passwd.new | wc -l)" = "$(cat $CWD/shadow.new | wc -l)" ]; then
  echo "#######################################################################################"
  echo "# WARNING: /etc/passwd.new and /etc/shadow.new don't have the same number of entries. #"
  echo "#######################################################################################"
  sleep 60
fi
# Install default passwd/shadow/group (sorted):
sort -n -t ':' -k3 $CWD/passwd.new > $PKG/etc/passwd.new
awk -F':' 'NR==FNR{z[$1]=$0;next}{print z[$1]}' $CWD/shadow.new $PKG/etc/passwd.new > $PKG/etc/shadow.new
sort -n -t ':' -k3 $CWD/group.new > $PKG/etc/group.new

# Fix for ARCH:
sed -i -e "s#/i586-slackware-linux#/${SLK_ARCH_HOST}#" etc/ld.so.conf.new

# Fix paths if lib64 is needed:
if [[ "${ARCH}" =~ (x86_64|aarch64) ]]; then
  sed -i -e "s#/lib#/lib64#" etc/ld.so.conf.new
fi

# Apply some of the generic Slackware packaging policies:
slackdesc       # install slack-desc

# Install package post installation script:
mkdir -vpm755 $PKG/install

# Allow root login on some of the known ARM serial consoles device names:
# The install script used to check the machine type during installation, and
# append the appropriate device names, but now it's a static list which
# could be moved back into this script.  However, it serves as a useful reminder
# should I want to go back to the previous way (unlikely though as it causes
# problems for the 'miniroot' since the machine building the root isn't the
# same as those for which the package modifications may be intended).
install -vpm644 $PORTCWD/sources/doinst.sh.serialconsole-allowroot $PKG/install/doinst.sh
# Append the standard (non ARM) post installation script:
zcat $CWD/doinst.sh.gz | sed -e 's?^#!.*??' >> $PKG/install/doinst.sh

slackgzpages -i # compress man & info pages and delete usr/info/dir
slackmp         # run makepkg -l y -c n

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