#!/bin/bash

function die () {
    echo $*
    exit 1
}

function fetch_as_is() {
    if uname -a | grep --silent arwin ; then
        curl -LO $1
    else
        wget --no-check-certificate $1
    fi
}

function fetch_as () {
    if uname -a | grep --silent arwin ; then
        curl -L -o $1 $2
    else
        wget --no-check-certificate -O $1 $2
    fi
}

clean=
makespace=
parallel=2
PREFIX=$HOME/a3/inst
this_script_dir="`/usr/bin/dirname \"$0\"`"

while [ $# -gt 0 ] ; do
   case $1 in
    --clean*) clean=1 ; shift ;;
    --prefix*) PREFIX=`echo $1 | sed 's/--prefix=//'` ; shift ;;
    --space*) makespace=1 ; shift ;;
    -j*) parallel=`echo $1 | sed 's/-j//'` ; shift ;;
   esac
done


if [ x$clean != x -o x$makespace != x ] ; then
    if [ ! -d LRDF ] ; then 
        echo "This doesn't appear to be your source directory. Don't run --clean or --space here."
        exit 1
    fi
    # sudo required because of sudo while installing some python/perl stuff
    sudo rm -rf `find . -maxdepth 1 -type d | grep -v "^.$"`
    # sudo required because of its use during the python install step
    if [ $clean ] ; then
        sudo rm -rf $PREFIX
    fi
    exit 0
fi

mkdir -p $PREFIX

export MAKEFLAGS=-j$parallel
PATH=$PREFIX/bin:$PATH

OSX=

if uname -a | grep --silent arwin ; then

    OSX=y
    
    # its OS X! run for the hills !!

    export DYLD_FALLBACK_LIBRARY_PATH=$PREFIX/lib${DYLD_FALLBACK_LIBRARY_PATH:+:$DYLD_FALLBACK_LIBRARY_PATH}
    # force compilation to use 10.4 APIs only. could be overkill, but better safe than sorry
    GLOBAL_CFLAGS="-DMAC_OSX_VERSION_MAX_ALLOWED=1040 -mmacosx-version-min=10.4 -O3"
    # for 10.5 and above, add to CFLAGS
    # -sysroot=/Developer/SDKs/MacOSX10.4u.sdk"
    GLOBAL_LDFLAGS="-headerpad_max_install_names"
    # for 10.5 and above, add to LDFLAGS
    # "-syslibroot /Developer/SDKs/MacOSX10.4u.sdk"
    export MACOSX_DEPLOYMENT_TARGET=10.4
    # If the default python is not new enough, set PYTHON to point to a 
    # suitably new (2.7 or later) version of Python's framework
    PYTHON=
    #PYTHON=/Library/Frameworks/Python.framework/Versions/2.7
else 
    export LD_LIBRARY_PATH=$PREFIX/lib{$LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
    GLOBAL_CFLAGS=
    GLOBAL_LDFLAGS=
fi

set -e

export PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig

if [ ! -f boost_1_49_0.tar.bz2 ] ; then 
    echo boost
    fetch_as boost_1_49_0.tar.bz2 http://sourceforge.net/projects/boost/files/boost/1.49.0/boost_1_49_0.tar.bz2/download
fi
if [ ! -f fftw-3.3.1.tar.gz ] ; then
    echo fftw
    fetch_as_is http://www.fftw.org/fftw-3.3.1.tar.gz
fi
if [ ! -f libsigc++-2.2.10.tar.xz ] ; then 
    echo sigc++
    fetch_as_is http://ftp.gnome.org/pub/GNOME/sources/libsigc++/2.2/libsigc++-2.2.10.tar.xz
fi
if [ ! -f glibmm-2.32.0.tar.xz ] ; then
    echo glibmm
    fetch_as_is http://ftp.gnome.org/pub/GNOME/sources/glibmm/2.32/glibmm-2.32.0.tar.xz
fi
if [ ! -f cairomm-1.10.0.tar.gz ] ; then
    echo cairomm
    fetch_as_is http://cairographics.org/releases/cairomm-1.10.0.tar.gz
fi
if [ ! -f pangomm-2.28.4.tar.xz ] ; then 
    echo pangomm
    fetch_as_is http://ftp.acc.umu.se/pub/gnome/sources/pangomm/2.28/pangomm-2.28.4.tar.xz    
fi
if [ ! -f atkmm-2.22.6.tar.xz ] ; then 
    echo atkmm
    fetch_as_is http://ftp.gnome.org/pub/GNOME/sources/atkmm/2.22/atkmm-2.22.6.tar.xz
fi
if [ ! -f gtkmm-2.24.2.tar.xz ] ; then
    echo gtkmm
    fetch_as_is http://ftp.acc.umu.se/pub/GNOME/sources/gtkmm/2.24/gtkmm-2.24.2.tar.xz
fi
if [ ! -f libart_lgpl-2.3.21.tar.bz2 ] ; then 
    echo libart_lgpl
    fetch_as_is http://ftp.gnome.org/pub/gnome/sources/libart_lgpl/2.3/libart_lgpl-2.3.21.tar.bz2
fi
if [ ! -f libgnomecanvas-2.30.3.tar.bz2 ] ; then 
    echo libgnomecanvas
    fetch_as_is http://ftp.gnome.org/pub/gnome/sources/libgnomecanvas/2.30/libgnomecanvas-2.30.3.tar.bz2
fi
if [ ! -f libgnomecanvasmm-2.26.0.tar.bz2 ] ; then
    echo libgnomecanvasmm
    fetch_as_is http://ftp.acc.umu.se/pub/GNOME/sources/libgnomecanvasmm/2.26/libgnomecanvasmm-2.26.0.tar.bz2
fi

if [ ! -f liblo-0.26.tar.gz ] ; then 
    echo liblo
    fetch_as_is http://downloads.sourceforge.net/liblo/liblo-0.26.tar.gz
fi
if [ ! -f raptor2-2.0.6.tar.gz ] ; then 
    echo raptor
    fetch_as_is http://download.librdf.org/source/raptor2-2.0.6.tar.gz
fi
if [ ! -f rasqal-0.9.28.tar.gz ] ; then 
    echo rasqal 
    fetch_as_is http://download.librdf.org/source/rasqal-0.9.28.tar.gz
fi
if [ ! -f  redland-1.0.15.tar.gz ] ; then 
    echo librdf
    fetch_as_is http://download.librdf.org/source/redland-1.0.15.tar.gz
fi
if [ ! -f libogg-1.3.0.tar.xz ] ; then
    echo libogg
    fetch_as_is http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.xz
fi
if [ ! -f flac-1.2.1.tar.gz ] ; then
    echo flac
    fetch_as_is http://downloads.xiph.org/releases/flac/flac-1.2.1.tar.gz
fi
if [ ! -f libvorbis-1.3.2.tar.bz2 ] ; then
    echo libvorbis
    fetch_as_is http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.2.tar.bz2
fi
if [ ! -f libsamplerate-0.1.8.tar.gz  ] ; then
    echo libsamplerate
    fetch_as_is http://www.mega-nerd.com/SRC/libsamplerate-0.1.8.tar.gz
fi
if [ ! -f libsndfile-1.0.25.tar.gz ] ; then 
    echo libsndfile
    fetch_as_is http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25.tar.gz
fi
if [ ! -f aubio-0.3.2.tar.gz ] ; then
    echo aubio
    fetch_as_is http://aubio.org/pub/aubio-0.3.2.tar.gz
fi
if [ ! -f rdflib-3.2.0.tar.gz ] ; then
    echo python rdflib
    fetch_as_is http://rdflib.googlecode.com/files/rdflib-3.2.0.tar.gz
fi
if [ ! -f setuptools-0.6c11.tar.gz ] ; then
    echo python setuptools
    curl -L -o setuptools-0.6c11.tar.gz http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
fi    
if [ ! -f isodate-0.4.0.tar.gz ] ; then
    echo python isodate
    curl -L -o isodate-0.4.0.tar.gz  http://pypi.python.org/packages/source/i/isodate/isodate-0.4.0.tar.gz#md5=74eb2ffca6ab3318d6627f25f88e24e5
fi
if [ ! -d LRDF ] ; then
    echo LRDF
    git clone git://github.com/swh/LRDF.git
fi

if [ ! -f lv2-1.0.0.tar.bz2 ] ; then
    echo LV2
    fetch_as_is http://lv2plug.in/spec/lv2-1.0.0.tar.bz2
fi
if [ ! -f serd-0.14.0.tar.bz2 ] ; then
    echo Serd
    fetch_as_is http://download.drobilla.net/serd-0.14.0.tar.bz2
fi
if [ ! -f sord-0.8.0.tar.bz2 ] ; then
    echo Sord
    fetch_as_is http://download.drobilla.net/sord-0.8.0.tar.bz2
fi
if [ ! -f sratom-0.2.0.tar.bz2 ] ; then
    echo Sratom
    fetch_as_is http://download.drobilla.net/sratom-0.2.0.tar.bz2
fi
if [ ! -f lilv-0.14.2.tar.bz2 ] ; then
    echo Lilv
    fetch_as_is http://download.drobilla.net/lilv-0.14.2.tar.bz2
fi
if [ ! -f suil-0.6.2.tar.bz2 ] ; then
    echo Suil
    fetch_as_is http://download.drobilla.net/suil-0.6.2.tar.bz2
fi
if [ ! -f curl-7.25.0.tar.bz2 ] ; then
    echo Suil
    fetch_as_is http://curl.haxx.se/download/curl-7.25.0.tar.bz2
fi

if [ x$OSX = x ] ; then 
    if [ ! -f  util-linux-2.21.tar.xz ] ; then
        echo util-linux/uuid
        fetch_as_is http://www.kernel.org/pub/linux/utils/util-linux/v2.21/util-linux-2.21.tar.xz
    fi
    
    tar xf util-linux-2.21.tar.xz && \
        (set -e && cd util-linux-2.21 && ./configure --prefix=$PREFIX --enable-shared --disable-most-builds --without-ncurses --enable-libuuid && cd libuuid && make && make install) || die "uuid build failed"
fi

tar xf boost_1_49_0.tar.bz2 && \
  (set -e && cd boost_1_49_0 && ./bootstrap.sh --prefix=$PREFIX --with-libraries=exception && ./b2 && ./b2 install) || die "boost build failed"

tar xf curl-7.25.0.tar.bz2 && \
    (set -e && cd curl-7.25.0 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "curl build failed"

tar xf fftw-3.3.1.tar.gz && \
   (set -e && cd fftw-3.3.1 && ./configure --prefix=$PREFIX --enable-shared && make && make install && make clean && ./configure --prefix=$PREFIX --enable-shared --enable-single && make && make install) || die "fftw build failed"

tar xf libsigc++-2.2.10.tar.xz && \
    (set -e && cd libsigc++-2.2.10 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "sigc++ build failed"
tar xf glibmm-2.32.0.tar.xz && \
    (set -e && cd glibmm-2.32.0 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "glibmm build failed"
tar xf cairomm-1.10.0.tar.gz && \
    (set -e && cd cairomm-1.10.0 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "cairomm build failed"
tar xf pangomm-2.28.4.tar.xz && \
    (set -e && cd pangomm-2.28.4 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "pangomm build failed"
tar xf atkmm-2.22.6.tar.xz && \
    (set -e && cd atkmm-2.22.6 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "atkmm build failed"

tar xf gtkmm-2.24.2.tar.xz && \
   (set -e && cd gtkmm-2.24.2 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX --disable-documentation && make && make install) || die "gtkmm build failed"
tar xf libart_lgpl-2.3.21.tar.bz2 && \
    (set -e && cd libart_lgpl-2.3.21 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "libart build failed"
tar xf libgnomecanvas-2.30.3.tar.bz2 && \
    (set -e && cd libgnomecanvas-2.30.3 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "gnomecanvas build failed"
tar xf libgnomecanvasmm-2.26.0.tar.bz2 && \
    (set -e && cd libgnomecanvasmm-2.26.0 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "gnomecanvasmm build failed"


tar xf liblo-0.26.tar.gz && \
    (set -e && cd liblo-0.26 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "liblo build failed"

tar xf raptor2-2.0.6.tar.gz && \
    (set -e && cd raptor2-2.0.6 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "raptor build failed"

tar xf rasqal-0.9.28.tar.gz && \
    (set -e && cd rasqal-0.9.28 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "rasqal build failed"

tar xf redland-1.0.15.tar.gz && \
    (set -e && cd redland-1.0.15 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "redland build failed"

tar xf libogg-1.3.0.tar.xz && \
    (set -e && cd libogg-1.3.0 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "ogg build failed"

# FLAC will not build correctly on OS X Lion with asm optimization enabled, so lets at least force SSE
if [ x$OSX != x ] ; then
    tar xf flac-1.2.1.tar.gz && \
        (set -e && cd flac-1.2.1 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX --disable-cpplibs --disable-asm-optimizations --enable-sse --disable-debug && make && make install) || die "flac build failed"
else
    tar xf flac-1.2.1.tar.gz && \
        (set -e && cd flac-1.2.1 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX --disable-cpplibs --disable-debug && make && make install) || die "flac build failed"
fi

tar xf libvorbis-1.3.2.tar.bz2 && \
    (set -e && cd libvorbis-1.3.2 && CXXFLAGS="$GLOBAL_CFLAGS" CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "vorbis build failed"

sndfile_patch=$this_script_dir/misc-patches/libsndfile-1.0.25.patch

if [ x$OSX != x ] ; then
    # sndfile-play incorrectly includes <Carbon.h> which no longer works on Lion
    tar xf libsndfile-1.0.25.tar.gz && \
        (set -e && cd libsndfile-1.0.25 && patch -p0 < $sndfile_patch && CFLAGS="$GLOBAL_CFLAGS -I/System/Library/Frameworks/Carbon.framework/Headers" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX --disable-silent-rules && make && make install) || die "sndfile build failed"
    # samplerate incorrectly includes <Carbon.h> which no longer works on Lion
tar xf libsamplerate-0.1.8.tar.gz && \
    (set -e && cd libsamplerate-0.1.8 && CFLAGS="$GLOBAL_CFLAGS -I/System/Library/Frameworks/Carbon.framework/Headers" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "samplerate build failed"
else 
    tar xf libsndfile-1.0.25.tar.gz && \
        (set -e && cd libsndfile-1.0.25 && patch -p0 < $sndfile_patch && CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX --disable-silent-rules && make && make install) || die "sndfile build failed"
    tar xf libsamplerate-0.1.8.tar.gz && \
        (set -e && cd libsamplerate-0.1.8 && CFLAGS="$GLOBAL_CFLAGS" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && make install) || die "samplerate build failed"
fi

# aubio has an old version of install.sh which can exit with an error during parallel make install
# it also doesn't use pkg-config to test for FFTW3, sigh.
remove_fftw3_h=
if [ ! -f /usr/include/fftw3.h ] ; then
    remove_fftw3_h=y
    sudo ln -s $PREFIX/include/fftw3.h /usr/include
fi
tar xf aubio-0.3.2.tar.gz 
   (set -e && cd aubio-0.3.2 && sed '/no-long-double/d' < ./configure > ./configure.XXX && mv ./configure.XXX ./configure && chmod +x ./configure && \
       CFLAGS="$GLOBAL_CFLAGS -I$PREFIX" LDFLAGS="$GLOBAL_LDFLAGS" ./configure --prefix=$PREFIX && make && MAKEFLAGS=-j1 make install) || die "aubio build failed"
if [ x$remove_fftw3_h != x ] ; then
    sudo rm /usr/include/fftw3.h
fi

# these python lib wants to install to a system-wide location
tar xf rdflib-3.2.0.tar.gz && \
    (set -e && cd rdflib-3.2.0 && unset MACOSX_DEPLOYMENT_TARGET && python ./setup.py build && sudo python ./setup.py install)
tar xf setuptools-0.6c11.tar.gz && \
    (set -e && cd setuptools-0.6c11 && unset MACOSX_DEPLOYMENT_TARGET && python ./setup.py build && sudo python ./setup.py install)
tar xf isodate-0.4.0.tar.gz && \
    (set -e && cd isodate-0.4.0 && unset MACOSX_DEPLOYMENT_TARGET && python ./setup.py build && sudo python ./setup.py install)

(set -e && cd LRDF && \
  sed 's/glibtoolize/libtoolize/g' < autogen.sh > ag.sh && mv ag.sh autogen.sh && \
  CFLAGS="$GLOBAL_CFLAGS -I$PREFIX/include" LDFLAGS="$GLOBAL_LDFLAGS" sh ./autogen.sh --prefix=$PREFIX && make && make install) || die "LRDF build failed"

tar xf lv2-1.0.0.tar.bz2 && \
    (set -e && cd lv2-1.0.0 && unset MACOSX_DEPLOYMENT_TARGET && ./waf configure --prefix=$PREFIX --copy-headers && ./waf build && sudo ./waf install) || die "LV2 build failed"
tar xf serd-0.14.0.tar.bz2 && \
    (set -e && cd serd-0.14.0 && ./waf configure --prefix=$PREFIX && ./waf build && ./waf install) || die "serd build failed"
tar xf sord-0.8.0.tar.bz2 && \
    (set -e && cd sord-0.8.0 && ./waf configure --prefix=$PREFIX  && ./waf build && ./waf install) || die "sord build failed"
tar xf sratom-0.2.0.tar.bz2 && \
    (set -e && cd sratom-0.2.0 && ./waf configure --prefix=$PREFIX && ./waf build && ./waf install) || die "sratom build failed"
tar xf lilv-0.14.2.tar.bz2 && \
    (set -e && cd lilv-0.14.2 && ./waf configure --prefix=$PREFIX && ./waf build && ./waf install) || die "lilv build failed"
tar xf suil-0.6.0.tar.bz2 && \
    (set -e && cd suil-0.6.0 && CFLAGS="-I$PREFIX/include" ./waf configure --prefix=$PREFIX && ./waf && ./waf install) || die "suil build failed"

# two post-install hacks

if uname -a | grep --silent arwin ; then
    #
    # this is only needed on OS X where some Carbon headers define nil
    #
    cat <<EOFA | patch -d $PREFIX -p1
--- ../include/sigc++-2.0/sigc++/functors/functor_trait.h~
+++ ../include/sigc++-2.0/sigc++/functors/functor_trait.h
@@ -24,6 +24,9 @@
 #define _SIGC_FUNCTORS_MACROS_FUNCTOR_TRAITHM4_
 #include <sigc++/type_traits.h>
 
+#ifdef nil
+#undef nil
+#endif
 
 namespace sigc {
 
EOFA
fi

#
# libgnomecanvas has an incorrect pkg-config file. it has an implicit dependency on pangoft2.
#

cat <<EOFB | patch -d $PREFIX -p1
--- ../lib/pkgconfig/libgnomecanvas-2.0.pc~
+++ ../lib/pkgconfig/libgnomecanvas-2.0.pc
@@ -5,8 +5,8 @@
 
 Name: libgnomecanvas-2.0
 Description: libgnomecanvas
-Requires: libart-2.0 gtk+-2.0
-Requires.private: pango pangoft2 gail
+Requires: libart-2.0 gtk+-2.0 pangoft2
+Requires.private: pango gail
 Version: 2.30.3
 Libs: -L${libdir} -lgnomecanvas-2
 Cflags: -I${includedir}/libgnomecanvas-2.0

EOFB 
