#!/bin/sh

# Run this script in an empty directory to create a TOMUSS environment in Docker
# Run this script in a previously created environment to restart the servers

IMAGE=tomuss-docker-bare

if [ "*" != "$(echo *)" -a ! -f do_NOT_delete ]
then
	echo "You must be in an empty directory or in a configured directory"
	exit 1
fi >&1

# Load the docker image
if [ "" = "$(docker images | grep $IMAGE)" ]
then
	# Line to create the image:
	# docker save $IMAGE | gzip -9 >~/public_html/TOMUSS/$IMAGE.tar.gz
	wget https://perso.univ-lyon1.fr/thierry.excoffier/TOMUSS/$IMAGE.tar.gz -O - |
		zcat | docker load
fi

# Get the TOMUSS source code and make the links to the local data.
# The local data is kept in the launch directory.
# The LOCAL source code is untouched
# To update TOMUSS source code, just do:  mv tomuss-code tomuss-code.$(date +%Y%m%d)
if [ ! -d tomuss-code ]
then
	if [ -d /home/exco/TOMUSS/TOMUSS ]
	then
		git clone /home/exco/TOMUSS/TOMUSS
		mv TOMUSS tomuss-code
		echo "Cloning from local dir ok"
	else
	if git clone git://git.code.sf.net/p/tomuss/code tomuss-code
	then
		echo "Cloning from SourceForge ok"
	else
		echo "Cloning from SourceForge failed, get tar file"
		wget https://perso.univ-lyon1.fr/thierry.excoffier/TOMUSS/tomuss.tar.bz2
		bzcat tomuss.tar.bz2 | tar -xvf -
		VERSION=$(bzcat tomuss.tar.bz2 | tar -tvf - | (read LINE ; echo $LINE) | sed 's/.* //')
		mv $VERSION tomuss-code
	fi
	fi
	if [ ! -d PICTURES ]
	then
		mv tomuss-code/PICTURES .
	fi
	if [ ! -d LOCAL ]
	then
		mv tomuss-code/LOCAL.template LOCAL
	fi
	for DIR in TMP DB PICTURES UPLOAD LOGS Trash LOCAL DBregtest
	do
		mkdir $DIR 2>/dev/null
		ln -s ../$DIR tomuss-code
	done
fi

# Ask the minimum information to run a TOMUSS server
if [ ! -f do_NOT_delete ]
then
	touch do_NOT_delete
	chmod 444 do_NOT_delete
	LEFT=16
	WIDTH=200
	LOGIN=$(id -nu)
	DOMAIN=$(hostname --domain)
	if [ "" = "$DOMAIN" ]
	then
		DOMAIN="univ-lyon1.fr"
	fi
	HOST=$(hostname)
	DC="DC=$(echo $DOMAIN | sed 's/[.]/,DC=/g')"
	dialog --nocancel --output-fd 3 \
	    --form "Minimum TOMUSS configuration for testing" 22 78 0 \
		"CAS login url:" 1 1 "https://cas.$DOMAIN/cas"      1 $LEFT $WIDTH 0 \
		"LDAP host:"     2 1 "ldap.$DOMAIN"                 2 $LEFT $WIDTH 0 \
		"LDAP port:"     3 1 "636"                          3 $LEFT 5 0 \
		"LDAP login:"    4 1 ""                             4 $LEFT $WIDTH 0 \
		"LDAP password:" 5 1 ""                             5 $LEFT $WIDTH 0 \
		"LDAP top:"      6 1 "$DC"                          6 $LEFT $WIDTH 0 \
		"LDAP teachers:" 7 1 "CN=teachers,$DC"              7 $LEFT $WIDTH 0 \
		"LDAP students:" 8 1 "CN=students,$DC"              8 $LEFT $WIDTH 0 \
		"LDAP UE list:"  9 1 "OU=ue,$DC"                    9 $LEFT $WIDTH 0 \
		"LDAP groups:"  10 1 "OU=groups,$DC"               10 $LEFT $WIDTH 0 \
		"SMTP server:"  11 1 "smtp.$DOMAIN"                11 $LEFT $WIDTH 0 \
		"TOMUSS admin:" 12 1 "$LOGIN"                      12 $LEFT $WIDTH 0 \
		"TOMUSS mail:"  13 1 "$LOGIN@$DOMAIN"              13 $LEFT $WIDTH 0 \
		"TOMUSS URL:"   14 1 "http://$HOST.$DOMAIN"        14 $LEFT $WIDTH 0 \
		3>xxx.dialog

	split -l 1 xxx.dialog xxx
	read cas <xxxaa
	read ldap_server <xxxab
	read ldap_server_port <xxxac
	read ldap_server_login <xxxad
	read ldap_server_password <xxxae
	read ou_top <xxxaf
	read cn_teachers <xxxag
	read cn_students <xxxah
	read ou_students <xxxai
	read ou_groups <xxxaj
	read smtpserver <xxxak
	read root <xxxal
	read maintainer <xxxam
	read server_base_url <xxxan
	rm xxx.dialog xxx??

	cas="'$cas'"
	ldap_server="(\"$ldap_server\",)"
	ldap_server_login="'$ldap_server_login'"
	ldap_server_password="'$ldap_server_password'"
	ou_top="'$ou_top'"
	cn_teachers="'$cn_teachers'"
	cn_students="'$cn_students'"
	ou_students="'$ou_students'"
	ou_groups="'$ou_groups'"
	smtpserver="\"$smtpserver\""
	root="('$root',)"
	maintainer="'$maintainer'"
	server_base_url="\"$server_base_url\""

	for I in cas ldap_server ldap_server_port ldap_server_login ldap_server_password ou_top cn_teachers cn_students ou_students ou_groups smtpserver root maintainer server_base_url
	do
		echo "configuration.$I=$(eval echo \$$I)"
	done >>LOCAL/config.py
	echo "configuration.server_url = configuration.server_base_url + ':8888'" >>LOCAL/config.py
	echo "configuration.teachers = (configuration.cn_teachers,)" >>LOCAL/config.py
	sed -i "s,socket.gethostbyname(socket.gethostname()),$server_base_url.split('/')[-1]," LOCAL/config.py
fi

EXPORT_PORTS="-p 8888:8888 -p 8889:8889"

run() {
	set -x
	docker run -i -t \
		-u $(id -u) \
		-v $(pwd):/home \
		-w /home/tomuss-code \
		$EXPORT_PORTS \
		-e HOME=/home \
		$IMAGE \
		"$@"
}

if [ ! -f tomuss-code/TRANSLATIONS/fr/LC_MESSAGES/tomuss.mo ]
then
	echo "Compiling translations and creating background images"
	run "make translations >/dev/null 2>&1"
fi

case "$1" in
list) run SCRIPTS/list.py "$2" ;;
bash) run /bin/bash ;;
plugins)
    run ./plugins.py ;
	xdg-open tomuss-code/DOCUMENTATION/xxx_tomuss_plugins.html
	xdg-open tomuss-code/DOCUMENTATION/xxx_suivi_plugins.html
	xdg-open tomuss-code/DOCUMENTATION/xxx_doc_plugins.html
	;;
regtests)
	EXPORT_PORTS= # Do not pollute host ports during regression tests
	if [ "" = "$2" ]
		then run make regtest1
		else run sh -c "cd REGTEST_SERVER ; ./tests.py $2"
	fi ;;
try) run make try ;;
'')	run make start_and_wait ;;
*) echo "Possible arguments:
   * 'NOTHING': run tomuss and suivi servers
   * 'try': launch servers without authentication
   * 'list login': list LDAP informations about this login
   * 'bash': starts a bash in the docker
   * 'regtests': run regression tests
   * 'plugins': display configured plugins
" >&2
	exit 1 ;;
esac
