#!/bin/bash

# lists-diff,
# a simple script to zephyr you when your list membership changes

# Author: Liz A. Denys (lizdenys@mit.edu)
# Last updated on February 5, 2012

# Warn the user about how this should be used
echo "This script checks updates to your list membership every 15 "
echo "minutes and zephyrs you any changes until it receives SIGTERM. "
echo "Changes are also echoed to the shell where the script was run "
echo "from."
echo
echo "This script also assumes that both you and your kerberos "
echo "principal are on at least one list each; if not, you may not be "
echo "notified of all list membership changes--namely, when either "
echo "you or your kerberos principal are no longer on any lists."
echo
echo "If you kill this script and rerun it in the same directory as "
echo "the previous OLD_LISTS and OLD_KLISTS files, then it will "
echo "seamlessly show you any changes to your list membership since "
echo "the last time you ran lists-diff."
echo
echo "You should be running this in a directory that only you can "
echo "read, such as ~/Private. Otherwise, you risk exposing your list "
echo "membership, and you should abort this script, delete OLD_LISTS "
echo "and OLD_KLISTS, delete NEW_LISTS and NEW_KLISTS if they exist, "
echo "switch to a protected directory, and rerun this script there."
echo

# Repeat until given SIGTERM
while true; do 
    # Check old list membership
    touch OLD_LISTS
    numLists="$(wc -l OLD_LISTS | awk '{print $1}')"
    touch OLD_KLISTS
    numKLists="$(wc -l OLD_KLISTS | awk '{print $1}')"

    # Get new list membership
    athrun ops qy -s glom ruser $USER | awk -F , '{print $1}' | sort > NEW_LISTS
    athrun ops qy -s glom rkerberos $USER@ATHENA.MIT.EDU | awk -F , '{print $1}' | sort > NEW_KLISTS

    if [ ! -s NEW_LISTS ] && [ $numLists -ne 0 ]; then
        # Write out that we couldn't get list membership because
        # either qy failed or they are no longer on any lists.
        echo "No list membership for "$USER"."
        continue
    else
        # Get changes to list membership
        removedfrom="$(comm -23 OLD_LISTS NEW_LISTS)"
        addedto="$(comm -13 OLD_LISTS NEW_LISTS)"

        # Zephyr changes if there are any
        if [ -n "$removedfrom" ]; then
            if [ -n "$addedto" ]; then
                (echo "You have been added to the following lists:"; \
                 echo "$addedto"; \
                 echo; \
                 echo "You have been removed from the following lists:"; \
                 echo "$removedfrom") \
                | zwrite $USER -O auto -s "lists-diff" -d -n
		echo "You have been added to the following lists:"
                echo "$addedto"
                echo
                echo "You have been removed from the following lists:"
                echo "$removedfrom"
            else
                (echo "You have been removed from the following lists:"; \
                 echo "$removedfrom") \
                | zwrite $USER -O auto -s "lists-diff" -d -n
                echo "You have been removed from the following lists:"
                echo "$removedfrom"
            fi
        elif [ -n "$addedto" ]; then
            (echo "You have been added to the following lists:"; \
             echo "$addedto") \
            | zwrite $USER -O auto -s "lists-diff" -d -n
	    echo "You have been added to the following lists:"
            echo "$addedto"
        fi
    fi

    if [ ! -s NEW_KLISTS ] && [ $numKLists -ne 0 ]; then
        # Write out that we couldn't get kerberos list membership
        # because either qy failed or they are no longer on any lists.
        echo "No kerberos list membership for "$USER"."
        continue
    else
        # Get changes to list membership
        kremovedfrom="$(comm -23 OLD_KLISTS NEW_KLISTS)"
        kaddedto="$(comm -13 OLD_KLISTS NEW_KLISTS)"

        # Zephyr changes if there are any
        if [ -n "$kremovedfrom" ]; then
            if [ -n "$kaddedto" ]; then
                (echo "Your kerberos principal has been added to the following lists:"; \
                 echo "$kaddedto"; \
                 echo; \
                 echo "Your kerberos principal has been removed from the following lists:"; \
                 echo "$kremovedfrom") \
                | zwrite $USER -O auto -s "lists-diff" -d -n 
	        echo "Your kerberos principal has been added to the following lists:"
                echo "$kaddedto"
                echo
                echo "Your kerberos principal has been removed from the following lists:"
                 echo "$kremovedfrom"
            else
                (echo "Your kerberos principal has been removed from the following lists:"; \
                 echo "$kremovedfrom") \
                | zwrite $USER -O auto -s "lists-diff" -d -n
                echo "Your kerberos principal has been removed from the following lists:"
                echo "$kremovedfrom"
            fi
        elif [ -n "$kaddedto" ]; then
            (echo "Your kerberos principal has been added to the following lists:"; \
             echo "$kaddedto") \
            | zwrite $USER -O auto -s "lists-diff" -d -n
            echo "Your kerberos principal has been added to the following lists:"
            echo "$kaddedto"
        fi
    fi

    # Clean up
    mv NEW_LISTS OLD_LISTS
    mv NEW_KLISTS OLD_KLISTS
    # Wait 15 minutes to repeat
    sleep 900
done
# Written by lizdenys originals in /mit/lizdenys/bin
