#!/bin/sh

# Check for previous cyrus user
/usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -read '/users/cyrus' >/dev/null 2>&1
if [ $? != 0 ] ; then
  exit 0
fi

# Check for previous cyrus user uid
/usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -read '/users/cyrus' 'uid' >/dev/null 2>&1
if [ $? != 0 ] ; then
  exit 0
fi

# Check for current cyrusimap user
/usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -read '/users/cyrusimap' >/dev/null 2>&1
if [ $? != 0 ] ; then
  exit 0
fi

# Check for current cyrusimap user uid
/usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -read '/users/cyrusimap' 'uid'  >/dev/null 2>&1
if [ $? != 0 ] ; then
  exit 0
fi

# if both uid's == 77, then nuke old cyrus user
if [ "`/usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -read '/users/cyrus' 'uid'`" = "uid: 77" ]; then
  if [ "`/usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -read '/users/cyrusimap' 'uid'`" = "uid: 77" ]; then
    /usr/bin/nicl -raw "$2/private/var/db/netinfo/local.nidb" -delete '/users/cyrus'
  fi
fi
