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

# 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
#
# Copyright 2005-2009, 2010  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

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

# Create package framework (most of this should be in _bin.tar.!(*sign|*asc|*sig) 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.!(*sign|*asc|*sig)
cd debianutils-$DUTILS || exit 1
slackhousekeeping
CFLAGS="$SLKCFLAGS" ./configure --prefix=/usr || failconfig
make $NUMJOBS || 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/mktemp-debianutils
install -m644 mktemp.1 $PKG/usr/man/man1/mktemp-debianutils.1
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.!(*sign|*asc|*sig)
cd fbset-* || exit 1
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

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

echo "+===============+"
echo "| todos/fromdos |"
echo "+===============+"
cd $TMPBUILD
tar xzvf $CWD/todos.tar.!(*sign|*asc|*sig)
cd todos || exit 1
slackhousekeeping
sed -i 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/' Makefile
make $NUMJOBS || 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

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
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
