# CONFIG file is insert above this line by make

if [ ! -f ${ACTIVE} ] ; then
	echo "Cannot locate active file ${ACTIVE}"
	exit 3
fi

OUTAGED=30
READERS=/tmp/nnq$$q
DIRS=/tmp/nnq$$d
echo > $DIRS

{
sort ${ACTIVE}

awk -F: '{print "%s %s\n", $1, $6}' /etc/passwd |
while read user homedir
do
  if [ -f $homedir/.nn/rc ] ; then
    if [ -n "`grep '^'$homedir'$' $DIRS`" ] ; then
      :
    else
      echo $homedir >> $DIRS
      if [ -n "`find $homedir/.nn -name rc -mtime -$OUTAGED -print`" ] ; then
        echo $user >> $READERS
        echo USER $user
        cat $homedir/.nn/rc
      fi
    fi
  fi
done
} |

awk '
BEGIN {
	a=1
	g=0
}

$1 == "USER" {
	a = 0
	user = " " $2
	next
}

a == 1 {
	g++
	group[$1] = g
	name[g] = $1
#	if ($2 == $3) last[g] = 0; else last[g] = $2+0
	sub[g] = 0
	users[g] = ""
	next
}		

$1 == "!" {
	next
}

$1 == "+" {
	n = group[$3]
#	if ( last[n] == 0 ) next
#	if ( ($2+200) < last[n] ) next
	sub[n]++;
	users[n] = users[n] user
	next
}

{
	next
}

END {
	for (n = 1; n <= g; n++) {
		printf "%5d %s\t%s\n", sub[n], name[n], users[n]
	}
}' |

if [ "$1" = "-s" ] ; then
	grep -v "^[ 	]*0"
elif [ "$1" = "-u" ] ; then
	grep "^[ 	]*0"
else
	cat
fi

rm -f $READERS $DIRS
