install-sh revision e8ac26b0
19fe995a9Smrg#!/bin/sh
29fe995a9Smrg# install - install a program, script, or datafile
39fe995a9Smrg
4e8ac26b0Smrgscriptversion=2013-12-25.23; # UTC
59fe995a9Smrg
69fe995a9Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79fe995a9Smrg# later released in X11R6 (xc/config/util/install.sh) with the
89fe995a9Smrg# following copyright and license.
99fe995a9Smrg#
109fe995a9Smrg# Copyright (C) 1994 X Consortium
119fe995a9Smrg#
129fe995a9Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139fe995a9Smrg# of this software and associated documentation files (the "Software"), to
149fe995a9Smrg# deal in the Software without restriction, including without limitation the
159fe995a9Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169fe995a9Smrg# sell copies of the Software, and to permit persons to whom the Software is
179fe995a9Smrg# furnished to do so, subject to the following conditions:
189fe995a9Smrg#
199fe995a9Smrg# The above copyright notice and this permission notice shall be included in
209fe995a9Smrg# all copies or substantial portions of the Software.
219fe995a9Smrg#
229fe995a9Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239fe995a9Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249fe995a9Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259fe995a9Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269fe995a9Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279fe995a9Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289fe995a9Smrg#
299fe995a9Smrg# Except as contained in this notice, the name of the X Consortium shall not
309fe995a9Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
319fe995a9Smrg# ings in this Software without prior written authorization from the X Consor-
329fe995a9Smrg# tium.
339fe995a9Smrg#
349fe995a9Smrg#
359fe995a9Smrg# FSF changes to this file are in the public domain.
369fe995a9Smrg#
379fe995a9Smrg# Calling this script install-sh is preferred over install.sh, to prevent
389d794632Smrg# 'make' implicit rules from creating a file called install from it
399fe995a9Smrg# when there is no Makefile.
409fe995a9Smrg#
419fe995a9Smrg# This script is compatible with the BSD install script, but was written
429fe995a9Smrg# from scratch.
439fe995a9Smrg
44e8ac26b0Smrgtab='	'
459fe995a9Smrgnl='
469fe995a9Smrg'
47e8ac26b0SmrgIFS=" $tab$nl"
489fe995a9Smrg
49e8ac26b0Smrg# Set DOITPROG to "echo" to test this script.
509fe995a9Smrg
51b62cc08cSmrgdoit=${DOITPROG-}
52e8ac26b0Smrgdoit_exec=${doit:-exec}
539fe995a9Smrg
549fe995a9Smrg# Put in absolute file names if you don't have them in your path;
559fe995a9Smrg# or use environment vars.
569fe995a9Smrg
57b62cc08cSmrgchgrpprog=${CHGRPPROG-chgrp}
58b62cc08cSmrgchmodprog=${CHMODPROG-chmod}
59b62cc08cSmrgchownprog=${CHOWNPROG-chown}
60b62cc08cSmrgcmpprog=${CMPPROG-cmp}
61b62cc08cSmrgcpprog=${CPPROG-cp}
62b62cc08cSmrgmkdirprog=${MKDIRPROG-mkdir}
63b62cc08cSmrgmvprog=${MVPROG-mv}
64b62cc08cSmrgrmprog=${RMPROG-rm}
65b62cc08cSmrgstripprog=${STRIPPROG-strip}
66b62cc08cSmrg
679fe995a9Smrgposix_mkdir=
689fe995a9Smrg
699fe995a9Smrg# Desired mode of installed file.
709fe995a9Smrgmode=0755
719fe995a9Smrg
72b62cc08cSmrgchgrpcmd=
739fe995a9Smrgchmodcmd=$chmodprog
749fe995a9Smrgchowncmd=
75b62cc08cSmrgmvcmd=$mvprog
769fe995a9Smrgrmcmd="$rmprog -f"
77b62cc08cSmrgstripcmd=
78b62cc08cSmrg
799fe995a9Smrgsrc=
809fe995a9Smrgdst=
819fe995a9Smrgdir_arg=
82b62cc08cSmrgdst_arg=
83b62cc08cSmrg
84b62cc08cSmrgcopy_on_change=false
85e8ac26b0Smrgis_target_a_directory=possibly
869fe995a9Smrg
87b62cc08cSmrgusage="\
88b62cc08cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
899fe995a9Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
909fe995a9Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
919fe995a9Smrg   or: $0 [OPTION]... -d DIRECTORIES...
929fe995a9Smrg
939fe995a9SmrgIn the 1st form, copy SRCFILE to DSTFILE.
949fe995a9SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
959fe995a9SmrgIn the 4th, create DIRECTORIES.
969fe995a9Smrg
979fe995a9SmrgOptions:
98b62cc08cSmrg     --help     display this help and exit.
99b62cc08cSmrg     --version  display version info and exit.
100b62cc08cSmrg
101b62cc08cSmrg  -c            (ignored)
102b62cc08cSmrg  -C            install only if different (preserve the last data modification time)
103b62cc08cSmrg  -d            create directories instead of installing files.
104b62cc08cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105b62cc08cSmrg  -m MODE       $chmodprog installed files to MODE.
106b62cc08cSmrg  -o USER       $chownprog installed files to USER.
107b62cc08cSmrg  -s            $stripprog installed files.
108b62cc08cSmrg  -t DIRECTORY  install into DIRECTORY.
109b62cc08cSmrg  -T            report an error if DSTFILE is a directory.
1109fe995a9Smrg
1119fe995a9SmrgEnvironment variables override the default commands:
112b62cc08cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113b62cc08cSmrg  RMPROG STRIPPROG
1149fe995a9Smrg"
1159fe995a9Smrg
1169fe995a9Smrgwhile test $# -ne 0; do
1179fe995a9Smrg  case $1 in
118b62cc08cSmrg    -c) ;;
119b62cc08cSmrg
120b62cc08cSmrg    -C) copy_on_change=true;;
1219fe995a9Smrg
122b62cc08cSmrg    -d) dir_arg=true;;
1239fe995a9Smrg
1249fe995a9Smrg    -g) chgrpcmd="$chgrpprog $2"
125e8ac26b0Smrg        shift;;
1269fe995a9Smrg
1279fe995a9Smrg    --help) echo "$usage"; exit $?;;
1289fe995a9Smrg
1299fe995a9Smrg    -m) mode=$2
130e8ac26b0Smrg        case $mode in
131e8ac26b0Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132e8ac26b0Smrg            echo "$0: invalid mode: $mode" >&2
133e8ac26b0Smrg            exit 1;;
134e8ac26b0Smrg        esac
135e8ac26b0Smrg        shift;;
1369fe995a9Smrg
1379fe995a9Smrg    -o) chowncmd="$chownprog $2"
138e8ac26b0Smrg        shift;;
1399fe995a9Smrg
140b62cc08cSmrg    -s) stripcmd=$stripprog;;
1419fe995a9Smrg
142e8ac26b0Smrg    -t)
143e8ac26b0Smrg        is_target_a_directory=always
144e8ac26b0Smrg        dst_arg=$2
145e8ac26b0Smrg        # Protect names problematic for 'test' and other utilities.
146e8ac26b0Smrg        case $dst_arg in
147e8ac26b0Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148e8ac26b0Smrg        esac
149e8ac26b0Smrg        shift;;
1509fe995a9Smrg
151e8ac26b0Smrg    -T) is_target_a_directory=never;;
1529fe995a9Smrg
1539fe995a9Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1549fe995a9Smrg
155e8ac26b0Smrg    --) shift
156e8ac26b0Smrg        break;;
1579fe995a9Smrg
158e8ac26b0Smrg    -*) echo "$0: invalid option: $1" >&2
159e8ac26b0Smrg        exit 1;;
1609fe995a9Smrg
1619fe995a9Smrg    *)  break;;
1629fe995a9Smrg  esac
163b62cc08cSmrg  shift
1649fe995a9Smrgdone
1659fe995a9Smrg
166e8ac26b0Smrg# We allow the use of options -d and -T together, by making -d
167e8ac26b0Smrg# take the precedence; this is for compatibility with GNU install.
168e8ac26b0Smrg
169e8ac26b0Smrgif test -n "$dir_arg"; then
170e8ac26b0Smrg  if test -n "$dst_arg"; then
171e8ac26b0Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172e8ac26b0Smrg    exit 1
173e8ac26b0Smrg  fi
174e8ac26b0Smrgfi
175e8ac26b0Smrg
176b62cc08cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1779fe995a9Smrg  # When -d is used, all remaining arguments are directories to create.
1789fe995a9Smrg  # When -t is used, the destination is already specified.
1799fe995a9Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1809fe995a9Smrg  for arg
1819fe995a9Smrg  do
182b62cc08cSmrg    if test -n "$dst_arg"; then
1839fe995a9Smrg      # $@ is not empty: it contains at least $arg.
184b62cc08cSmrg      set fnord "$@" "$dst_arg"
1859fe995a9Smrg      shift # fnord
1869fe995a9Smrg    fi
1879fe995a9Smrg    shift # arg
188b62cc08cSmrg    dst_arg=$arg
1899d794632Smrg    # Protect names problematic for 'test' and other utilities.
1909d794632Smrg    case $dst_arg in
1919d794632Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1929d794632Smrg    esac
1939fe995a9Smrg  done
1949fe995a9Smrgfi
1959fe995a9Smrg
1969fe995a9Smrgif test $# -eq 0; then
1979fe995a9Smrg  if test -z "$dir_arg"; then
1989fe995a9Smrg    echo "$0: no input file specified." >&2
1999fe995a9Smrg    exit 1
2009fe995a9Smrg  fi
2019d794632Smrg  # It's OK to call 'install-sh -d' without argument.
2029fe995a9Smrg  # This can happen when creating conditional directories.
2039fe995a9Smrg  exit 0
2049fe995a9Smrgfi
2059fe995a9Smrg
206e8ac26b0Smrgif test -z "$dir_arg"; then
207e8ac26b0Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208e8ac26b0Smrg    if test ! -d "$dst_arg"; then
209e8ac26b0Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210e8ac26b0Smrg      exit 1
211e8ac26b0Smrg    fi
212e8ac26b0Smrg  fi
213e8ac26b0Smrgfi
214e8ac26b0Smrg
2159fe995a9Smrgif test -z "$dir_arg"; then
2169d794632Smrg  do_exit='(exit $ret); exit $ret'
2179d794632Smrg  trap "ret=129; $do_exit" 1
2189d794632Smrg  trap "ret=130; $do_exit" 2
2199d794632Smrg  trap "ret=141; $do_exit" 13
2209d794632Smrg  trap "ret=143; $do_exit" 15
2219fe995a9Smrg
2229fe995a9Smrg  # Set umask so as not to create temps with too-generous modes.
2239fe995a9Smrg  # However, 'strip' requires both read and write access to temps.
2249fe995a9Smrg  case $mode in
2259fe995a9Smrg    # Optimize common cases.
2269fe995a9Smrg    *644) cp_umask=133;;
2279fe995a9Smrg    *755) cp_umask=22;;
2289fe995a9Smrg
2299fe995a9Smrg    *[0-7])
2309fe995a9Smrg      if test -z "$stripcmd"; then
231e8ac26b0Smrg        u_plus_rw=
2329fe995a9Smrg      else
233e8ac26b0Smrg        u_plus_rw='% 200'
2349fe995a9Smrg      fi
2359fe995a9Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2369fe995a9Smrg    *)
2379fe995a9Smrg      if test -z "$stripcmd"; then
238e8ac26b0Smrg        u_plus_rw=
2399fe995a9Smrg      else
240e8ac26b0Smrg        u_plus_rw=,u+rw
2419fe995a9Smrg      fi
2429fe995a9Smrg      cp_umask=$mode$u_plus_rw;;
2439fe995a9Smrg  esac
2449fe995a9Smrgfi
2459fe995a9Smrg
2469fe995a9Smrgfor src
2479fe995a9Smrgdo
2489d794632Smrg  # Protect names problematic for 'test' and other utilities.
2499fe995a9Smrg  case $src in
2509d794632Smrg    -* | [=\(\)!]) src=./$src;;
2519fe995a9Smrg  esac
2529fe995a9Smrg
2539fe995a9Smrg  if test -n "$dir_arg"; then
2549fe995a9Smrg    dst=$src
2559fe995a9Smrg    dstdir=$dst
2569fe995a9Smrg    test -d "$dstdir"
2579fe995a9Smrg    dstdir_status=$?
2589fe995a9Smrg  else
2599fe995a9Smrg
2609fe995a9Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2619fe995a9Smrg    # might cause directories to be created, which would be especially bad
2629fe995a9Smrg    # if $src (and thus $dsttmp) contains '*'.
2639fe995a9Smrg    if test ! -f "$src" && test ! -d "$src"; then
2649fe995a9Smrg      echo "$0: $src does not exist." >&2
2659fe995a9Smrg      exit 1
2669fe995a9Smrg    fi
2679fe995a9Smrg
268b62cc08cSmrg    if test -z "$dst_arg"; then
2699fe995a9Smrg      echo "$0: no destination specified." >&2
2709fe995a9Smrg      exit 1
2719fe995a9Smrg    fi
272b62cc08cSmrg    dst=$dst_arg
2739fe995a9Smrg
2749fe995a9Smrg    # If destination is a directory, append the input filename; won't work
2759fe995a9Smrg    # if double slashes aren't ignored.
2769fe995a9Smrg    if test -d "$dst"; then
277e8ac26b0Smrg      if test "$is_target_a_directory" = never; then
278e8ac26b0Smrg        echo "$0: $dst_arg: Is a directory" >&2
279e8ac26b0Smrg        exit 1
2809fe995a9Smrg      fi
2819fe995a9Smrg      dstdir=$dst
2829fe995a9Smrg      dst=$dstdir/`basename "$src"`
2839fe995a9Smrg      dstdir_status=0
2849fe995a9Smrg    else
285e8ac26b0Smrg      dstdir=`dirname "$dst"`
2869fe995a9Smrg      test -d "$dstdir"
2879fe995a9Smrg      dstdir_status=$?
2889fe995a9Smrg    fi
2899fe995a9Smrg  fi
2909fe995a9Smrg
2919fe995a9Smrg  obsolete_mkdir_used=false
2929fe995a9Smrg
2939fe995a9Smrg  if test $dstdir_status != 0; then
2949fe995a9Smrg    case $posix_mkdir in
2959fe995a9Smrg      '')
296e8ac26b0Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297e8ac26b0Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298e8ac26b0Smrg        umask=`umask`
299e8ac26b0Smrg        case $stripcmd.$umask in
300e8ac26b0Smrg          # Optimize common cases.
301e8ac26b0Smrg          *[2367][2367]) mkdir_umask=$umask;;
302e8ac26b0Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303e8ac26b0Smrg
304e8ac26b0Smrg          *[0-7])
305e8ac26b0Smrg            mkdir_umask=`expr $umask + 22 \
306e8ac26b0Smrg              - $umask % 100 % 40 + $umask % 20 \
307e8ac26b0Smrg              - $umask % 10 % 4 + $umask % 2
308e8ac26b0Smrg            `;;
309e8ac26b0Smrg          *) mkdir_umask=$umask,go-w;;
310e8ac26b0Smrg        esac
311e8ac26b0Smrg
312e8ac26b0Smrg        # With -d, create the new directory with the user-specified mode.
313e8ac26b0Smrg        # Otherwise, rely on $mkdir_umask.
314e8ac26b0Smrg        if test -n "$dir_arg"; then
315e8ac26b0Smrg          mkdir_mode=-m$mode
316e8ac26b0Smrg        else
317e8ac26b0Smrg          mkdir_mode=
318e8ac26b0Smrg        fi
319e8ac26b0Smrg
320e8ac26b0Smrg        posix_mkdir=false
321e8ac26b0Smrg        case $umask in
322e8ac26b0Smrg          *[123567][0-7][0-7])
323e8ac26b0Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324e8ac26b0Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325e8ac26b0Smrg            ;;
326e8ac26b0Smrg          *)
327e8ac26b0Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328e8ac26b0Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329e8ac26b0Smrg
330e8ac26b0Smrg            if (umask $mkdir_umask &&
331e8ac26b0Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332e8ac26b0Smrg            then
333e8ac26b0Smrg              if test -z "$dir_arg" || {
334e8ac26b0Smrg                   # Check for POSIX incompatibilities with -m.
335e8ac26b0Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336e8ac26b0Smrg                   # other-writable bit of parent directory when it shouldn't.
337e8ac26b0Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338e8ac26b0Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339e8ac26b0Smrg                   case $ls_ld_tmpdir in
340e8ac26b0Smrg                     d????-?r-*) different_mode=700;;
341e8ac26b0Smrg                     d????-?--*) different_mode=755;;
342e8ac26b0Smrg                     *) false;;
343e8ac26b0Smrg                   esac &&
344e8ac26b0Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345e8ac26b0Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346e8ac26b0Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347e8ac26b0Smrg                   }
348e8ac26b0Smrg                 }
349e8ac26b0Smrg              then posix_mkdir=:
350e8ac26b0Smrg              fi
351e8ac26b0Smrg              rmdir "$tmpdir/d" "$tmpdir"
352e8ac26b0Smrg            else
353e8ac26b0Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354e8ac26b0Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355e8ac26b0Smrg            fi
356e8ac26b0Smrg            trap '' 0;;
357e8ac26b0Smrg        esac;;
3589fe995a9Smrg    esac
3599fe995a9Smrg
3609fe995a9Smrg    if
3619fe995a9Smrg      $posix_mkdir && (
362e8ac26b0Smrg        umask $mkdir_umask &&
363e8ac26b0Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3649fe995a9Smrg      )
3659fe995a9Smrg    then :
3669fe995a9Smrg    else
3679fe995a9Smrg
3689fe995a9Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3699fe995a9Smrg      # or it failed possibly due to a race condition.  Create the
3709fe995a9Smrg      # directory the slow way, step by step, checking for races as we go.
3719fe995a9Smrg
3729fe995a9Smrg      case $dstdir in
373e8ac26b0Smrg        /*) prefix='/';;
374e8ac26b0Smrg        [-=\(\)!]*) prefix='./';;
375e8ac26b0Smrg        *)  prefix='';;
3769fe995a9Smrg      esac
3779fe995a9Smrg
3789fe995a9Smrg      oIFS=$IFS
3799fe995a9Smrg      IFS=/
380e8ac26b0Smrg      set -f
3819fe995a9Smrg      set fnord $dstdir
3829fe995a9Smrg      shift
383e8ac26b0Smrg      set +f
3849fe995a9Smrg      IFS=$oIFS
3859fe995a9Smrg
3869fe995a9Smrg      prefixes=
3879fe995a9Smrg
3889fe995a9Smrg      for d
3899fe995a9Smrg      do
390e8ac26b0Smrg        test X"$d" = X && continue
391e8ac26b0Smrg
392e8ac26b0Smrg        prefix=$prefix$d
393e8ac26b0Smrg        if test -d "$prefix"; then
394e8ac26b0Smrg          prefixes=
395e8ac26b0Smrg        else
396e8ac26b0Smrg          if $posix_mkdir; then
397e8ac26b0Smrg            (umask=$mkdir_umask &&
398e8ac26b0Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399e8ac26b0Smrg            # Don't fail if two instances are running concurrently.
400e8ac26b0Smrg            test -d "$prefix" || exit 1
401e8ac26b0Smrg          else
402e8ac26b0Smrg            case $prefix in
403e8ac26b0Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404e8ac26b0Smrg              *) qprefix=$prefix;;
405e8ac26b0Smrg            esac
406e8ac26b0Smrg            prefixes="$prefixes '$qprefix'"
407e8ac26b0Smrg          fi
408e8ac26b0Smrg        fi
409e8ac26b0Smrg        prefix=$prefix/
4109fe995a9Smrg      done
4119fe995a9Smrg
4129fe995a9Smrg      if test -n "$prefixes"; then
413e8ac26b0Smrg        # Don't fail if two instances are running concurrently.
414e8ac26b0Smrg        (umask $mkdir_umask &&
415e8ac26b0Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416e8ac26b0Smrg          test -d "$dstdir" || exit 1
417e8ac26b0Smrg        obsolete_mkdir_used=true
4189fe995a9Smrg      fi
4199fe995a9Smrg    fi
4209fe995a9Smrg  fi
4219fe995a9Smrg
4229fe995a9Smrg  if test -n "$dir_arg"; then
4239fe995a9Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4249fe995a9Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4259fe995a9Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4269fe995a9Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4279fe995a9Smrg  else
4289fe995a9Smrg
4299fe995a9Smrg    # Make a couple of temp file names in the proper directory.
4309fe995a9Smrg    dsttmp=$dstdir/_inst.$$_
4319fe995a9Smrg    rmtmp=$dstdir/_rm.$$_
4329fe995a9Smrg
4339fe995a9Smrg    # Trap to clean up those temp files at exit.
4349fe995a9Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4359fe995a9Smrg
4369fe995a9Smrg    # Copy the file name to the temp name.
4379fe995a9Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4389fe995a9Smrg
4399fe995a9Smrg    # and set any options; do chmod last to preserve setuid bits.
4409fe995a9Smrg    #
4419fe995a9Smrg    # If any of these fail, we abort the whole thing.  If we want to
4429fe995a9Smrg    # ignore errors from any of these, just make sure not to ignore
4439fe995a9Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4449fe995a9Smrg    #
445b62cc08cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446b62cc08cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447b62cc08cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448b62cc08cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449b62cc08cSmrg
450b62cc08cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
451b62cc08cSmrg    if $copy_on_change &&
452e8ac26b0Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453e8ac26b0Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454e8ac26b0Smrg       set -f &&
455b62cc08cSmrg       set X $old && old=:$2:$4:$5:$6 &&
456b62cc08cSmrg       set X $new && new=:$2:$4:$5:$6 &&
457e8ac26b0Smrg       set +f &&
458b62cc08cSmrg       test "$old" = "$new" &&
459b62cc08cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460b62cc08cSmrg    then
461b62cc08cSmrg      rm -f "$dsttmp"
462b62cc08cSmrg    else
463b62cc08cSmrg      # Rename the file to the real destination.
464b62cc08cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465b62cc08cSmrg
466b62cc08cSmrg      # The rename failed, perhaps because mv can't rename something else
467b62cc08cSmrg      # to itself, or perhaps because mv is so ancient that it does not
468b62cc08cSmrg      # support -f.
469b62cc08cSmrg      {
470e8ac26b0Smrg        # Now remove or move aside any old file at destination location.
471e8ac26b0Smrg        # We try this two ways since rm can't unlink itself on some
472e8ac26b0Smrg        # systems and the destination file might be busy for other
473e8ac26b0Smrg        # reasons.  In this case, the final cleanup might fail but the new
474e8ac26b0Smrg        # file should still install successfully.
475e8ac26b0Smrg        {
476e8ac26b0Smrg          test ! -f "$dst" ||
477e8ac26b0Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478e8ac26b0Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479e8ac26b0Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480e8ac26b0Smrg          } ||
481e8ac26b0Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482e8ac26b0Smrg            (exit 1); exit 1
483e8ac26b0Smrg          }
484e8ac26b0Smrg        } &&
485e8ac26b0Smrg
486e8ac26b0Smrg        # Now rename the file to the real destination.
487e8ac26b0Smrg        $doit $mvcmd "$dsttmp" "$dst"
488b62cc08cSmrg      }
489b62cc08cSmrg    fi || exit 1
4909fe995a9Smrg
4919fe995a9Smrg    trap '' 0
4929fe995a9Smrg  fi
4939fe995a9Smrgdone
4949fe995a9Smrg
4959fe995a9Smrg# Local variables:
4969fe995a9Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4979fe995a9Smrg# time-stamp-start: "scriptversion="
4989fe995a9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499b62cc08cSmrg# time-stamp-time-zone: "UTC"
500b62cc08cSmrg# time-stamp-end: "; # UTC"
5019fe995a9Smrg# End:
502