xref: /netbsd-src/external/gpl2/xcvs/dist/build-aux/install-sh (revision 75e57c5640ae4c43e46f3b9f431ed4fbffac2b5c)
1a7c91847Schristos#!/bin/sh
2a7c91847Schristos# install - install a program, script, or datafile
3*75e57c56Schristos
4*75e57c56Schristosscriptversion=2009-04-28.21; # UTC
5*75e57c56Schristos
6*75e57c56Schristos# This originates from X11R5 (mit/util/scripts/install.sh), which was
7*75e57c56Schristos# later released in X11R6 (xc/config/util/install.sh) with the
8*75e57c56Schristos# following copyright and license.
9a7c91847Schristos#
10*75e57c56Schristos# Copyright (C) 1994 X Consortium
11a7c91847Schristos#
12*75e57c56Schristos# Permission is hereby granted, free of charge, to any person obtaining a copy
13*75e57c56Schristos# of this software and associated documentation files (the "Software"), to
14*75e57c56Schristos# deal in the Software without restriction, including without limitation the
15*75e57c56Schristos# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16*75e57c56Schristos# sell copies of the Software, and to permit persons to whom the Software is
17*75e57c56Schristos# furnished to do so, subject to the following conditions:
18*75e57c56Schristos#
19*75e57c56Schristos# The above copyright notice and this permission notice shall be included in
20*75e57c56Schristos# all copies or substantial portions of the Software.
21*75e57c56Schristos#
22*75e57c56Schristos# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*75e57c56Schristos# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*75e57c56Schristos# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25*75e57c56Schristos# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26*75e57c56Schristos# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27*75e57c56Schristos# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*75e57c56Schristos#
29*75e57c56Schristos# Except as contained in this notice, the name of the X Consortium shall not
30*75e57c56Schristos# be used in advertising or otherwise to promote the sale, use or other deal-
31*75e57c56Schristos# ings in this Software without prior written authorization from the X Consor-
32*75e57c56Schristos# tium.
33*75e57c56Schristos#
34*75e57c56Schristos#
35*75e57c56Schristos# FSF changes to this file are in the public domain.
36a7c91847Schristos#
37a7c91847Schristos# Calling this script install-sh is preferred over install.sh, to prevent
38a7c91847Schristos# `make' implicit rules from creating a file called install from it
39a7c91847Schristos# when there is no Makefile.
40a7c91847Schristos#
41a7c91847Schristos# This script is compatible with the BSD install script, but was written
42889c434eSchristos# from scratch.
43a7c91847Schristos
44*75e57c56Schristosnl='
45*75e57c56Schristos'
46*75e57c56SchristosIFS=" ""	$nl"
47*75e57c56Schristos
48a7c91847Schristos# set DOITPROG to echo to test this script
49a7c91847Schristos
50a7c91847Schristos# Don't use :- since 4.3BSD and earlier shells don't like it.
51*75e57c56Schristosdoit=${DOITPROG-}
52*75e57c56Schristosif test -z "$doit"; then
53*75e57c56Schristos  doit_exec=exec
54*75e57c56Schristoselse
55*75e57c56Schristos  doit_exec=$doit
56*75e57c56Schristosfi
57a7c91847Schristos
58*75e57c56Schristos# Put in absolute file names if you don't have them in your path;
59*75e57c56Schristos# or use environment vars.
60889c434eSchristos
61*75e57c56Schristoschgrpprog=${CHGRPPROG-chgrp}
62*75e57c56Schristoschmodprog=${CHMODPROG-chmod}
63*75e57c56Schristoschownprog=${CHOWNPROG-chown}
64*75e57c56Schristoscmpprog=${CMPPROG-cmp}
65*75e57c56Schristoscpprog=${CPPROG-cp}
66*75e57c56Schristosmkdirprog=${MKDIRPROG-mkdir}
67*75e57c56Schristosmvprog=${MVPROG-mv}
68*75e57c56Schristosrmprog=${RMPROG-rm}
69*75e57c56Schristosstripprog=${STRIPPROG-strip}
70a7c91847Schristos
71*75e57c56Schristosposix_glob='?'
72*75e57c56Schristosinitialize_posix_glob='
73*75e57c56Schristos  test "$posix_glob" != "?" || {
74*75e57c56Schristos    if (set -f) 2>/dev/null; then
75*75e57c56Schristos      posix_glob=
76*75e57c56Schristos    else
77*75e57c56Schristos      posix_glob=:
78*75e57c56Schristos    fi
79*75e57c56Schristos  }
80*75e57c56Schristos'
81a7c91847Schristos
82*75e57c56Schristosposix_mkdir=
83*75e57c56Schristos
84*75e57c56Schristos# Desired mode of installed file.
85*75e57c56Schristosmode=0755
86*75e57c56Schristos
87*75e57c56Schristoschgrpcmd=
88*75e57c56Schristoschmodcmd=$chmodprog
89*75e57c56Schristoschowncmd=
90*75e57c56Schristosmvcmd=$mvprog
91a7c91847Schristosrmcmd="$rmprog -f"
92*75e57c56Schristosstripcmd=
93a7c91847Schristos
94*75e57c56Schristossrc=
95*75e57c56Schristosdst=
96*75e57c56Schristosdir_arg=
97*75e57c56Schristosdst_arg=
98*75e57c56Schristos
99*75e57c56Schristoscopy_on_change=false
100*75e57c56Schristosno_target_directory=
101*75e57c56Schristos
102*75e57c56Schristosusage="\
103*75e57c56SchristosUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104*75e57c56Schristos   or: $0 [OPTION]... SRCFILES... DIRECTORY
105*75e57c56Schristos   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106*75e57c56Schristos   or: $0 [OPTION]... -d DIRECTORIES...
107*75e57c56Schristos
108*75e57c56SchristosIn the 1st form, copy SRCFILE to DSTFILE.
109*75e57c56SchristosIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110*75e57c56SchristosIn the 4th, create DIRECTORIES.
111*75e57c56Schristos
112*75e57c56SchristosOptions:
113*75e57c56Schristos     --help     display this help and exit.
114*75e57c56Schristos     --version  display version info and exit.
115*75e57c56Schristos
116*75e57c56Schristos  -c            (ignored)
117*75e57c56Schristos  -C            install only if different (preserve the last data modification time)
118*75e57c56Schristos  -d            create directories instead of installing files.
119*75e57c56Schristos  -g GROUP      $chgrpprog installed files to GROUP.
120*75e57c56Schristos  -m MODE       $chmodprog installed files to MODE.
121*75e57c56Schristos  -o USER       $chownprog installed files to USER.
122*75e57c56Schristos  -s            $stripprog installed files.
123*75e57c56Schristos  -t DIRECTORY  install into DIRECTORY.
124*75e57c56Schristos  -T            report an error if DSTFILE is a directory.
125*75e57c56Schristos
126*75e57c56SchristosEnvironment variables override the default commands:
127*75e57c56Schristos  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128*75e57c56Schristos  RMPROG STRIPPROG
129*75e57c56Schristos"
130*75e57c56Schristos
131*75e57c56Schristoswhile test $# -ne 0; do
132a7c91847Schristos  case $1 in
133*75e57c56Schristos    -c) ;;
134889c434eSchristos
135*75e57c56Schristos    -C) copy_on_change=true;;
136889c434eSchristos
137*75e57c56Schristos    -d) dir_arg=true;;
138a7c91847Schristos
139889c434eSchristos    -g) chgrpcmd="$chgrpprog $2"
140*75e57c56Schristos	shift;;
141a7c91847Schristos
142*75e57c56Schristos    --help) echo "$usage"; exit $?;;
143a7c91847Schristos
144*75e57c56Schristos    -m) mode=$2
145*75e57c56Schristos	case $mode in
146*75e57c56Schristos	  *' '* | *'	'* | *'
147*75e57c56Schristos'*	  | *'*'* | *'?'* | *'['*)
148*75e57c56Schristos	    echo "$0: invalid mode: $mode" >&2
149*75e57c56Schristos	    exit 1;;
150a7c91847Schristos	esac
151*75e57c56Schristos	shift;;
152*75e57c56Schristos
153*75e57c56Schristos    -o) chowncmd="$chownprog $2"
154*75e57c56Schristos	shift;;
155*75e57c56Schristos
156*75e57c56Schristos    -s) stripcmd=$stripprog;;
157*75e57c56Schristos
158*75e57c56Schristos    -t) dst_arg=$2
159*75e57c56Schristos	shift;;
160*75e57c56Schristos
161*75e57c56Schristos    -T) no_target_directory=true;;
162*75e57c56Schristos
163*75e57c56Schristos    --version) echo "$0 $scriptversion"; exit $?;;
164*75e57c56Schristos
165*75e57c56Schristos    --)	shift
166*75e57c56Schristos	break;;
167*75e57c56Schristos
168*75e57c56Schristos    -*)	echo "$0: invalid option: $1" >&2
169*75e57c56Schristos	exit 1;;
170*75e57c56Schristos
171*75e57c56Schristos    *)  break;;
172*75e57c56Schristos  esac
173*75e57c56Schristos  shift
174a7c91847Schristosdone
175a7c91847Schristos
176*75e57c56Schristosif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177*75e57c56Schristos  # When -d is used, all remaining arguments are directories to create.
178*75e57c56Schristos  # When -t is used, the destination is already specified.
179*75e57c56Schristos  # Otherwise, the last argument is the destination.  Remove it from $@.
180*75e57c56Schristos  for arg
181*75e57c56Schristos  do
182*75e57c56Schristos    if test -n "$dst_arg"; then
183*75e57c56Schristos      # $@ is not empty: it contains at least $arg.
184*75e57c56Schristos      set fnord "$@" "$dst_arg"
185*75e57c56Schristos      shift # fnord
186*75e57c56Schristos    fi
187*75e57c56Schristos    shift # arg
188*75e57c56Schristos    dst_arg=$arg
189*75e57c56Schristos  done
190*75e57c56Schristosfi
191*75e57c56Schristos
192*75e57c56Schristosif test $# -eq 0; then
193*75e57c56Schristos  if test -z "$dir_arg"; then
194*75e57c56Schristos    echo "$0: no input file specified." >&2
195a7c91847Schristos    exit 1
196a7c91847Schristos  fi
197*75e57c56Schristos  # It's OK to call `install-sh -d' without argument.
198*75e57c56Schristos  # This can happen when creating conditional directories.
199*75e57c56Schristos  exit 0
200a7c91847Schristosfi
201*75e57c56Schristos
202*75e57c56Schristosif test -z "$dir_arg"; then
203*75e57c56Schristos  trap '(exit $?); exit' 1 2 13 15
204*75e57c56Schristos
205*75e57c56Schristos  # Set umask so as not to create temps with too-generous modes.
206*75e57c56Schristos  # However, 'strip' requires both read and write access to temps.
207*75e57c56Schristos  case $mode in
208*75e57c56Schristos    # Optimize common cases.
209*75e57c56Schristos    *644) cp_umask=133;;
210*75e57c56Schristos    *755) cp_umask=22;;
211*75e57c56Schristos
212*75e57c56Schristos    *[0-7])
213*75e57c56Schristos      if test -z "$stripcmd"; then
214*75e57c56Schristos	u_plus_rw=
215a7c91847Schristos      else
216*75e57c56Schristos	u_plus_rw='% 200'
217889c434eSchristos      fi
218*75e57c56Schristos      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219*75e57c56Schristos    *)
220*75e57c56Schristos      if test -z "$stripcmd"; then
221*75e57c56Schristos	u_plus_rw=
222889c434eSchristos      else
223*75e57c56Schristos	u_plus_rw=,u+rw
224*75e57c56Schristos      fi
225*75e57c56Schristos      cp_umask=$mode$u_plus_rw;;
226*75e57c56Schristos  esac
227*75e57c56Schristosfi
228889c434eSchristos
229*75e57c56Schristosfor src
230*75e57c56Schristosdo
231*75e57c56Schristos  # Protect names starting with `-'.
232*75e57c56Schristos  case $src in
233*75e57c56Schristos    -*) src=./$src;;
234*75e57c56Schristos  esac
235*75e57c56Schristos
236*75e57c56Schristos  if test -n "$dir_arg"; then
237*75e57c56Schristos    dst=$src
238*75e57c56Schristos    dstdir=$dst
239*75e57c56Schristos    test -d "$dstdir"
240*75e57c56Schristos    dstdir_status=$?
241*75e57c56Schristos  else
242*75e57c56Schristos
243*75e57c56Schristos    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244a7c91847Schristos    # might cause directories to be created, which would be especially bad
245a7c91847Schristos    # if $src (and thus $dsttmp) contains '*'.
246*75e57c56Schristos    if test ! -f "$src" && test ! -d "$src"; then
247*75e57c56Schristos      echo "$0: $src does not exist." >&2
248a7c91847Schristos      exit 1
249a7c91847Schristos    fi
250a7c91847Schristos
251*75e57c56Schristos    if test -z "$dst_arg"; then
252*75e57c56Schristos      echo "$0: no destination specified." >&2
253*75e57c56Schristos      exit 1
254a7c91847Schristos    fi
255a7c91847Schristos
256*75e57c56Schristos    dst=$dst_arg
257*75e57c56Schristos    # Protect names starting with `-'.
258*75e57c56Schristos    case $dst in
259*75e57c56Schristos      -*) dst=./$dst;;
260889c434eSchristos    esac
261889c434eSchristos
262*75e57c56Schristos    # If destination is a directory, append the input filename; won't work
263*75e57c56Schristos    # if double slashes aren't ignored.
264*75e57c56Schristos    if test -d "$dst"; then
265*75e57c56Schristos      if test -n "$no_target_directory"; then
266*75e57c56Schristos	echo "$0: $dst_arg: Is a directory" >&2
267*75e57c56Schristos	exit 1
268*75e57c56Schristos      fi
269*75e57c56Schristos      dstdir=$dst
270*75e57c56Schristos      dst=$dstdir/`basename "$src"`
271*75e57c56Schristos      dstdir_status=0
272*75e57c56Schristos    else
273*75e57c56Schristos      # Prefer dirname, but fall back on a substitute if dirname fails.
274*75e57c56Schristos      dstdir=`
275*75e57c56Schristos	(dirname "$dst") 2>/dev/null ||
276*75e57c56Schristos	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277*75e57c56Schristos	     X"$dst" : 'X\(//\)[^/]' \| \
278*75e57c56Schristos	     X"$dst" : 'X\(//\)$' \| \
279*75e57c56Schristos	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280*75e57c56Schristos	echo X"$dst" |
281*75e57c56Schristos	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282*75e57c56Schristos		   s//\1/
283*75e57c56Schristos		   q
284*75e57c56Schristos		 }
285*75e57c56Schristos		 /^X\(\/\/\)[^/].*/{
286*75e57c56Schristos		   s//\1/
287*75e57c56Schristos		   q
288*75e57c56Schristos		 }
289*75e57c56Schristos		 /^X\(\/\/\)$/{
290*75e57c56Schristos		   s//\1/
291*75e57c56Schristos		   q
292*75e57c56Schristos		 }
293*75e57c56Schristos		 /^X\(\/\).*/{
294*75e57c56Schristos		   s//\1/
295*75e57c56Schristos		   q
296*75e57c56Schristos		 }
297*75e57c56Schristos		 s/.*/./; q'
298*75e57c56Schristos      `
299889c434eSchristos
300*75e57c56Schristos      test -d "$dstdir"
301*75e57c56Schristos      dstdir_status=$?
302*75e57c56Schristos    fi
303*75e57c56Schristos  fi
304889c434eSchristos
305*75e57c56Schristos  obsolete_mkdir_used=false
306889c434eSchristos
307*75e57c56Schristos  if test $dstdir_status != 0; then
308*75e57c56Schristos    case $posix_mkdir in
309*75e57c56Schristos      '')
310*75e57c56Schristos	# Create intermediate dirs using mode 755 as modified by the umask.
311*75e57c56Schristos	# This is like FreeBSD 'install' as of 1997-10-28.
312*75e57c56Schristos	umask=`umask`
313*75e57c56Schristos	case $stripcmd.$umask in
314*75e57c56Schristos	  # Optimize common cases.
315*75e57c56Schristos	  *[2367][2367]) mkdir_umask=$umask;;
316*75e57c56Schristos	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317889c434eSchristos
318*75e57c56Schristos	  *[0-7])
319*75e57c56Schristos	    mkdir_umask=`expr $umask + 22 \
320*75e57c56Schristos	      - $umask % 100 % 40 + $umask % 20 \
321*75e57c56Schristos	      - $umask % 10 % 4 + $umask % 2
322*75e57c56Schristos	    `;;
323*75e57c56Schristos	  *) mkdir_umask=$umask,go-w;;
324*75e57c56Schristos	esac
325*75e57c56Schristos
326*75e57c56Schristos	# With -d, create the new directory with the user-specified mode.
327*75e57c56Schristos	# Otherwise, rely on $mkdir_umask.
328*75e57c56Schristos	if test -n "$dir_arg"; then
329*75e57c56Schristos	  mkdir_mode=-m$mode
330*75e57c56Schristos	else
331*75e57c56Schristos	  mkdir_mode=
332*75e57c56Schristos	fi
333*75e57c56Schristos
334*75e57c56Schristos	posix_mkdir=false
335*75e57c56Schristos	case $umask in
336*75e57c56Schristos	  *[123567][0-7][0-7])
337*75e57c56Schristos	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338*75e57c56Schristos	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339*75e57c56Schristos	    ;;
340*75e57c56Schristos	  *)
341*75e57c56Schristos	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342*75e57c56Schristos	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343*75e57c56Schristos
344*75e57c56Schristos	    if (umask $mkdir_umask &&
345*75e57c56Schristos		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346*75e57c56Schristos	    then
347*75e57c56Schristos	      if test -z "$dir_arg" || {
348*75e57c56Schristos		   # Check for POSIX incompatibilities with -m.
349*75e57c56Schristos		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350*75e57c56Schristos		   # other-writeable bit of parent directory when it shouldn't.
351*75e57c56Schristos		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352*75e57c56Schristos		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353*75e57c56Schristos		   case $ls_ld_tmpdir in
354*75e57c56Schristos		     d????-?r-*) different_mode=700;;
355*75e57c56Schristos		     d????-?--*) different_mode=755;;
356*75e57c56Schristos		     *) false;;
357*75e57c56Schristos		   esac &&
358*75e57c56Schristos		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359*75e57c56Schristos		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360*75e57c56Schristos		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361*75e57c56Schristos		   }
362*75e57c56Schristos		 }
363*75e57c56Schristos	      then posix_mkdir=:
364*75e57c56Schristos	      fi
365*75e57c56Schristos	      rmdir "$tmpdir/d" "$tmpdir"
366*75e57c56Schristos	    else
367*75e57c56Schristos	      # Remove any dirs left behind by ancient mkdir implementations.
368*75e57c56Schristos	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369*75e57c56Schristos	    fi
370*75e57c56Schristos	    trap '' 0;;
371*75e57c56Schristos	esac;;
372*75e57c56Schristos    esac
373*75e57c56Schristos
374*75e57c56Schristos    if
375*75e57c56Schristos      $posix_mkdir && (
376*75e57c56Schristos	umask $mkdir_umask &&
377*75e57c56Schristos	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378*75e57c56Schristos      )
379*75e57c56Schristos    then :
380*75e57c56Schristos    else
381*75e57c56Schristos
382*75e57c56Schristos      # The umask is ridiculous, or mkdir does not conform to POSIX,
383*75e57c56Schristos      # or it failed possibly due to a race condition.  Create the
384*75e57c56Schristos      # directory the slow way, step by step, checking for races as we go.
385*75e57c56Schristos
386*75e57c56Schristos      case $dstdir in
387*75e57c56Schristos	/*) prefix='/';;
388*75e57c56Schristos	-*) prefix='./';;
389*75e57c56Schristos	*)  prefix='';;
390*75e57c56Schristos      esac
391*75e57c56Schristos
392*75e57c56Schristos      eval "$initialize_posix_glob"
393*75e57c56Schristos
394*75e57c56Schristos      oIFS=$IFS
395*75e57c56Schristos      IFS=/
396*75e57c56Schristos      $posix_glob set -f
397*75e57c56Schristos      set fnord $dstdir
398*75e57c56Schristos      shift
399*75e57c56Schristos      $posix_glob set +f
400*75e57c56Schristos      IFS=$oIFS
401*75e57c56Schristos
402*75e57c56Schristos      prefixes=
403*75e57c56Schristos
404*75e57c56Schristos      for d
405*75e57c56Schristos      do
406*75e57c56Schristos	test -z "$d" && continue
407*75e57c56Schristos
408*75e57c56Schristos	prefix=$prefix$d
409*75e57c56Schristos	if test -d "$prefix"; then
410*75e57c56Schristos	  prefixes=
411*75e57c56Schristos	else
412*75e57c56Schristos	  if $posix_mkdir; then
413*75e57c56Schristos	    (umask=$mkdir_umask &&
414*75e57c56Schristos	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415*75e57c56Schristos	    # Don't fail if two instances are running concurrently.
416*75e57c56Schristos	    test -d "$prefix" || exit 1
417*75e57c56Schristos	  else
418*75e57c56Schristos	    case $prefix in
419*75e57c56Schristos	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420*75e57c56Schristos	      *) qprefix=$prefix;;
421*75e57c56Schristos	    esac
422*75e57c56Schristos	    prefixes="$prefixes '$qprefix'"
423*75e57c56Schristos	  fi
424*75e57c56Schristos	fi
425*75e57c56Schristos	prefix=$prefix/
426*75e57c56Schristos      done
427*75e57c56Schristos
428*75e57c56Schristos      if test -n "$prefixes"; then
429*75e57c56Schristos	# Don't fail if two instances are running concurrently.
430*75e57c56Schristos	(umask $mkdir_umask &&
431*75e57c56Schristos	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432*75e57c56Schristos	  test -d "$dstdir" || exit 1
433*75e57c56Schristos	obsolete_mkdir_used=true
434*75e57c56Schristos      fi
435*75e57c56Schristos    fi
436*75e57c56Schristos  fi
437*75e57c56Schristos
438*75e57c56Schristos  if test -n "$dir_arg"; then
439*75e57c56Schristos    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440*75e57c56Schristos    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441*75e57c56Schristos    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442*75e57c56Schristos      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443*75e57c56Schristos  else
444*75e57c56Schristos
445*75e57c56Schristos    # Make a couple of temp file names in the proper directory.
446*75e57c56Schristos    dsttmp=$dstdir/_inst.$$_
447*75e57c56Schristos    rmtmp=$dstdir/_rm.$$_
448*75e57c56Schristos
449*75e57c56Schristos    # Trap to clean up those temp files at exit.
450*75e57c56Schristos    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451*75e57c56Schristos
452*75e57c56Schristos    # Copy the file name to the temp name.
453*75e57c56Schristos    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454*75e57c56Schristos
455*75e57c56Schristos    # and set any options; do chmod last to preserve setuid bits.
456*75e57c56Schristos    #
457a7c91847Schristos    # If any of these fail, we abort the whole thing.  If we want to
458a7c91847Schristos    # ignore errors from any of these, just make sure not to ignore
459*75e57c56Schristos    # errors from the above "$doit $cpprog $src $dsttmp" command.
460*75e57c56Schristos    #
461*75e57c56Schristos    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462*75e57c56Schristos    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463*75e57c56Schristos    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464*75e57c56Schristos    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465889c434eSchristos
466*75e57c56Schristos    # If -C, don't bother to copy if it wouldn't change the file.
467*75e57c56Schristos    if $copy_on_change &&
468*75e57c56Schristos       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469*75e57c56Schristos       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470*75e57c56Schristos
471*75e57c56Schristos       eval "$initialize_posix_glob" &&
472*75e57c56Schristos       $posix_glob set -f &&
473*75e57c56Schristos       set X $old && old=:$2:$4:$5:$6 &&
474*75e57c56Schristos       set X $new && new=:$2:$4:$5:$6 &&
475*75e57c56Schristos       $posix_glob set +f &&
476*75e57c56Schristos
477*75e57c56Schristos       test "$old" = "$new" &&
478*75e57c56Schristos       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479*75e57c56Schristos    then
480*75e57c56Schristos      rm -f "$dsttmp"
481*75e57c56Schristos    else
482*75e57c56Schristos      # Rename the file to the real destination.
483*75e57c56Schristos      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484*75e57c56Schristos
485*75e57c56Schristos      # The rename failed, perhaps because mv can't rename something else
486*75e57c56Schristos      # to itself, or perhaps because mv is so ancient that it does not
487*75e57c56Schristos      # support -f.
488*75e57c56Schristos      {
489*75e57c56Schristos	# Now remove or move aside any old file at destination location.
490*75e57c56Schristos	# We try this two ways since rm can't unlink itself on some
491*75e57c56Schristos	# systems and the destination file might be busy for other
492*75e57c56Schristos	# reasons.  In this case, the final cleanup might fail but the new
493*75e57c56Schristos	# file should still install successfully.
494*75e57c56Schristos	{
495*75e57c56Schristos	  test ! -f "$dst" ||
496*75e57c56Schristos	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497*75e57c56Schristos	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498*75e57c56Schristos	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499*75e57c56Schristos	  } ||
500*75e57c56Schristos	  { echo "$0: cannot unlink or rename $dst" >&2
501*75e57c56Schristos	    (exit 1); exit 1
502*75e57c56Schristos	  }
503*75e57c56Schristos	} &&
504a7c91847Schristos
505a7c91847Schristos	# Now rename the file to the real destination.
506*75e57c56Schristos	$doit $mvcmd "$dsttmp" "$dst"
507*75e57c56Schristos      }
508*75e57c56Schristos    fi || exit 1
509a7c91847Schristos
510*75e57c56Schristos    trap '' 0
511a7c91847Schristos  fi
512*75e57c56Schristosdone
513a7c91847Schristos
514*75e57c56Schristos# Local variables:
515*75e57c56Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
516*75e57c56Schristos# time-stamp-start: "scriptversion="
517*75e57c56Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
518*75e57c56Schristos# time-stamp-time-zone: "UTC"
519*75e57c56Schristos# time-stamp-end: "; # UTC"
520*75e57c56Schristos# End:
521