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