xref: /netbsd-src/external/gpl2/groff/dist/install-sh (revision 89a07cf815a29524268025a1139fac4c5190f765)
1*89a07cf8Schristos#!/bin/sh
2*89a07cf8Schristos# install - install a program, script, or datafile
3*89a07cf8Schristos
4*89a07cf8Schristosscriptversion=2005-07-09.12
5*89a07cf8Schristos
6*89a07cf8Schristos# This originates from X11R5 (mit/util/scripts/install.sh), which was
7*89a07cf8Schristos# later released in X11R6 (xc/config/util/install.sh) with the
8*89a07cf8Schristos# following copyright and license.
9*89a07cf8Schristos#
10*89a07cf8Schristos# Copyright (C) 1994 X Consortium
11*89a07cf8Schristos#
12*89a07cf8Schristos# Permission is hereby granted, free of charge, to any person obtaining a copy
13*89a07cf8Schristos# of this software and associated documentation files (the "Software"), to
14*89a07cf8Schristos# deal in the Software without restriction, including without limitation the
15*89a07cf8Schristos# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16*89a07cf8Schristos# sell copies of the Software, and to permit persons to whom the Software is
17*89a07cf8Schristos# furnished to do so, subject to the following conditions:
18*89a07cf8Schristos#
19*89a07cf8Schristos# The above copyright notice and this permission notice shall be included in
20*89a07cf8Schristos# all copies or substantial portions of the Software.
21*89a07cf8Schristos#
22*89a07cf8Schristos# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*89a07cf8Schristos# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*89a07cf8Schristos# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25*89a07cf8Schristos# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26*89a07cf8Schristos# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27*89a07cf8Schristos# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*89a07cf8Schristos#
29*89a07cf8Schristos# Except as contained in this notice, the name of the X Consortium shall not
30*89a07cf8Schristos# be used in advertising or otherwise to promote the sale, use or other deal-
31*89a07cf8Schristos# ings in this Software without prior written authorization from the X Consor-
32*89a07cf8Schristos# tium.
33*89a07cf8Schristos#
34*89a07cf8Schristos#
35*89a07cf8Schristos# FSF changes to this file are in the public domain.
36*89a07cf8Schristos#
37*89a07cf8Schristos# Calling this script install-sh is preferred over install.sh, to prevent
38*89a07cf8Schristos# `make' implicit rules from creating a file called install from it
39*89a07cf8Schristos# when there is no Makefile.
40*89a07cf8Schristos#
41*89a07cf8Schristos# This script is compatible with the BSD install script, but was written
42*89a07cf8Schristos# from scratch.  It can only install one file at a time, a restriction
43*89a07cf8Schristos# shared with many OS's install programs.
44*89a07cf8Schristos
45*89a07cf8Schristos# set DOITPROG to echo to test this script
46*89a07cf8Schristos
47*89a07cf8Schristos# Don't use :- since 4.3BSD and earlier shells don't like it.
48*89a07cf8Schristosdoit="${DOITPROG-}"
49*89a07cf8Schristos
50*89a07cf8Schristos# put in absolute paths if you don't have them in your path; or use env. vars.
51*89a07cf8Schristos
52*89a07cf8Schristosmvprog="${MVPROG-mv}"
53*89a07cf8Schristoscpprog="${CPPROG-cp}"
54*89a07cf8Schristoschmodprog="${CHMODPROG-chmod}"
55*89a07cf8Schristoschownprog="${CHOWNPROG-chown}"
56*89a07cf8Schristoschgrpprog="${CHGRPPROG-chgrp}"
57*89a07cf8Schristosstripprog="${STRIPPROG-strip}"
58*89a07cf8Schristosrmprog="${RMPROG-rm}"
59*89a07cf8Schristosmkdirprog="${MKDIRPROG-mkdir}"
60*89a07cf8Schristos
61*89a07cf8Schristoschmodcmd="$chmodprog 0755"
62*89a07cf8Schristoschowncmd=
63*89a07cf8Schristoschgrpcmd=
64*89a07cf8Schristosstripcmd=
65*89a07cf8Schristosrmcmd="$rmprog -f"
66*89a07cf8Schristosmvcmd="$mvprog"
67*89a07cf8Schristossrc=
68*89a07cf8Schristosdst=
69*89a07cf8Schristosdir_arg=
70*89a07cf8Schristosdstarg=
71*89a07cf8Schristosno_target_directory=
72*89a07cf8Schristos
73*89a07cf8Schristosusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
74*89a07cf8Schristos   or: $0 [OPTION]... SRCFILES... DIRECTORY
75*89a07cf8Schristos   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
76*89a07cf8Schristos   or: $0 [OPTION]... -d DIRECTORIES...
77*89a07cf8Schristos
78*89a07cf8SchristosIn the 1st form, copy SRCFILE to DSTFILE.
79*89a07cf8SchristosIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
80*89a07cf8SchristosIn the 4th, create DIRECTORIES.
81*89a07cf8Schristos
82*89a07cf8SchristosOptions:
83*89a07cf8Schristos-c         (ignored)
84*89a07cf8Schristos-d         create directories instead of installing files.
85*89a07cf8Schristos-g GROUP   $chgrpprog installed files to GROUP.
86*89a07cf8Schristos-m MODE    $chmodprog installed files to MODE.
87*89a07cf8Schristos-o USER    $chownprog installed files to USER.
88*89a07cf8Schristos-s         $stripprog installed files.
89*89a07cf8Schristos-t DIRECTORY  install into DIRECTORY.
90*89a07cf8Schristos-T         report an error if DSTFILE is a directory.
91*89a07cf8Schristos--help     display this help and exit.
92*89a07cf8Schristos--version  display version info and exit.
93*89a07cf8Schristos
94*89a07cf8SchristosEnvironment variables override the default commands:
95*89a07cf8Schristos  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
96*89a07cf8Schristos"
97*89a07cf8Schristos
98*89a07cf8Schristoswhile test -n "$1"; do
99*89a07cf8Schristos  case $1 in
100*89a07cf8Schristos    -c) shift
101*89a07cf8Schristos        continue;;
102*89a07cf8Schristos
103*89a07cf8Schristos    -d) dir_arg=true
104*89a07cf8Schristos        shift
105*89a07cf8Schristos        continue;;
106*89a07cf8Schristos
107*89a07cf8Schristos    -g) chgrpcmd="$chgrpprog $2"
108*89a07cf8Schristos        shift
109*89a07cf8Schristos        shift
110*89a07cf8Schristos        continue;;
111*89a07cf8Schristos
112*89a07cf8Schristos    --help) echo "$usage"; exit $?;;
113*89a07cf8Schristos
114*89a07cf8Schristos    -m) chmodcmd="$chmodprog $2"
115*89a07cf8Schristos        shift
116*89a07cf8Schristos        shift
117*89a07cf8Schristos        continue;;
118*89a07cf8Schristos
119*89a07cf8Schristos    -o) chowncmd="$chownprog $2"
120*89a07cf8Schristos        shift
121*89a07cf8Schristos        shift
122*89a07cf8Schristos        continue;;
123*89a07cf8Schristos
124*89a07cf8Schristos    -s) stripcmd=$stripprog
125*89a07cf8Schristos        shift
126*89a07cf8Schristos        continue;;
127*89a07cf8Schristos
128*89a07cf8Schristos    -t) dstarg=$2
129*89a07cf8Schristos	shift
130*89a07cf8Schristos	shift
131*89a07cf8Schristos	continue;;
132*89a07cf8Schristos
133*89a07cf8Schristos    -T) no_target_directory=true
134*89a07cf8Schristos	shift
135*89a07cf8Schristos	continue;;
136*89a07cf8Schristos
137*89a07cf8Schristos    --version) echo "$0 $scriptversion"; exit $?;;
138*89a07cf8Schristos
139*89a07cf8Schristos    *)  # When -d is used, all remaining arguments are directories to create.
140*89a07cf8Schristos	# When -t is used, the destination is already specified.
141*89a07cf8Schristos	test -n "$dir_arg$dstarg" && break
142*89a07cf8Schristos        # Otherwise, the last argument is the destination.  Remove it from $@.
143*89a07cf8Schristos	for arg
144*89a07cf8Schristos	do
145*89a07cf8Schristos          if test -n "$dstarg"; then
146*89a07cf8Schristos	    # $@ is not empty: it contains at least $arg.
147*89a07cf8Schristos	    set fnord "$@" "$dstarg"
148*89a07cf8Schristos	    shift # fnord
149*89a07cf8Schristos	  fi
150*89a07cf8Schristos	  shift # arg
151*89a07cf8Schristos	  dstarg=$arg
152*89a07cf8Schristos	done
153*89a07cf8Schristos	break;;
154*89a07cf8Schristos  esac
155*89a07cf8Schristosdone
156*89a07cf8Schristos
157*89a07cf8Schristosif test -z "$1"; then
158*89a07cf8Schristos  if test -z "$dir_arg"; then
159*89a07cf8Schristos    echo "$0: no input file specified." >&2
160*89a07cf8Schristos    exit 1
161*89a07cf8Schristos  fi
162*89a07cf8Schristos  # It's OK to call `install-sh -d' without argument.
163*89a07cf8Schristos  # This can happen when creating conditional directories.
164*89a07cf8Schristos  exit 0
165*89a07cf8Schristosfi
166*89a07cf8Schristos
167*89a07cf8Schristosfor src
168*89a07cf8Schristosdo
169*89a07cf8Schristos  # Protect names starting with `-'.
170*89a07cf8Schristos  case $src in
171*89a07cf8Schristos    -*) src=./$src ;;
172*89a07cf8Schristos  esac
173*89a07cf8Schristos
174*89a07cf8Schristos  if test -n "$dir_arg"; then
175*89a07cf8Schristos    dst=$src
176*89a07cf8Schristos    src=
177*89a07cf8Schristos
178*89a07cf8Schristos    if test -d "$dst"; then
179*89a07cf8Schristos      mkdircmd=:
180*89a07cf8Schristos      chmodcmd=
181*89a07cf8Schristos    else
182*89a07cf8Schristos      mkdircmd=$mkdirprog
183*89a07cf8Schristos    fi
184*89a07cf8Schristos  else
185*89a07cf8Schristos    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
186*89a07cf8Schristos    # might cause directories to be created, which would be especially bad
187*89a07cf8Schristos    # if $src (and thus $dsttmp) contains '*'.
188*89a07cf8Schristos    if test ! -f "$src" && test ! -d "$src"; then
189*89a07cf8Schristos      echo "$0: $src does not exist." >&2
190*89a07cf8Schristos      exit 1
191*89a07cf8Schristos    fi
192*89a07cf8Schristos
193*89a07cf8Schristos    if test -z "$dstarg"; then
194*89a07cf8Schristos      echo "$0: no destination specified." >&2
195*89a07cf8Schristos      exit 1
196*89a07cf8Schristos    fi
197*89a07cf8Schristos
198*89a07cf8Schristos    dst=$dstarg
199*89a07cf8Schristos    # Protect names starting with `-'.
200*89a07cf8Schristos    case $dst in
201*89a07cf8Schristos      -*) dst=./$dst ;;
202*89a07cf8Schristos    esac
203*89a07cf8Schristos
204*89a07cf8Schristos    # If destination is a directory, append the input filename; won't work
205*89a07cf8Schristos    # if double slashes aren't ignored.
206*89a07cf8Schristos    if test -d "$dst"; then
207*89a07cf8Schristos      if test -n "$no_target_directory"; then
208*89a07cf8Schristos	echo "$0: $dstarg: Is a directory" >&2
209*89a07cf8Schristos	exit 1
210*89a07cf8Schristos      fi
211*89a07cf8Schristos      dst=$dst/`basename "$src"`
212*89a07cf8Schristos    fi
213*89a07cf8Schristos  fi
214*89a07cf8Schristos
215*89a07cf8Schristos  # This sed command emulates the dirname command.
216*89a07cf8Schristos  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
217*89a07cf8Schristos
218*89a07cf8Schristos  # Make sure that the destination directory exists.
219*89a07cf8Schristos
220*89a07cf8Schristos  # Skip lots of stat calls in the usual case.
221*89a07cf8Schristos  if test ! -d "$dstdir"; then
222*89a07cf8Schristos    case $dstdir in
223*89a07cf8Schristos      /*) pathcomp=/ ;;
224*89a07cf8Schristos      -*) pathcomp=./ ;;
225*89a07cf8Schristos      *)  pathcomp= ;;
226*89a07cf8Schristos    esac
227*89a07cf8Schristos    oIFS=$IFS
228*89a07cf8Schristos    IFS=/
229*89a07cf8Schristos    set fnord $dstdir
230*89a07cf8Schristos    shift
231*89a07cf8Schristos    IFS=$oIFS
232*89a07cf8Schristos
233*89a07cf8Schristos    for d
234*89a07cf8Schristos    do
235*89a07cf8Schristos      test "x$d" = x && continue
236*89a07cf8Schristos
237*89a07cf8Schristos      pathcomp=$pathcomp$d
238*89a07cf8Schristos      if test ! -d "$pathcomp"; then
239*89a07cf8Schristos        $mkdirprog "$pathcomp"
240*89a07cf8Schristos	# mkdir can fail with a `File exist' error in case several
241*89a07cf8Schristos	# install-sh are creating the directory concurrently.  This
242*89a07cf8Schristos	# is OK.
243*89a07cf8Schristos	test -d "$pathcomp" || exit 1
244*89a07cf8Schristos      fi
245*89a07cf8Schristos      pathcomp=$pathcomp/
246*89a07cf8Schristos    done
247*89a07cf8Schristos  fi
248*89a07cf8Schristos
249*89a07cf8Schristos  if test -n "$dir_arg"; then
250*89a07cf8Schristos    $doit $mkdircmd "$dst" \
251*89a07cf8Schristos      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
252*89a07cf8Schristos      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
253*89a07cf8Schristos      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
254*89a07cf8Schristos      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
255*89a07cf8Schristos
256*89a07cf8Schristos  else
257*89a07cf8Schristos    dstfile=`basename "$dst"`
258*89a07cf8Schristos
259*89a07cf8Schristos    # Make a couple of temp file names in the proper directory.
260*89a07cf8Schristos    dsttmp=$dstdir/_inst.$$_
261*89a07cf8Schristos    rmtmp=$dstdir/_rm.$$_
262*89a07cf8Schristos
263*89a07cf8Schristos    # Trap to clean up those temp files at exit.
264*89a07cf8Schristos    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
265*89a07cf8Schristos    trap '(exit $?); exit' 1 2 13 15
266*89a07cf8Schristos
267*89a07cf8Schristos    # Copy the file name to the temp name.
268*89a07cf8Schristos    $doit $cpprog "$src" "$dsttmp" &&
269*89a07cf8Schristos
270*89a07cf8Schristos    # and set any options; do chmod last to preserve setuid bits.
271*89a07cf8Schristos    #
272*89a07cf8Schristos    # If any of these fail, we abort the whole thing.  If we want to
273*89a07cf8Schristos    # ignore errors from any of these, just make sure not to ignore
274*89a07cf8Schristos    # errors from the above "$doit $cpprog $src $dsttmp" command.
275*89a07cf8Schristos    #
276*89a07cf8Schristos    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
277*89a07cf8Schristos      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
278*89a07cf8Schristos      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
279*89a07cf8Schristos      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
280*89a07cf8Schristos
281*89a07cf8Schristos    # Now rename the file to the real destination.
282*89a07cf8Schristos    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
283*89a07cf8Schristos      || {
284*89a07cf8Schristos	   # The rename failed, perhaps because mv can't rename something else
285*89a07cf8Schristos	   # to itself, or perhaps because mv is so ancient that it does not
286*89a07cf8Schristos	   # support -f.
287*89a07cf8Schristos
288*89a07cf8Schristos	   # Now remove or move aside any old file at destination location.
289*89a07cf8Schristos	   # We try this two ways since rm can't unlink itself on some
290*89a07cf8Schristos	   # systems and the destination file might be busy for other
291*89a07cf8Schristos	   # reasons.  In this case, the final cleanup might fail but the new
292*89a07cf8Schristos	   # file should still install successfully.
293*89a07cf8Schristos	   {
294*89a07cf8Schristos	     if test -f "$dstdir/$dstfile"; then
295*89a07cf8Schristos	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
296*89a07cf8Schristos	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
297*89a07cf8Schristos	       || {
298*89a07cf8Schristos		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
299*89a07cf8Schristos		 (exit 1); exit 1
300*89a07cf8Schristos	       }
301*89a07cf8Schristos	     else
302*89a07cf8Schristos	       :
303*89a07cf8Schristos	     fi
304*89a07cf8Schristos	   } &&
305*89a07cf8Schristos
306*89a07cf8Schristos	   # Now rename the file to the real destination.
307*89a07cf8Schristos	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
308*89a07cf8Schristos	 }
309*89a07cf8Schristos    }
310*89a07cf8Schristos  fi || { (exit 1); exit 1; }
311*89a07cf8Schristosdone
312*89a07cf8Schristos
313*89a07cf8Schristos# The final little trick to "correctly" pass the exit status to the exit trap.
314*89a07cf8Schristos{
315*89a07cf8Schristos  (exit 0); exit 0
316*89a07cf8Schristos}
317*89a07cf8Schristos
318*89a07cf8Schristos# Local variables:
319*89a07cf8Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
320*89a07cf8Schristos# time-stamp-start: "scriptversion="
321*89a07cf8Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
322*89a07cf8Schristos# time-stamp-end: "$"
323*89a07cf8Schristos# End:
324