#!/bin/sh
# Script to make a cdrtools .tce extension from
# source
# Requires the compiletc.dsl extension
# Cdrtools License: CDDL/GPL


CFLAGS="-march=i686 -02 -pipe"
CXXFLAGS="-march=i686 -02 -pipe"

NAME=cdrtools
VERSION=2.01.01a42
DOWNLOAD=ftp://ftp.berlios.de/pub/cdrecord/alpha/
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=f32a50af3e5caafd0c20a3577e4a342e
TMPDIR=/tmp/"$NAME"
PKG=/tmp/"$NAME"/pkg
LIST="$TMPDIR"/"$NAME".list
SOURCEDIR=/tmp


download() {
cd "$SRCDIR"
if [ -e "$SOURCE" ]; then
if [ $(md5sum "$SOURCE" | cut -c1-32) != "$MD5SUM" ]; then
rm "$SOURCE"
fi
fi
if [ ! -e "$SOURCE" ]; then
wget "$DOWNLOAD"/"$SOURCE"
fi
if [ $(md5sum "$SOURCE" | cut -c1-32) = "$MD5SUM" ]; then
echo "md5sum passed."
else
echo "Download failed. aborting"
exit 1;
fi
}
build_source() {
tar xzvf "$SOURCEDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-$(echo $VERSION | cut -c1-7)
make INS_BASE=$PKG/usr/local DEFINSUSR=root DEFINSGRP=root
make INS_BASE=$PKG/usr/local DEFINSUSR=root DEFINSGRP=root install

cd "$PKG"
find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null
cd "$TMPDIR"
}

make_extension() {
cd "$PKG"
find . -not -type d > "$LIST"
tar -T "$LIST" -czvf /home/dsl/"$NAME"-"$VERSION".tce
cd "$TMPDIR"
}

if [ -e "$TMPDIR" ]; then
rm -rf "$TMPDIR" &&
mkdir -p "$PKG" &&
download &&
build_source &&
make_extension &&
echo ""$NAME"-"$VERSION".tce is now in your home directory."