#!/bin/sh

# Script to take a package installed version of
# Apache Directory Server 2.0 (eg apacheds-2.xxx.rpm)
# and make things ready to be used in a typical production deploy


cd /opt

test -e apacheds
if [ $? -eq 0 ] ; then
	echo /opt/apacheds already exists
else
	ln -s apacheds-* apacheds
	echo created symlink for /opt/apacheds
fi
cd apacheds/bin || exit 1

grep '^RUN_AS' apacheds >/dev/null
if [ $? -eq 0 ] ; then
	echo Getting rid of RUN_AS_USER so we can use port 389
	cp -p apacheds a.new   #for ownership copying
	sed 's/^RUN_AS/#RUN_AS/g' apacheds >a.new
	mv apacheds apacheds.orig
	mv a.new apacheds
fi

cd /var/lib
test -e apacheds
if [ $? -eq 0 ] ; then
	echo /var/lib/apacheds already exists
else
	ln -s apacheds-* apacheds
	echo created symlink for /var/lib/apacheds
fi

h=`hostname`

cd /var/lib/apacheds/default || exit 1

test -e keystore.ks
if [ $? -eq 0 ] ; then
	echo keystore.ks already exists
else
	echo Generating SSL key. When prompted, press return
	keytool -genkey -keyalg "RSA" -dname "cn=$h"  -alias $h -keystore keystore.ks -storepass secret
fi

# rename the servicename to something SANE, instead of
#  "apacheds-2.0.0.AM25-default"
cd /etc/init.d
test -e apacheds-2*-* || exit 0
ls -L apacheds-default >/dev/null 2>&1 && exit 0
rm -f apacheds
ln -s apacheds-2*-* apacheds-default
systemctl enable apacheds-default
echo enabled apacheds-default service
