#!/bin/sh
#
# $Id:$

ptsgrouplist=/var/local/sync/groups
tmpdir=/tmp/sync.$$
pts=/usr/afs/bin/pts
echo=echo

trap "rm -rf ${tmpdir}" 1 15

case $1 in
"-n")
      maybe=$echo
      ;;
"")
      ;;
*)
      echo "Usage: $0 [-n]"
      exit 1
      ;;
esac

# Authentication functions
authme() {
    $maybe /usr/athena/bin/kinit -k
    $maybe /bin/athena/aklog -force athena sipb
}

unauthme() {
    $maybe /usr/athena/bin/kdestroy -q
    $maybe /bin/athena/unlog
}

if [ ! -r "$ptsgrouplist" ]; then
    echo "$0: $ptsgrouplist doesn't exist!"
    exit 1
fi

mkdir ${tmpdir}
if [ $? != 0 ]; then
    echo "$0: Couldn't create temp dir!"
    exit 1
fi

authme
for group in `cat $ptsgrouplist`; do
    $pts mem system:${group} -c athena.mit.edu 2>/dev/null | \
	grep -v '^Members' | sort > ${tmpdir}/${group}.athena
    $pts mem system:${group} -c sipb.mit.edu 2>/dev/null | grep -v '^Members' \
	| sort > ${tmpdir}/${group}.sipb
    comm -23 ${tmpdir}/${group}.athena ${tmpdir}/${group}.sipb \
	> ${tmpdir}/${group}.changes
    for member in `cat ${tmpdir}/${group}.changes`; do
	$maybe $pts adduser ${member} system:${group} -c sipb.mit.edu
    done
done
unauthme

$maybe rm -rf ${tmpdir}
exit 0
