#!/bin/bash

# util-linux.SlackBuild
# Modified by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Mostly based on the original Slackware build script by Patrick Volkerding.
# 1-May-2004

# I wanted to rewrite this script from scratch because it's pretty crufty
# but then if Patrick makes a small change in the framework tar ball then
# it'll no doubt pass me by, so it's safer to just copy it verbatim.

# 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 and re-create temporary directories

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

# Start the fakeroot server:
start_fakeroot

# Explode the package framework:
( cd $PKG && explodepkg $CWD/_util-linux.tar.gz )

echo "+================+"
echo "| net-tools-1.60 |"
echo "+================+"
cd $TMPBUILD
rm -rf net-tools-1.60
tar jxvvf $CWD/net-tools-1.60.tar.bz2
cd net-tools-1.60
slackhousekeeping
zcat $CWD/net-tools.diff.gz | patch -p1
#zcat $PORTCWD/patches/net-tools-1.60-gcc34-2.patch.gz | patch -p1 || failpatch
#zcat $PORTCWD/patches/net-tools-1.60-kernel-headers-1.patch.gz | patch -p1 || failpatch
zcat $CWD/net-tools_1.60-17.diff.gz | patch -p1 || failpatch
sed 's?HAVE_AFDECnet 1?HAVE_AFDECnet 0?g' debian/config.h    > config.h
sed 's?HAVE_AFDECnet=1?HAVE_AFDECnet=0?g' debian/config.make > config.make
# Build:
make || failmake
make hostname || failmake
# Install:
install -m755 hostname $PKG/bin
cd man/en_US
for page in hostname.1 dnsdomainname.1 nisdomainname.1 ypdomainname.1 \
domainname.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done

# This is supposedly due to be merged into util-linux upstream...
echo "+==================+"
echo "| schedutils-1.5.0 |"
echo "+==================+"
cd $TMPBUILD
tar zxvvf $CWD/schedutils-1.5.0.tar.gz
cd schedutils-1.5.0
# There is no ARM support for ionice, so we just build the two that build
# but if there ever is ARM support, let's just give it a go anyway 
# For the moment, the first make will fail.
make 
make chrt taskset || failmake
mkdir -p $PKG/usr/bin
install -m755 chrt ionice taskset $PKG/usr/bin
mkdir -pm755 $PKG/usr/man/man1
install -m644 man/*.1 $PKG/usr/man/man1
mkdir -p $PKG/usr/doc/schedutils-1.5.0
cp -a \
  AUTHORS COPYING ChangeLog INSTALL README \
  $PKG/usr/doc/schedutils-1.5.0

echo "+================+"
echo "| setserial-2.17 |"
echo "+================+"
cd $TMPBUILD
rm -rf setserial-2.17
tar xzvf $CWD/setserial-2.17.tar.gz
cd setserial-2.17
slackhousekeeping
zcat $CWD/setserial-rc.serial.diff.gz | patch -E -p1 --verbose || failpatch
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr || failconfig
make || failmake
strip setserial
cat setserial > $PKG/sbin/setserial
cat rc.serial > $PKG/etc/rc.d/rc.serial.new
cat serial.conf > $PKG/etc/serial.conf.new
cat setserial.8 | gzip -9c > $PKG/usr/man/man8/setserial.8.gz

echo "+============+"
echo "| bsdstrings |"
echo "+============+"
cd $TMPBUILD
rm -rf bsdstrings
tar xzvf $CWD/bsdstrings.tar.gz
cd bsdstrings
slackhousekeeping
make || failmake
strip strings
cat strings > $PKG/usr/bin/strings
cat strings.1 | gzip -9c > $PKG/usr/man/man1/strings.1.gz

echo "+==========+"
echo "| adjtimex |"
echo "+==========+"
cd $TMPBUILD
rm -rf adjtimex-*
tar xzvf $CWD/adjtimex-1.20.tar.gz
cd adjtimex-*
slackhousekeeping
CFLAGS="-O2 $SLKCFLAGS" \
./configure \
  --prefix=/usr || failconfig
make || failmake
strip adjtimex
cat adjtimex > $PKG/sbin/adjtimex
chmod 755 $PKG/sbin/adjtimex
cat adjtimex.8 | gzip -9c > $PKG/usr/man/man8/adjtimex.8.gz
mkdir -p $PKG/usr/doc/adjtimex-1.20
cp -a COPYING COPYRIGHT README README.ru adjtimex.lsm \
          $PKG/usr/doc/adjtimex-1.20

echo "+==================+"
echo "| util-linux-$VERSION |"
echo "+==================+"
cd $TMPBUILD
rm -rf util-linux-$VERSION
tar xjvf $CWD/util-linux-$VERSION.tar.bz2
cd util-linux-$VERSION
slackhousekeeping
zcat $CWD/util-linux.MCONFIG.diff.gz | patch -E --backup -p1 --suffix=.orig
#zcat $PORTCWD/patches/util-linux-2.12a-minix-gcc35-1.patch.gz | patch -p1 
#zcat $PORTCWD/patches/util-linux-2.12a-kernel-headers-1.patch.gz | patch -p1
zcat $CWD/util-linux.fdisk-no-solaris.diff.gz | patch -E --backup -p1 --suffix=.orig

# Configure:
CFLAGS="$SLKCFLAGS" \
CPPFLAGS="$SLKCFLAGS" \
./configure || failconfig

# Build:
make || failmake
( cd sys-utils ; make rdev ) 
cat sys-utils/arch > $PKG/bin/arch
cat sys-utils/dmesg > $PKG/bin/dmesg
cat sys-utils/flock > $PKG/usr/bin/flock
cat getopt/getopt > $PKG/bin/getopt
cat misc-utils/mcookie > $PKG/usr/bin/mcookie
cat misc-utils/rename > $PKG/usr/bin/rename
#cat misc-utils/hostname > $PKG/bin/hostname
cat misc-utils/kill > $PKG/bin/kill
#cat login-utils/login > $PKG/bin/login
cat text-utils/more > $PKG/bin/more
cat text-utils/line > $PKG/usr/bin/line
cat text-utils/pg > $PKG/usr/bin/pg
cat mount/mount > $PKG/bin/mount
cat mount/umount > $PKG/bin/umount
cat mount/losetup > $PKG/sbin/losetup
cat mount/pivot_root > $PKG/sbin/pivot_root
#cat sys-utils/setserial > $PKG/bin/setserial
cat misc-utils/setterm > $PKG/bin/setterm
cat sys-utils/sln > $PKG/bin/sln
#cat sys-utils/sync > $PKG/bin/sync
cat login-utils/agetty > $PKG/sbin/agetty
#cat login-utils/chfn > $PKG/usr/bin/chfn
#cat bdflush > $PKG/sbin/bdflush
#cat sys-utils/clock > $PKG/sbin/clock
cat hwclock/hwclock > $PKG/sbin/hwclock
cat fdisk/fdisk > $PKG/sbin/fdisk
cat fdisk/sfdisk > $PKG/usr/sbin/sfdisk
#cat fsck > $PKG/sbin/fsck
cat disk-utils/fsck.minix > $PKG/sbin/fsck.minix
cat disk-utils/fsck.cramfs > $PKG/sbin/fsck.cramfs
cat disk-utils/raw > $PKG/usr/bin/raw
cat sys-utils/readprofile > $PKG/usr/bin/readprofile
cat disk-utils/blockdev > $PKG/sbin/blockdev
cat disk-utils/elvtune > $PKG/sbin/elvtune
cat disk-utils/mkfs.bfs > $PKG/sbin/mkfs.bfs
cat disk-utils/mkfs.cramfs > $PKG/sbin/mkfs.cramfs
cat disk-utils/mkfs.minix > $PKG/sbin/mkfs.minix
cat disk-utils/mkfs > $PKG/sbin/mkfs
cat disk-utils/mkswap > $PKG/sbin/mkswap
cat sys-utils/rdev > $PKG/sbin/rdev
cat mount/swapon > $PKG/sbin/swapon
cat misc-utils/cal > $PKG/usr/bin/cal
#cat login-utils/chsh > $PKG/usr/bin/chsh
#cat clear > $PKG/usr/bin/clear
cat text-utils/col > $PKG/usr/bin/col
cat text-utils/colcrt > $PKG/usr/bin/colcrt
cat text-utils/colrm > $PKG/usr/bin/colrm
cat text-utils/column > $PKG/usr/bin/column
cat misc-utils/ddate > $PKG/usr/bin/ddate
cat disk-utils/fdformat > $PKG/usr/bin/fdformat
cat text-utils/hexdump > $PKG/usr/bin/hexdump
#cat misc-utils/hostid > $PKG/usr/bin/hostid
cat sys-utils/ipcrm > $PKG/usr/bin/ipcrm
cat disk-utils/isosize > $PKG/usr/bin/isosize
cat sys-utils/cytune > $PKG/usr/bin/cytune
cat sys-utils/ipcs > $PKG/usr/bin/ipcs
#cat login-utils/last > $PKG/usr/bin/last
cat misc-utils/logger > $PKG/usr/bin/logger
cat misc-utils/look > $PKG/usr/bin/look
cat login-utils/mesg > $PKG/usr/bin/mesg
cat misc-utils/namei > $PKG/usr/bin/namei
#cat login-utils/newgrp > $PKG/usr/bin/newgrp
#cat login-utils/passwd > $PKG/usr/bin/passwd
#cat login-utils/vipw > $PKG/usr/sbin/vipw
cat sys-utils/renice > $PKG/usr/bin/renice
cat misc-utils/reset.sh > $PKG/usr/bin/reset
cat text-utils/rev > $PKG/usr/bin/rev
cat misc-utils/script > $PKG/usr/bin/script
cat disk-utils/setfdprm > $PKG/usr/bin/setfdprm
cat sys-utils/setsid > $PKG/usr/bin/setsid
#cat text-utils/strings > $PKG/usr/bin/strings
#cat misc-utils/tsort > $PKG/usr/bin/tsort
cat sys-utils/tunelp > $PKG/usr/bin/tunelp
cat text-utils/ul > $PKG/usr/bin/ul
cat login-utils/wall > $PKG/usr/bin/wall
cat misc-utils/whereis > $PKG/usr/bin/whereis
cat misc-utils/write > $PKG/usr/bin/write
cat misc-utils/chkdupexe > $PKG/usr/bin/chkdupexe
cat fdisk/cfdisk > $PKG/usr/sbin/cfdisk
#cat sys-utils/chroot > $PKG/usr/sbin/chroot
cat sys-utils/ctrlaltdel > $PKG/usr/sbin/ctrlaltdel
mkdir -p $PKG/usr/info
cat sys-utils/ipc.info | gzip -9c > $PKG/usr/info/ipc.info.gz
mkdir -p $PKG/usr/share/misc/getopt
( cd getopt
  cat getopt-parse.bash > $PKG/usr/share/misc/getopt/getopt-parse.bash
  cat getopt-parse.tcsh > $PKG/usr/share/misc/getopt/getopt-parse.tcsh
  cat getopt-test.bash > $PKG/usr/share/misc/getopt/getopt-test.bash
  cat getopt-test.tcsh > $PKG/usr/share/misc/getopt/getopt-test.tcsh )
( cd po
  for message in *.gmo ; do
    mkdir -p $PKG/usr/share/locale/`basename $message .gmo`/LC_MESSAGES
    cat $message > $PKG/usr/share/locale/`basename $message .gmo`/LC_MESSAGES/util-linux.mo
  done )
gzip -9c getopt/getopt.1 > $PKG/usr/man/man1/getopt.1.gz
for file in sys-utils/arch.1 \
text-utils/more.1 text-utils/line.1 text-utils/pg.1 misc-utils/setterm.1 \
text-utils/col.1 text-utils/colcrt.1 \
text-utils/colrm.1 text-utils/column.1 text-utils/hexdump.1 \
misc-utils/kill.1 misc-utils/logger.1 misc-utils/look.1 \
login-utils/mesg.1 \
misc-utils/rename.1 \
misc-utils/reset.1 text-utils/rev.1 misc-utils/script.1 \
text-utils/ul.1 login-utils/wall.1 misc-utils/whereis.1 \
misc-utils/write.1 misc-utils/namei.1 misc-utils/ddate.1 \
misc-utils/chkdupexe.1 sys-utils/flock.1 sys-utils/readprofile.1 \
misc-utils/mcookie.1 misc-utils/cal.1 ; do
  gzip -9c $file > $PKG/usr/man/man1/`basename $file`.gz
done
#gzip -9c mount/pivot_root.2 > $PKG/usr/man/man2/pivot_root.2.gz
gzip -9c mount/fstab.5 > $PKG/usr/man/man5/fstab.5.gz
gzip -9c mount/nfs.5 > $PKG/usr/man/man5/nfs.5.gz
gzip -9c mount/pivot_root.8 > $PKG/usr/man/man8/pivot_root.8.gz
for file in sys-utils/dmesg.8 mount/mount.8 \
mount/losetup.8 mount/umount.8 login-utils/agetty.8 \
hwclock/hwclock.8 sys-utils/rdev.8 \
sys-utils/cytune.8 sys-utils/sln.8 fdisk/fdisk.8 disk-utils/fsck.minix.8 \
disk-utils/blockdev.8 disk-utils/elvtune.8 disk-utils/mkfs.bfs.8 \
disk-utils/isosize.8 disk-utils/raw.8 \
disk-utils/mkfs.minix.8 disk-utils/mkswap.8 \
mount/swapon.8 disk-utils/mkfs.8 disk-utils/fdformat.8 \
sys-utils/ipcrm.8 sys-utils/ipcs.8 sys-utils/renice.8 disk-utils/setfdprm.8 \
sys-utils/setsid.8 sys-utils/tunelp.8 \
sys-utils/ctrlaltdel.8 fdisk/cfdisk.8 fdisk/sfdisk.8 \
sys-utils/ramsize.8 mount/swapoff.8 sys-utils/vidmode.8 \
sys-utils/rootflags.8 ; do
  gzip -9c $file > $PKG/usr/man/man8/`basename $file`.gz
done
mkdir -p $PKG/usr/doc/util-linux-$VERSION
cp -favv HISTORY INSTALL hwclock/README* fdisk/README* login-utils/README* misc-utils/README* mount/README* text-utils/README* text-utils/LICENSE* \
         $PKG/usr/doc/util-linux-$VERSION
cp -a getopt/README \
      $PKG/usr/doc/util-linux-$VERSION/README.getopt-1.1.3

# ziptool doesn't exist for ARM so we'll skip this one:
if [ $PORTARCH != arm ]; then
   echo "+=============+"
   echo "| ziptool    |"
   echo "+=============+"
   cd $TMPBUILD
   tar jxvvf $CWD/ziptool-1.4.0.tar.bz2
   cd ziptool-*
   make || failmake
   strip ziptool
   cat ziptool > $PKG/sbin/ziptool
   mkdir -p $PKG/usr/doc/ziptool-1.4.0
   cp -a README $PKG/usr/doc/ziptool-1.4.0
   install -m644 ziptool.1.gz $PKG/usr/man/man1
else
   # don't need this empty placeholder for ARM nor the jaztool -> ziptool symlink:
   rm -f $PKG/sbin/{ziptool,jaztool}
   # Remove the stuff from the static doinst.sh script:
   sed -i -e '/.*jaztool.*/d' doinst.sh $PKG/install/doinst.sh
fi

############### tput ###################################################
# Grab 'tput' from the running system.  Hopefully it's new enough,
# and we already have source for it in d/ncurses/.  This is needed
# by the /usr/bin/clear we include.
# It's included here because we may not install the ncurses package
# on a very bare system because the ncurses libs are included in 
# a/aaa_elflibs thus it's not necessary to install the ncurses package.
cp -fa /usr/bin/tput $PKG/usr/bin/
chown root:root $PKG/usr/bin/tput

########################################################################

# Set some of the generic Slackware packaging policies:
cd $PKG
slackgzpages -i # compress man & info pages and delete usr/info/dir
slack644docs    # chmod -R 644 usr/doc
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
