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

# sysvinit-scripts.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>

# 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

# Install Slackware scripts and config files from $CWD/scripts/:
mkdir -vpm755 $PKG/etc/rc.d
for file in rc.{4,6,K,M,S} rc.cpufreq rc.local rc.loop rc.sysvinit rc.modules rc.modules.local ; do
  install -oroot -groot -vpm755 $CWD/scripts/$file $PKG/etc/rc.d/${file}.new
done

install -oroot -groot -vpm644 $CWD/scripts/inittab $PKG/etc/inittab.new

# Install /etc/default/cpufreq.new:
mkdir -p $PKG/etc/default
cat $CWD/default/cpufreq > $PKG/etc/default/cpufreq.new

# Apply RiscPC fix for hwclock:
# 02-Aug-2007: no longer required because the Acorn clock works with
# Linux 2.6.  Wooo!
#
# Jawkins says:
# The RPC RTC chip can only actually count 4 years.
# The year field is only 2 bits long.
# It has some CMOS RAM as well where it stores the year offset.
# Well, as long as you update the CMOS as well.
# Which is driver does appear to do, but something's broken with it somewhere.
#( cd $PKG/etc/rc.d
#  patch --no-backup-if-mismatch --verbose -lp0 < $PORTCWD/sources/rc.6.diff
#  patch --no-backup-if-mismatch --verbose -lp0 < $PORTCWD/sources/rc.S.diff || failpatch ) || failpatch

# Stop fc-cache from complaining on machines where the system clock isn't
# set during boot -- ARM Versatile under QEMU, for example:
# http://www.mail-archive.com/debian-bugs-dist@lists.debian.org/msg266900.html
( cd $PKG/etc/rc.d
  patch -p0 --verbose < $PORTCWD/sources/fc-cache.diff
  rm -f *.orig )

# Add the /etc/rc.d/rc.platform script to perform post OS boot
# tasks, such as setting the fan speed and LCD brightness.
install -vpm755 $PORTCWD/sources/rc.platform $PKG/etc/rc.d/
# Add it to /etc/rc.d/rc.M to execute immediately prior to /etc/rc.d/rc.local
sed -i '\?^# Start the local setup procedure.*? i\# Configure the Hardware Model platform:' $PKG/etc/rc.d/rc.M.new || failpatch
sed -i '\?^# Configure the Hardware Model platform.*? a\[ -x /etc/rc.d/rc.platform ] && /etc/rc.d/rc.platform\n' $PKG/etc/rc.d/rc.M.new || failpatch

# Install a user configuration file as an example.
# This file is user serviceable:
install -vpm644 $PORTCWD/sources/rc.platform.conf-$SLKPORTARCH $PKG/etc/rc.d/rc.platform.conf.new

# Install any platform and Hardware Model-specific post OS boot scripts:
# These are not intended to be user serviceable:
if [ -d $PORTCWD/sources/etc-platform-skeleton/platform/$SLKPORTARCH ]; then
   mkdir -vpm755 $PKG/etc/rc.d/rc.platform.d/$SLKPORTARCH
   cp -fa $PORTCWD/sources/etc-platform-skeleton/platform/$SLKPORTARCH $PKG/etc/rc.d/rc.platform.d/
   chown -R root:root $PKG/etc/rc.d/rc.platform.d
   find $PKG/etc/rc.d/rc.platform.d/ -type f -print0 | xargs -0 chmod 644
   find $PKG/etc/rc.d/rc.platform.d/ -type d -print0 | xargs -0 chmod 755
fi

# 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

# For the Marvell SheevaPlug unit (and probably many other embedded ARM devices)
# we want to open a serial login on ttyS0.
# It's important that this little script is first in the post-install
# script, so that this in-place hack appears as if it was always in
# the original shipped package file; this allows us to (in theory) upgrade
# the sysvinit-scripts package and not leave an /etc/inittab.new file around
# (most people don't edit their inittab files I don't think!)
mkdir -vpm755 $PKG/install
cat $PORTCWD/sources/doinst.sh-set-serial-port >> install/doinst.sh

# Install slack-desc and doinst.sh.  The standard doinst.sh is appended
# to the serial handling code above.
slackdesc

# Install /etc/rc.d/rc.platform.conf.new as /etc/rc.d/rc.platform.conf
# if there's not one already present on the file system:
# This has to be placed after installing the standard doinst.sh because
# the 'config' file handling function needs to be defined prior to
# calling it.
echo 'config etc/rc.d/rc.platform.conf.new' >> 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
