install-sh revision c1e3c5d0
16f03b1f6Smrg#!/bin/sh
26f03b1f6Smrg# install - install a program, script, or datafile
36f03b1f6Smrg
4c1e3c5d0Smrgscriptversion=2013-12-25.23; # UTC
56f03b1f6Smrg
66f03b1f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76f03b1f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86f03b1f6Smrg# following copyright and license.
96f03b1f6Smrg#
106f03b1f6Smrg# Copyright (C) 1994 X Consortium
116f03b1f6Smrg#
126f03b1f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136f03b1f6Smrg# of this software and associated documentation files (the "Software"), to
146f03b1f6Smrg# deal in the Software without restriction, including without limitation the
156f03b1f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166f03b1f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
176f03b1f6Smrg# furnished to do so, subject to the following conditions:
186f03b1f6Smrg#
196f03b1f6Smrg# The above copyright notice and this permission notice shall be included in
206f03b1f6Smrg# all copies or substantial portions of the Software.
216f03b1f6Smrg#
226f03b1f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236f03b1f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246f03b1f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256f03b1f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266f03b1f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276f03b1f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286f03b1f6Smrg#
296f03b1f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
306f03b1f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316f03b1f6Smrg# ings in this Software without prior written authorization from the X Consor-
326f03b1f6Smrg# tium.
336f03b1f6Smrg#
346f03b1f6Smrg#
356f03b1f6Smrg# FSF changes to this file are in the public domain.
366f03b1f6Smrg#
376f03b1f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3817caa701Smrg# 'make' implicit rules from creating a file called install from it
396f03b1f6Smrg# when there is no Makefile.
406f03b1f6Smrg#
416f03b1f6Smrg# This script is compatible with the BSD install script, but was written
4298af18c5Smrg# from scratch.
4398af18c5Smrg
44c1e3c5d0Smrgtab='	'
4598af18c5Smrgnl='
4698af18c5Smrg'
47c1e3c5d0SmrgIFS=" $tab$nl"
486f03b1f6Smrg
49c1e3c5d0Smrg# Set DOITPROG to "echo" to test this script.
506f03b1f6Smrg
5198af18c5Smrgdoit=${DOITPROG-}
52c1e3c5d0Smrgdoit_exec=${doit:-exec}
536f03b1f6Smrg
5498af18c5Smrg# Put in absolute file names if you don't have them in your path;
5598af18c5Smrg# or use environment vars.
5698af18c5Smrg
5798af18c5Smrgchgrpprog=${CHGRPPROG-chgrp}
5898af18c5Smrgchmodprog=${CHMODPROG-chmod}
5998af18c5Smrgchownprog=${CHOWNPROG-chown}
6098af18c5Smrgcmpprog=${CMPPROG-cmp}
6198af18c5Smrgcpprog=${CPPROG-cp}
6298af18c5Smrgmkdirprog=${MKDIRPROG-mkdir}
6398af18c5Smrgmvprog=${MVPROG-mv}
6498af18c5Smrgrmprog=${RMPROG-rm}
6598af18c5Smrgstripprog=${STRIPPROG-strip}
6698af18c5Smrg
6798af18c5Smrgposix_mkdir=
6898af18c5Smrg
6998af18c5Smrg# Desired mode of installed file.
7098af18c5Smrgmode=0755
716f03b1f6Smrg
726f03b1f6Smrgchgrpcmd=
7398af18c5Smrgchmodcmd=$chmodprog
7498af18c5Smrgchowncmd=
7598af18c5Smrgmvcmd=$mvprog
766f03b1f6Smrgrmcmd="$rmprog -f"
7798af18c5Smrgstripcmd=
7898af18c5Smrg
796f03b1f6Smrgsrc=
806f03b1f6Smrgdst=
816f03b1f6Smrgdir_arg=
8298af18c5Smrgdst_arg=
8398af18c5Smrg
8498af18c5Smrgcopy_on_change=false
85c1e3c5d0Smrgis_target_a_directory=possibly
866f03b1f6Smrg
8798af18c5Smrgusage="\
8898af18c5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
896f03b1f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
906f03b1f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
916f03b1f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
926f03b1f6Smrg
936f03b1f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
946f03b1f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
956f03b1f6SmrgIn the 4th, create DIRECTORIES.
966f03b1f6Smrg
976f03b1f6SmrgOptions:
9898af18c5Smrg     --help     display this help and exit.
9998af18c5Smrg     --version  display version info and exit.
10098af18c5Smrg
10198af18c5Smrg  -c            (ignored)
10298af18c5Smrg  -C            install only if different (preserve the last data modification time)
10398af18c5Smrg  -d            create directories instead of installing files.
10498af18c5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10598af18c5Smrg  -m MODE       $chmodprog installed files to MODE.
10698af18c5Smrg  -o USER       $chownprog installed files to USER.
10798af18c5Smrg  -s            $stripprog installed files.
10898af18c5Smrg  -t DIRECTORY  install into DIRECTORY.
10998af18c5Smrg  -T            report an error if DSTFILE is a directory.
1106f03b1f6Smrg
1116f03b1f6SmrgEnvironment variables override the default commands:
11298af18c5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11398af18c5Smrg  RMPROG STRIPPROG
1146f03b1f6Smrg"
1156f03b1f6Smrg
11698af18c5Smrgwhile test $# -ne 0; do
1176f03b1f6Smrg  case $1 in
11898af18c5Smrg    -c) ;;
11998af18c5Smrg
12098af18c5Smrg    -C) copy_on_change=true;;
1216f03b1f6Smrg
12298af18c5Smrg    -d) dir_arg=true;;
1236f03b1f6Smrg
1246f03b1f6Smrg    -g) chgrpcmd="$chgrpprog $2"
125c1e3c5d0Smrg        shift;;
1266f03b1f6Smrg
1276f03b1f6Smrg    --help) echo "$usage"; exit $?;;
1286f03b1f6Smrg
12998af18c5Smrg    -m) mode=$2
130c1e3c5d0Smrg        case $mode in
131c1e3c5d0Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132c1e3c5d0Smrg            echo "$0: invalid mode: $mode" >&2
133c1e3c5d0Smrg            exit 1;;
134c1e3c5d0Smrg        esac
135c1e3c5d0Smrg        shift;;
1366f03b1f6Smrg
1376f03b1f6Smrg    -o) chowncmd="$chownprog $2"
138c1e3c5d0Smrg        shift;;
1396f03b1f6Smrg
14098af18c5Smrg    -s) stripcmd=$stripprog;;
1416f03b1f6Smrg
142c1e3c5d0Smrg    -t)
143c1e3c5d0Smrg        is_target_a_directory=always
144c1e3c5d0Smrg        dst_arg=$2
145c1e3c5d0Smrg        # Protect names problematic for 'test' and other utilities.
146c1e3c5d0Smrg        case $dst_arg in
147c1e3c5d0Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148c1e3c5d0Smrg        esac
149c1e3c5d0Smrg        shift;;
1506f03b1f6Smrg
151c1e3c5d0Smrg    -T) is_target_a_directory=never;;
1526f03b1f6Smrg
1536f03b1f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1546f03b1f6Smrg
155c1e3c5d0Smrg    --) shift
156c1e3c5d0Smrg        break;;
15798af18c5Smrg
158c1e3c5d0Smrg    -*) echo "$0: invalid option: $1" >&2
159c1e3c5d0Smrg        exit 1;;
16098af18c5Smrg
16198af18c5Smrg    *)  break;;
1626f03b1f6Smrg  esac
16398af18c5Smrg  shift
1646f03b1f6Smrgdone
1656f03b1f6Smrg
166c1e3c5d0Smrg# We allow the use of options -d and -T together, by making -d
167c1e3c5d0Smrg# take the precedence; this is for compatibility with GNU install.
168c1e3c5d0Smrg
169c1e3c5d0Smrgif test -n "$dir_arg"; then
170c1e3c5d0Smrg  if test -n "$dst_arg"; then
171c1e3c5d0Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172c1e3c5d0Smrg    exit 1
173c1e3c5d0Smrg  fi
174c1e3c5d0Smrgfi
175c1e3c5d0Smrg
17698af18c5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17798af18c5Smrg  # When -d is used, all remaining arguments are directories to create.
17898af18c5Smrg  # When -t is used, the destination is already specified.
17998af18c5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18098af18c5Smrg  for arg
18198af18c5Smrg  do
18298af18c5Smrg    if test -n "$dst_arg"; then
18398af18c5Smrg      # $@ is not empty: it contains at least $arg.
18498af18c5Smrg      set fnord "$@" "$dst_arg"
18598af18c5Smrg      shift # fnord
18698af18c5Smrg    fi
18798af18c5Smrg    shift # arg
18898af18c5Smrg    dst_arg=$arg
18917caa701Smrg    # Protect names problematic for 'test' and other utilities.
19017caa701Smrg    case $dst_arg in
19117caa701Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19217caa701Smrg    esac
19398af18c5Smrg  done
19498af18c5Smrgfi
19598af18c5Smrg
19698af18c5Smrgif test $# -eq 0; then
1976f03b1f6Smrg  if test -z "$dir_arg"; then
1986f03b1f6Smrg    echo "$0: no input file specified." >&2
1996f03b1f6Smrg    exit 1
2006f03b1f6Smrg  fi
20117caa701Smrg  # It's OK to call 'install-sh -d' without argument.
2026f03b1f6Smrg  # This can happen when creating conditional directories.
2036f03b1f6Smrg  exit 0
2046f03b1f6Smrgfi
2056f03b1f6Smrg
206c1e3c5d0Smrgif test -z "$dir_arg"; then
207c1e3c5d0Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208c1e3c5d0Smrg    if test ! -d "$dst_arg"; then
209c1e3c5d0Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210c1e3c5d0Smrg      exit 1
211c1e3c5d0Smrg    fi
212c1e3c5d0Smrg  fi
213c1e3c5d0Smrgfi
214c1e3c5d0Smrg
21598af18c5Smrgif test -z "$dir_arg"; then
21617caa701Smrg  do_exit='(exit $ret); exit $ret'
21717caa701Smrg  trap "ret=129; $do_exit" 1
21817caa701Smrg  trap "ret=130; $do_exit" 2
21917caa701Smrg  trap "ret=141; $do_exit" 13
22017caa701Smrg  trap "ret=143; $do_exit" 15
22198af18c5Smrg
22298af18c5Smrg  # Set umask so as not to create temps with too-generous modes.
22398af18c5Smrg  # However, 'strip' requires both read and write access to temps.
22498af18c5Smrg  case $mode in
22598af18c5Smrg    # Optimize common cases.
22698af18c5Smrg    *644) cp_umask=133;;
22798af18c5Smrg    *755) cp_umask=22;;
22898af18c5Smrg
22998af18c5Smrg    *[0-7])
23098af18c5Smrg      if test -z "$stripcmd"; then
231c1e3c5d0Smrg        u_plus_rw=
23298af18c5Smrg      else
233c1e3c5d0Smrg        u_plus_rw='% 200'
23498af18c5Smrg      fi
23598af18c5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23698af18c5Smrg    *)
23798af18c5Smrg      if test -z "$stripcmd"; then
238c1e3c5d0Smrg        u_plus_rw=
23998af18c5Smrg      else
240c1e3c5d0Smrg        u_plus_rw=,u+rw
24198af18c5Smrg      fi
24298af18c5Smrg      cp_umask=$mode$u_plus_rw;;
24398af18c5Smrg  esac
24498af18c5Smrgfi
24598af18c5Smrg
2466f03b1f6Smrgfor src
2476f03b1f6Smrgdo
24817caa701Smrg  # Protect names problematic for 'test' and other utilities.
2496f03b1f6Smrg  case $src in
25017caa701Smrg    -* | [=\(\)!]) src=./$src;;
2516f03b1f6Smrg  esac
2526f03b1f6Smrg
2536f03b1f6Smrg  if test -n "$dir_arg"; then
2546f03b1f6Smrg    dst=$src
25598af18c5Smrg    dstdir=$dst
25698af18c5Smrg    test -d "$dstdir"
25798af18c5Smrg    dstdir_status=$?
2586f03b1f6Smrg  else
25998af18c5Smrg
2606f03b1f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2616f03b1f6Smrg    # might cause directories to be created, which would be especially bad
2626f03b1f6Smrg    # if $src (and thus $dsttmp) contains '*'.
2636f03b1f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
2646f03b1f6Smrg      echo "$0: $src does not exist." >&2
2656f03b1f6Smrg      exit 1
2666f03b1f6Smrg    fi
2676f03b1f6Smrg
26898af18c5Smrg    if test -z "$dst_arg"; then
2696f03b1f6Smrg      echo "$0: no destination specified." >&2
2706f03b1f6Smrg      exit 1
2716f03b1f6Smrg    fi
27298af18c5Smrg    dst=$dst_arg
2736f03b1f6Smrg
2746f03b1f6Smrg    # If destination is a directory, append the input filename; won't work
2756f03b1f6Smrg    # if double slashes aren't ignored.
2766f03b1f6Smrg    if test -d "$dst"; then
277c1e3c5d0Smrg      if test "$is_target_a_directory" = never; then
278c1e3c5d0Smrg        echo "$0: $dst_arg: Is a directory" >&2
279c1e3c5d0Smrg        exit 1
2806f03b1f6Smrg      fi
28198af18c5Smrg      dstdir=$dst
28298af18c5Smrg      dst=$dstdir/`basename "$src"`
28398af18c5Smrg      dstdir_status=0
28498af18c5Smrg    else
285c1e3c5d0Smrg      dstdir=`dirname "$dst"`
28698af18c5Smrg      test -d "$dstdir"
28798af18c5Smrg      dstdir_status=$?
2886f03b1f6Smrg    fi
2896f03b1f6Smrg  fi
2906f03b1f6Smrg
29198af18c5Smrg  obsolete_mkdir_used=false
29298af18c5Smrg
29398af18c5Smrg  if test $dstdir_status != 0; then
29498af18c5Smrg    case $posix_mkdir in
29598af18c5Smrg      '')
296c1e3c5d0Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297c1e3c5d0Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298c1e3c5d0Smrg        umask=`umask`
299c1e3c5d0Smrg        case $stripcmd.$umask in
300c1e3c5d0Smrg          # Optimize common cases.
301c1e3c5d0Smrg          *[2367][2367]) mkdir_umask=$umask;;
302c1e3c5d0Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303c1e3c5d0Smrg
304c1e3c5d0Smrg          *[0-7])
305c1e3c5d0Smrg            mkdir_umask=`expr $umask + 22 \
306c1e3c5d0Smrg              - $umask % 100 % 40 + $umask % 20 \
307c1e3c5d0Smrg              - $umask % 10 % 4 + $umask % 2
308c1e3c5d0Smrg            `;;
309c1e3c5d0Smrg          *) mkdir_umask=$umask,go-w;;
310c1e3c5d0Smrg        esac
311c1e3c5d0Smrg
312c1e3c5d0Smrg        # With -d, create the new directory with the user-specified mode.
313c1e3c5d0Smrg        # Otherwise, rely on $mkdir_umask.
314c1e3c5d0Smrg        if test -n "$dir_arg"; then
315c1e3c5d0Smrg          mkdir_mode=-m$mode
316c1e3c5d0Smrg        else
317c1e3c5d0Smrg          mkdir_mode=
318c1e3c5d0Smrg        fi
319c1e3c5d0Smrg
320c1e3c5d0Smrg        posix_mkdir=false
321c1e3c5d0Smrg        case $umask in
322c1e3c5d0Smrg          *[123567][0-7][0-7])
323c1e3c5d0Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324c1e3c5d0Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325c1e3c5d0Smrg            ;;
326c1e3c5d0Smrg          *)
327c1e3c5d0Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328c1e3c5d0Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329c1e3c5d0Smrg
330c1e3c5d0Smrg            if (umask $mkdir_umask &&
331c1e3c5d0Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332c1e3c5d0Smrg            then
333c1e3c5d0Smrg              if test -z "$dir_arg" || {
334c1e3c5d0Smrg                   # Check for POSIX incompatibilities with -m.
335c1e3c5d0Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336c1e3c5d0Smrg                   # other-writable bit of parent directory when it shouldn't.
337c1e3c5d0Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338c1e3c5d0Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339c1e3c5d0Smrg                   case $ls_ld_tmpdir in
340c1e3c5d0Smrg                     d????-?r-*) different_mode=700;;
341c1e3c5d0Smrg                     d????-?--*) different_mode=755;;
342c1e3c5d0Smrg                     *) false;;
343c1e3c5d0Smrg                   esac &&
344c1e3c5d0Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345c1e3c5d0Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346c1e3c5d0Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347c1e3c5d0Smrg                   }
348c1e3c5d0Smrg                 }
349c1e3c5d0Smrg              then posix_mkdir=:
350c1e3c5d0Smrg              fi
351c1e3c5d0Smrg              rmdir "$tmpdir/d" "$tmpdir"
352c1e3c5d0Smrg            else
353c1e3c5d0Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354c1e3c5d0Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355c1e3c5d0Smrg            fi
356c1e3c5d0Smrg            trap '' 0;;
357c1e3c5d0Smrg        esac;;
35898af18c5Smrg    esac
3596f03b1f6Smrg
36098af18c5Smrg    if
36198af18c5Smrg      $posix_mkdir && (
362c1e3c5d0Smrg        umask $mkdir_umask &&
363c1e3c5d0Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
36498af18c5Smrg      )
36598af18c5Smrg    then :
36698af18c5Smrg    else
3676f03b1f6Smrg
36898af18c5Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
36998af18c5Smrg      # or it failed possibly due to a race condition.  Create the
37098af18c5Smrg      # directory the slow way, step by step, checking for races as we go.
3716f03b1f6Smrg
37298af18c5Smrg      case $dstdir in
373c1e3c5d0Smrg        /*) prefix='/';;
374c1e3c5d0Smrg        [-=\(\)!]*) prefix='./';;
375c1e3c5d0Smrg        *)  prefix='';;
37698af18c5Smrg      esac
3776f03b1f6Smrg
37898af18c5Smrg      oIFS=$IFS
37998af18c5Smrg      IFS=/
380c1e3c5d0Smrg      set -f
38198af18c5Smrg      set fnord $dstdir
3826f03b1f6Smrg      shift
383c1e3c5d0Smrg      set +f
38498af18c5Smrg      IFS=$oIFS
38598af18c5Smrg
38698af18c5Smrg      prefixes=
38798af18c5Smrg
38898af18c5Smrg      for d
38998af18c5Smrg      do
390c1e3c5d0Smrg        test X"$d" = X && continue
391c1e3c5d0Smrg
392c1e3c5d0Smrg        prefix=$prefix$d
393c1e3c5d0Smrg        if test -d "$prefix"; then
394c1e3c5d0Smrg          prefixes=
395c1e3c5d0Smrg        else
396c1e3c5d0Smrg          if $posix_mkdir; then
397c1e3c5d0Smrg            (umask=$mkdir_umask &&
398c1e3c5d0Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399c1e3c5d0Smrg            # Don't fail if two instances are running concurrently.
400c1e3c5d0Smrg            test -d "$prefix" || exit 1
401c1e3c5d0Smrg          else
402c1e3c5d0Smrg            case $prefix in
403c1e3c5d0Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404c1e3c5d0Smrg              *) qprefix=$prefix;;
405c1e3c5d0Smrg            esac
406c1e3c5d0Smrg            prefixes="$prefixes '$qprefix'"
407c1e3c5d0Smrg          fi
408c1e3c5d0Smrg        fi
409c1e3c5d0Smrg        prefix=$prefix/
41098af18c5Smrg      done
41198af18c5Smrg
41298af18c5Smrg      if test -n "$prefixes"; then
413c1e3c5d0Smrg        # Don't fail if two instances are running concurrently.
414c1e3c5d0Smrg        (umask $mkdir_umask &&
415c1e3c5d0Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416c1e3c5d0Smrg          test -d "$dstdir" || exit 1
417c1e3c5d0Smrg        obsolete_mkdir_used=true
4186f03b1f6Smrg      fi
41998af18c5Smrg    fi
4206f03b1f6Smrg  fi
4216f03b1f6Smrg
4226f03b1f6Smrg  if test -n "$dir_arg"; then
42398af18c5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
42498af18c5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
42598af18c5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
42698af18c5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4276f03b1f6Smrg  else
4286f03b1f6Smrg
4296f03b1f6Smrg    # Make a couple of temp file names in the proper directory.
4306f03b1f6Smrg    dsttmp=$dstdir/_inst.$$_
4316f03b1f6Smrg    rmtmp=$dstdir/_rm.$$_
4326f03b1f6Smrg
4336f03b1f6Smrg    # Trap to clean up those temp files at exit.
4346f03b1f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4356f03b1f6Smrg
4366f03b1f6Smrg    # Copy the file name to the temp name.
43798af18c5Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4386f03b1f6Smrg
4396f03b1f6Smrg    # and set any options; do chmod last to preserve setuid bits.
4406f03b1f6Smrg    #
4416f03b1f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
4426f03b1f6Smrg    # ignore errors from any of these, just make sure not to ignore
4436f03b1f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4446f03b1f6Smrg    #
44598af18c5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44698af18c5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44798af18c5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44898af18c5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44998af18c5Smrg
45098af18c5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
45198af18c5Smrg    if $copy_on_change &&
452c1e3c5d0Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453c1e3c5d0Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454c1e3c5d0Smrg       set -f &&
45598af18c5Smrg       set X $old && old=:$2:$4:$5:$6 &&
45698af18c5Smrg       set X $new && new=:$2:$4:$5:$6 &&
457c1e3c5d0Smrg       set +f &&
45898af18c5Smrg       test "$old" = "$new" &&
45998af18c5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46098af18c5Smrg    then
46198af18c5Smrg      rm -f "$dsttmp"
46298af18c5Smrg    else
46398af18c5Smrg      # Rename the file to the real destination.
46498af18c5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46598af18c5Smrg
46698af18c5Smrg      # The rename failed, perhaps because mv can't rename something else
46798af18c5Smrg      # to itself, or perhaps because mv is so ancient that it does not
46898af18c5Smrg      # support -f.
46998af18c5Smrg      {
470c1e3c5d0Smrg        # Now remove or move aside any old file at destination location.
471c1e3c5d0Smrg        # We try this two ways since rm can't unlink itself on some
472c1e3c5d0Smrg        # systems and the destination file might be busy for other
473c1e3c5d0Smrg        # reasons.  In this case, the final cleanup might fail but the new
474c1e3c5d0Smrg        # file should still install successfully.
475c1e3c5d0Smrg        {
476c1e3c5d0Smrg          test ! -f "$dst" ||
477c1e3c5d0Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478c1e3c5d0Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479c1e3c5d0Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480c1e3c5d0Smrg          } ||
481c1e3c5d0Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482c1e3c5d0Smrg            (exit 1); exit 1
483c1e3c5d0Smrg          }
484c1e3c5d0Smrg        } &&
485c1e3c5d0Smrg
486c1e3c5d0Smrg        # Now rename the file to the real destination.
487c1e3c5d0Smrg        $doit $mvcmd "$dsttmp" "$dst"
48898af18c5Smrg      }
48998af18c5Smrg    fi || exit 1
49098af18c5Smrg
49198af18c5Smrg    trap '' 0
49298af18c5Smrg  fi
4936f03b1f6Smrgdone
4946f03b1f6Smrg
4956f03b1f6Smrg# Local variables:
4966f03b1f6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4976f03b1f6Smrg# time-stamp-start: "scriptversion="
4986f03b1f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
49998af18c5Smrg# time-stamp-time-zone: "UTC"
50098af18c5Smrg# time-stamp-end: "; # UTC"
5016f03b1f6Smrg# End:
502