#!/bin/sh
#
# Compilation script for capifax
# Generates libcapi20_fb and libspandsp before compiling
# capifax. Useful for most systems as both are most likely not to be
# found.
# libtiff, on the other hand, IS available on most systems, so it won't
# be compiled here.
#
# Tested on Gentoo
#
# Copyright (c) 2009, Phillip Berndt < mail at pberndt dot cöm >
#
mecho() { echo -en "\033[32m* \033[30m"; echo $@; }
mecho "Making directory ./gen-capifax/ and chdir'ing into it"
mkdir gen-capifax || exit 1
cd gen-capifax || exit 1
mkdir dest || exit 1

mecho "Generating libcapi20_fb"
mecho " Downloading"
wget -q -O - "http://fbrcapi.v3v.de/dl/libcapi20_fb-0.7.tar.bz2" | tar xj || exit 1
cd libcapi* || exit 1
mecho " Patching source"
patch -p1 > /dev/null <<EOF
diff -Nru libcapi20_fb-0.7~/libcapi20_fb/src/capi20.h libcapi20_fb-0.7/libcapi20_fb/src/capi20.h
--- libcapi20_fb-0.7~/libcapi20_fb/src/capi20.h	2009-11-01 21:42:29.184836090 +0100
+++ libcapi20_fb-0.7/libcapi20_fb/src/capi20.h	2009-11-01 21:42:44.873835219 +0100
@@ -78,7 +78,7 @@
 
 #ifndef WIN32
 #ifndef __NO_CAPIUTILS__
-#include <capiutils.h>
+#include "capiutils.h"
 #endif
 #endif
 
diff -Nru libcapi20_fb-0.7~/libcapi20_fb/src/capiutils.h libcapi20_fb-0.7/libcapi20_fb/src/capiutils.h
--- libcapi20_fb-0.7~/libcapi20_fb/src/capiutils.h	2009-11-01 21:42:29.184836090 +0100
+++ libcapi20_fb-0.7/libcapi20_fb/src/capiutils.h	2009-11-01 21:43:19.171835419 +0100
@@ -5,7 +5,7 @@
 #include <sys/time.h>
 #include <time.h>
 
-#include <capicmd.h>
+#include "capicmd.h"
 
 #ifdef __cplusplus
 extern "C" {
EOF
if [ "$?" != "0" ]; then exit 1; fi
mecho " Running configure"
./configure --prefix=/usr/ > /dev/null || exit 1
mecho " Running make"
make > /dev/null || exit 1
make install DESTDIR=`pwd`/../dest/ > /dev/null || exit 1
cd ../

mecho "Generating spandsp"
mecho " Downloading"
wget -q -O - "http://www.soft-switch.org/downloads/spandsp/spandsp-0.0.5.tgz" | tar xz || exit 1
cd spand* || exit 1
mecho " Running configure"
./configure --prefix=/usr/ > /dev/null || exit 1
mecho " Running make"
make > /dev/null || exit 1
make install DESTDIR=`pwd`/../dest/ > /dev/null || exit 1
cd ../
(
mv dest/usr/lib64/* dest/usr/lib
mv dest/usr/lib32/* dest/usr/lib
rmdir dest/usr/lib64 dest/usr/lib32
) 2> /dev/null

mecho "Generating capifax"
mecho " Downloading"
wget -q -O - "http://capifax.v3v.de/dl/capifax-0.7.3.tar.bz2" | tar xj || exit 1
cd capifax* || exit 1
mecho " Running configure"
export LDFLAGS="-L`pwd`/../dest/usr/lib"
export CFLAGS="-I`pwd`/../dest/usr/include -I`pwd`/`echo ../libcapi20*/libcapi20_fb`/src/"
./configure --prefix=/usr/ > /dev/null || exit 1
mecho " Running make"
make > /dev/null || exit 1
make install DESTDIR=`pwd`/../dest/ > /dev/null || exit 1
cd ../

mecho "Cleanup"
rm -rf capifax* spand* libcapi*
mv dest/* .
rmdir dest

cat > capifax.sh <<EOF
#!/bin/sh
export LD_LIBRARY_PATH=\`dirname \$0\`/usr/lib
\`dirname \$0\`/usr/bin/capifax \$@
EOF
chmod u+x capifax.sh

mecho
mecho "Done."
mecho "capifax was generated in ./gen-capifax/, prefixed for /usr/"
mecho "I also generated a script for you to run it from there if you don't want"
mecho "to install it system-wide."
mecho
