##!/bin/bash

# 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
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.*
cd $PKGNAM* || exit 1
slackhousekeeping

# Fix install location:
# Don't do this because the temporary build path gets embedded into the
# binaries!
#sed -i 's?INS_BASE=.*?INS_BASE=\t'"$PKG/usr"'?' DEFAULTS/Defaults.linux
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 )

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

# Build & install into package:
make -e $NUMJOBS INS_BASE=/usr || exit 1
# Apparently, Without this second run, cdda2wav is skipped...
# I didn't notice that with ARM, but let's do it anyway.
make -e INS_BASE=/usr || exit 1
make -e install INS_BASE=/usr DEST_DIR=$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.?z?
cd zisofs-tools-$ZISOFSTOOLS || exit 1
slackhousekeeping

# Build:
./configure \
  --prefix=/usr \
  --build=$ARCH-slackware-linux-gnueabi || failconfig
make $NUMJOBS || 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

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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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
