#!/bin/sh
#
# usage: copyActually <directory of origin> <destination directory> <file>
# 
# Well the first 2 args are just commands to the SED !! to obtain source file(name)s
#
# The *aim* is to avoid copying hard-links as different files. The <file> contains the destination file-names.
# So we first look at i-nodes, sort and unique... divide
#
######
file=$3
tmpPrefix=/tmp/NFS

originals=${tmpPrefix}originals


#
# Some tmp files to divide file-names into groups corresponding to the same i-node
# ------------------------

links=${tmpPrefix}links
info=${tmpPrefix}base
relation=${tmpPrefix}relation

#
# The idea is to divide into -say- master file-names (first in alphabet)  and other hard-links
# then make *join*  -> finally copy masters, and create local hard-links.
#--------------------------

xargs --no-run-if-empty  ls -i  <$file > $info

sort -k1,1 -u $info > $originals
sort -k1,1 $info    |comm -1  -3 $originals  - >$links
join -j1 1  -j2 1 -o 1.2 2.2   $originals  $links >$relation



# Now copy:
#----------------------


cut --fields=2  -d ' '   $originals|       \
xargs  --max-lines=1  --no-run-if-empty   /usr/local/sbin/updateNFScache.d/copy $1 $2  



# echo "Now making links"
sed "s/$1/$2/g"  $relation  >${relation}.new
xargs --no-run-if-empty  --max-lines=1 /usr/local/sbin/updateNFScache.d/link  <${relation}.new

#
# Clean
rm $originals $links  $info  $relation ${relation}.new

