#!/bin/sh

VERSION=$1

cd $TMP
rm -rf fdutils-*

tar xzvf $ORIGPATH/fdutils-5.4.tar.gz
cd fdutils-*

# Apply standard Slackware patches
zcat $ORIGPATH/fdutils-5.4-20020222.diff.gz | patch -p1 --verbose --backup --suffix=.orig
zcat $ORIGPATH/fdutils.mediaprm.diff.gz | patch -p1 --verbose --backup --suffix=.orig

# Because we can't get 'make install' to work since it can't
# execute the binaries it just compiled, we'll cheat instead !
# We natively compile & install it first, then build the binaries
# for ARM.  That way we get the man pages and other bits with minimal effort.
unset GCC CC LD # otherwise it'll build for cross compilation.
rm -f config.cache
./configure --prefix=/usr --sysconfdir=/etc
make install

# Now we compile for ARM.
# Before we can get fdmount.c to compile, we need to have some Kernel headers.
# Handily, my ARM Linux toolchain has the headers we need
# 
# Re-source the cross compilation environment stuff so we get $GCC and so on set correctly.
source /armedslack/source/armslack-utils/arm-cross-envs.sh
# Delete cos there's loads of junk 'make clean' won't clean.
cd $TMP
rm -rf fdutils-*
tar xzvf $ORIGPATH/fdutils-5.4.tar.gz
cd fdutils-*
# Apply standard Slackware patches
zcat $ORIGPATH/fdutils-5.4-20020222.diff.gz | patch -p1 --verbose --backup --suffix=.orig
zcat $ORIGPATH/fdutils.mediaprm.diff.gz | patch -p1 --verbose --backup --suffix=.orig
#
CPPFLAGS="-O -I${ARMCROSSINCLUDE}/" CFLAGS="-O -I${ARMCROSSINCLUDE}/" \
 $ARMCONFIGURE -prefix=/usr --sysconfdir=/etc
make -e
# Now, the binaries are compiled (hopefully!)
# We have all the x86 binaries installed in slacktrack's fake root dir
# so we know which binaries we need to move ! :-)
# The man & info pages are installed by the x86 make install.
( cd src
  for i in $( find ${SLACKTRACKFAKEROOT}/usr/bin -type f -printf "%f\n" ); do 
      echo "Moving: $i into /usr/bin/"
      # catting retains the original perms & ownerships
      cat $i > ${SLACKTRACKFAKEROOT}/usr/bin/$i
  done
)

mkdir /usr/doc/fdutils-5.4
install -m644 COPYING CREDITS Changelog INSTALL doc/FAQ.html doc/README \
              /usr/doc/fdutils-$VERSION/

#

echo "-------------------"
echo "Now building mtools"
echo "-------------------"
read -p key

cd $TMP
tar xzvf $ORIGPATH/mtools-3.9.8.tar.gz
cd mtools-*
# Slackware's floppy tools must be built with X support.  However, 
# right at this moment in time I haven't compiled any X stuff for
# ARMedSlack and thus I can't compile mtools unless I specify --without-x.
# I have *no idea* what the X stuff is for.  Perhaps I'll look at some stage !
#
CFLAGS=-O $ARMCONFIGURE --prefix=/usr --sysconfdir=/etc --without-x
mkdir /etc
install -m644 ${ORIGPATH}/mtools.conf ${SLACKTRACKFAKEROaOT}/etc/mtools.conf
make -e && make -e install

mkdir /usr/doc/mtools-3.9.8
install -m644 COPYING Changelog INSTALL README Release.notes TODO \
              /usr/doc/mtools-3.9.8

# These are obsolete and/or cruft:
( cd ${SLACKTRACKFAKEROOT}
  rm -f usr/bin/MAKEFLOPPIES usr/bin/lz usr/bin/mcheck usr/bin/mcomp 
  rm -f usr/bin/mxtar usr/bin/tgz usr/bin/uz usr/man/man1/makefloppies.1
)

mkdir /install
install -m644 ${ORIGPATH}/slack-desc /install/

#EOF
