##!/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=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
esac

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.*
cd $PKGNAM*
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
  ln -vfs i586-linux-cc.rul armv5l-linux-cc.rul
  ln -vfs i586-linux-cc.rul armv5tejl-linux-cc.rul
  ln -vfs i586-linux-gcc.rul armv5l-linux-gcc.rul 
  ln -vfs i586-linux-gcc.rul armv5tejl-linux-gcc.rul )

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

# Build & install into package:
INS_BASE=/usr make -e $NUMJOBS || failmake
# Apparently in Slackware, 'cdda2wav' is skipped unless this next build is done -- it wasn't
# with ARMedslack, but let's do it anyway:
INS_BASE=/usr make -e || failmake
INS_BASE=$PKG/usr make -e install || failinstall

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

# 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.
mkdir -vpm755 $PKG/etc/default
install -vpm644 cdrecord/cdrecord.dfl $PKG/etc/default/cdrecord.new
install -vpm644 rscsi/rscsi.dfl $PKG/etc/default/rscsi.new

# Install docs (removed a number of hardlinks that exist in the Slackware package; they
# were an oversight anyway):
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/{mkisofs,cdda2wav}
  cp -a \
  ABOUT AN-* BUILD CDDL* COMPILE CONTRIBUTING COPYING* Changelog \
  *GPL* INSTALL README* START \
  $PKG/usr/doc/$PKGNAM-$VERSION
rm -fv $PKG/usr/doc/$PKGNAM-$VERSION/README.compile # this is hardlinked to INSTALL

( cd mkisofs 
  cp -favv COPYING* ChangeLog README* TODO \
       $PKG/usr/doc/$PKGNAM-$VERSION/mkisofs )
( cd cdda2wav
  cp -favv \
        Changelog FAQ Frontends HOWTOUSE NEEDED OtherProgs README THANKS TODO \
        $PKG/usr/doc/$PKGNAM-$VERSION/cdda2wav )

# Delete junk: 
rm -rf $PKG/usr/include $PKG/usr/man/man5 $PKG/usr/man/man1/cdda2ogg.1 $PKG/usr/lib/profiled $PKG/usr/lib/*.a
rm -r $PKG/usr/sbin
rm -f $PKG/usr/doc/cdrtools-$VERSION/README.rscsi

### Build mkisofs with Acorn file type support ################################

# I could include the RISC OS patch with the main version but I'd rather keep
# it separate.
#
# 21-Oct-2008: No longer supporting the RiscPC, and secondly this no longer compiles.

#cd $TMPBUILD
#mkdir acorn-cdrtools
#cd acorn-cdrtools
#tar xvvf $PORTCWD/sources/cdrtools-2.01.tar.*
#cd cdrtools*
#slackhousekeeping

## Apply the RISC OS file type patch:
#bzcat $PORTCWD/sources/acorn-filetypes-cdrtools-2.0.1.patch.bz2 | patch -p1 --verbose || failpatch
##patch -p1 --verbose < $PORTCWD/sources/cdrtools-2.01-arm.patch || failpatch

## Configure: 
#( cd RULES
#  ln -vfs i586-linux-cc.rul armv5l-linux-cc.rul
#  ln -vfs i586-linux-cc.rul armv5tejl-linux-cc.rul
#  ln -vfs i586-linux-gcc.rul armv5l-linux-gcc.rul
#  ln -vfs i586-linux-gcc.rul armv5tejl-linux-gcc.rul )

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

## Install into package:
#install -vpm755 OBJ/*/mkisofs $PKG/usr/bin/mkisofs-riscos
##install -vm644 mkisofs.8 $PKG/usr/man/man8/mkisofs-riscos.8
#sed 's?mkisofs?mkisofs-riscos?g' mkisofs.8 > $PKG/usr/man/man8/mkisofs-riscos.8

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

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/zisofs-tools-$ZISOFSTOOLS.tar.?z?
cd zisofs-tools-$ZISOFSTOOLS
slackhousekeeping

# Build:
./configure \
  --prefix=/usr || 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
