1#!/usr/bin/perl -w
2# copy a file if it is both newer and bigger in size
3# if copying, first rename older file to .orig
4
5$src = $ARGV[0];
6$dst = $ARGV[1];
7# dev,ino,mode,nlink,uid,gid,rdev,size,atime,mtime,ctime,blksize,blocks
8@srcstat = stat($src);
9@dststat = stat($dst);
10
11$srcsize = $srcstat[7];
12$srcmtime = $srcstat[9];
13$dstsize = $dststat[7];
14$dstmtime = $dststat[9];
15
16# copy if src file is bigger and newer
17if ($srcsize > $dstsize && $srcmtime > $dstmtime) {
18    print "mv -f $dst $dst.orig\n";
19    system("mv -f $dst $dst.orig");
20    print "cp -p $src $dst\n";
21    system("cp -p $src $dst");
22    die "cp command failed" if ($? != 0);
23}
24# make sure dst file has newer timestamp
25if ($srcmtime > $dstmtime) {
26    print "touch $dst\n";
27    system("touch $dst");
28}
29exit(0);
30