xref: /netbsd-src/external/gpl3/binutils/dist/install-sh (revision 9573673d78c64ea1eac42d7f2e9521be89932ae5)
12a6b7db3Sskrll#!/bin/sh
22a6b7db3Sskrll# install - install a program, script, or datafile
32a6b7db3Sskrll
4*9573673dSchristosscriptversion=2013-12-25.23; # UTC
52a6b7db3Sskrll
62a6b7db3Sskrll# This originates from X11R5 (mit/util/scripts/install.sh), which was
72a6b7db3Sskrll# later released in X11R6 (xc/config/util/install.sh) with the
82a6b7db3Sskrll# following copyright and license.
92a6b7db3Sskrll#
102a6b7db3Sskrll# Copyright (C) 1994 X Consortium
112a6b7db3Sskrll#
122a6b7db3Sskrll# Permission is hereby granted, free of charge, to any person obtaining a copy
132a6b7db3Sskrll# of this software and associated documentation files (the "Software"), to
142a6b7db3Sskrll# deal in the Software without restriction, including without limitation the
152a6b7db3Sskrll# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
162a6b7db3Sskrll# sell copies of the Software, and to permit persons to whom the Software is
172a6b7db3Sskrll# furnished to do so, subject to the following conditions:
182a6b7db3Sskrll#
192a6b7db3Sskrll# The above copyright notice and this permission notice shall be included in
202a6b7db3Sskrll# all copies or substantial portions of the Software.
212a6b7db3Sskrll#
222a6b7db3Sskrll# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
232a6b7db3Sskrll# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
242a6b7db3Sskrll# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
252a6b7db3Sskrll# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
262a6b7db3Sskrll# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
272a6b7db3Sskrll# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
282a6b7db3Sskrll#
292a6b7db3Sskrll# Except as contained in this notice, the name of the X Consortium shall not
302a6b7db3Sskrll# be used in advertising or otherwise to promote the sale, use or other deal-
312a6b7db3Sskrll# ings in this Software without prior written authorization from the X Consor-
322a6b7db3Sskrll# tium.
332a6b7db3Sskrll#
342a6b7db3Sskrll#
352a6b7db3Sskrll# FSF changes to this file are in the public domain.
362a6b7db3Sskrll#
372a6b7db3Sskrll# Calling this script install-sh is preferred over install.sh, to prevent
38*9573673dSchristos# 'make' implicit rules from creating a file called install from it
392a6b7db3Sskrll# when there is no Makefile.
402a6b7db3Sskrll#
412a6b7db3Sskrll# This script is compatible with the BSD install script, but was written
42be9ac0eaSchristos# from scratch.
43be9ac0eaSchristos
44*9573673dSchristostab='	'
45be9ac0eaSchristosnl='
46be9ac0eaSchristos'
47*9573673dSchristosIFS=" $tab$nl"
482a6b7db3Sskrll
49*9573673dSchristos# Set DOITPROG to "echo" to test this script.
502a6b7db3Sskrll
51be9ac0eaSchristosdoit=${DOITPROG-}
52*9573673dSchristosdoit_exec=${doit:-exec}
532a6b7db3Sskrll
54be9ac0eaSchristos# Put in absolute file names if you don't have them in your path;
55be9ac0eaSchristos# or use environment vars.
562a6b7db3Sskrll
57be9ac0eaSchristoschgrpprog=${CHGRPPROG-chgrp}
58be9ac0eaSchristoschmodprog=${CHMODPROG-chmod}
59be9ac0eaSchristoschownprog=${CHOWNPROG-chown}
60be9ac0eaSchristoscmpprog=${CMPPROG-cmp}
61be9ac0eaSchristoscpprog=${CPPROG-cp}
62be9ac0eaSchristosmkdirprog=${MKDIRPROG-mkdir}
63be9ac0eaSchristosmvprog=${MVPROG-mv}
64be9ac0eaSchristosrmprog=${RMPROG-rm}
65be9ac0eaSchristosstripprog=${STRIPPROG-strip}
662a6b7db3Sskrll
67be9ac0eaSchristosposix_mkdir=
68be9ac0eaSchristos
69be9ac0eaSchristos# Desired mode of installed file.
70be9ac0eaSchristosmode=0755
71be9ac0eaSchristos
722a6b7db3Sskrllchgrpcmd=
73be9ac0eaSchristoschmodcmd=$chmodprog
74be9ac0eaSchristoschowncmd=
75be9ac0eaSchristosmvcmd=$mvprog
762a6b7db3Sskrllrmcmd="$rmprog -f"
77be9ac0eaSchristosstripcmd=
78be9ac0eaSchristos
792a6b7db3Sskrllsrc=
802a6b7db3Sskrlldst=
812a6b7db3Sskrlldir_arg=
82be9ac0eaSchristosdst_arg=
83be9ac0eaSchristos
84be9ac0eaSchristoscopy_on_change=false
85*9573673dSchristosis_target_a_directory=possibly
862a6b7db3Sskrll
87be9ac0eaSchristosusage="\
88be9ac0eaSchristosUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
892a6b7db3Sskrll   or: $0 [OPTION]... SRCFILES... DIRECTORY
902a6b7db3Sskrll   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
912a6b7db3Sskrll   or: $0 [OPTION]... -d DIRECTORIES...
922a6b7db3Sskrll
932a6b7db3SskrllIn the 1st form, copy SRCFILE to DSTFILE.
942a6b7db3SskrllIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
952a6b7db3SskrllIn the 4th, create DIRECTORIES.
962a6b7db3Sskrll
972a6b7db3SskrllOptions:
98be9ac0eaSchristos     --help     display this help and exit.
99be9ac0eaSchristos     --version  display version info and exit.
100be9ac0eaSchristos
1012a6b7db3Sskrll  -c            (ignored)
102be9ac0eaSchristos  -C            install only if different (preserve the last data modification time)
1032a6b7db3Sskrll  -d            create directories instead of installing files.
1042a6b7db3Sskrll  -g GROUP      $chgrpprog installed files to GROUP.
1052a6b7db3Sskrll  -m MODE       $chmodprog installed files to MODE.
1062a6b7db3Sskrll  -o USER       $chownprog installed files to USER.
1072a6b7db3Sskrll  -s            $stripprog installed files.
1082a6b7db3Sskrll  -t DIRECTORY  install into DIRECTORY.
1092a6b7db3Sskrll  -T            report an error if DSTFILE is a directory.
1102a6b7db3Sskrll
1112a6b7db3SskrllEnvironment variables override the default commands:
112be9ac0eaSchristos  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113be9ac0eaSchristos  RMPROG STRIPPROG
1142a6b7db3Sskrll"
1152a6b7db3Sskrll
116be9ac0eaSchristoswhile test $# -ne 0; do
1172a6b7db3Sskrll  case $1 in
118be9ac0eaSchristos    -c) ;;
1192a6b7db3Sskrll
120be9ac0eaSchristos    -C) copy_on_change=true;;
121be9ac0eaSchristos
122be9ac0eaSchristos    -d) dir_arg=true;;
1232a6b7db3Sskrll
1242a6b7db3Sskrll    -g) chgrpcmd="$chgrpprog $2"
125be9ac0eaSchristos        shift;;
1262a6b7db3Sskrll
1272a6b7db3Sskrll    --help) echo "$usage"; exit $?;;
1282a6b7db3Sskrll
129be9ac0eaSchristos    -m) mode=$2
130be9ac0eaSchristos        case $mode in
131*9573673dSchristos          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132be9ac0eaSchristos            echo "$0: invalid mode: $mode" >&2
133be9ac0eaSchristos            exit 1;;
134be9ac0eaSchristos        esac
135be9ac0eaSchristos        shift;;
1362a6b7db3Sskrll
1372a6b7db3Sskrll    -o) chowncmd="$chownprog $2"
138be9ac0eaSchristos        shift;;
1392a6b7db3Sskrll
140be9ac0eaSchristos    -s) stripcmd=$stripprog;;
1412a6b7db3Sskrll
142*9573673dSchristos    -t)
143*9573673dSchristos        is_target_a_directory=always
144*9573673dSchristos        dst_arg=$2
145*9573673dSchristos        # Protect names problematic for 'test' and other utilities.
146*9573673dSchristos        case $dst_arg in
147*9573673dSchristos          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148*9573673dSchristos        esac
149be9ac0eaSchristos        shift;;
1502a6b7db3Sskrll
151*9573673dSchristos    -T) is_target_a_directory=never;;
1522a6b7db3Sskrll
1532a6b7db3Sskrll    --version) echo "$0 $scriptversion"; exit $?;;
1542a6b7db3Sskrll
155be9ac0eaSchristos    --) shift
156be9ac0eaSchristos        break;;
157be9ac0eaSchristos
158be9ac0eaSchristos    -*) echo "$0: invalid option: $1" >&2
159be9ac0eaSchristos        exit 1;;
160be9ac0eaSchristos
161be9ac0eaSchristos    *)  break;;
162be9ac0eaSchristos  esac
163be9ac0eaSchristos  shift
164be9ac0eaSchristosdone
165be9ac0eaSchristos
166*9573673dSchristos# We allow the use of options -d and -T together, by making -d
167*9573673dSchristos# take the precedence; this is for compatibility with GNU install.
168*9573673dSchristos
169*9573673dSchristosif test -n "$dir_arg"; then
170*9573673dSchristos  if test -n "$dst_arg"; then
171*9573673dSchristos    echo "$0: target directory not allowed when installing a directory." >&2
172*9573673dSchristos    exit 1
173*9573673dSchristos  fi
174*9573673dSchristosfi
175*9573673dSchristos
176be9ac0eaSchristosif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177be9ac0eaSchristos  # When -d is used, all remaining arguments are directories to create.
1782a6b7db3Sskrll  # When -t is used, the destination is already specified.
1792a6b7db3Sskrll  # Otherwise, the last argument is the destination.  Remove it from $@.
1802a6b7db3Sskrll  for arg
1812a6b7db3Sskrll  do
182be9ac0eaSchristos    if test -n "$dst_arg"; then
1832a6b7db3Sskrll      # $@ is not empty: it contains at least $arg.
184be9ac0eaSchristos      set fnord "$@" "$dst_arg"
1852a6b7db3Sskrll      shift # fnord
1862a6b7db3Sskrll    fi
1872a6b7db3Sskrll    shift # arg
188be9ac0eaSchristos    dst_arg=$arg
189*9573673dSchristos    # Protect names problematic for 'test' and other utilities.
190*9573673dSchristos    case $dst_arg in
191*9573673dSchristos      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192*9573673dSchristos    esac
1932a6b7db3Sskrll  done
194be9ac0eaSchristosfi
1952a6b7db3Sskrll
196be9ac0eaSchristosif test $# -eq 0; then
1972a6b7db3Sskrll  if test -z "$dir_arg"; then
1982a6b7db3Sskrll    echo "$0: no input file specified." >&2
1992a6b7db3Sskrll    exit 1
2002a6b7db3Sskrll  fi
201*9573673dSchristos  # It's OK to call 'install-sh -d' without argument.
2022a6b7db3Sskrll  # This can happen when creating conditional directories.
2032a6b7db3Sskrll  exit 0
2042a6b7db3Sskrllfi
2052a6b7db3Sskrll
206be9ac0eaSchristosif test -z "$dir_arg"; then
207*9573673dSchristos  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208*9573673dSchristos    if test ! -d "$dst_arg"; then
209*9573673dSchristos      echo "$0: $dst_arg: Is not a directory." >&2
210*9573673dSchristos      exit 1
211*9573673dSchristos    fi
212*9573673dSchristos  fi
213*9573673dSchristosfi
214*9573673dSchristos
215*9573673dSchristosif test -z "$dir_arg"; then
216*9573673dSchristos  do_exit='(exit $ret); exit $ret'
217*9573673dSchristos  trap "ret=129; $do_exit" 1
218*9573673dSchristos  trap "ret=130; $do_exit" 2
219*9573673dSchristos  trap "ret=141; $do_exit" 13
220*9573673dSchristos  trap "ret=143; $do_exit" 15
221be9ac0eaSchristos
222be9ac0eaSchristos  # Set umask so as not to create temps with too-generous modes.
223be9ac0eaSchristos  # However, 'strip' requires both read and write access to temps.
224be9ac0eaSchristos  case $mode in
225be9ac0eaSchristos    # Optimize common cases.
226be9ac0eaSchristos    *644) cp_umask=133;;
227be9ac0eaSchristos    *755) cp_umask=22;;
228be9ac0eaSchristos
229be9ac0eaSchristos    *[0-7])
230be9ac0eaSchristos      if test -z "$stripcmd"; then
231be9ac0eaSchristos        u_plus_rw=
232be9ac0eaSchristos      else
233be9ac0eaSchristos        u_plus_rw='% 200'
234be9ac0eaSchristos      fi
235be9ac0eaSchristos      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236be9ac0eaSchristos    *)
237be9ac0eaSchristos      if test -z "$stripcmd"; then
238be9ac0eaSchristos        u_plus_rw=
239be9ac0eaSchristos      else
240be9ac0eaSchristos        u_plus_rw=,u+rw
241be9ac0eaSchristos      fi
242be9ac0eaSchristos      cp_umask=$mode$u_plus_rw;;
243be9ac0eaSchristos  esac
244be9ac0eaSchristosfi
245be9ac0eaSchristos
2462a6b7db3Sskrllfor src
2472a6b7db3Sskrlldo
248*9573673dSchristos  # Protect names problematic for 'test' and other utilities.
2492a6b7db3Sskrll  case $src in
250*9573673dSchristos    -* | [=\(\)!]) src=./$src;;
2512a6b7db3Sskrll  esac
2522a6b7db3Sskrll
2532a6b7db3Sskrll  if test -n "$dir_arg"; then
2542a6b7db3Sskrll    dst=$src
255be9ac0eaSchristos    dstdir=$dst
256be9ac0eaSchristos    test -d "$dstdir"
257be9ac0eaSchristos    dstdir_status=$?
258be9ac0eaSchristos  else
2592a6b7db3Sskrll
2602a6b7db3Sskrll    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2612a6b7db3Sskrll    # might cause directories to be created, which would be especially bad
2622a6b7db3Sskrll    # if $src (and thus $dsttmp) contains '*'.
2632a6b7db3Sskrll    if test ! -f "$src" && test ! -d "$src"; then
2642a6b7db3Sskrll      echo "$0: $src does not exist." >&2
2652a6b7db3Sskrll      exit 1
2662a6b7db3Sskrll    fi
2672a6b7db3Sskrll
268be9ac0eaSchristos    if test -z "$dst_arg"; then
2692a6b7db3Sskrll      echo "$0: no destination specified." >&2
2702a6b7db3Sskrll      exit 1
2712a6b7db3Sskrll    fi
272be9ac0eaSchristos    dst=$dst_arg
2732a6b7db3Sskrll
2742a6b7db3Sskrll    # If destination is a directory, append the input filename; won't work
2752a6b7db3Sskrll    # if double slashes aren't ignored.
2762a6b7db3Sskrll    if test -d "$dst"; then
277*9573673dSchristos      if test "$is_target_a_directory" = never; then
278be9ac0eaSchristos        echo "$0: $dst_arg: Is a directory" >&2
2792a6b7db3Sskrll        exit 1
2802a6b7db3Sskrll      fi
281be9ac0eaSchristos      dstdir=$dst
282be9ac0eaSchristos      dst=$dstdir/`basename "$src"`
283be9ac0eaSchristos      dstdir_status=0
284be9ac0eaSchristos    else
285*9573673dSchristos      dstdir=`dirname "$dst"`
286be9ac0eaSchristos      test -d "$dstdir"
287be9ac0eaSchristos      dstdir_status=$?
2882a6b7db3Sskrll    fi
2892a6b7db3Sskrll  fi
2902a6b7db3Sskrll
291be9ac0eaSchristos  obsolete_mkdir_used=false
2922a6b7db3Sskrll
293be9ac0eaSchristos  if test $dstdir_status != 0; then
294be9ac0eaSchristos    case $posix_mkdir in
295be9ac0eaSchristos      '')
296be9ac0eaSchristos        # Create intermediate dirs using mode 755 as modified by the umask.
297be9ac0eaSchristos        # This is like FreeBSD 'install' as of 1997-10-28.
298be9ac0eaSchristos        umask=`umask`
299be9ac0eaSchristos        case $stripcmd.$umask in
300be9ac0eaSchristos          # Optimize common cases.
301be9ac0eaSchristos          *[2367][2367]) mkdir_umask=$umask;;
302be9ac0eaSchristos          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3032a6b7db3Sskrll
304be9ac0eaSchristos          *[0-7])
305be9ac0eaSchristos            mkdir_umask=`expr $umask + 22 \
306be9ac0eaSchristos              - $umask % 100 % 40 + $umask % 20 \
307be9ac0eaSchristos              - $umask % 10 % 4 + $umask % 2
308be9ac0eaSchristos            `;;
309be9ac0eaSchristos          *) mkdir_umask=$umask,go-w;;
310be9ac0eaSchristos        esac
311be9ac0eaSchristos
312be9ac0eaSchristos        # With -d, create the new directory with the user-specified mode.
313be9ac0eaSchristos        # Otherwise, rely on $mkdir_umask.
314be9ac0eaSchristos        if test -n "$dir_arg"; then
315be9ac0eaSchristos          mkdir_mode=-m$mode
316be9ac0eaSchristos        else
317be9ac0eaSchristos          mkdir_mode=
318be9ac0eaSchristos        fi
319be9ac0eaSchristos
320be9ac0eaSchristos        posix_mkdir=false
321be9ac0eaSchristos        case $umask in
322be9ac0eaSchristos          *[123567][0-7][0-7])
323be9ac0eaSchristos            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324be9ac0eaSchristos            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325be9ac0eaSchristos            ;;
326be9ac0eaSchristos          *)
327be9ac0eaSchristos            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328be9ac0eaSchristos            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329be9ac0eaSchristos
330be9ac0eaSchristos            if (umask $mkdir_umask &&
331be9ac0eaSchristos                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332be9ac0eaSchristos            then
333be9ac0eaSchristos              if test -z "$dir_arg" || {
334be9ac0eaSchristos                   # Check for POSIX incompatibilities with -m.
335be9ac0eaSchristos                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336*9573673dSchristos                   # other-writable bit of parent directory when it shouldn't.
337be9ac0eaSchristos                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338be9ac0eaSchristos                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339be9ac0eaSchristos                   case $ls_ld_tmpdir in
340be9ac0eaSchristos                     d????-?r-*) different_mode=700;;
341be9ac0eaSchristos                     d????-?--*) different_mode=755;;
342be9ac0eaSchristos                     *) false;;
343be9ac0eaSchristos                   esac &&
344be9ac0eaSchristos                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345be9ac0eaSchristos                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346be9ac0eaSchristos                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347be9ac0eaSchristos                   }
348be9ac0eaSchristos                 }
349be9ac0eaSchristos              then posix_mkdir=:
350be9ac0eaSchristos              fi
351be9ac0eaSchristos              rmdir "$tmpdir/d" "$tmpdir"
352be9ac0eaSchristos            else
353be9ac0eaSchristos              # Remove any dirs left behind by ancient mkdir implementations.
354be9ac0eaSchristos              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355be9ac0eaSchristos            fi
356be9ac0eaSchristos            trap '' 0;;
357be9ac0eaSchristos        esac;;
358be9ac0eaSchristos    esac
359be9ac0eaSchristos
360be9ac0eaSchristos    if
361be9ac0eaSchristos      $posix_mkdir && (
362be9ac0eaSchristos        umask $mkdir_umask &&
363be9ac0eaSchristos        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364be9ac0eaSchristos      )
365be9ac0eaSchristos    then :
366be9ac0eaSchristos    else
367be9ac0eaSchristos
368be9ac0eaSchristos      # The umask is ridiculous, or mkdir does not conform to POSIX,
369be9ac0eaSchristos      # or it failed possibly due to a race condition.  Create the
370be9ac0eaSchristos      # directory the slow way, step by step, checking for races as we go.
371be9ac0eaSchristos
372be9ac0eaSchristos      case $dstdir in
373be9ac0eaSchristos        /*) prefix='/';;
374*9573673dSchristos        [-=\(\)!]*) prefix='./';;
375be9ac0eaSchristos        *)  prefix='';;
376be9ac0eaSchristos      esac
377be9ac0eaSchristos
3782a6b7db3Sskrll      oIFS=$IFS
379be9ac0eaSchristos      IFS=/
380*9573673dSchristos      set -f
381be9ac0eaSchristos      set fnord $dstdir
3822a6b7db3Sskrll      shift
383*9573673dSchristos      set +f
3842a6b7db3Sskrll      IFS=$oIFS
3852a6b7db3Sskrll
386be9ac0eaSchristos      prefixes=
3872a6b7db3Sskrll
388be9ac0eaSchristos      for d
389be9ac0eaSchristos      do
390*9573673dSchristos        test X"$d" = X && continue
391be9ac0eaSchristos
392be9ac0eaSchristos        prefix=$prefix$d
393be9ac0eaSchristos        if test -d "$prefix"; then
394be9ac0eaSchristos          prefixes=
395be9ac0eaSchristos        else
396be9ac0eaSchristos          if $posix_mkdir; then
397be9ac0eaSchristos            (umask=$mkdir_umask &&
398be9ac0eaSchristos             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399be9ac0eaSchristos            # Don't fail if two instances are running concurrently.
400be9ac0eaSchristos            test -d "$prefix" || exit 1
401be9ac0eaSchristos          else
402be9ac0eaSchristos            case $prefix in
403be9ac0eaSchristos              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404be9ac0eaSchristos              *) qprefix=$prefix;;
405be9ac0eaSchristos            esac
406be9ac0eaSchristos            prefixes="$prefixes '$qprefix'"
4072a6b7db3Sskrll          fi
408be9ac0eaSchristos        fi
409be9ac0eaSchristos        prefix=$prefix/
4102a6b7db3Sskrll      done
411be9ac0eaSchristos
412be9ac0eaSchristos      if test -n "$prefixes"; then
413be9ac0eaSchristos        # Don't fail if two instances are running concurrently.
414be9ac0eaSchristos        (umask $mkdir_umask &&
415be9ac0eaSchristos         eval "\$doit_exec \$mkdirprog $prefixes") ||
416be9ac0eaSchristos          test -d "$dstdir" || exit 1
417be9ac0eaSchristos        obsolete_mkdir_used=true
418be9ac0eaSchristos      fi
419be9ac0eaSchristos    fi
4202a6b7db3Sskrll  fi
4212a6b7db3Sskrll
4222a6b7db3Sskrll  if test -n "$dir_arg"; then
423be9ac0eaSchristos    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424be9ac0eaSchristos    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425be9ac0eaSchristos    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426be9ac0eaSchristos      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4272a6b7db3Sskrll  else
4282a6b7db3Sskrll
4292a6b7db3Sskrll    # Make a couple of temp file names in the proper directory.
4302a6b7db3Sskrll    dsttmp=$dstdir/_inst.$$_
4312a6b7db3Sskrll    rmtmp=$dstdir/_rm.$$_
4322a6b7db3Sskrll
4332a6b7db3Sskrll    # Trap to clean up those temp files at exit.
4342a6b7db3Sskrll    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4352a6b7db3Sskrll
4362a6b7db3Sskrll    # Copy the file name to the temp name.
437be9ac0eaSchristos    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4382a6b7db3Sskrll
4392a6b7db3Sskrll    # and set any options; do chmod last to preserve setuid bits.
4402a6b7db3Sskrll    #
4412a6b7db3Sskrll    # If any of these fail, we abort the whole thing.  If we want to
4422a6b7db3Sskrll    # ignore errors from any of these, just make sure not to ignore
4432a6b7db3Sskrll    # errors from the above "$doit $cpprog $src $dsttmp" command.
4442a6b7db3Sskrll    #
445be9ac0eaSchristos    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446be9ac0eaSchristos    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447be9ac0eaSchristos    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448be9ac0eaSchristos    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4492a6b7db3Sskrll
450be9ac0eaSchristos    # If -C, don't bother to copy if it wouldn't change the file.
451be9ac0eaSchristos    if $copy_on_change &&
452be9ac0eaSchristos       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453be9ac0eaSchristos       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454*9573673dSchristos       set -f &&
455be9ac0eaSchristos       set X $old && old=:$2:$4:$5:$6 &&
456be9ac0eaSchristos       set X $new && new=:$2:$4:$5:$6 &&
457*9573673dSchristos       set +f &&
458be9ac0eaSchristos       test "$old" = "$new" &&
459be9ac0eaSchristos       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460be9ac0eaSchristos    then
461be9ac0eaSchristos      rm -f "$dsttmp"
462be9ac0eaSchristos    else
463be9ac0eaSchristos      # Rename the file to the real destination.
464be9ac0eaSchristos      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465be9ac0eaSchristos
4662a6b7db3Sskrll      # The rename failed, perhaps because mv can't rename something else
4672a6b7db3Sskrll      # to itself, or perhaps because mv is so ancient that it does not
4682a6b7db3Sskrll      # support -f.
469be9ac0eaSchristos      {
4702a6b7db3Sskrll        # Now remove or move aside any old file at destination location.
4712a6b7db3Sskrll        # We try this two ways since rm can't unlink itself on some
4722a6b7db3Sskrll        # systems and the destination file might be busy for other
4732a6b7db3Sskrll        # reasons.  In this case, the final cleanup might fail but the new
4742a6b7db3Sskrll        # file should still install successfully.
4752a6b7db3Sskrll        {
476be9ac0eaSchristos          test ! -f "$dst" ||
477be9ac0eaSchristos          $doit $rmcmd -f "$dst" 2>/dev/null ||
478be9ac0eaSchristos          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479be9ac0eaSchristos            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480be9ac0eaSchristos          } ||
481be9ac0eaSchristos          { echo "$0: cannot unlink or rename $dst" >&2
4822a6b7db3Sskrll            (exit 1); exit 1
4832a6b7db3Sskrll          }
4842a6b7db3Sskrll        } &&
4852a6b7db3Sskrll
4862a6b7db3Sskrll        # Now rename the file to the real destination.
487be9ac0eaSchristos        $doit $mvcmd "$dsttmp" "$dst"
4882a6b7db3Sskrll      }
489be9ac0eaSchristos    fi || exit 1
4902a6b7db3Sskrll
491be9ac0eaSchristos    trap '' 0
492be9ac0eaSchristos  fi
493be9ac0eaSchristosdone
4942a6b7db3Sskrll
4952a6b7db3Sskrll# Local variables:
4962a6b7db3Sskrll# eval: (add-hook 'write-file-hooks 'time-stamp)
4972a6b7db3Sskrll# time-stamp-start: "scriptversion="
4982a6b7db3Sskrll# time-stamp-format: "%:y-%02m-%02d.%02H"
499be9ac0eaSchristos# time-stamp-time-zone: "UTC"
500be9ac0eaSchristos# time-stamp-end: "; # UTC"
5012a6b7db3Sskrll# End:
502