#!/bin/bash

# floppy.SlackBuild
# Build the floppy package for ARMedslack, 
# by Stuart Winter <stuart@armedslack.org>
# Based on the original build script by Patrick Volkerding.

# Bundled packages:
MTOOLSVER=3.9.9

# 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

# Create some package framework:
mkdir -pm755 $PKG/{usr,etc}

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

##### Build fdutils###################################

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/source/fdutils_*.orig.tar.gz
cd fdutils-*
slackhousekeeping

# Apply Debian's diff:
#zcat $PORTCWD/source/fdutils_*-1.diff.gz | patch -p1

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --host=$ARCH-slackware-linux \
   --build=$ARCH-slackware-linux || failconfig

# Start fakeroot server:
start_fakeroot

# Make:
#make INSTALL_PROGRAM=install INSTALL_PROG=install install || failinstall
make prefix=$PKG/usr sysconfdir=$PKG/etc install || failmake

# Install docs:
mkdir -pm755 $PKG/usr/doc/fdutils-$VERSION
cp -a COPYING CREDITS Changelog INSTALL doc/FAQ.html doc/README \
     $PKG/usr/doc/fdutils-$VERSION

##### Build mtools###################################

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/source/mtools_*.orig.tar.gz
cd mtools-*

# Apply Tim Baldwin's patch to use RISC OS filetypes & filename
# trunctation:
zcat $PORTCWD/sources/mtools-3.9.9.riscos.gz | patch --verbose -p1 || failpatch

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   $ARCH-slackware-linux \
   --prefix=/usr \
   --sysconfdir=/etc || failconfig

# Install:
make install prefix=$PKG/usr
install -m644 $CWD/mtools.conf $PKG/etc/mtools.conf

# Install docs:
mkdir -pm755 $PKG/usr/doc/mtools-$MTOOLSVER
cp -a COPYING Changelog INSTALL README Release.notes TODO \
     $PKG/usr/doc/mtools-$MTOOLSVER

# These are obsolete and/or cruft:
rm -f $PKG/usr/bin/{MAKEFLOPPIES,lz,mcheck,mcomp,mxtar,tgz,uz} \
      $PKG/usr/man/man1/makefloppies.1
rm -rf $PKG/usr/man/man{5,8}

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

# Fix ownerships of those binaries *not* installed as root:floppy
chown root:bin /usr/bin/mkmanifest /usr/bin/mtools

# 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
slack644docs    # Set document permisisons
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
