#!/bin/bash

# bin.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Almost _entirely_ based on the original Slackware build script.
# 01-May-2004

# 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

# Individual software version numbers:
DUTILS=2.7
# The fbset package
FBSET=2.1

# 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

# Create package framework (most of this should be in _bin.tar.gz though):
mkdir -p $PKG/usr/man/man{1,5,8}
mkdir -p $PKG/usr/{sbin,bin}
mkdir -p $PKG/etc

############################# debianutils ####################################

echo "+=============+"
echo "| debianutils |"
echo "+=============+"
cd $TMPBUILD
tar xzvf $CWD/debianutils_$DUTILS.tar.gz
cd debianutils-$DUTILS
slackhousekeeping
CFLAGS="$SLKCFLAGS" ./configure --prefix=/usr || failconfig
make || failmake
## We use our own very simple run-parts script
## in the dcron package instead.
#cat run-parts > $PKG/usr/bin/run-parts
#cat run-parts.8 | gzip -9c > $PKG/usr/man/man8/run-parts.8.gz
install -m755 mktemp $PKG/usr/bin
install -m644 mktemp.1 $PKG/usr/man/man1
install -m755 savelog $PKG/usr/bin
install -m644 savelog.8 $PKG/usr/man/man8
install -m755 tempfile $PKG/usr/bin
install -m644 tempfile.1 $PKG/usr/man/man1
mkdir -p $PKG/usr/doc/debianutils-$DUTILS
cp -favv debian/copyright $PKG/usr/doc/debianutils-$DUTILS

############################# fbset ##########################################

echo "+===========+"
echo "| fbset-$FBSET |"
echo "+===========+"
cd $TMPBUILD
tar xzvf $CWD/fbset-$FBSET.tar.gz
cd fbset-*
slackhousekeeping
make CC="gcc -Wall -O2 -I. ${SLKCFLAGS}" || failmake
install -m755 fbset $PKG/usr/sbin
install -m644 etc/fb.modes.ATI $PKG/etc/fb.modes
install -m644 fb.modes.5 $PKG/usr/man/man5
install -m644 fbset.8 $PKG/usr/man/man8
mkdir -p $PKG/usr/doc/fbset-$FBSET
cp -favv INSTALL etc/* \
         $PKG/usr/doc/fbset-$FBSET
rm -r $PKG/usr/doc/fbset-*/CVS

############################# banners #######################################

echo "+=========+"
echo "| banners |"
echo "+=========+"
cd $TMPBUILD
tar xzvf $CWD/banners.tar.gz
cd banners
slackhousekeeping
sed -i 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/' Makefile 
make || failmake
install -m755 sysvbanner  bban $PKG/usr/bin

############################# todos/fromdos #################################

echo "+===============+"
echo "| todos/fromdos |"
echo "+===============+"
cd $TMPBUILD
tar xzvf $CWD/todos.tar.gz
cd todos
slackhousekeeping
sed -i 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/' Makefile 
make || failmake
install -m755 todos fromdos $PKG/usr/bin
install -m644 fromdos.1.gz todos.1.gz $PKG/usr/man/man1

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

# These are a couple of really old scripts that might still
# be useful for a couple more years.  :-)
zcat $CWD/scripts/diskcopy.gz > $PKG/usr/bin/diskcopy
zcat $CWD/scripts/xx.gz > $PKG/usr/bin/xx
chmod 755 $PKG/usr/bin/diskcopy
chmod 755 $PKG/usr/bin/xx

# 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
