xref: /freebsd/contrib/bmake/install-sh (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1:
2# NAME:
3#	install.sh - portable version of install(1)
4#
5# SYNOPSIS:
6#	install [-CNcs] [-f flags] [-i errs] [-o owner] [-g group] [-m mode] file1 file2 ...
7#	install -d  [-i errs] [-o owner] [-g group] [-m mode] directory ...
8#
9# DESCRIPTION:
10#	Compatible with BSD install(1).  Except that '-c' is always
11#	true and we always move an already installed target aside as
12#	this is important on many systems.  Recent BSD install(1)
13#	versions have a '-b' option for this.
14#
15#
16# OPTIONS:
17#	-b	move previous target file aside (always true).
18#
19#	-B "suffix"
20#		use "suffix" instead of .old for saving existing target.
21#
22#	-c	copy rather than move the file into place (always true).
23#
24#	-C	compare.  Only install if target is missing or
25#		different.
26#
27#	-N	newer. Only install if target is missing or older.
28#
29#	-s	strip target
30#
31#	-o "owner"
32#		make target owned by "owner"
33#
34#	-g "group"
35#		make target group owned by "group"
36#
37#	-m "mode"
38#		set permissions to "mode"
39#
40#	-f "flags"
41#		Pass "flags" onto chflags(1)
42#
43#	-i "errs"
44#		Ignore errors from steps indicated by "errs" (``s,o,g,m'').
45#
46# BUGS:
47#	The '-i' option is to save your sanity when 'bsd.prog.mk'
48#	insists on haveing a '-o' "owner" option which is doomed to
49#	fail on many systems.  We ignore '-b', '-B' and '-c' options.
50#
51# AUTHOR:
52#	Simon J. Gerraty <sjg@quick.com.au>
53#
54
55# RCSid:
56#	$Id: install-sh,v 1.18 2001/03/16 17:33:02 sjg Exp $
57#
58#	@(#) Copyright (c) 1993 Simon J. Gerraty
59#
60#	This file is provided in the hope that it will
61#	be of use.  There is absolutely NO WARRANTY.
62#	Permission to copy, redistribute or otherwise
63#	use this file is hereby granted provided that
64#	the above copyright notice and this notice are
65#	left intact.
66#
67#	Please send copies of changes and bug-fixes to:
68#	sjg@quick.com.au
69#
70
71set -- `getopt B:bpxCNcsdo:g:m:i:f: $*`
72
73Mydir=`dirname $0`
74[ -s $Mydir/.installrc ] && . $Mydir/.installrc
75
76owner=:
77group=:
78mode=:
79strip=:
80mkdirs=
81compare=:
82newer=:
83chflags=:
84LS1=
85CP_P=
86
87while [ $# -gt 1 ]
88do
89	case $1 in
90	--)	shift; break;;
91	-p)	CP_P=-p;;
92	-x)	set -x;;
93	-B)	OLD_EXT=$2; shift;;
94	-C)	compare=Different;;
95	-N)	newer=Newer;
96		# check if /bin/ls supports -1
97		/bin/ls -1 $0 >/dev/null 2>&1 && LS1=1
98		;;
99	-o)	owner="${CHOWN:-chown} $2 "; shift;;
100	-g)	group="${CHGRP:-chgrp} $2 "; shift;;
101	-m)	mode="${CHMOD:-chmod} $2 "; shift;;
102	-s)	strip=${STRIP:-strip};;
103	-d)	mkdirs="mkdir -p";;
104	-i)	ignore_err="$ignore_err$2"; shift;;
105	-f)	chflags="${CHFLAGS:-chflags} $2 "; shift;;
106	esac
107	shift
108done
109
110Newer() {
111	n=`/bin/ls -t$LS1 $* 2>/dev/null | head -1`
112	[ $1 = $n ]
113}
114
115Different() {
116	cmp -s $*
117	[ $? != 0 ]
118}
119
120Err() {
121	case "$ignore_err" in
122	*$1*)	;;
123	*)	exit 1;;
124	esac
125}
126
127Setem() {
128	# the order is important
129	if [ ! -d $1 ]; then
130		$strip $1 || Err s
131	fi
132	$group $1 || Err g
133	$owner $1 || Err o
134	$mode  $1 || Err m
135	$chflags  $1 || Err f
136	return 0
137}
138
139# a bug in HP-UX's /bin/sh, means we need to re-set $*
140# after any calls to add_path()
141args="$*"
142
143# all this just for chown!
144add_path () { [ -d $1 ] && eval ${2:-PATH}="\$${2:-PATH}:$1"; }
145add_path /etc
146add_path /usr/etc
147add_path /sbin
148add_path /usr/sbin
149
150# restore saved $*
151set -- $args
152
153# make directories if needed
154# and ensure mode etc are as desired
155if [ "$mkdirs" ]; then
156	for d in $*
157	do
158		[ ! -d $d ] && $mkdirs $d
159		Setem $d
160	done
161	exit 0			# that's all we do
162fi
163
164# install files
165if [ $# -gt 2 ]; then
166	dest_dir=yes
167elif [ $# -eq 1 ]; then
168	echo "what should I do with $*?" >&2
169	exit 1
170fi
171
172# get list of files
173while [ $# -gt 1 ]
174do
175	files="$files $1"
176	shift
177done
178# last one is dest
179dest=$1
180shift
181
182
183if [ "$dest_dir" = yes -a  ! -d $dest ]; then
184	echo "no directory $dest" >&2
185	exit 1
186fi
187
188for f in $files
189do
190	b=`basename $f`
191	if [ -d $dest ]; then
192		t=$dest/$b
193	else
194		t=$dest
195	fi
196	$newer $f $t || continue
197	$compare $f $t || continue
198	[ -f $t ] && { mv -f $t $t.old || exit 1; }
199	{ cp $CP_P $f $t && Setem $t; } || exit 1
200done
201exit 0
202