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