#!/bin/bash
# Script to make a Ogg Vorbis related .tcel extension from source
# Requires the compiletc.dsl, OSS.dsl, bash-3.2.tce and optionally the curl-7.18.2.tce extension.
# Licenses:
# Libogg: GPL
# Libvorbis: GPL
# Vorbis-tools: GPL
# Libao: GPL

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


NAME=ogg-vorbis
VERSION=1.2.0
DOWNLOAD=http://downloads.xiph.org/releases/ogg
DOWNLOAD1=http://downloads.xiph.org/releases/vorbis
DOWNLOAD2=http://downloads.xiph.org/releases/ao
SOURCE=libogg-1.1.3.tar.gz
SOURCE1=libvorbis-1.2.0.tar.gz
SOURCE2=libao-0.8.8.tar.gz
SOURCE3=vorbis-tools-1.2.0.tar.gz
MD5SUM=eaf7dc6ebbff30975de7527a80831585
MD5SUM1=478646358c49f34aedcce58948793619
MD5SUM2=b92cba3cbcf1ee9bc221118a85d23dcd
MD5SUM3=df976d24e51ef3d87cd462edf747bf9a
TMPDIR=/tmp/"$NAME"1357
PKG="$TMPDIR"/pkg
LIST="$TMPDIR"/"$NAME".list
SRCDIR=/tmp

download() {
cd "$SRCDIR"
if [ ! -e "$SOURCE" ]; then
wget "$DOWNLOAD"/"$SOURCE"
fi
md5sum "$SOURCE" | cut -c1-32 > "$TMPDIR"/md5.txt
if [ `cat "$TMPDIR"/md5.txt` = "$MD5SUM" ]; then
echo "md5sum passed."
else
echo "md5sum failed. aborting"
exit 1;
fi
}

download1() {
cd "$SRCDIR"
if [ ! -e "$SOURCE1" ]; then
wget "$DOWNLOAD1"/"$SOURCE1"
fi
md5sum "$SOURCE1" | cut -c1-32 > "$TMPDIR"/md51.txt
if [ `cat "$TMPDIR"/md51.txt` = "$MD5SUM1" ]; then
echo "md5sum passed."
else
echo "md5sum failed. aborting"
exit 1;
fi
}

download2() {
cd "$SRCDIR"
if [ ! -e "$SOURCE2" ]; then
wget "$DOWNLOAD2"/"$SOURCE2"
fi
md5sum "$SOURCE2" | cut -c1-32 > "$TMPDIR"/md52.txt
if [ `cat "$TMPDIR"/md52.txt` = "$MD5SUM2" ]; then
echo "md5sum passed."
else
echo "md5sum failed. aborting"
exit 1;
fi
}

download3() {
cd "$SRCDIR"
if [ ! -e "$SOURCE3" ]; then
wget "$DOWNLOAD1"/"$SOURCE3"
fi
md5sum "$SOURCE3" | cut -c1-32 > "$TMPDIR"/md53.txt
if [ `cat "$TMPDIR"/md53.txt` = "$MD5SUM3" ]; then
echo "md5sum passed."
else
echo "md5sum failed. aborting"
exit 1;
fi
}

build_source() {
tar xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/libogg-1.1.3

./configure --prefix=/usr/local
make
make DESTDIR="$PKG" install

cd "$TMPDIR"
}




build_source1() {
tar xzvf "$SRCDIR"/"$SOURCE1" -C "$TMPDIR"
cd "$TMPDIR"/libvorbis-1.2.0
export LD_LIBRARY_PATH="$PKG"/usr/local/lib
export LDFLAGS="-L"$PKG"/usr/local/lib"
export CPPFLAGS="-I"$PKG"/usr/local/include"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:"$PKG"/usr/local/lib/pkgconfig
./configure --prefix=/usr/local --with-ogg="$PKG" \
--with-ogg-libraries="$PKG"/usr/local/lib \
--with-ogg-includes="$PKG"/usr/local/include --disable-docs \
--disable-oggtest
make
make DESTDIR="$PKG" install

cd "$TMPDIR"
}

build_source2() {
tar xzvf "$SRCDIR"/"$SOURCE2" -C "$TMPDIR"
cd "$TMPDIR"/libao-0.8.8
./configure --prefix=/usr/local
make
make DESTDIR="$PKG" install

cd "$TMPDIR"
}

build_source3() {
tar xzvf "$SRCDIR"/"$SOURCE3" -C "$TMPDIR"
cd "$TMPDIR"/vorbis-tools-1.2.0

./configure --prefix=/usr/local
make
make DESTDIR="$PKG" 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
}

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

if [ -e "$TMPDIR" ]; then
rm -rf "$TMPDIR"
fi
mkdir -p "$PKG" &&
download &&
download1 &&
download2 &&
download3 &&
build_source &&
build_source1 &&
build_source2 &&
build_source3 &&
make_extension &&
echo ""$NAME"-"$VERSION".tcel is now in your home directory."
