1236769Sobrien:
2236769Sobrien# NAME:
3236769Sobrien#	install.sh - portable version of install(1)
4236769Sobrien#
5236769Sobrien# SYNOPSIS:
6236769Sobrien#	install [-CNcs] [-f flags] [-i errs] [-o owner] [-g group] [-m mode] file1 file2 ...
7236769Sobrien#	install -d  [-i errs] [-o owner] [-g group] [-m mode] directory ...
8236769Sobrien#
9236769Sobrien# DESCRIPTION:
10236769Sobrien#	Compatible with BSD install(1).  Except that '-c' is always
11236769Sobrien#	true and we always move an already installed target aside as
12236769Sobrien#	this is important on many systems.  Recent BSD install(1)
13236769Sobrien#	versions have a '-b' option for this.
14236769Sobrien#	
15236769Sobrien#
16236769Sobrien# OPTIONS:
17236769Sobrien#	-b	move previous target file aside (always true).
18236769Sobrien#
19236769Sobrien#	-B "suffix"
20236769Sobrien#		use "suffix" instead of .old for saving existing target.
21236769Sobrien#	
22236769Sobrien#	-c	copy rather than move the file into place (always true).
23236769Sobrien#
24236769Sobrien#	-C	compare.  Only install if target is missing or
25236769Sobrien#		different.
26236769Sobrien#
27236769Sobrien#	-N	newer. Only install if target is missing or older.
28236769Sobrien#	
29236769Sobrien#	-s	strip target
30236769Sobrien#
31236769Sobrien#	-o "owner"
32236769Sobrien#		make target owned by "owner"
33236769Sobrien#
34236769Sobrien#	-g "group"
35236769Sobrien#		make target group owned by "group"
36236769Sobrien#
37236769Sobrien#	-m "mode"
38236769Sobrien#		set permissions to "mode"
39236769Sobrien#
40236769Sobrien#	-f "flags"
41236769Sobrien#		Pass "flags" onto chflags(1)
42236769Sobrien#		
43236769Sobrien#	-i "errs"
44236769Sobrien#		Ignore errors from steps indicated by "errs" (``s,o,g,m'').
45236769Sobrien#
46236769Sobrien# BUGS:
47236769Sobrien#	The '-i' option is to save your sanity when 'bsd.prog.mk'
48236769Sobrien#	insists on haveing a '-o' "owner" option which is doomed to
49236769Sobrien#	fail on many systems.  We ignore '-b', '-B' and '-c' options.
50236769Sobrien#	
51236769Sobrien# AUTHOR:
52236769Sobrien#	Simon J. Gerraty <sjg@quick.com.au>
53236769Sobrien#
54236769Sobrien
55236769Sobrien# RCSid:
56236769Sobrien#	$Id: install-sh,v 1.18 2001/03/16 17:33:02 sjg Exp $
57236769Sobrien#
58236769Sobrien#	@(#) Copyright (c) 1993 Simon J. Gerraty
59236769Sobrien#
60236769Sobrien#	This file is provided in the hope that it will
61236769Sobrien#	be of use.  There is absolutely NO WARRANTY.
62236769Sobrien#	Permission to copy, redistribute or otherwise
63236769Sobrien#	use this file is hereby granted provided that 
64236769Sobrien#	the above copyright notice and this notice are
65236769Sobrien#	left intact. 
66236769Sobrien#      
67236769Sobrien#	Please send copies of changes and bug-fixes to:
68236769Sobrien#	sjg@quick.com.au
69236769Sobrien#
70236769Sobrien
71236769Sobrienset -- `getopt B:bpxCNcsdo:g:m:i:f: $*`
72236769Sobrien
73236769SobrienMydir=`dirname $0`
74236769Sobrien[ -s $Mydir/.installrc ] && . $Mydir/.installrc
75236769Sobrien
76236769Sobrienowner=:
77236769Sobriengroup=:
78236769Sobrienmode=:
79236769Sobrienstrip=:
80236769Sobrienmkdirs=
81236769Sobriencompare=:
82236769Sobriennewer=:
83236769Sobrienchflags=:
84236769SobrienLS1=
85236769SobrienCP_P=
86236769Sobrien
87236769Sobrienwhile [ $# -gt 1 ]
88236769Sobriendo
89236769Sobrien	case $1 in
90236769Sobrien	--)	shift; break;;
91236769Sobrien	-p)	CP_P=-p;;
92236769Sobrien	-x)	set -x;;
93236769Sobrien	-B)	OLD_EXT=$2; shift;;
94236769Sobrien	-C)	compare=Different;;
95236769Sobrien	-N)	newer=Newer;
96236769Sobrien		# check if /bin/ls supports -1
97236769Sobrien		/bin/ls -1 $0 >/dev/null 2>&1 && LS1=1
98236769Sobrien		;;
99236769Sobrien	-o)	owner="${CHOWN:-chown} $2 "; shift;;
100236769Sobrien	-g)	group="${CHGRP:-chgrp} $2 "; shift;;
101236769Sobrien	-m)	mode="${CHMOD:-chmod} $2 "; shift;;
102236769Sobrien	-s)	strip=${STRIP:-strip};;
103236769Sobrien	-d)	mkdirs="mkdir -p";;
104236769Sobrien	-i)	ignore_err="$ignore_err$2"; shift;;
105236769Sobrien	-f)	chflags="${CHFLAGS:-chflags} $2 "; shift;;
106236769Sobrien	esac
107236769Sobrien	shift
108236769Sobriendone
109236769Sobrien
110236769SobrienNewer() {
111236769Sobrien	n=`/bin/ls -t$LS1 $* 2>/dev/null | head -1`
112236769Sobrien	[ $1 = $n ]
113236769Sobrien}
114236769Sobrien
115236769SobrienDifferent() {
116236769Sobrien	cmp -s $*
117236769Sobrien	[ $? != 0 ]
118236769Sobrien}
119236769Sobrien
120236769SobrienErr() {
121236769Sobrien	case "$ignore_err" in
122236769Sobrien	*$1*)	;;
123236769Sobrien	*)	exit 1;;
124236769Sobrien	esac
125236769Sobrien}
126236769Sobrien
127236769SobrienSetem() {
128236769Sobrien	# the order is important
129236769Sobrien	if [ ! -d $1 ]; then
130236769Sobrien		$strip $1 || Err s
131236769Sobrien	fi
132236769Sobrien	$group $1 || Err g
133236769Sobrien	$owner $1 || Err o
134236769Sobrien	$mode  $1 || Err m
135236769Sobrien	$chflags  $1 || Err f
136236769Sobrien	return 0
137236769Sobrien}
138236769Sobrien
139236769Sobrien# a bug in HP-UX's /bin/sh, means we need to re-set $*
140236769Sobrien# after any calls to add_path()
141236769Sobrienargs="$*"
142236769Sobrien
143236769Sobrien# all this just for chown!
144236769Sobrienadd_path () { [ -d $1 ] && eval ${2:-PATH}="\$${2:-PATH}:$1"; }
145236769Sobrienadd_path /etc
146236769Sobrienadd_path /usr/etc
147236769Sobrienadd_path /sbin
148236769Sobrienadd_path /usr/sbin
149236769Sobrien
150236769Sobrien# restore saved $*
151236769Sobrienset -- $args
152236769Sobrien
153236769Sobrien# make directories if needed
154236769Sobrien# and ensure mode etc are as desired
155236769Sobrienif [ "$mkdirs" ]; then
156236769Sobrien	for d in $*
157236769Sobrien	do
158236769Sobrien		[ ! -d $d ] && $mkdirs $d
159236769Sobrien		Setem $d
160236769Sobrien	done
161236769Sobrien	exit 0			# that's all we do
162236769Sobrienfi
163236769Sobrien
164236769Sobrien# install files
165236769Sobrienif [ $# -gt 2 ]; then
166236769Sobrien	dest_dir=yes
167236769Sobrienelif [ $# -eq 1 ]; then
168236769Sobrien	echo "what should I do with $*?" >&2
169236769Sobrien	exit 1
170236769Sobrienfi
171236769Sobrien
172236769Sobrien# get list of files
173236769Sobrienwhile [ $# -gt 1 ]
174236769Sobriendo
175236769Sobrien	files="$files $1"
176236769Sobrien	shift
177236769Sobriendone
178236769Sobrien# last one is dest
179236769Sobriendest=$1
180236769Sobrienshift
181236769Sobrien
182236769Sobrien
183236769Sobrienif [ "$dest_dir" = yes -a  ! -d $dest ]; then
184236769Sobrien	echo "no directory $dest" >&2
185236769Sobrien	exit 1
186236769Sobrienfi
187236769Sobrien
188236769Sobrienfor f in $files
189236769Sobriendo
190236769Sobrien	b=`basename $f`
191236769Sobrien	if [ -d $dest ]; then
192236769Sobrien		t=$dest/$b
193236769Sobrien	else
194236769Sobrien		t=$dest
195236769Sobrien	fi
196236769Sobrien	$newer $f $t || continue
197236769Sobrien	$compare $f $t || continue
198236769Sobrien	[ -f $t ] && { mv -f $t $t.old || exit 1; }
199236769Sobrien	{ cp $CP_P $f $t && Setem $t; } || exit 1
200236769Sobriendone
201236769Sobrienexit 0
202