#!/bin/sh
set -e
tomcats="tomcat7 tomcat8 tomcat9 tomcat10 tomcat"
for d in $tomcats
do
	testdir=/var/lib/$d
	if [ -d "$testdir" ]
	then
		tomcatdir=$testdir
	fi
done

for u in $tomcats
do
	if id $u >/dev/null 2>&1
	then
		tomcatuser=$u
	fi
done

if [ -z "$tomcatdir" -o -z "$tomcatuser" ]
then
	echo "tomcat not found, this should not happen."
	exit 1
fi

webappdir=$tomcatdir/webapps/dassadmin

if [ ! -f $webappdir/WEB-INF/web.xml ]
then
	cp $webappdir/WEB-INF/web.xml.dist $webappdir/WEB-INF/web.xml
	cp $webappdir/WEB-INF/velocity.properties.template $webappdir/WEB-INF/velocity.properties

	update-rc.d dassadmin defaults
fi


if [ ! -d /var/spool/dassadmin ]
then
	mkdir /var/spool/dassadmin
	echo 1 >/var/spool/dassadmin/lock
	chown -R $tomcatuser /var/spool/dassadmin
fi

if [ ! -d /var/spool/dassadmin/devicetypes ]
then
	mkdir /var/spool/dassadmin/devicetypes
	chown -R $tomcatuser /var/spool/dassadmin/devicetypes
fi


invoke-rc.d dassadmin restart
