#!/bin/sh
# ---------------------- CONFIGURATION PART ----------------------

HAVE_MATLAB=yes
SWI_GUI=no
SETUP_X=yes
HEADLESS=yes
DRY_RUN=yes

if [ $# -gt 0 ]; then
	echo "Setting DRY_RUN to $1" 1>&2
	DRY_RUN=$1
fi

# If DRY_RUN=yes, then this script will do nothing, but will output the commands
# it would have done to stdout.

# ------------------- END OF CONFIGURATION PART ------------------

if [ $DRY_RUN == yes ]; then
	echo "I am doing a DRY RUN ONLY. The commands to be executed (as root) will" 1>&2
	echo "be output to stdout. Messages (such as this one) are output to stderr." 1>&2
	echo "Run the following to write a script to run later:" 1>&2
	echo  1>&2
	echo "  \$ sh setup_as_root.sh > install.sh" 1>&2
	echo 1>&2
	echo "Alternatively, run" 1>&2
	echo 1>&2
	echo "  \$ sh setup_as_root.sh no" 1>&2
	echo 1>&2
	DO=echo
else
	echo "I am going to do everything FOR REAL. Type 'yes' and hit return to continue" 
	read CONTINUE
	if [ $CONTINUE == yes ]; then
		echo "Ok, going ahead with setup"
	else
		exit 1
	fi
	DO=
fi
# APT GET installs
$DO apt-get install build-essential autoconf mercurial git pkg-config screen
$DO apt-get install libossp-uuid-dev libssl-dev zlib1g-dev unixodbc-dev libsmf-dev libsqlite3-dev
$DO apt-get install graphviz lilypond pdf2svg texlive-font-utils p7zip-full
$DO apt-get install vorbis-tools lame sox faad flac libsox-fmt-all fluidsynth

# Only if you want SWI Prolog GUI
if [ $SWI_GUI == yes ]; then
	$DO apt-get install libxpm-dev libxt-dev libjpeg-dev libx11-dev libxft-dev libxinerama1 libfontconfig1-dev
fi

# If you have MATLAB
if [ $HAVE_MATLAB == yes ]; then
	echo "Checking for MATLAB..." 1>&2
	MATLAB_ROOT=$(matlab -e | grep '^MATLAB=' | sed -e 's/MATLAB=//')
	echo "MATLAB installed in $MATLAB_ROOT" 1>&2
	$DO apt-get csh libtritonus-java libvorbisspi-java libmp3spi-java
	$DO ln -sf /lib/x86_64-linux-gnu/libc.so.6 /lib64
	$DO ln -sf $MATLAB_ROOT/bin/matlab /usr/local/bin 

	# MATLAB (R2012a at least) looks for Java extension jars in this directory
	$DO mkdir -p /usr/java/packages/lib/ext
	# NB - there may be some duplication of service providers here - I'm not sure
	# which of them works best.
	$DO ln -sf /usr/shara/java/{vorbisspi,mp3spi}.jar /usr/java/packages/lib/ext
	$DO ln -sf /usr/shara/java/tritonus_{aos,core,jorbis,mp3,remaining,share}.jar /usr/java/packages/lib/ext

	# need to add native library paths to this file
	if grep '/usr/lib/jni' $MATLAB_ROOT/toolbox/local/librarypath.txt; then
		echo "MATLAB Library path already set." 1>&2
	else
		if [ $DRY_RUN == yes ]; then
			echo "echo '/usr/lib/jni' >> $MATLAB_ROOT/toolbox/local/librarypath.txt"
		else
			echo '/usr/lib/jni' >> $MATLAB_ROOT/toolbox/local/librarypath.txt
		fi
	fi
fi

$DO install -bC rc.local /etc


if [ $SETUP_X  == yes ]; then
	echo "Setting up X server..." 1>&2
	if [ $HEADLESS == yes ]; then
		echo "Installing dummy video driver for headless X server..." 1>&2
		$DO apt-get install xinit xserver-xorg-video-dummy

		$DO install --mode=644 -bC xorg.conf /etc/X11
	else
		echo "Installing X server and window manager..." 1>&2
		$DO apt-get install xinit blackbox
	fi

	if [ $DRY_RUN == yes ]; then
		$DO dpkg-reconfigure x11-common
	else
		echo
		echo "I am about to call dpkg-reconfigure x11-common."
		echo "Please select 'Anybody' to allow dml to start the X server automatically."
		echo "Press RETURN to continue."
		read
		$DO dpkg-reconfigure x11-common
	fi
fi

