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