#!/bin/csh
# create sfingerd files, for each user, by issuing
# a finger an filtering out sensible informations
# It might not produce perfect output at first shot... double check...
# I need to learn sed ! (and you need to know awk to understand what this does)
set VERS="sfingerd 1.8"
# Jun 9 1994 - Laurent Demailly - dl@hplyot.obspm.fr
# Thanks to Duster for fishy .plan detection suggestion

# Configure below :

# if your awk syntax is :
# awk [-F fs] [-v var=value] [prog | -f file ... ] [file ...]
# keep the following :
set AWK1="-v skip=1"
set AWK2=""
# if your awk syntax is :
# awk [ -f program-file ] [ -Fc ] [ program ] [ variable=value ] ...
# (like some SUN OS), uncomment below :
# set AWK1=""
# set AWK2="skip=1"

# check that this is the same one as in sfingerd.c :
set CHROOT_PATH=/usr/local/etc/fingerdroot

if ( `whoami` == root ) echo "If non-root can chown, better run this script as non root"

# remove or comment the next two lines, once you checked
# the CHROOT_PATH path, in order to be able to run it !
echo "You must edit 'make-files' before running it"
exit 1

umask 022
if ( -d $CHROOT_PATH ) then
echo "Fine, $CHROOT_PATH already exists..."
else
echo "Creating $CHROOT_PATH"
mkdir $CHROOT_PATH
endif

cd $CHROOT_PATH
echo "$VERS - by Laurent Demailly - <dl@hplyot.obspm.fr>" >! version
rm -f Wversion
ln -s version Wversion

# for each user that have a gecos field : (rather check those
# that have a valid login shell ?)
foreach user ( `awk -F: '$5!="" {print $1}' /etc/passwd` )
echo -n "working on $user..."
if ( -f $CHROOT_PATH/$user ) then
echo " nothing done, $CHROOT_PATH/$user already exists ! "
else
# try to detect 'fishy' .plans : (ie links)
# some ls output differs in number of space before link number...
( ls -l ~$user/.plan | egrep -v "^-......... +1 " ) >& /dev/null 
if ( $status == 0 ) then
   echo " "
   echo "****WARNING: Fishy ~$user/.plan, better double check finger output..."
endif
if ( -r ~$user/.plan ) then
# using -s, which gives size in blocks (512) or kbytes depending of OS
# You may want to adjust the thresold...
@ size=`ls -s ~$user/.plan|awk '{print $1}'`
  if ( $size > 20 ) then
   echo " "
   echo "****WARNING: Big ~$user/.plan, better double check finger output..."
  endif
endif
# remove sensible informations, get only one copy :
# ( maybe needs tailoring depending on your 'finger' output and the
#   amount of data you want to remove/keep )
finger $user | awk $AWK1 '/^Login name:/ || /^Login:/ {if (skip==1) skip=0; else skip=2;} \
/^Last login/ || /^Directory:/ || /^Never logged in/ || /^    from / || /^On since/ || /Idle Time$/ || /^No unread mail/ || /^No Mail./ || /^New mail/ || /^Mail last read/ || /unread since/ || /^Bldg:/ {skip++} \
skip==0 {print $0} skip==1 {skip=0}' $AWK2 > $user
chown $user $user
echo " successfully created $CHROOT_PATH/$user file..."
ln -s $user W$user
endif
end
echo "*** All done, have a look at the files in $CHROOT_PATH."
