#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2012, 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 

source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars

LDAP_URL=`echo ${ldap_url} | awk '{ print $1 }'`

GSA_ID_LIST=`/opt/zimbra/common/bin/ldapsearch -x -H ${LDAP_URL} -w ${zimbra_ldap_password} -D uid=zimbra,cn=admins,cn=zimbra -b '' '(&(objectClass=zimbraDomain)(zimbraGalAccountId=*))' zimbraGalAccountId | awk '{if ($1 ~ /^zimbraGalAccountId:$/) { print $2 } }'`

MY_HOSTNAME=$zimbra_server_hostname
for ID in $GSA_ID_LIST
do
  GSA_MAIL_HOST=`/opt/zimbra/common/bin/ldapsearch -x -H ${LDAP_URL} -w ${zimbra_ldap_password} -D uid=zimbra,cn=admins,cn=zimbra -b '' '(&(objectClass=zimbraAccount)(zimbraId='${ID}'))' zimbraMailHost | awk '{if ($1 ~ /^zimbraMailHost:$/) { print $2 } }'`
  if [ "x${GSA_MAIL_HOST}" == "x${MY_HOSTNAME}" ]
  then
    /opt/zimbra/bin/zmgsautil trickleSync -i ${ID} -n InternalGAL
  fi
done
