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

# cdrtools.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter
# 26-Aug-2004
#
# Copyright 2008, 2009  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.

# This may need an update at some stage, so don't forget!
ZISOFSTOOLS=1.0.8

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/ap/cdrtools

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

### Build cdrtools ########################################

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

# Apply patches:
#patch -p1 --verbose < $PORTCWD/sources/cdrtools-2.01-arm.patch || failpatch

# Create package structure:
mkdir -vpm755 $PKG/usr

# Let it know about our later ARM systems.
# This schilly build system is quite adaptable -- I wish GNU'd thought of something
# like this instead of autoconf ;-)
( cd RULES
  # Create links for which ever ARM arch we happen to be building on:
  ln -vfs i586-linux-cc.rul $(uname -m)-linux-cc.rul
  ln -vfs i586-linux-cc.rul $(uname -m)-linux-cc.rul
  ln -vfs i586-linux-gcc.rul $(uname -m)-linux-gcc.rul
  ln -vfs i586-linux-gcc.rul $(uname -m)-linux-gcc.rul )


# When running make below, you'll see "errors" like these:
# config.h:34:10: fatal error: lconfig.h: No such file or directory
#    34 | #include "lconfig.h"
# However, lconfig.h will eventually be generated from lconfig.h.in, the build
# will restart, and the package will be correctly built. Since there are no
# ill effects from this I see no compelling reason to try to patch it here.
make -e INS_BASE=/usr DEFMANBASE=. || exit 1

# Install into package:
make -e install INS_BASE=/usr DEFMANBASE=. DESTDIR=$PKG || exit 1

# I don't know why this couldn't be "/etc/cdrtools" or something, but since
# getty uses it already I'm not going to argue the point.
mv $PKG/etc/default/cdrecord $PKG/etc/default/cdrecord.new

# These don't need to be in the package, IMHO.
rm -rf $PKG/usr/include \
       $PKG/usr/lib/profiled \
       $PKG/usr/lib/*.a \
       $PKG/usr/man/man5

# Next big hole? ;)   (don't ship rscsi)
rm -r $PKG/usr/sbin
rm -f $PKG/etc/default/rscsi $PKG/usr/man/man1/rscsi.1

# Remove dupes:
rm -rf $PKG/usr/share/doc
rmdir $PKG/usr/share

# Copy docs:
mkdir -p $PKG/usr/doc/cdrtools-$VERSION/mkisofs
cp -a \
  ABOUT AN-* CDDL* CONTRIBUTING COPYING* Changelog \
  *GPL* README* START \
  $PKG/usr/doc/cdrtools-$VERSION
(
   cd mkisofs
   cp -a \
     COPYING* ChangeLog* README* RELEASE TODO \
     $PKG/usr/doc/cdrtools-$VERSION/mkisofs
)
(
   cd cdda2wav
   mkdir -p $PKG/usr/doc/cdrtools-$VERSION/cdda2wav
   cp -a \
     Changelog FAQ Frontends HOWTOUSE NEEDED OtherProgs README THANKS TODO \
     $PKG/usr/doc/cdrtools-$VERSION/cdda2wav
)

# The entire ChangeLog is excessive for most users:
cat $PKG/usr/doc/cdrtools-$VERSION/Changelog | head -n 1000 > $PKG/usr/doc/cdrtools-$VERSION/CL
mv -f $PKG/usr/doc/cdrtools-$VERSION/CL $PKG/usr/doc/cdrtools-$VERSION/Changelog
cat $PKG/usr/doc/cdrtools-$VERSION/mkisofs/ChangeLog | head -n 1000 > $PKG/usr/doc/cdrtools-$VERSION/mkisofs/CL
mv -f $PKG/usr/doc/cdrtools-$VERSION/mkisofs/CL $PKG/usr/doc/cdrtools-$VERSION/mkisofs/ChangeLog

### Add the mkzftree tool for zisofs ########################################

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/zisofs-tools-$ZISOFSTOOLS.tar.!(*sign|*asc|*sig)?
cd zisofs-tools-$ZISOFSTOOLS || exit 1
slackhousekeeping

# Build:
./configure \
   --prefix=/usr \
   --build=${SLK_ARCH_BUILD} || failconfig
make $NUMJOBS || make || failmake

# Install:
install -vpm755 mkzftree $PKG/usr/bin
install -vpm644 mkzftree.1 $PKG/usr/man/man1

# Install docs:
mkdir -p $PKG/usr/doc/zisofs-tools-$ZISOFSTOOLS
cp -a CHANGES COPYING* INSTALL README* zisofs.magic \
      $PKG/usr/doc/zisofs-tools-$ZISOFSTOOLS

# Remove these useless man pages that stomp on actual C library pages:
rm -rf $PKG/usr/man/man3

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

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, 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
