#!/bin/bash

# floppy.SlackBuild
# Build the floppy package for ARMedslack, 
# by Stuart Winter <mozes@slackware.com>
# Based on the original build script by Patrick Volkerding.

# Bundled packages:
MTOOLSVER=3.9.11

# 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

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

# Extract source:
cd $TMPBUILD
tar xvvf $PORTCWD/sources/fdutils_*.orig.tar.gz
cd fdutils-* || exit 1
slackhousekeeping

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

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

# Start fakeroot server:
start_fakeroot

# Make:
#make INSTALL_PROGRAM=install INSTALL_PROG=install install || failinstall
make $NUMJOBS 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 xvvf $PORTCWD/sources/mtools_*.orig.tar.gz
cd mtools-* || exit 1

# Apply Debian patch to fix a build problem:
zcat $PORTCWD/sources/mtools*diff*gz | patch -p1 || failpatch

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

# Install:
make $NUMJOBS all || exit 1

# Install:
make install prefix=$PKG/usr sysconfdir=$PKG/etc bindir=$PKG/usr/bin \
  MAN1DIR=$PKG/usr/man/man1  MAN5DIR=$PKG/usr/man/man5
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}

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

# 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
