xref: /freebsd/share/mk/install-new.mk (revision a0409676120c1e558d0ade943019934e0f15118d)
1# $FreeBSD$
2# $Id: install-new.mk,v 1.3 2012/03/24 18:25:49 sjg Exp $
3#
4#	@(#) Copyright (c) 2009, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17.if !defined(InstallNew)
18
19# copy if src and target are different making a backup if desired
20CmpCp= CmpCp() { \
21	src=$$1 target=$$2 _bak=$$3; \
22	if ! test -s $$target || ! cmp -s $$target $$src; then \
23		trap "" 1 2 3 15; \
24		if test -s $$target; then \
25			if test "x$$_bak" != x; then \
26				rm -f $$target$$_bak; \
27				mv $$target $$target$$_bak; \
28			else \
29				rm -f $$target; \
30			fi; \
31		fi; \
32		cp $$src $$target; \
33	fi; }
34
35# Replace the file if they are different and make a backup if desired
36CmpReplace= CmpReplace() { \
37	src=$$1 target=$$2 _bak=$$3; \
38	if ! test -s $$target || ! cmp -s $$target $$src; then \
39		trap "" 1 2 3 15; \
40		if test -s $$target; then \
41			if test "x$$_bak" != x; then \
42				rm -f $$target$$_bak; \
43				cp -f $$target $$target$$_bak; \
44			fi; \
45		fi; \
46		mv -f $$src $$target; \
47	fi; }
48
49# If the .new file is different, we want it.
50# Note: this function will work as is for *.new$RANDOM"
51InstallNew= ${CmpReplace}; InstallNew() { \
52	_t=-e; _bak=; \
53	while :; do \
54		case "$$1" in \
55		-?) _t=$$1; shift;; \
56		--bak) _bak=$$2; shift 2;; \
57		*) break;; \
58		esac; \
59	done; \
60	for new in "$$@"; do \
61		if test $$_t $$new; then \
62			target=`expr $$new : '\(.*\).new'`; \
63			CmpReplace $$new $$target $$_bak; \
64		fi; \
65		rm -f $$new; \
66	done; :; }
67
68.endif
69