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

# floppy.SlackBuild
# Build the floppy package for ARMedslack,
# by Stuart Winter <mozes@slackware.com>
# Based on the original build script by Patrick Volkerding.
#
# Copyright 2008, 2009, 2010, 2013, 2018  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.

# Bundled packages:
MTOOLSVER=4.0.18

# 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

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

##### Build fdutils###################################
# August 2019: this no longer builds on ARM and since it's purpose is working with
# ancient technology that hasn't existed on ARM since the Acorn era, we'll dump it.
# If you do happen to get it to build, let me know.
function build_fdutils() {
# Extract source:
cd $TMPBUILD
tar xvvf $CWD/fdutils-$VERSION.tar.!(*sign|*asc|*sig)
#tar xvvf $PORTCWD/sources/fdutils-*.tar.!(*sign|*asc|*sig)
cd fdutils* || exit 1
slackhousekeeping

# Apply Debian's diff:
#tar xvvf $PORTCWD/sources/fdutils_*debian*z
#cat debian/patches/series | while read ptch ; do
#  auto_apply_patch debian/patches/$ptch || exit 1
#done
zcat $CWD/fdutils-5.5-20081027.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1
zcat $CWD/fdutils.mediaprm.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1
sed -i 's|<linux/ext2_fs.h>|<ext2fs/ext2_fs.h>|' src/fdmount.c || exit 1
zcat $CWD/fdutils.glibc228.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1

# Fix from gentoo - https://gitweb.gentoo.org/repo/gentoo.git/tree/app-misc/fdutils/fdutils-5.5.20060227.ebuild
# The build sets up config.h and uses some symbols, but forgots to actually include it in most places.
sed -i '1i#include "../config.h"' src/*.c || exit

# Refresh to support latest architectures:
slackupdatelibtool

# Configure:
CFLAGS="$SLKCFLAGS -fcommon" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
#make INSTALL_PROGRAM=install INSTALL_PROG=install install || failinstall
make $NUMJOBS || make || exit 1

# Install:
make install prefix=$PKG/usr exec_prefix=$PKG/usr bindir=$PKG/usr/bin \
  infodir=$PKG/usr/info mandir=$PKG/usr/man sysconfdir=$PKG/etc || exit 1

# 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 $CWD/mtools-$MTOOLSVER.tar.!(*sign|*asc|*sig)
cd mtools-*/ || exit 1

# Refresh to support latest architectures:
slackupdatelibtool

# Configure:
CFLAGS="$SLKCFLAGS -fcommon" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --sysconfdir=/etc \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS all || exit 1

# Install:
make install DESTDIR=$PKG || exit 1
install -m644 $CWD/mtools.conf $PKG/etc/mtools.conf
install -m644 $CWD/mediaprm $PKG/etc/

# 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
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
