#!/bin/bash

# sysvinit.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 05-Jun-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="" ;;
   sparc)   SLKCFLAGS="" ;;
   *)       SLKCFLAGS="" ;;
esac

# Extract source:
tar zxvvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/sysvinit.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig || failpatch

# Start fakeroot server:
start_fakeroot

# Explode the package framework:
( cd $PKG
  explodepkg $CWD/_sysvinit.tar.gz 
  rm -f sbin/init )

# Install scripts and config files from $CWD/scripts/:
cat $CWD/scripts/inittab > $PKG/etc/inittab.new
cat $CWD/scripts/rc.4 > $PKG/etc/rc.d/rc.4.new
cat $CWD/scripts/rc.6 > $PKG/etc/rc.d/rc.6.new
cat $CWD/scripts/rc.K > $PKG/etc/rc.d/rc.K.new
cat $CWD/scripts/rc.M > $PKG/etc/rc.d/rc.M.new
cat $CWD/scripts/rc.S > $PKG/etc/rc.d/rc.S.new
cat $CWD/scripts/rc.local > $PKG/etc/rc.d/rc.local.new
cat $CWD/scripts/rc.scanluns > $PKG/etc/rc.d/rc.scanluns.new
cat $CWD/scripts/rc.sysvinit > $PKG/etc/rc.d/rc.sysvinit.new
# Add a very handy tool for scanning new SCSI devices.  Most useful for
# finding new USB and Firewire storage devices which show up as SCSI.
cat $CWD/scripts/rescan-scsi-bus.sh > $PKG/sbin/rescan-scsi-bus

# Install docs:
( cd doc
  mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
  cp -a Install Propaganda sysvinit-$VERSION.lsm \
        $PKG/usr/doc/$PACKAGE-$VERSION )

# Build:
( cd src
  make clobber
  sed -i 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/' Makefile
  make || failmake

  # Install binaries:
  install -m755 halt initscript.sample killall5 shutdown runlevel $PKG/sbin
  install -m755 init $PKG/sbin/init.new
  install -m755 last $PKG/usr/bin )
( cd $PKG/usr/bin ; ln -sf last lastb )

# Install man pages:
( cd man
  install -m644 last.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 \
                $PKG/usr/man/man8 )
( cd $PKG/usr/man/man1 ; ln -sf last.1 lastb.1 )

# POSIX fix (otherwise coreutils' head, tail et al consider head -1 obsolete)
# This fix will go away eventually as and when scripts are updated to use head -n1.
# (other ports will probably want to do this too)
# 29-Jul-2005: This was fixed ages ago when I reported it to Pat.
#              If not then I'll simply uncomment it and rebuild this package :)
#if [ "${PORTARCH}" = "arm" ]; then
#   sed -i '7s/.*/export _POSIX2_VERSION=199209 # override glibc posix version/' $PKG/etc/rc.d/rc.S.new
#fi

# Apply RiscPC fix for hwclock:
# 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/rc.6.diff 
  patch --no-backup-if-mismatch --verbose -lp0 < $PORTCWD/rc.S.diff || failpatch ) || failpatch

# 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

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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