#!/bin/bash

slapcat | \
  while read LINE ; do 
    case "$LINE" in 
      dn:*) 
        POSACC= 
        POSUSER=
        IMAP= 
        DN="$LINE" ;; 
      objectClass:*posixAccount)
        POSACC=OK;;
      objectClass:*imapUser) IMAP=OK ;;
      uid:*) 
        if [ -n "$POSACC" ] ; then 
          POSUSER=$(echo $LINE | awk '{print $2}') 
        fi ;;
      cn:*) 
        CN="$LINE" ;;
      *) 
        if [ -n "$LINE" -o -z "$CN" ] ; then 
          continue ; 
        fi
        echo "$DN"
        echo "changetype: modify"
        echo "replace: cn"
        echo "$CN"
        if [ -n "$POSACC" -a -z "$IMAP" -a -n "$POSUSER" ] ; then 
          echo "-"
          echo "add: objectClass"
          echo "objectClass: imapUser"
          echo "-"
          echo "add: mailMessageStore"
          echo "mailMessageStore: /var/lib/maildirs/$POSUSER"
        fi 
        echo ""
        ;; 
    esac 
  done | \
    ldapmodify -x -P 3 -r -W \
      -D "cn=admin,ou=ldap-access,dc=skole,dc=skolelinux,dc=no"


