#! /usr/bin/perl
##
## Copyright (c) 2000, 2001 University of Utah and the Flux Group.
## All rights reserved.
## 
## This file is part of the Knit component composition software.
## 
## Permission to use, copy, modify, distribute, and sell this software and
## its documentation is hereby granted without fee, provided that the above
## copyright notice and this permission/disclaimer notice is retained in all
## copies or modified versions, and that both notices appear in supporting
## documentation.  THE COPYRIGHT HOLDERS PROVIDE THIS SOFTWARE "AS IS" AND
## WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION,
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE.  THE COPYRIGHT HOLDERS DISCLAIM ANY LIABILITY OF ANY KIND FOR
## ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
## 
## Users are requested, but not required, to send to csl-dist@cs.utah.edu any
## improvements that they make and grant Univ. of Utah redistribution rights.
##

# Usage: knit-smartmv srcfile dstfile

# Overwrites one file with another -if they are different or destination
# is missing- and deletes the source file.  This can be useful when
# generating files that make will treat as source files because it will
# not affect the timestamp if nothing changed.  (It would be pretty bad
# for files that make treats as intermediate or destination files.)

die "usage: $0 srcfile dstfile" unless @ARGV == 2;

($src,$dst) = @ARGV;
die "$0: source file '$src'does not exist" unless -f $src;

if (!(-f $dst) || system("cmp -s $src $dst") != 0) {
    rename($src,$dst);
} else {
    unlink($src);
}

exit(0);
