xref: /netbsd-src/external/gpl2/grep/dist/install-sh (revision a8fa202a6440953be7b92a8960a811bff58203f4)
1*a8fa202aSchristos#!/bin/sh
2*a8fa202aSchristos#
3*a8fa202aSchristos# install - install a program, script, or datafile
4*a8fa202aSchristos# This comes from X11R5 (mit/util/scripts/install.sh).
5*a8fa202aSchristos#
6*a8fa202aSchristos# Copyright 1991 by the Massachusetts Institute of Technology
7*a8fa202aSchristos#
8*a8fa202aSchristos# Permission to use, copy, modify, distribute, and sell this software and its
9*a8fa202aSchristos# documentation for any purpose is hereby granted without fee, provided that
10*a8fa202aSchristos# the above copyright notice appear in all copies and that both that
11*a8fa202aSchristos# copyright notice and this permission notice appear in supporting
12*a8fa202aSchristos# documentation, and that the name of M.I.T. not be used in advertising or
13*a8fa202aSchristos# publicity pertaining to distribution of the software without specific,
14*a8fa202aSchristos# written prior permission.  M.I.T. makes no representations about the
15*a8fa202aSchristos# suitability of this software for any purpose.  It is provided "as is"
16*a8fa202aSchristos# without express or implied warranty.
17*a8fa202aSchristos#
18*a8fa202aSchristos# Calling this script install-sh is preferred over install.sh, to prevent
19*a8fa202aSchristos# `make' implicit rules from creating a file called install from it
20*a8fa202aSchristos# when there is no Makefile.
21*a8fa202aSchristos#
22*a8fa202aSchristos# This script is compatible with the BSD install script, but was written
23*a8fa202aSchristos# from scratch.  It can only install one file at a time, a restriction
24*a8fa202aSchristos# shared with many OS's install programs.
25*a8fa202aSchristos
26*a8fa202aSchristos
27*a8fa202aSchristos# set DOITPROG to echo to test this script
28*a8fa202aSchristos
29*a8fa202aSchristos# Don't use :- since 4.3BSD and earlier shells don't like it.
30*a8fa202aSchristosdoit="${DOITPROG-}"
31*a8fa202aSchristos
32*a8fa202aSchristos
33*a8fa202aSchristos# put in absolute paths if you don't have them in your path; or use env. vars.
34*a8fa202aSchristos
35*a8fa202aSchristosmvprog="${MVPROG-mv}"
36*a8fa202aSchristoscpprog="${CPPROG-cp}"
37*a8fa202aSchristoschmodprog="${CHMODPROG-chmod}"
38*a8fa202aSchristoschownprog="${CHOWNPROG-chown}"
39*a8fa202aSchristoschgrpprog="${CHGRPPROG-chgrp}"
40*a8fa202aSchristosstripprog="${STRIPPROG-strip}"
41*a8fa202aSchristosrmprog="${RMPROG-rm}"
42*a8fa202aSchristosmkdirprog="${MKDIRPROG-mkdir}"
43*a8fa202aSchristos
44*a8fa202aSchristostransformbasename=""
45*a8fa202aSchristostransform_arg=""
46*a8fa202aSchristosinstcmd="$mvprog"
47*a8fa202aSchristoschmodcmd="$chmodprog 0755"
48*a8fa202aSchristoschowncmd=""
49*a8fa202aSchristoschgrpcmd=""
50*a8fa202aSchristosstripcmd=""
51*a8fa202aSchristosrmcmd="$rmprog -f"
52*a8fa202aSchristosmvcmd="$mvprog"
53*a8fa202aSchristossrc=""
54*a8fa202aSchristosdst=""
55*a8fa202aSchristosdir_arg=""
56*a8fa202aSchristos
57*a8fa202aSchristoswhile [ x"$1" != x ]; do
58*a8fa202aSchristos    case $1 in
59*a8fa202aSchristos	-c) instcmd="$cpprog"
60*a8fa202aSchristos	    shift
61*a8fa202aSchristos	    continue;;
62*a8fa202aSchristos
63*a8fa202aSchristos	-d) dir_arg=true
64*a8fa202aSchristos	    shift
65*a8fa202aSchristos	    continue;;
66*a8fa202aSchristos
67*a8fa202aSchristos	-m) chmodcmd="$chmodprog $2"
68*a8fa202aSchristos	    shift
69*a8fa202aSchristos	    shift
70*a8fa202aSchristos	    continue;;
71*a8fa202aSchristos
72*a8fa202aSchristos	-o) chowncmd="$chownprog $2"
73*a8fa202aSchristos	    shift
74*a8fa202aSchristos	    shift
75*a8fa202aSchristos	    continue;;
76*a8fa202aSchristos
77*a8fa202aSchristos	-g) chgrpcmd="$chgrpprog $2"
78*a8fa202aSchristos	    shift
79*a8fa202aSchristos	    shift
80*a8fa202aSchristos	    continue;;
81*a8fa202aSchristos
82*a8fa202aSchristos	-s) stripcmd="$stripprog"
83*a8fa202aSchristos	    shift
84*a8fa202aSchristos	    continue;;
85*a8fa202aSchristos
86*a8fa202aSchristos	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
87*a8fa202aSchristos	    shift
88*a8fa202aSchristos	    continue;;
89*a8fa202aSchristos
90*a8fa202aSchristos	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
91*a8fa202aSchristos	    shift
92*a8fa202aSchristos	    continue;;
93*a8fa202aSchristos
94*a8fa202aSchristos	*)  if [ x"$src" = x ]
95*a8fa202aSchristos	    then
96*a8fa202aSchristos		src=$1
97*a8fa202aSchristos	    else
98*a8fa202aSchristos		# this colon is to work around a 386BSD /bin/sh bug
99*a8fa202aSchristos		:
100*a8fa202aSchristos		dst=$1
101*a8fa202aSchristos	    fi
102*a8fa202aSchristos	    shift
103*a8fa202aSchristos	    continue;;
104*a8fa202aSchristos    esac
105*a8fa202aSchristosdone
106*a8fa202aSchristos
107*a8fa202aSchristosif [ x"$src" = x ]
108*a8fa202aSchristosthen
109*a8fa202aSchristos	echo "install:	no input file specified"
110*a8fa202aSchristos	exit 1
111*a8fa202aSchristoselse
112*a8fa202aSchristos	true
113*a8fa202aSchristosfi
114*a8fa202aSchristos
115*a8fa202aSchristosif [ x"$dir_arg" != x ]; then
116*a8fa202aSchristos	dst=$src
117*a8fa202aSchristos	src=""
118*a8fa202aSchristos
119*a8fa202aSchristos	if [ -d $dst ]; then
120*a8fa202aSchristos		instcmd=:
121*a8fa202aSchristos		chmodcmd=""
122*a8fa202aSchristos	else
123*a8fa202aSchristos		instcmd=mkdir
124*a8fa202aSchristos	fi
125*a8fa202aSchristoselse
126*a8fa202aSchristos
127*a8fa202aSchristos# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
128*a8fa202aSchristos# might cause directories to be created, which would be especially bad
129*a8fa202aSchristos# if $src (and thus $dsttmp) contains '*'.
130*a8fa202aSchristos
131*a8fa202aSchristos	if [ -f $src -o -d $src ]
132*a8fa202aSchristos	then
133*a8fa202aSchristos		true
134*a8fa202aSchristos	else
135*a8fa202aSchristos		echo "install:  $src does not exist"
136*a8fa202aSchristos		exit 1
137*a8fa202aSchristos	fi
138*a8fa202aSchristos
139*a8fa202aSchristos	if [ x"$dst" = x ]
140*a8fa202aSchristos	then
141*a8fa202aSchristos		echo "install:	no destination specified"
142*a8fa202aSchristos		exit 1
143*a8fa202aSchristos	else
144*a8fa202aSchristos		true
145*a8fa202aSchristos	fi
146*a8fa202aSchristos
147*a8fa202aSchristos# If destination is a directory, append the input filename; if your system
148*a8fa202aSchristos# does not like double slashes in filenames, you may need to add some logic
149*a8fa202aSchristos
150*a8fa202aSchristos	if [ -d $dst ]
151*a8fa202aSchristos	then
152*a8fa202aSchristos		dst="$dst"/`basename $src`
153*a8fa202aSchristos	else
154*a8fa202aSchristos		true
155*a8fa202aSchristos	fi
156*a8fa202aSchristosfi
157*a8fa202aSchristos
158*a8fa202aSchristos## this sed command emulates the dirname command
159*a8fa202aSchristosdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
160*a8fa202aSchristos
161*a8fa202aSchristos# Make sure that the destination directory exists.
162*a8fa202aSchristos#  this part is taken from Noah Friedman's mkinstalldirs script
163*a8fa202aSchristos
164*a8fa202aSchristos# Skip lots of stat calls in the usual case.
165*a8fa202aSchristosif [ ! -d "$dstdir" ]; then
166*a8fa202aSchristosdefaultIFS='
167*a8fa202aSchristos'
168*a8fa202aSchristosIFS="${IFS-${defaultIFS}}"
169*a8fa202aSchristos
170*a8fa202aSchristosoIFS="${IFS}"
171*a8fa202aSchristos# Some sh's can't handle IFS=/ for some reason.
172*a8fa202aSchristosIFS='%'
173*a8fa202aSchristosset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
174*a8fa202aSchristosIFS="${oIFS}"
175*a8fa202aSchristos
176*a8fa202aSchristospathcomp=''
177*a8fa202aSchristos
178*a8fa202aSchristoswhile [ $# -ne 0 ] ; do
179*a8fa202aSchristos	pathcomp="${pathcomp}${1}"
180*a8fa202aSchristos	shift
181*a8fa202aSchristos
182*a8fa202aSchristos	if [ ! -d "${pathcomp}" ] ;
183*a8fa202aSchristos        then
184*a8fa202aSchristos		$mkdirprog "${pathcomp}"
185*a8fa202aSchristos	else
186*a8fa202aSchristos		true
187*a8fa202aSchristos	fi
188*a8fa202aSchristos
189*a8fa202aSchristos	pathcomp="${pathcomp}/"
190*a8fa202aSchristosdone
191*a8fa202aSchristosfi
192*a8fa202aSchristos
193*a8fa202aSchristosif [ x"$dir_arg" != x ]
194*a8fa202aSchristosthen
195*a8fa202aSchristos	$doit $instcmd $dst &&
196*a8fa202aSchristos
197*a8fa202aSchristos	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
198*a8fa202aSchristos	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
199*a8fa202aSchristos	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
200*a8fa202aSchristos	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
201*a8fa202aSchristoselse
202*a8fa202aSchristos
203*a8fa202aSchristos# If we're going to rename the final executable, determine the name now.
204*a8fa202aSchristos
205*a8fa202aSchristos	if [ x"$transformarg" = x ]
206*a8fa202aSchristos	then
207*a8fa202aSchristos		dstfile=`basename $dst`
208*a8fa202aSchristos	else
209*a8fa202aSchristos		dstfile=`basename $dst $transformbasename |
210*a8fa202aSchristos			sed $transformarg`$transformbasename
211*a8fa202aSchristos	fi
212*a8fa202aSchristos
213*a8fa202aSchristos# don't allow the sed command to completely eliminate the filename
214*a8fa202aSchristos
215*a8fa202aSchristos	if [ x"$dstfile" = x ]
216*a8fa202aSchristos	then
217*a8fa202aSchristos		dstfile=`basename $dst`
218*a8fa202aSchristos	else
219*a8fa202aSchristos		true
220*a8fa202aSchristos	fi
221*a8fa202aSchristos
222*a8fa202aSchristos# Make a temp file name in the proper directory.
223*a8fa202aSchristos
224*a8fa202aSchristos	dsttmp=$dstdir/#inst.$$#
225*a8fa202aSchristos
226*a8fa202aSchristos# Move or copy the file name to the temp name
227*a8fa202aSchristos
228*a8fa202aSchristos	$doit $instcmd $src $dsttmp &&
229*a8fa202aSchristos
230*a8fa202aSchristos	trap "rm -f ${dsttmp}" 0 &&
231*a8fa202aSchristos
232*a8fa202aSchristos# and set any options; do chmod last to preserve setuid bits
233*a8fa202aSchristos
234*a8fa202aSchristos# If any of these fail, we abort the whole thing.  If we want to
235*a8fa202aSchristos# ignore errors from any of these, just make sure not to ignore
236*a8fa202aSchristos# errors from the above "$doit $instcmd $src $dsttmp" command.
237*a8fa202aSchristos
238*a8fa202aSchristos	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
239*a8fa202aSchristos	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
240*a8fa202aSchristos	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
241*a8fa202aSchristos	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
242*a8fa202aSchristos
243*a8fa202aSchristos# Now rename the file to the real destination.
244*a8fa202aSchristos
245*a8fa202aSchristos	$doit $rmcmd -f $dstdir/$dstfile &&
246*a8fa202aSchristos	$doit $mvcmd $dsttmp $dstdir/$dstfile
247*a8fa202aSchristos
248*a8fa202aSchristosfi &&
249*a8fa202aSchristos
250*a8fa202aSchristos
251*a8fa202aSchristosexit 0
252