#!/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-2025  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

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

# Versions of bundled packages:
# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull the version numbers from the x86 trunk:
slack_setvarfromupstream MTOOLS

##### 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 xf $CWD/fdutils-$VERSION.tar.!(*sign|*asc|*sig)
#tar xvvf $PORTCWD/sources/fdutils-*.tar.!(*sign|*asc|*sig)
cd fdutils*/ || failextract
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
cat $CWD/fdutils-patches/series | while read patch ; do
  cat $CWD/fdutils-patches/$patch | patch -p1 --verbose || exit 1
done || 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:
autoreconf -vif
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 || failmake

# Install:
make install DESTDIR=$PKG || failinstall
make install-doc DESTDIR=$PKG || failinstall

# 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 xf $CWD/mtools-$MTOOLS.tar.!(*sign|*asc|*sig)
cd mtools-*/ || failextract

# Apply patches:
zcat $CWD/mtools.conf.diff.gz | patch -p1 --verbose || 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 || failmake

# Install:
make install DESTDIR=$PKG || failinstall
install -m644 mtools.conf $PKG/etc/mtools.conf.new

# Install docs:
mkdir -pm755 $PKG/usr/doc/mtools-$MTOOLS
cp -fav \
   COPYING INSTALL NEWS README README.BEBOX Release.notes \
   $PKG/usr/doc/mtools-$MTOOLS

# 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
