#!/bin/bash
typeset -i NEXTNUM=100

if [ ! -e /usr/bin/getent.ldap ]; then
    echo "ERREUR: la commande /usr/bin/getent.ldap n'est pas installée"
    echo "apt-get install nslcd-utils ..."
fi

getent.ldap group | cut -d ":" -f3 | sort -u > /tmp/gid_serveur
cat /etc/group | cut -d ':' -f3 > /tmp/locaux

function getNext() {
    NEXTNUM+=1
    TESTDISPO=`grep -w "${NEXTNUM}" /etc/group /tmp/gid_serveur`
    if [ -n "${TESTDISPO}" ]; then
	getNext
    fi
}

for gid in `cat /tmp/gid_serveur`
do
    TEST=`grep -w ${gid} /tmp/locaux`
    if [ -n "${TEST}" ]; then
	NOM=`grep ":${gid}:" /etc/group | cut -d ":" -f1`
	echo "Collision pour $gid ... ${NOM}"
	getNext
	echo " on change le GID en ${NEXTNUM}"
	groupmod -g ${NEXTNUM} ${NOM}
    fi
done

