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

# aaa_base.SlackBuild
# by Stuart Winter <mozes@slackware.com>
# 1-May-2004
#
# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2018  Patrick J. Volkerding, Sebeka, Minnesota, 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.

# 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

cd $PKG
explodepkg $CWD/_aaa_base.tar.!(*sign|*asc|*sig)

install -vpm644 $CWD/slack-desc $PKG/install
install -vpm644 $CWD/slackware-version $PKG/etc

# Replace the root mail from Patrick with my own.  We don't want
# people mailing Patrick about Slackware ARM:
if [[ "${ARCH}" =~ (arm|aarch64) ]]; then
   #install -vpm640 -oroot -groot $PORTCWD/arm/root.new ${PKG}/var/spool/mail
   # Set the email's date to be the current date:
   # This also updates the time stamp on the file so that when this package
   # is installed, it shows up as a new email.
   xz -dc $PORTCWD/arm/var_spool_mail_root-new.xz | sed 's?%CURRENTDATE%?'"$( date "+%a %b %d %R:%S %Y" )"'?g' > ${PKG}/var/spool/mail/root.new
   chmod 640 ${PKG}/var/spool/mail/root.new
   chown root:root ${PKG}/var/spool/mail/root.new
fi

# Add lib64 for aarch64:
[ "${ARCH}" = "aarch64" ] && mkdir -vpm755 $PKG/{lib64,usr/lib64}

# Edit the /etc/os-release to use Slackware ARM's homepage:
sed \
    -e 's?/questions/slackware-14/?/questions/slackware-arm-108/?g' \
    -e 's?HOME_URL=.*?HOME_URL="https://arm.slackware.com"?g' \
    $CWD/os-release > $PKG/etc/os-release
# Set $ARCH in PRETTY_NAME:
sed -i "s/%ARCH%/$ARCH/g" $PKG/etc/os-release
# Switch to https:
sed -i 's?http://?https://?g' $PKG/etc/os-release
chmod 644 $PKG/etc/os-release
chown root:root $PKG/etc/os-release

# If this is a -current version, label it as such:
if [ -r $CWD/current-release ]; then
   # Read variables:
   . $CWD/current-release
   echo "$SLACKWARE_VERSION" > $PKG/etc/slackware-version
   echo "VERSION_CODENAME=$VERSION_CODENAME" >> $PKG/etc/os-release
   sed -i "s/PRETTY_NAME=.*/PRETTY_NAME=\"$PRETTY_NAME\"/g" $PKG/etc/os-release
 else
   echo "VERSION_CODENAME=stable" >> $PKG/etc/os-release
fi

# Install the Kernel README into /boot:
mkdir -vpm755 ${PKG}/boot
install -vpm644 $PORTCWD/README-kernels.txt ${PKG}/boot/

# Apply generic Slackware packaging policies:
cd $PKG
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

# Update post installation script for ARM:
sed -i 's?"Welcome to Linux (Slackware.*)"?"Welcome to Slackware Linux"?g' 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
