#!/bin/bash

# Some easy means to start/stop the mail spool
if [ -x /etc/init.d/exim ] ; then
  MTAINIT=/etc/init.d/exim
elif [ -x /etc/init.d/exim4 ] ; then
  MTAINIT=/etc/init.d/exim4
else
  echo "Unknown MTA, exiting..."
  exit 9
fi


copy_example_DB_CONFIG() {
#function to set a DB_CONFIG, ripped from slapd.postinst
# Copy an example DB_CONFIG file
# copy_example_DB_CONFIG <directory>
        local directory srcdir

        directory="$1"
        srcdir="/usr/share/slapd"

        if ! [ -f "${directory}/DB_CONFIG" ] && [ -d "$directory" ]; then
                cp $srcdir/DB_CONFIG "${directory}/DB_CONFIG"
        fi
}


# Init tree
init_ldap () {
  rm -f /var/lib/ldap/*
  copy_example_DB_CONFIG /var/lib/ldap


    if [ -f /etc/shadow ]
    then
	PW=`cat /etc/shadow | \
		 grep ^root | \
		 cut -d':' -f2`
    else
	PW=`cat /etc/passwd | \
		 grep ^root | \
		 cut -d':' -f2`
    fi

  HOSTNAME=`hostname -s`
  echo "info: Fetching SMB local SID from hostname '$HOSTNAME'."
  SAMBASID=`net getlocalsid $HOSTNAME 2>/dev/null | awk '{ print $6; }'`

  for ldif in \
    /etc/ldap/root.ldif \
    /etc/ldap/netgroup.ldif \
    /etc/ldap/autofs.ldif \
    /etc/ldap/dhcp.ldif
  do
    cat $ldif | sed -e "s:\$ROOTPW:$PW:" -e "s:\$SAMBASID:$SAMBASID:" | \
    /usr/sbin/slapadd

    if [ ! $? ]
      then
	echo "error: Unable to load $ldif"
	exit 1
    fi
  done
}

set -e

# Create ldap-tree on the initial install
$MTAINIT stop

# Check if slapd is running.  Use pidfile to avoid detecting the wrong
# daemon when running in a chroot.
if [ -f /var/run/slapd/slapd.pid ] &&
  kill -0 $(cat /var/run/slapd/slapd.pid) ; then
  RESTARTSLAPD=true
  /etc/init.d/slapd stop

  # Make sure slapd is really stopped
  SLAPPIDS=$(pidof slapd || /bin/true)
  if [ "$SLAPPIDS" ] ; then
    echo -n "Warning: slapd is still running, trying to TERM it"
    for SLAPPID in $SLAPPIDS ; do
      kill $SLAPPID || /bin/true
    done
  fi

  # Not sure why, but it seem like slapd takes some time to shut down
  LOOP=0
  while [ $LOOP -lt 10 ] ; do
    SLAPPIDS=$(pidof slapd || /bin/true)
    if [ "$SLAPPIDS" ] ; then
      let LOOP=($LOOP + 1)
      sleep 1
      echo -n "."
    else
	LOOP=10
    fi
  done
  echo

  if [ "$SLAPPIDS" ] ; then
    echo -n "Error: slapd is still running, I'll KILL it"
    for SLAPPID in $SLAPPIDS ; do
      kill -9 $SLAPPID || /bin/true
    done
  fi

  # Not sure why, but it seem like slapd takes some time to shut down
  LOOP=0
  while [ $LOOP -lt 10 ] ; do
    SLAPPIDS=$(pidof slapd || /bin/true)
    if [ "$SLAPPIDS" ] ; then
      let LOOP=($LOOP + 1)
      sleep 1
      echo -n "."
    else
	LOOP=10
    fi
  done
  echo
  if [ "$SLAPPIDS" ] ; then
    echo "Error: Critical: slapd is still running, I'm giving up"
    exit 9
  fi
fi

if slapcat 2> /dev/null \
  |grep 'dn: cn=all-hosts,ou=Netgroup,dc=skole,dc=skolelinux,dc=no' \
  >/dev/null 2>&1
then
  echo "Found existing data: skipping initalization"
else
  init_ldap

  #in Etch and Lenny, the database must be owned by openldap
  if getent passwd openldap | grep  -q openldap ; then
     chown -R openldap:openldap /var/lib/ldap
  fi
  # Add the samba-admin user
  # Do not try if you do not have the samba restart script
  [ -x /etc/init.d/samba ] && /usr/bin/samba-debian-edu-admin
fi

# Restart ldap server if we stopped it and it aint already running
# (samba-debian-edu-admin also tries to start slapd)
SLAPPIDS=$(pidof slapd || /bin/true)
if [ true = "$RESTARTSLAPD" ] && [ -z "$SLAPPIDS" ] ; then
  /etc/init.d/slapd start
fi

chown mail.mail /var/lib/maildirs/
$MTAINIT start
