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

# e2fsprogs.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 31-May-2004
#
# Copyright 2008, 2009, 2010, 2015, 2016  Patrick J. Volkerding, Sebeka, MN, 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 and re-create temporary directories

# arm)     SLKCFLAGS="-march=armv5te -g -O2" # -D__NO_STRING_INLINES"

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# Do not use 64-bit by default on ext4, since some bootloaders cannot handle
# it yet.  But use "auto_64-bit_support = 1", which will enable 64-bit if the
# number of blocks for the file system requires the feature to be enabled.
zcat $CWD/e2fsprogs.no.64bit.by.default.diff.gz | patch -p1 --verbose || exit 1

# Disable libblkid and libuuid, as we'll be using the ones in u-l-ng
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix= \
   --libdir=/lib${LIBDIRSUFFIX} \
   --bindir=/usr/bin \
   --includedir=/usr/include \
   --datadir=/usr/share \
   --datarootdir=/usr/share \
   --libexecdir=/usr/libexec \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --docdir=/usr/doc/e2fsprogs-$VERSION \
   --enable-elf-shlibs \
   --disable-libblkid \
   --disable-libuuid \
   --disable-uuidd \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG
make install-libs DESTDIR=$PKG

# I guess Ted would rather not have this included, so we won't.
# ( cd misc
#   make findsuper
#   cat findsuper > $PKG/sbin/findsuper
#   chmod 0755 $PKG/sbin/findsuper )

# Don't clobber an existing config file
mv $PKG/etc/mke2fs.conf $PKG/etc/mke2fs.conf.new

# Fix up package:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}
mv -fv $PKG/lib${LIBDIRSUFFIX}/pkgconfig $PKG/lib${LIBDIRSUFFIX}/*.so \
  $PKG/usr/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  for i in *.so ; do
    ln -vsf /lib${LIBDIRSUFFIX}/$(readlink $i) $i ;
  done
)

# Don't package the static libraries:
rm -f $PKG/lib${LIBDIRSUFFIX}/*.a

# Since fsck is now part of util-linux, let's prefer that version:
if [ -r $PKG/sbin/fsck ]; then
   mv -fv $PKG/sbin/fsck $PKG/sbin/fsck-e2fsprogs
fi
if [ -r $PKG/usr/man/man8/fsck.8 ]; then
   mv -fv $PKG/usr/man/man8/fsck.8 $PKG/usr/man/man8/fsck-e2fsprogs.8
fi

# Get rid of systemd cron.d cruft:
rm -rf $PKG/etc/cron.d

# findfs is intentionally left out here - we use the one in util-linux-ng
( cd $PKG/sbin
  rm -f \
    mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev \
    fsck.ext2 fsck.ext3 fsck.ext4dev e2label findfs
  ln -sf mke2fs mkfs.ext2
  ln -sf mke2fs mkfs.ext3
  ln -sf mke2fs mkfs.ext4
  ln -sf mke2fs mkfs.ext4dev
  ln -sf tune2fs e2label
  cat << EOF > fsck.ext2
#!/bin/sh
ulimit -s unlimited
shopt -s extglob
exec /sbin/e2fsck -C 0 \$*
EOF
  chmod 0755 fsck.ext2
  # Why won't symlinks work here?  --RW
  # Because $0 will always be "fsck.ext2" in that case.  --PJV
  cp -a fsck.ext2 fsck.ext3
  cp -a fsck.ext2 fsck.ext4
  cp -a fsck.ext2 fsck.ext4dev
)
( cd $PKG/usr/man/man5
  ln -sf ext4.5 ext2.5
  ln -sf ext4.5 ext3.5
)
( cd $PKG/usr/man/man8
  rm -f fsck.ext2.8 fsck.ext3.8 mkfs.ext2.8 mkfs.ext3.8 \
    mkfs.ext4.8 mkfs.ext4dev.8
  ln -sf e2fsck.8 fsck.ext2.8
  ln -sf e2fsck.8 fsck.ext3.8
  ln -sf e2fsck.8 fsck.ext4.8
  ln -sf e2fsck.8 fsck.ext4dev.8
  ln -sf mke2fs.8 mkfs.ext2.8
  ln -sf mke2fs.8 mkfs.ext3.8
  ln -sf mke2fs.8 mkfs.ext4.8
  ln -sf mke2fs.8 mkfs.ext4dev.8
)

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  COPYING* INSTALL* NOTICE README* RELEASE-NOTES SHLIBS SUBMITTING-PATCHES \
  $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav ext2ed/COPYRIGHT $PKG/usr/doc/e2fsprogs-$VERSION/COPYRIGHT.ext2ed
cp -fav ext2ed/README $PKG/usr/doc/e2fsprogs-$VERSION/README.ext2ed
cp -fav lib/uuid/COPYING $PKG/usr/doc/e2fsprogs-$VERSION/COPYING.uuid

# Trim RELEASE-NOTES to only include recent history:
if [ -r RELEASE-NOTES ]; then
  DOCSDIR=$(echo $PKG/usr/doc/${PKGNAM}-$VERSION)
  head -n1000 RELEASE-NOTES > $DOCSDIR/RELEASE-NOTES
  touch -r RELEASE-NOTES $DOCSDIR/RELEASE-NOTES
fi

# If RELEASE-NOTES is a symlink, fix that:
if [ -L $PKG/usr/doc/e2fsprogs-$VERSION/RELEASE-NOTES ]; then
   rm $PKG/usr/doc/e2fsprogs-$VERSION/RELEASE-NOTES
   cp -a $(readlink RELEASE-NOTES) $PKG/usr/doc/e2fsprogs-$VERSION/RELEASE-NOTES
fi

# 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

# Hacks for Slackware ARM.
# On the ARM Versatile (under QEMU emulator), there isn't an RTC.
# e2fsprog-1.41.x enforces a check and breaks, so we can configure e2fsprogs
# if we find ourselves on this platform (when the package is installed)
# to ignore this problem.
# Create the config file:
cat << EOF > etc/e2fsck.conf.new
# Automatically added during the installation of the e2fsprogs package
# because we detected you were installing on an ARM-Versatile platform.
# These options stop e2fsck from erroring/requiring manual intervention
# when it encounters bad time stamps on filesystems -- which happens on
# the Versatile platform because QEMU does not have RTC (real time clock)
# support.
# Other devices such as the Seagate Dockstar also don't have an RTC so you'll
# want to do this for those too.  Unfortunately I don't know of a way to check
# for whether we're running on such a device so I can't automate it.
#
[options]
        accept_time_fudge = 1
        broken_system_clock =1
EOF
# If we're found to be running on the Versatile platform then switch the config file
# into place, otherwise wipe the conf.new (since it's not shipped in Slackware)
cat << EOF >> install/doinst.sh

# Hack for Slackware ARM.
# If the package is being installed on an ARM Versatile board then
# drop in the e2fsck config file to ignore broken system clocks, otherwise
# wipe the config file since it's not shipped in Slackware.
grep -q "ARM-Versatile" /proc/cpuinfo > /dev/null 2>&1 && config etc/e2fsck.conf.new || rm -f etc/e2fsck.conf.new

EOF

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
