#!/bin/bash
#
# Copyright (c) 2016 Frank Schuette fschuett@gymhim.de
# All rights reserved.
#


#
# check if we are started as root
# only one of UID and USER must be set correctly
#

if test "$UID" != 0 -a "$USER" != root; then
    echo "You must be root to start $0."
    exit 1
fi

if test ! -x /usr/bin/php; then
    echo "You need /usr/bin/php to start $0."
    exit 1
fi

r="$ROOT"

patchFile(){
file=$1
[ "$file" == "" ] && return;
sed \
-e "s/#SECRET_KEY#/$SECRET_KEY/g;" \
-e "s/#HORDE_PASS#/$HORDE_PASS/g;" \
-e "s/#CYRADM_PASS#/$CYRADM_PASS/g;" \
-e "s/#SCHOOL_DOMAIN#/$SCHOOL_DOMAIN/g;" \
-e "s/#SCHOOL_BASEDN#/$SCHOOL_BASEDN/g;" \
-e "s/#SCHOOL_NAME#/$SCHOOL_NAME/g;" \
-e "s/#SCHOOL_NETBIOSNAME#/$SCHOOL_NETBIOSNAME/g;" \
-e "s/#SCHOOL_SERVER#/$SCHOOL_SERVER/g;" \
-i $file
}

#
# load config files
#
osssysconf="$r/etc/sysconfig/schoolserver"
test -f $osssysconf || {
    echo "ERROR - can not find $osssysconf!!"
    echo "This should not happen.  Exit..."
    exit 1
}
. $osssysconf

ldapconf="$r/etc/sysconfig/ldap"
test -f $ldapconf || {
    echo "ERROR - can not find $ldapconf!!"
    echo "This should not happen.  Exit..."
    exit 1
}
. $ldapconf
SCHOOL_BASEDN=$(echo ${BIND_DN} | sed 's/[^,]*,//')

hordedist="$r/usr/share/oss-horde/config/dist.conf"
test -f $hordedist || {
    echo "ERROR - can not find $hordedist!!"
    echo "This should not happen.  Exit..."
    exit 1
}

. $hordedist

echo "Saving horde pw and secret key ..."
osshordeconf="$r/etc/oss-horde/horde/conf.php"
HORDE_PASS=
SECRET_KEY=
SCHOOL_DOMAIN_OLD=
if [ -e $osshordeconf ]
then
  # fetch horde db password and secret from conf.php
  phorde_pass="^\(\$conf\[sql\]\[password\]\)[[:space:]]*=[[:space:]]*\([^;]*\)[[:space:]]*;"
  psecret_key="^\(\$conf\[secret_key\]\)[[:space:]]*=[[:space:]]*\([^;]*\)[[:space:]]*;"
  pschool_domain="^\(\$conf\[problems\]\[maildomain\]\)[[:space:]]*=[[:space:]]*\([^;]*\)[[:space:]]*;"
  HORDE_PASS="$(cat $osshordeconf | sed "s/'//g" |sed -n "s/$phorde_pass/\2/p")"
  SECRET_KEY="$(cat $osshordeconf | sed "s/'//g" |sed -n "s/$psecret_key/\2/p")"
  SCHOOL_DOMAIN_OLD="$(cat $osshordeconf | sed "s/'//g" |sed -n "s/$pschool_domain/\2/p")"
fi
[ -n "$HORDE_PASS" ] || HORDE_PASS=`pwgen -s 24 1`
[ -n "$SECRET_KEY" ] || SECRET_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9-' | fold -w 60 | head -n 1)

DBEXISTS=$(mysqlshow | grep -w horde)
if [ -z "$DBEXISTS" ]; then
  echo "Creating horde database ..."
  mysql <<EOF
CREATE database horde;
EOF
fi

# recreate user
echo "(Re)creating horde user ..."
  mysql <<EOF
DROP USER horde@localhost;
FLUSH PRIVILEGES;
EOF
  mysql <<EOF
CREATE USER horde@localhost IDENTIFIED BY '$HORDE_PASS';
GRANT ALL ON horde.* TO horde@localhost;
FLUSH PRIVILEGES;
EOF

for f in `find $r$HORDESYSCONFDIR -name '*.in'`; do
  target=${f%.in}
  echo "Patching $target ..."
  cp $f $target
  patchFile $target
done

# rename mail domain
if [ -n "$SCHOOL_DOMAIN_OLD" ]; then
  echo "Renaming mail domain from $SCHOOL_DOMAIN_OLD to $SCHOOL_DOMAIN ..."
  mysql <<EOF
USE horde;
UPDATE horde_prefs SET pref_value = '$SCHOOL_DOMAIN' WHERE pref_value = '$SCHOOL_DOMAIN_OLD';
EOF

fi

# secure horde configuration
chown root:www $r/etc/oss-horde -R
find $r/etc/oss-horde -type f -exec chmod 440 '{}' \;

# ensure correct configuration
update-alternatives --set horde-config /etc/oss-horde/horde/conf.php

if [ -z "$DBEXISTS" ]; then
echo "Initializing database ..."
# create database and finish setup
(
  cd $r/usr/share/oss-horde/webmail/bin;
  echo -e "Administrator\n" | ./webmail-install
)

fi
