#!/bin/bash
#
#    Placed in public domain by Michal Maruka, 1997.   email: mmaruska@tin.it 
#
#
#  This program-script, preferably exec'd by cron, mirrors/caches   NFS-directories into local ones. For example I mount /usr but I want to store the 
#  most frequently used libraries, binaries, man-pages etc.  locally. 
#  So I use a directory /.usr/ (what would be an appropriate name ? /USR, /backup/usr ???) 
# 
# Select the directories to be cached
#-----------



# LOGGING:
#-----------
# I use Color ECHO, in case  you don't like it, I included a simple substitution (to skip the color information -- arg# 1)

silent ()
{
    echo $* >/dev/null
}


skipOne ()
{
    shift 1
    echo $*
}


#cecho=/usr/local/bin/cecho
cecho=skipOne


# Other commands:
#-----------------
    copycmd=/usr/local/sbin/updateNFScache.d/copy
    updatecmd=/usr/local/sbin/updateNFScache.d/update
    rmcmd=/bin/rm



# QUIET
getopts 'q' OPTS 

case $OPTS in
	'q')
	cecho=silent;;
esac    	









# WHAT TO MIRROR, AND WHERE 
# I use heavily only the /usr directory, not /usr/local where -- I make best to --  store only my own progs. I simply hate noting after months that the new 
# version of --say gawk-- got installed in /usr/local/bin  and I am still using /usr/bin/gawk the same for man p., libs ...
#
# For me `local'  is mine, Sorry I read FSSTDN long ago, last time.
#------------------------------------------------------------------------------------------------------------

# I don't  write "/usr",  because I use it for *sed* ...
remotename=usr
localname=\.$remotename

RemoteDir=/$remotename
LocalDir=/$localname



# BASH doesn't  perform file-expansion on assignment, as far as I know, so that's why I  use this trick:

SUBDIRS=`echo $RemoteDir/{sbin,bin,man/man[1-9n],lib}`





#Algorithm starts here:
#======================================



for originald in $SUBDIRS

do



#------------------------------------------
# Generate the (local) sub-directory where we `mirror' to
#------------------------------------------
copyd=`echo $originald|sed "s/$remotename/$localname/" `



#------------
# The times:
#  
#  It would be better not to make it absolute (imagine you work only once a week), and relate it to the last time  the
#  PROGRAM was run
#
#
#  after OLDT  days  not being used the file is removed -- unwanted.
#  file used (by another work-station) used in last NEWT  days  enters -- considered of interest.
#----------

OLDT='+2'
NEWT='-2'


$cecho blue $originald $copyd




# DELETE unused files from cache
#--------------------------------------
$cecho yellow "deleting  $copyd"
find $copyd -maxdepth 1  -mindepth  1      -atime $OLDT        -exec $rmcmd '{}'    \;  -fprint   $copyd/.deleted



# UPDATE files in the cache! Keep the  access/modified  times  un-touched!
#----------------------------
$cecho yellow "updating  $copyd (from $originald)"
find $copyd  -maxdepth 1   -mindepth  1             -fprint $copyd/.updated  



# COPY used files to cache
#----------------------
$cecho yellow "adding  from $originald (to $copyd)"
find $originald -maxdepth 1  -mindepth  1       -type f    -atime $NEWT    -fprint $copyd/.added



# find commands only `printed' the file-names. Now copy actually:

/usr/local/sbin/updateNFScache.d/copyActually   $remotename $localname   $copyd/.added


done




# As we copied libraries, update the /etc/ld.cache:

ldconfig
