#!/bin/bash
# ---------------------- CONFIGURATION PART ----------------------

HAVE_MATLAB=no
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." 
	#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 -y build-essential autoconf mercurial git pkg-config screen curl
$DO apt-get install -y libossp-uuid-dev libssl-dev zlib1g-dev unixodbc-dev libsmf-dev libsqlite3-dev
$DO apt-get install -y graphviz lilypond pdf2svg texlive-font-utils p7zip-full
$DO apt-get install -y vorbis-tools lame sox faad flac libsox-fmt-all fluidsynth
$DO apt-get install -y libasound2-dev libgmp3-dev libreadline-dev zip unzip libarchive-dev # needed for prolog install in setup_dml
$DO apt-get install -y r-base # R support, may need more modern version 
# below may not be necessary
#sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password $DML_MYSQL_PASS'
#sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password $DML_MYSQL_PASS'
$DO apt-get install -y mysql-server-5.5 libmyodbc

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

# If you have MATLAB
if [ $HAVE_MATLAB == yes ]; then
	DO=$DO DRY_RUN=$DRY_RUN as_root.d/init_matlab.sh
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 -y 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 -y xinit blackbox
	fi

	if [ $DRY_RUN == yes ]; then
                $DO sudo sed -i \
                    's/allowed_users=console/allowed_users=anybody/' /etc/X11/Xwrapper.config

	else
		echo
		echo "I am about to configure 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
                $DO sudo sed -i \
                    's/allowed_users=console/allowed_users=anybody/' /etc/X11/Xwrapper.config
	fi
fi
echo "ALL DONE"
