#! /bin/sh

obmconf=/etc/obm/obm-rpm.conf
NEWFILE=/etc/obm/obm_conf.ini

if [ `id -g` -gt 0 ] ; then
	echo "ERROR: You need to be logged in as root"
	exit 0
else
	continue
fi

# check if the file exist
if [ -f $obmconf ] ; then
	mv $obmconf $obmconf.old
fi
echo -e "=============== OBM main configuration ================\n"
echo -e "o Please enter External Url (ex: localhost) : \c"
read value
echo "EXTERNALURL=$value" >> $obmconf
#echo -e "o Plese enter LDAP server name : \c"
#read value
#echo "OBM_LDAPSERVER=$value" >> $obmconf

# Modules stuff
for modules in LDAP MAIL SAMBA WEB ; do
	echo -e "o Enable module $modules : (y)es (n)o ? \c"
	read value
	if [ $value = "y" -o $value = "yes" ] ; then
		echo "${modules}_MODULE=true" >> $obmconf
		if [ $modules = "LDAP" ] ; then
			echo -e "o Plese enter LDAP server name : \c"
			read value
			echo "OBM_LDAPSERVER=$value" >> $obmconf
		fi
	else
		echo "${modules}_MODULE=false" >> $obmconf
	fi
done
              
echo -e "=============== OBM DataBase configuration ================\n"
echo -e "o Please enter the DataBase hostname: \c"
read value
echo "OBM_HOST=$value" >> $obmconf

echo -e "o Enter the DataBase name: \c"
read value
echo "OBM_DBNAME=$value" >> $obmconf

echo -e "o Enter the DataBase user: \c"
read value
echo "OBM_DBUSER=$value" >> $obmconf
echo -e "o Enter the DataBase user password: \c"
stty -echo
read value
stty echo
   OBM_PASSWD=$value
echo -e "\n"
echo "OBM_PASSWD=$value" >> $obmconf

. $obmconf
# DB
sed -i -e "s|^host = .*|host = ${OBM_HOST}|" ${NEWFILE}
sed -i -e "s|^db = .*|db = ${OBM_DBNAME}|"  ${NEWFILE}
sed -i -e "s|^user = .*|user = ${OBM_DBUSER}|"  ${NEWFILE}
sed -i -e "s|^password = .*|password = \"${OBM_PASSWD}\"|" ${NEWFILE}

# main
sed -i -e "s|^external-url = obm|external-url = $EXTERNALURL|" ${NEWFILE}
sed -i -e "s|^external-protocol = http|external-protocol = http|" ${NEWFILE}
sed -i -e "s|^ldapServer = localhost|ldapServer = $OBM_LDAPSERVER|" ${NEWFILE}
sed -i -e "s|^obm-ldap =.*|obm-ldap = $LDAP_MODULE|" ${NEWFILE}
sed -i -e "s|^obm-mail =.*|obm-mail = $MAIL_MODULE|" ${NEWFILE}
sed -i -e "s|^obm-samba =.*|obm-samba = $SAMBA_MODULE|" ${NEWFILE}
sed -i -e "s|^obm-web =.*|obm-web = $WEB_MODULE|" ${NEWFILE}

echo -e "o Would you like to install OBM DataBase now (y)es, (n)o ?: \c"
read answear
	if [ $answear = "y" -o $answear = "yes" ] ; then
		/usr/bin/obm-mysql
		echo -e "- Restarting Apache Server.....\c"
		$(/sbin/service httpd restart > /dev/null) && echo -e "OK"
	else
		echo -e "\n"
		echo "For later install just use </usr/bin/obm-mysql> command."
		exit 0
	fi
exit 0
