1246149Ssjg# $Id: install-new.mk,v 1.3 2012/03/24 18:25:49 sjg Exp $
2246149Ssjg#
3246149Ssjg#	@(#) Copyright (c) 2009, Simon J. Gerraty
4246149Ssjg#
5246149Ssjg#	This file is provided in the hope that it will
6246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
7246149Ssjg#	Permission to copy, redistribute or otherwise
8246149Ssjg#	use this file is hereby granted provided that 
9246149Ssjg#	the above copyright notice and this notice are
10246149Ssjg#	left intact. 
11246149Ssjg#      
12246149Ssjg#	Please send copies of changes and bug-fixes to:
13246149Ssjg#	sjg@crufty.net
14246149Ssjg#
15246149Ssjg
16246149Ssjg.if !defined(InstallNew)
17246149Ssjg
18246149Ssjg# copy if src and target are different making a backup if desired
19246149SsjgCmpCp= CmpCp() { \
20246149Ssjg	src=$$1 target=$$2 _bak=$$3; \
21246149Ssjg	if ! test -s $$target || ! cmp -s $$target $$src; then \
22246149Ssjg		trap "" 1 2 3 15; \
23246149Ssjg		if test -s $$target; then \
24246149Ssjg			if test "x$$_bak" != x; then \
25246149Ssjg				rm -f $$target$$_bak; \
26246149Ssjg				mv $$target $$target$$_bak; \
27246149Ssjg			else \
28246149Ssjg				rm -f $$target; \
29246149Ssjg			fi; \
30246149Ssjg		fi; \
31246149Ssjg		cp $$src $$target; \
32246149Ssjg	fi; }
33246149Ssjg
34246149Ssjg# If the .new file is different, we want it.
35246149Ssjg# Note: this function will work as is for *.new$RANDOM"
36246149SsjgInstallNew= ${CmpCp}; InstallNew() { \
37246149Ssjg	_t=-e; _bak=; \
38246149Ssjg	while :; do \
39246149Ssjg		case "$$1" in \
40246149Ssjg		-?) _t=$$1; shift;; \
41246149Ssjg		--bak) _bak=$$2; shift 2;; \
42246149Ssjg		*) break;; \
43246149Ssjg		esac; \
44246149Ssjg	done; \
45246149Ssjg	for new in "$$@"; do \
46246149Ssjg		if test $$_t $$new; then \
47246149Ssjg			target=`expr $$new : '\(.*\).new'`; \
48246149Ssjg			CmpCp $$new $$target $$_bak; \
49246149Ssjg		fi; \
50246149Ssjg		rm -f $$new; \
51246149Ssjg	done; :; }
52246149Ssjg
53246149Ssjg.endif
54