2013-11-12 13:09:02 +01:00
|
|
|
#!/bin/awk -f
|
|
|
|
|
|
|
|
function _printhelp() {
|
2013-11-12 13:41:08 +01:00
|
|
|
print "Usage: hsync [-vr=1] SOURCE TARGET\n"
|
|
|
|
print "Filesystem Hierarchy synchronizer."
|
|
|
|
print "Move files in TARGET identical to files in SOURCE such that they follow the same hierarchy."
|
|
|
|
print "Symlinks are ignored."
|
|
|
|
print "Set 'r' to 1 to actually run the process."
|
2013-11-12 13:09:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
if (ARGC != 3)
|
|
|
|
{
|
|
|
|
_printhelp()
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
## Array subindices
|
|
|
|
dup=1
|
|
|
|
|
|
|
|
print "==> Scanning " ARGV[1] "..."
|
2013-12-27 11:16:50 +01:00
|
|
|
while ( ("cd " ARGV[1] " && find . -type f -exec md5sum {} +" | getline ) > 0)
|
2013-11-12 13:09:02 +01:00
|
|
|
{
|
|
|
|
## We need to remove sum from line since filename may contain spaces.
|
|
|
|
sum=$1
|
|
|
|
sub(/[^ ]+ /, "")
|
|
|
|
|
|
|
|
if (source[sum])
|
|
|
|
{
|
|
|
|
print ":: Skipping duplicate: " $0
|
|
|
|
source[sum, dup]=1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
source[sum]=$0
|
|
|
|
}
|
|
|
|
|
|
|
|
print "\n==> Scanning " ARGV[2] "..."
|
2013-12-27 11:16:50 +01:00
|
|
|
while ( ("cd " ARGV[2] " && find . -type f -exec md5sum {} +" | getline ) > 0)
|
2013-11-12 13:09:02 +01:00
|
|
|
{
|
|
|
|
sum=$1
|
|
|
|
if (source[sum, dup] || ! source[sum])
|
|
|
|
continue
|
|
|
|
sub(/[^ ]+ /, "")
|
|
|
|
|
|
|
|
if (target[sum])
|
|
|
|
{
|
|
|
|
print ":: Skipping duplicate: " $0
|
|
|
|
target[sum, dup]=1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
target[sum]=$0
|
|
|
|
}
|
|
|
|
|
2013-11-12 13:41:08 +01:00
|
|
|
print "\n==> Syncing hierarchy from " ARGV[1] " to " ARGV[2]
|
2013-11-12 13:09:02 +01:00
|
|
|
for (i in target)
|
|
|
|
{
|
2013-12-27 11:16:50 +01:00
|
|
|
## Some indices are compound, e.g. 'sum, dup'. We split the indices to
|
|
|
|
## check if they contain 'dup'.
|
2013-11-12 13:09:02 +01:00
|
|
|
split(i, subi, SUBSEP)
|
2013-12-27 11:16:50 +01:00
|
|
|
## Conditions:
|
|
|
|
## - i is not compound.
|
|
|
|
## - i is not a duplicate.
|
|
|
|
## - i is different in target and source.
|
2013-11-12 13:09:02 +01:00
|
|
|
if (subi[2] != dup && ! target[subi[1],dup] && source[subi[1]] != target[subi[1]])
|
|
|
|
{
|
|
|
|
dirname=source[subi[1]]
|
|
|
|
sub(/[^/]+$/, "", dirname)
|
|
|
|
|
2013-11-12 13:41:08 +01:00
|
|
|
## Target -> Source
|
|
|
|
change="'" ARGV[2] "/" target[subi[1]] "' '" ARGV[2] "/" source[subi[1]] "'"
|
|
|
|
|
2013-11-12 13:09:02 +01:00
|
|
|
## We only create a new folder if necessary.
|
2013-11-12 13:41:08 +01:00
|
|
|
cmd="[ ! -d '" dirname "' ] && mkdir -p \"" ARGV[2] "/" dirname "\" && mv -n " change
|
2013-11-12 13:09:02 +01:00
|
|
|
|
2013-11-12 13:41:08 +01:00
|
|
|
## TODO: sort preview.
|
|
|
|
print change
|
|
|
|
if (r)
|
2013-11-12 13:09:02 +01:00
|
|
|
system(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|