#!/usr/local/bin/bash
#
# $Revision: 1.3 $
#
# Oliver Laumann <net@informatik.uni-bremen.de>
#
#
# Shell script that periodically runs rwho and ruptime (with all
# permutations of options) and stores the output in files in an
# NFS-exported directory.  On the NFS client systems, rwho and
# ruptime can then be replaced by trivial shell scripts that just
# cat(1) the cache files.
#
# Usually started at boot time.


# All cache files are updated once every $refresh_time seconds:

declare -r refresh_time=60


# Directory where the cache files reside:

declare -r cache_dir=/usr/spool/rwho/cache


# The original rwho command:

declare -r rwho_command=/usr/spool/rwho/bin/rwho


# The original ruptime command:

declare -r ruptime_command=/usr/spool/rwho/bin/ruptime



new_cache() {
    local filename=$cache_dir/$1
    shift
    "$@" > $filename.new && mv $filename.new $filename
}

set -u

PATH=$PATH:/usr/local/bin

finish_time=$[SECONDS+refresh_time]

while true; do
    new_cache rwho   $rwho_command
    new_cache rwho-a $rwho_command -a
    
    for i in "" -a -r -ar -l -al -rl -arl -u -au -ru -aru -t -at -rt -art; do
        new_cache ruptime$i $ruptime_command $i
    done

    sleep_time=$[finish_time-SECONDS]
    [ $sleep_time -gt 0 ] && sleep $sleep_time
    finish_time=$[finish_time+refresh_time]
done
