#!/bin/bash

if [ -f /etc/ldap.secret ]; then
  LDAP_PWD=`cat /etc/ldap.secret`
  LDAP_ADMIN="cn=admin"
elif [ -f /etc/ldap-backup.secret ]; then
  LDAP_PWD=`cat /etc/ldap-backup.secret`
  LDAP_ADMIN="cn=backup"
else
  exit 1
fi
BASEDN=`ucr get ldap/base`
UMASTERFQDN=`ucr get ldap/master`
UMASTERPORT=`ucr get ldap/master/port`
dbadminpasswd=`cat /etc/mysql.secret`
dbadminuser="root"
dbhost="localhost"
glpipasswd=`cat /etc/glpi/config/config_db.php  | grep 'dbpassword' | cut -f2 -d'=' | sed 's!^[[:space:]]*"!!' | sed 's!";[[:space:]]*$!!'`

pulse2-setup --batch \
    --mysql-host ${dbhost} \
    --mysql-user ${dbadminuser} \
    --mysql-passwd ${dbadminpasswd} \
    --ldap-uri ldap://${UMASTERFQDN}:${UMASTERPORT} \
    --ldap-basedn "${BASEDN}" \
    --ldap-admindn "${LDAP_ADMIN}, ${BASEDN}" \
    --ldap-passwd ${LDAP_PWD} \
    --glpi-enable True \
    --glpi-url http://localhost/glpi \
    --glpi-dbhost localhost \
    --glpi-dbname glpi \
    --glpi-dbuser glpi \
    --glpi-dbpasswd ${glpipasswd} \
    --reset-db

# mmc-agent is known to crash after running pulse2-setup
sleep 2
invoke-rc.d mmc-agent restart

# Associate imaging server with root entity and generate default menu
pulse2-load-defaults --link-server --default-menu
