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