#!/bin/bash

GLPI_ORGANIZATION="Pulse2"

echo
echo "#############################"
echo "Reinstalling GLPI database..."
echo "#############################"
echo

# Get root password
ROOT_PASSWD=`cat /etc/mysql.secret`
LDAP_ROOT_PASSWD=`cat /etc/ldap.secret`

echo "drop database glpi;" | mysql -uroot -p${ROOT_PASSWD}
echo "drop user 'glpi'@'localhost';" | mysql -uroot -p${ROOT_PASSWD}
echo "glpi    glpi/dbconfig-reinstall   boolean true" | debconf-set-selections
echo "glpi    glpi/mysql/admin-pass	password ${ROOT_PASSWD}" | debconf-set-selections
dpkg-reconfigure -fnoninteractive glpi

# Superadmin username
GLPI_SUPERADMIN_NAME="glpi"

# Get its ID
GLPI_SUPERADMIN_ID=`echo "select name,id from glpi_users;" | mysql -uroot -p${ROOT_PASSWD} glpi --silent | grep "^${GLPI_SUPERADMIN_NAME}[[:space:]]\+" | awk -F'\t' '{ print $2 }'`
echo "${GLPI_SUPERADMIN_ID}" | grep -q '^[0-9]\+$'
if [ ! $? -eq 0 ]; then
  echo "Unable to find super-admin ID in GLPI database."
  exit 1
fi

# Clean users
echo "delete from glpi_users where name not like '${GLPI_SUPERADMIN_NAME}';" | mysql -uroot glpi -p${ROOT_PASSWD}

# Clean users/profiles joint table
echo "delete from glpi_profiles_users where id not like '${GLPI_SUPERADMIN_ID}';" | mysql -uroot glpi -p${ROOT_PASSWD}

# Rename superadmin name to root
echo "update glpi_users set name = 'root' where name = '${GLPI_SUPERADMIN_NAME}';" | mysql -uroot glpi -p${ROOT_PASSWD}

# Set its password
echo "update glpi_users set password = MD5('${LDAP_ROOT_PASSWD}') where name = 'root';" | mysql -uroot glpi -p${ROOT_PASSWD}

# Create root entity for this organization
echo "INSERT INTO glpi_entities (id,entities_id,level,name,completename) VALUES (1,0,1,'${GLPI_ORGANIZATION}','${GLPI_ORGANIZATION}');" | mysql -uroot glpi -p${ROOT_PASSWD}

# Create Univention Administrator user
echo "INSERT INTO glpi_users (name, password) VALUES ('Administrator', MD5('${LDAP_ROOT_PASSWD}'));" | mysql -uroot glpi -p${ROOT_PASSWD}

# Get its ID
GLPI_ADMINISTRATOR_ID=`echo "SELECT id FROM glpi_users WHERE name = 'Administrator'" | mysql -uroot -p${ROOT_PASSWD} glpi --silent`
GLPI_SUPERADMIN_PROFILE_ID=`echo "SELECT id FROM glpi_profiles WHERE name = 'Super-Admin'" | mysql -uroot -p${ROOT_PASSWD} glpi --silent`
GLPI_ENTITY_ID=`echo "SELECT id FROM glpi_entities WHERE name = '${GLPI_ORGANIZATION}'" | mysql -uroot -p${ROOT_PASSWD} glpi --silent`

# Give Administrator entity super-admin rights
echo "INSERT INTO glpi_profiles_users (users_id, profiles_id, entities_id, is_recursive, is_dynamic) VALUES ('${GLPI_ADMINISTRATOR_ID}', '${GLPI_SUPERADMIN_PROFILE_ID}', '${GLPI_ENTITY_ID}', 1, 0)" | mysql -uroot glpi -p${ROOT_PASSWD}

# Enable LDAP
BASEDN=`ucr get ldap/base`
echo "insert into glpi_authldaps values (1,'LocalLDAP','127.0.0.1','${BASEDN}','',7389,'','uid',0,'','(&(objectClass=posixGroup)(cn=GLPI-*))',1,'memberuid','mail','sn','givenname','telephonenumber','','mobile','',0,0,0,'',NULL,'preferredlanguage','','','2012-11-02 17:03:27','',1,1,'','','','','');" | mysql -uroot glpi -p${ROOT_PASSWD}
