#!/bin/bash
echo '################################################'
echo '# EZ-CLAM installation script for Ubuntu 11.04 #'
echo '# By Murray Meehan, <murray.meehan@gmail.com>  #'
echo '# Revised: August 15, 2011                      #'
echo '################################################'
###############################################################################
#
#	NOTE: This release of EZ-CLAM.sh is intended for marking, and will
# 		also download a set of tutorial files to ~/clam-tutorials
# 		which are intended to be worked through in conjunction with
# 		the EZ-CLAM report. A copy can probably be found at 
# 			http://web.uvic.ca/meehanmw/clam/
#
# This script downloads and installs dependencies, runs SConstruct files, and
#  provides extra maintenance where appropriate to install the development 
#  branch of the CLAM C++ Library for Audio and Music. It requires superuser 
#  priveleges, and will ask for them if not provided. 
# 
# Default options:
#  Install CLAM and CLAM plugins in $CLAM_PREFIX=/usr/local
#  Download CLAM and CLAM dependency source code to $CLAM_SRC=~/src/clam-svn/
#  CLAM development headers will be installed to /usr/local/include/CLAM
#  
# The CLAM installers available on the http://clam-project.org website install
#  everything under the /usr/ prefix, instead of /usr/local, so this will allow
#  the stable and development (aka svn) branches to coexist somewhat.
#  You may not be able to cleanly install the SVN (trunk) and stable branches 
#  concurrently due to their common use of the CLAM_PREFIX and CLAM_PLUGIN_PATH 
#  environment variables. If you are careful about setting these before running 
#  either /usr/local/bin/NetworkEditor or /usr/bin/NetworkEditor, for instance, 
#  then you should be fine. The cautious thing to do is: assume that installing 
#  CLAM will break any previous CLAM installations. 
# 
# TODO (EZ-CLAM related): 
# 	check if data files are already downloaded before wget/svn'ing them.
# 	remove any plugin .so files from plugin folder which are not installed 
# 		during running of this file
# 
# TODO (CLAM project related): 
# 	check that the scons file for each plugin is copying .so files into the 
# 		plugin directory as required. (eg. "sudo cp *.so /usr/local/lib/clam/")
# 	fix all unsigned int to signed int comparison compiler warnings by 
# 		fixing the c++ source code, and submit patches.
# 		(static_cast<unsigned int>(variable) // Might work)
# 	Move Spectral plugin outside of Spacialization folder in source code. 
# 		This is a bug in the repository, as they are two seperate plugins
# 		and Spacialization is dependant on Spectral. 
# 	Ask for permission to bundle a few HRTF databases with CLAM, and 
# 		include them so that HRTFDatabaseReader can be used out of the box.
# 	Suggest that RoomImpulseResponseSimulator and related processings be moved 
# 		to a seperate plugin folder for code which is dependant on the non-GPL
# 		Barcelona Media Ray Tracing Library. This is currently causing errors 
# 		like "RoomImpulseResponseSimulator is not available" during compile.
# 	CLAM requires a 'scons configure' stage, but Network Editor and most plugins
# 		do not. Is there a particular convention we should follow?
# 	Review unit tests. The CLAM/tests folder should compile a set of unit tests 
# 		but it is unclear if these are working correctly.
###############################################################################
echo "#############################################################################"
echo "# Step 1. Installing all dependencies available via apt-get. (Ubuntu-11.04) #"
sudo apt-get -qq update # on a fresh wubi ubuntu (natty) 11.04 installations, scons was not found in the list of available packages until apt-get update was run.
sudo apt-get -qq install build-essential scons ladspa-sdk lv2core libfftw3-dev libjack-dev libmad0-dev libogg-dev libsndfile1-dev libvorbis-dev libid3-3.8.3-dev libasound2-dev portaudio19-dev libxerces-c-dev python-dev qt4-designer qt4-dev-tools libqt4-dev libqt4-opengl-dev libqtwebkit-dev blender libcppunit-dev help2man subversion cvs pkg-config  libportmidi0 libportmidi-dev liblo-tools liblo-dev python-liblo
echo "#################################"
echo "# Step 2. Environment variables #"
###################
# Build Variables
###################
# Save current directory to return to @ eof
export DOWNLOAD=~/EZ-CLAM
export CURRENT_DIR=$PWD 	
# CLAM install folder
export CLAM_PREFIX=/usr/local
export clam_prefix=$CLAM_PREFIX #Because some plugins require different capitalization.
export PREFIX=$CLAM_PREFIX # Network-Editor install folder
# CLAM source code download & compile directory, in case you want to modify or debug CLAM processings.
export CLAM_SRC=$DOWNLOAD/clam-svn 
# CLAM test data, used in CLAM example networks and by CLAM unit testing
export CLAM_TEST_DATA=$DOWNLOAD/clam-test-data

#export CLAM_EXAMPLE_NETWORK_DATA=$CLAM_PREFIX/share/clam # just for murrays reference
export QTDIR=/usr
export qt4=$QTDIR # A plugin (spacialization) requires a differently named qt4 path variable. If I don't supply one at all, I believe they mostly default to the correct place, /usr/local.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CLAM_PREFIX/lib:/usr/lib
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$CLAM_PREFIX/lib/pkgconfig/:/usr/lib/pkgconfig
export CLAM_PLUGIN_PATH=~/.clam/plugins:$CLAM_PREFIX/lib/clam 
###################
# CLAM runtime variables
###################
echo export CLAM_PREFIX=/usr/local >> ~/.bashrc 
echo export CLAM_PLUGIN_PATH=~/.clam/plugins:$CLAM_PREFIX/lib/clam >> ~/.bashrc
echo export QTDIR=/usr >> ~/.bashrc
echo export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CLAM_PREFIX/lib:/usr/lib >> ~/.bashrc
echo $LD_LIBRARY_PATH > ./CLAM.conf && sudo mv ./CLAM.conf /etc/ld.so.conf.d/CLAM.conf
###################
# If you don't want to repeatedly add lines to your .bashrc file, use these lines instead of CLAM runtime variables. (They may not work)
###################
#if [ -n $CLAM_PLUGIN_PATH ]; then echo export CLAM_PLUGIN_PATH=~/.clam/plugins:$CLAM_PREFIX/lib/clam >> ~/.bashrc; fi 
#if [ -n $CLAM_PLUGIN_PATH ]; then export CLAM_PLUGIN_PATH=~/.clam/plugins:$CLAM_PREFIX/lib/clam; fi
#if [ -n $QTDIR ]; then echo export QTDIR=/usr >> ~/.bashrc; fi 
#if [ -n $QTDIR ]; then export QTDIR=/usr; fi
#if [ -n $CLAM_PREFIX ]; then echo export CLAM_PREFIX=/usr/local >> ~/.bashrc; fi
#if [ -n $CLAM_PREFIX ]; then export CLAM_PREFIX=/usr/local; fi

######################################
# Step 2.b) Compiler (scons) options #
######################################
QUIET="-s" # QUIET MODE (values:-s or undefined)
export QUIET # If only this worked in real life...
ENABLE_COMPILER_OPTIMIZATONS=yes
ENABLE_WINDOWS_CROSS_COMPILATION=no
ENABLE_CLAM_VERBOSE_MODE=no
ENABLE_MULTITHREADING_SUPPORT=no # Useful, but will not work on all platforms
ENABLE_JACKD=no # This may be risky. JACKD does not seem to work on some platforms.
ENABLE_PORTMIDI=no # ENABLE_PORTMIDI=yes is not working because portmidi.h can't be found by src/PortMIDIDevice.cxx:25:22. Bizarre.

echo "#############################################"
echo "# Step 3. Precompute fftw-wisdom (optional) #"
# Speed boost. If we don't calculate wisdom now, it will be calculated during
# 	the first attempt to use fftw3, which may be inconvenient. So the INSTALL
# 	file says, anyway.
fftw-wisdom -v -x -o wisdom rof32768 cif32768
sudo mkdir /etc/fftw
sudo mv wisdom /etc/fftw/

echo "####################################"
echo "# Step 4. Refresh SVN source code. #"
svn co http://clam-project.org/clam/trunk $CLAM_SRC

echo "#########################"
echo "# Step 5. Compile CLAM. #"
cd $CLAM_SRC/CLAM
if [ $ENABLE_MULTITHREADING_SUPPORT == "yes" ]; then FLAG="-j3"; fi
scons -cs && scons configure $QUIET prefix=$PREFIX release=$ENABLE_COMPILER_OPTIMIZATONS crossmingw=$ENABLE_WINDOWS_CROSS_COMPILATION verbose=$ENABLE_CLAM_VERBOSE_MODE with_jack=$ENABLE_JACKD with_portmidi=$ENABLE_PORTMIDI && scons $QUIET $FLAG && sudo scons install $QUIET

echo "##################################"
echo "# Step 6. Compile NetworkEditor. #"
# Part 1: Download faust from CVS
cd $DOWNLOAD
cvs -q -d:pserver:anonymous@faudiostream.cvs.sourceforge.net:/cvsroot/faudiostream #login
cvs -q -z3 -d:pserver:anonymous@faudiostream.cvs.sourceforge.net:/cvsroot/faudiostream co -P faust
cd $DOWNLOAD/faust && make -s && sudo make install -s && echo 'Installation of faust was successful.'

# Part 2: Compile and install Network Editor itself.
# Fix bug which causes fatal compile error but has not been accepted as a patch yet. 
cd $CLAM_SRC/NetworkEditor/src/binders && rm ./InControlQtBinder.hxx && wget http://web.uvic.ca/~meehanmw/clam/patches/InControlQtBinder.hxx
cd $CLAM_SRC/NetworkEditor/ && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX release=$ENABLE_COMPILER_OPTIMIZATONS crossmingw=$ENABLE_WINDOWS_CROSS_COMPILATION verbose=$ENABLE_CLAM_VERBOSE_MODE && sudo scons install $QUIET || echo 'Error: NetworkEditor compile failed.'

echo "###############################################"
echo "# Step 7. Compile Plugins                     #"
echo "#    Tip: Comment out plugins you do not want.#"

echo "###################"
echo "# Spectral Plugin #"
cd $CLAM_SRC/CLAM/plugins/spacialization/spectral && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Spectral plugin failed.'

echo "#########################"
echo "# Spacialization Plugin #"
cd $CLAM_SRC/CLAM/plugins/spacialization && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Spacialization plugin failed.'
#scons -cs && #Scons clean command

echo "#################"
echo "# Filter Plugin #"
cd $CLAM_SRC/CLAM/plugins/Filters && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Filter plugin failed.'

echo "######################"
echo "# Resampling Plugin #"
cd $CLAM_SRC/CLAM/plugins/resampling && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Resampling plugin failed.'

echo "############################"
echo "# Guitar Effects Plugin    #"
cd $CLAM_SRC/CLAM/plugins/GuitarEffects && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Guitar Effects plugin failed.'

echo "##############################"
echo "# Time Domain Effects Plugin #"
cd $CLAM_SRC/CLAM/plugins/timeDomainEffects && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Time Domain Effects plugin failed.'

echo "####################"
echo "# Sndfile Plugin  #"
# Dependancy: sndfile
LIBSNDFILE="libsndfile-1.0.25"
cd $DOWNLOAD
#rm $LIBSNDFILE.tar.gz
wget -q http://www.mega-nerd.com/libsndfile/files/$LIBSNDFILE.tar.gz && tar zxf $LIBSNDFILE.tar.gz 
cd $LIBSNDFILE && ./configure -q && make -s && sudo make -s install  
cd $CLAM_SRC/CLAM/plugins/sndfile && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Sndfile plugin failed.'

echo "################"
echo "# OSC Plugin  #"
# Dependancies: liblo
cd $CLAM_SRC/CLAM/plugins/osc && scons $QUIET prefix=$PREFIX clam_prefix=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: OSC plugin failed.'

echo "#################"
echo "# Midi Plugin  #"
cd $CLAM_SRC/CLAM/plugins/MIDI && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Midi plugin failed.'

#echo "####################################"
#echo "# temporal_oboeSynthesizer Plugins #"
## Currently Unusable code. See compile errors
#cd $CLAM_SRC/CLAM/plugins/temporal_oboeSynthesizer && scons -cs && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET  || echo 'Error: temporal_oboeSynthesizer plugin failed.'

#echo "###################"
#echo "# Speech Plugins #"
## Currently Unusable code. See compile errors
#cd $CLAM_SRC/CLAM/plugins/speech && scons -cs && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET  || echo 'Error: Speech plugin failed.'

#echo "######################"
#echo "# Expression Plugins #"
## NOTE: Expression dependancies is too tricky to include in EZ-CLAM right now.
## It depends on different versions of libs than provided in Lucid. (Natty uses boost_1_42_0)
## Expression depends on boost.spirit v2.3 from boost_1_43_0 
## So you need a newer version that the one in Ubuntu Lucid.
## You can either A. use a backport or B. install from source.
## (Backport is only 32-bit compatible at the moment...)
#cd $CLAM_SRC/CLAM/plugins/expression && scons -cs && scons $QUIET prefix=$PREFIX CLAM_PREFIX=$CLAM_PREFIX && sudo scons install $QUIET || echo 'Error: Expression plugin failed.'

echo "################################"
echo "# Plugin installation complete.  #"
echo "# Contents of plugin directory $CLAM_PLUGIN_PATH : #"
ls /usr/local/lib/clam

echo "####################################"
echo "# Step 8. Prepare CLAM Unit Tests  #"
cd $DOWNLOAD && svn -q co http://clam-project.org/clam_data/trunk clam_data
cd $CLAM_SRC/CLAM/test && scons $QUIET -c && scons $QUIET test_data_path=$CLAM_TEST_DATA prefix=$PREFIX clam_prefix="$CLAM_PREFIX" || echo 'Error: Compiling CLAM Unit Tests failed.'
#$CLAM_SRC/CLAM/test/generateScriptManuals.sh

echo "###################################"
echo "# CLAM Installation complete.   #"
echo "###################################"
echo "# Downloading EZ-CLAM tutorials. #"
cd $DOWNLOAD 
#rm $DOWNLOAD/EZ-CLAM-tutorials.tgz
wget http://web.uvic.ca/~meehanmw/clam/EZ-CLAM-tutorials.tgz && tar -zxf ./EZ-CLAM-tutorials.tgz
# Copy EZ-CLAM-tutorials data to a system directory because CLAM Processing objects can't handle relative path file names.
sudo cp ./EZ-CLAM-tutorials.tgz /usr/local/share/clam
cd /usr/local/share/clam
sudo tar -zxf ./EZ-CLAM-tutorials.tgz

echo "#######################################"
echo "# Testing EZ-CLAM / CLAM Instalation. #"
echo "# Compiling and installing sample plugin: #"
cd $DOWNLOAD/EZ-CLAM-tutorials/mmExamplePlugin && scons && sudo scons install && echo 'Compiling and installing sample plugin complete.'
echo "#######################################"
echo "# Compiling sample c++ clam program: #"
cd $DOWNLOAD/EZ-CLAM-tutorials/mmExampleCompiledC++ClamProgram && scons && echo 'Compiling and installing sample compiled program complete.'
echo "#######################################"
echo "# Converting sample clam networks to c++ and compiling them: #"
$CLAM_SRC/CLAM/scripts/clamnetwork2code.py $DOWNLOAD/EZ-CLAM-tutorials/Sample\ Project\ Example\ 3.clamnetwork > $DOWNLOAD/EZ-CLAM-tutorials/mmExampleCompiledNetwork/Sample\ Project\ Example\ 1.cxx
$CLAM_SRC/CLAM/scripts/clamnetwork2code.py $DOWNLOAD/EZ-CLAM-tutorials/Sample\ Project\ Example\ 3.clamnetwork > $DOWNLOAD/EZ-CLAM-tutorials/mmExampleCompiledNetwork/Sample\ Project\ Example\ 2.cxx
$CLAM_SRC/CLAM/scripts/clamnetwork2code.py $DOWNLOAD/EZ-CLAM-tutorials/Sample\ Project\ Example\ 3.clamnetwork > $DOWNLOAD/EZ-CLAM-tutorials/mmExampleCompiledNetwork/Sample\ Project\ Example\ 3.cxx
cd $DOWNLOAD/EZ-CLAM-tutorials/mmExampleCompiledNetwork/ && scons
echo "#######################################"
echo "Fun fact: The EZ-CLAM-tutorials folder is: $DOWNLOAD/EZ-CLAM-tutorials "

