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

# sysvinit.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2005-2026  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.

# 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

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

# Fix paths for /etc/forcefsck and /etc/fastboot:
cat $CWD/sysvinit.paths.diff | patch -p1 -E --verbose --backup --suffix=.orig || exit 1

# Seems not needed. Commented out.
## Fix failure to show program version:
#cat $CWD/sysvinit.always.define.version.diff | patch -p1 --verbose || exit 1

# Set default HOME=/root to prevent dotfiles in /:
cat $CWD/sysvinit.default.home.is.root.diff | patch -p1 --verbose || exit 1

# Add /usr/local/sbin and /usr/local/bin to the default PATH:
cat $CWD/sysvinit.PATH_DEFAULT.diff | patch -p1 --verbose || exit 1

# Use /dev/initctl instead of /run/initctl ; this is a named pipe and probably
# should be in /dev anyway, but more importantly, having the upgraded package
# expect it in /run breaks shutdown/reboot without manual intervention:
for replace in src/shutdown.c src/Makefile src/initreq.h src/init.c doc/initctl doc/Install man/init.8 man/initctl.5 man/po/* ; do
  sed -i "s|run/initctl|dev/initctl|g" $replace
done

# Since they usually don't update the version here, let's try to make sure:
sed -i "s/define VERSION.*/define VERSION \"$VERSION\"/g" src/init.c

# Copy docs:
mkdir -pm755 $PKG/usr/doc/$PKGNAM-$VERSION/contrib
cp -fav \
    COPYING* COPYRIGHT README* \
    doc/* \
    $PKG/usr/doc/$PKGNAM-$VERSION
# This is interesting, but it looks like the output won't really work out
# of the box, so we'll add it as a contrib example to the docs:
cp -fav contrib/sysd2v.sh $PKG/usr/doc/$PKGNAM-$VERSION/contrib

# Build:
cd src
make clobber || failmake
make $NUMJOBS || make || failmake

# bootstrap hack - remove the failmake above.
#sed -i 's?/usr/lib\*/libcrypt.a?/glibc-slk/usr/lib/libcrypt.a?g' Makefile
#make || exit 1

# Install in to package:
mkdir -vpm755 $PKG/{sbin,bin,usr/bin,etc/inittab.d}
install -m755 -oroot -groot \
         bootlogd halt killall5 shutdown runlevel \
         $PKG/sbin/
install -m755 -oroot -groot init $PKG/sbin/init.new

## Don't use this -- the last/lastb in util-linux is an enchanced version
## derived from this one.
#install -m755 -oroot -groot \
#         last \
#         $PKG/usr/bin/
#( cd $PKG/usr/bin ; ln -sf last lastb )

cd ../man
mkdir -p $PKG/usr/man/man{5,8}
#mkdir -p $PKG/usr/man/man{1,5,8}
#install -m644 last.1 $PKG/usr/man/man1/
install -m644 inittab.5 initscript.5 initctl.5 $PKG/usr/man/man5/
install -m644 halt.8 init.8 killall5.8 poweroff.8 reboot.8 runlevel.8 \
              shutdown.8 telinit.8 bootlogd.8 \
              $PKG/usr/man/man8/
#( cd $PKG/usr/man/man1 ; ln -vsf last.1.gz lastb.1.gz )

# 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
