#!/bin/sh


####################################
####################################
#
# Monome SerialOSC MacOSX (and possibly other *nixes) Setup Script
#
# About :
#					This script was tested under MacOSX 10.6.7
#					It will fetch, extract, and install all serialosc required dependencies
#					On MacOSX it will try to locate your Max5 folder and if found, install OSCBonjour
#
# Note :
#					This script requires XCODE to compile the sources !!
#
# Usage : 
#					Download anywhere, and from terminal simply run :
#					sh ./monome-serialosc-setup.sh
#
# Credits : 
#					rud #monome@freenode
#
# License :
#					modify, distribute, as you will, but keep Credits.
#
# Changelog :
#					v0.3 detect max5 presence before fetching oscbonjour (trivial) + cosmetics
#					v0.2 Darwin detection (for possible linux friendliness)
#					v0.1 initial version
#
# Note :
#					You shouldn't need to edit this script.
#					But if you need to, please keep me in touch with your mods.
#
####################################
####################################


# Where to download and compile stuff - Note, everything is than installed under /usr/local/
tmp="/tmp/monome-builds";

###############
# Dependencies
src_liblo="liblo-0.26";
url_liblo="http://downloads.sourceforge.net/project/liblo/liblo/0.26/liblo-0.26.tar.gz";

src_confuse="confuse-2.7";
url_confuse="http://savannah.nongnu.org/download/confuse/confuse-2.7.tar.gz";

src_libmonome="monome-libmonome";
url_libmonome="https://github.com/monome/libmonome/tarball/master";

src_serialosc="monome-serialosc";
url_serialosc="https://github.com/monome/serialosc/tarball/master";

src_oscbonjour="oscbonjour";
url_oscbonjour="http://docs.monome.org/lib/exe/fetch.php?media=app:zeroconf-max5-osx.zip";

# Version
version=0.3

###############
# Fetching function
fetch() {
	logfile=$tmp/log.$1

	echo "* Downloading $1";
	curl -L "$2" -o $tmp/${1}.tar.gz > $logfile 2>&1; 
	if [ ! $? -eq 0 ]; then cat $logfile; exit 1; fi

	cd $tmp

	echo "* Uncompressing $1";
	tar zxvf ${1}.tar.gz > $logfile 2>&1; 
	if [ ! $? -eq 0 ]; then cat $logfile; exit 1; fi
}


###############
# Building function
build() {
	logfile=$tmp/log.$1
	if [ -f $logfile ]; then rm -f $logfile; fi
	touch $logfile;
	
	fetch $1 $2

	#cd $1 # will not work with github sources :/
	cd `find . \( -name "${1}*" -not -iname "*.tar.gz" \)` # will work instead :)

	if [ -f ./configure ]; then 
		echo "* Configuring $1"; 	
		./configure > $logfile 2>&1; 
		if [ ! $? -eq 0 ]; then cat $logfile; exit 1; fi
		
	fi

	echo "* Compiling $1"
	make > $logfile 2>&1
		if [ ! $? -eq 0 ]; then cat $logfile; exit 1; fi

	echo "* Installing $1"
	echo "** Note sudo might ask for your password in order to install files under /usr/local"
	sudo make install  > $logfile 2>&1
	if [ ! $? -eq 0 ]; then cat $logfile; exit 1; fi
}

###############
# Max OSCBonjour function
installbonjour() {
	logfile=$tmp/log.$1
	if [ -f $logfile ]; then rm -f $logfile; fi
	touch $logfile;

	echo "* Guessing your Max/MSP folder"
	maxpath=`ls -lrt /Applications/Max* | head -1 | sed s#:##`;
	if [ -z $maxpath ]; then
		echo "! It appears you don't have Max/MSP, skipping oscbonjour installation ..";
		return;
	fi

	fetch $src_oscbonjour $url_oscbonjour
	
	echo "** Is your Max/MSP folder : $maxpath ?"
	echo "[y/n]";
	read conf
	if [ $conf != "y" ]; then 
		echo "!! Please copy the content of $tmp/$src_oscbonjour as described in the provided README"
		exit 1;
	fi
	
	echo "* Copying files to $maxpath"
	cd $tmp;
	sudo cp -R ./*.mxo ${maxpath}/"Cycling '74"/max-externals/
	sudo cp ./*.maxpat ${maxpath}/"Cycling '74"/max-externals/
	sudo cp ./*.maxhelp ${maxpath}/"Cycling '74"/max-externals/
}



###############
# The logic

# Clear dirty terminals
clear

# A little vanity..
echo "******";
echo "* monome-serialosc-setup v${version}";
echo "**";
echo "";

# Ensure XCODE or a suitable compiler is already installed (basically check that we have "make")
if [ `which -s make; echo $?` -eq 1 ]; then
	if [ `uname` = "Darwin" ]; then
		echo "You don't have XCODE ! Please download and install XCODE in order to compile everything !"
	else
		echo "You don't have a compiler ! Please download and install one in order to compile everything !"
	fi
	exit 1;
else
	if [ `uname` = "Darwin" ]; then
		echo "* XCODE found !"
	else
		echo "* Found "make" !"
	fi
fi

# Clear possible previous build attempts
if [ -d /tmp/monome-builds ]; then
	echo "* Cleaning old temporary dir";
	rm -rf $tmp
fi
# Make temp dir
if [ ! -d /tmp/monome-builds ]; then
	echo "* Creating temporary build dir";
	mkdir $tmp
fi

# Build everything
build $src_liblo $url_liblo
build $src_confuse $url_confuse
build $src_libmonome $url_libmonome
build $src_serialosc $url_serialosc
# Mac only, install oscbonjour
if [ `uname` = "Darwin" ]; then installbonjour; fi

# Self congratulations ..
echo ""
echo "All done !!"
echo "You can now start serialosc by typing \"./serialosc\".. THAN plug your monome.."