#!/bin/bash

# lists-owned-diff,
# a simple script to zephyr you when your list, fs, machine, etc.
# ownership 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 ownership 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 own at least one list each; if not, you may not be "
echo "notified of all list ownership changes--namely, when either "
echo "you or your kerberos principal no longer own any lists."
echo
echo "If you kill this script and rerun it in the same directory as "
echo "the previous OLD_OWNED_LISTS and OLD_OWNED_KLISTS files, then "
echo "it will seamlessly show you any changes to your list ownership "
echo "since the last time you ran lists-owned-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 "ownership, and you should abort this script, delete "
echo "OLD_OWNED_LISTS and OLD_OWNED_KLISTS, delete NEW_OWNED_LISTS and "
echo "NEW_OWNED_KLISTS if they exist, switch to a protected directory, "
echo "and rerun this script there."
echo

# Repeat until given SIGTERM
while true; do 
    # Check old list ownership
    touch OLD_OWNED_LISTS
    numOwnedLists="$(wc -l OLD_OWNED_LISTS | awk '{print $1}')"
    touch OLD_OWNED_KLISTS
    numOwnedKLists="$(wc -l OLD_OWNED_KLISTS | awk '{print $1}')"

    # Get new list ownership
    athrun ops qy -s gaus ruser $USER | sort > NEW_OWNED_LISTS
    athrun ops qy -s gaus rkerberos $USER@ATHENA.MIT.EDU | sort > NEW_OWNED_KLISTS

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

        # Zephyr changes if there are any
        if [ -n "$removedowns" ]; then
            if [ -n "$addedowns" ]; then
                (echo "You now own the following:"; \
                 echo "$addedowns"; \
                 echo; \
                 echo "You no longer own the following:"; \
                 echo "$removedowns") \
                | zwrite $USER -O auto -s "lists-owned-diff" -d -n
                echo "You now own the following:"
                echo "$addedowns"
                echo
                echo "You no longer own the following:"
                echo "$removedowns"
            else
                (echo "You no longer own the following:"; \
                 echo "$removedowns") \
                | zwrite $USER -O auto -s "lists-owned-diff" -d -n
                echo "You no longer own the following:"
                echo "$removedowns"
            fi
        elif [ -n "$addedowns" ]; then
            (echo "You now own the following:"; \
             echo "$addedowns") \
            | zwrite $USER -O auto -s "lists-owned-diff" -d -n
            echo "You now own the following:"
            echo "$addedowns"
        fi
    fi

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

        # Zephyr changes if there are any
        if [ -n "$kremovedowns" ]; then
            if [ -n "$kaddedowns" ]; then
                (echo "Your kerberos principal now owns the following:"; \
                 echo "$kaddedowns"; \
                 echo; \
                 echo "Your kerberos principal no longer owns the following:"; \
                 echo "$kremovedowns") \
                | zwrite $USER -O auto -s "lists-owned-diff" -d -n
                echo "Your kerberos principal now owns the following:"
                echo "$kaddedowns"
                echo
                echo "Your kerberos principal no longer owns the following:"
                echo "$kremovedowns"
            else
                (echo "Your kerberos principal no longer owns the following:"; \
                 echo "$kremovedowns") \
                | zwrite $USER -O auto -s "lists-owned-diff" -d -n
                echo "Your kerberos principal no longer owns the following:"
                echo "$kremovedowns"
            fi
        elif [ -n "$kaddedowns" ]; then
            (echo "Your kerberos principal now owns the following:"; \
             echo "$kaddedowns") \
            | zwrite $USER -O auto -s "lists-owned-diff" -d -n
            echo "Your kerberos principal now owns the following:"
            echo "$kaddedowns"
        fi
    fi

    # Clean up
    mv NEW_OWNED_LISTS OLD_OWNED_LISTS
    mv NEW_OWNED_KLISTS OLD_OWNED_KLISTS
    # Wait 15 minutes to repeat
    sleep 900
done
# Written by lizdenys originals in /mit/lizdenys/bin
