#!/bin/sh
#
# source: http://cdrkit.org/releases/cdrkit-1.1.11.tar.gz
#
# maintainer: gmg (g.monpontet@gmail.com)
#

soft='cdrkit'
softversion='1.1.11'
packageversion='66.1'

arch='i486'
cpu='i686'

buildir=$(pwd)
srcpkg="$buildir/$soft-$softversion.tar.gz"
src="$buildir/$soft-$softversion"
package="$soft-$softversion-$arch-$packageversion"
dest="$buildir/$package"

cd $buildir
rm -rf $src
tar -xvzf $srcpkg
rm -f $dest.tgz
rm -rf $dest
mkdir -p $dest

cd $src
cat $buildir/cdrkit-1.1.9-wformat-error.patch | patch -p1 --verbose
cat $buildir/cdrkit-1.1.9-cmake2.8-build.patch | patch -p1 --verbose
cat $buildir/cdrkit-1.1.9-buffer_overflow.patch | patch -p1 --verbose
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"
make
make install PREFIX=$dest/usr

find $dest | xargs file | grep -e "shared object" -e "executable" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# fix symlinks for cdrtools compatibility

cd $dest/usr/bin
  ln -s genisoimage mkisofs
  ln -s genisoimage mkhybrid
  ln -s icedax cdda2wav
  ln -s wodim cdrecord
  ln -s wodim dvdrecord

mv $dest/usr/share/man $dest/usr
gzip -9 $dest/usr/man/man?/*.?
rmdir $dest/usr/share

cd $dest/usr/man/man1
  ln -s icedax.1.gz cdda2wav.1.gz
  ln -s readom.1.gz readcd.1.gz
  ln -s wodim.1.gz cdrecord.1.gz
  ln -s wodim.1.gz netscsid.1.gz
  ln -s genisoimage.1.gz mkisofs.1.gz
cd $dest/usr/man/man5
  ln -s genisoimagerc.5.gz mkisofsrc.5.gz

mkdir -p $dest/install
cat << EODESC > $dest/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

      |-----handy-ruler---------------------------------------------------|
cdrkit: cdrkit - Tools for mastering and writing compact discs
cdrkit:
cdrkit: This is a fork of the last free GPL version of cdrtools.
cdrkit:
cdrkit: cdda2wav -- CD audio sampling utility.
cdrkit: wodim -- burn discs in most ATAPI and SCSI CD-R drives.
cdrkit: mkisofs  -- create ISO9660/HFS/Joliet CD-ROM images.
cdrkit: mkzftree -- compress a file tree to make a compressed CD-ROM.
cdrkit:
cdrkit:
cdrkit:
EODESC

cd $src
mkdir -p $dest/usr/doc/$soft-$softversion
cp -a ABOUT Changelog COPYING FAQ FORK TODO VERSION doc/* $dest/usr/doc/$soft-$softversion

mkdir -p $dest/usr/src/$soft-$softversion
cp $buildir/build-$soft.sh $dest/usr/src/$soft-$softversion

# Packaging

cd $dest
makepkg -l y -c n $dest.txz

cd $buildir
md5sum $package.txz > $package.md5