#!/bin/bash

# sysvinit.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:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION
slackhousekeeping
zcat $CWD/sysvinit.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig || failpatch
zcat $CWD/sysvinit.pidof.rhforkseverything.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig || failpatch

# Copy docs:
cd doc
mkdir -pm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav Install Propaganda sysvinit-$VERSION.lsm \
        $PKG/usr/doc/$PKGNAM-$VERSION

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

# Install into package:
mkdir -p $PKG/{sbin,bin,usr/bin}
install -m755 -oroot -groot \
         bootlogd halt killall5 shutdown runlevel \
         $PKG/sbin/
install -m755 -oroot -groot init $PKG/sbin/init.new
install -m755 -oroot -groot \
         last mountpoint \
         $PKG/usr/bin/
( cd $PKG/usr/bin ; ln -sf last lastb )
cp -fav initscript.sample $PKG/sbin/initscript.sample

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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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
