install-sh revision e6d2e958
1bd1da9d7Smrg#!/bin/sh
2bd1da9d7Smrg# install - install a program, script, or datafile
3bd1da9d7Smrg
4e6d2e958Smrgscriptversion=2013-12-25.23; # UTC
5bd1da9d7Smrg
6bd1da9d7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7bd1da9d7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8bd1da9d7Smrg# following copyright and license.
9bd1da9d7Smrg#
10bd1da9d7Smrg# Copyright (C) 1994 X Consortium
11bd1da9d7Smrg#
12bd1da9d7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13bd1da9d7Smrg# of this software and associated documentation files (the "Software"), to
14bd1da9d7Smrg# deal in the Software without restriction, including without limitation the
15bd1da9d7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16bd1da9d7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17bd1da9d7Smrg# furnished to do so, subject to the following conditions:
18bd1da9d7Smrg#
19bd1da9d7Smrg# The above copyright notice and this permission notice shall be included in
20bd1da9d7Smrg# all copies or substantial portions of the Software.
21bd1da9d7Smrg#
22bd1da9d7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23bd1da9d7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24bd1da9d7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25bd1da9d7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26bd1da9d7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27bd1da9d7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28bd1da9d7Smrg#
29bd1da9d7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30bd1da9d7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31bd1da9d7Smrg# ings in this Software without prior written authorization from the X Consor-
32bd1da9d7Smrg# tium.
33bd1da9d7Smrg#
34bd1da9d7Smrg#
35bd1da9d7Smrg# FSF changes to this file are in the public domain.
36bd1da9d7Smrg#
37bd1da9d7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38899129b3Smrg# 'make' implicit rules from creating a file called install from it
39bd1da9d7Smrg# when there is no Makefile.
40bd1da9d7Smrg#
41bd1da9d7Smrg# This script is compatible with the BSD install script, but was written
425bcb6992Smrg# from scratch.
435bcb6992Smrg
44e6d2e958Smrgtab='	'
455bcb6992Smrgnl='
465bcb6992Smrg'
47e6d2e958SmrgIFS=" $tab$nl"
48bd1da9d7Smrg
49e6d2e958Smrg# Set DOITPROG to "echo" to test this script.
50bd1da9d7Smrg
515bcb6992Smrgdoit=${DOITPROG-}
52e6d2e958Smrgdoit_exec=${doit:-exec}
53bd1da9d7Smrg
545bcb6992Smrg# Put in absolute file names if you don't have them in your path;
555bcb6992Smrg# or use environment vars.
565bcb6992Smrg
575bcb6992Smrgchgrpprog=${CHGRPPROG-chgrp}
585bcb6992Smrgchmodprog=${CHMODPROG-chmod}
595bcb6992Smrgchownprog=${CHOWNPROG-chown}
605bcb6992Smrgcmpprog=${CMPPROG-cmp}
615bcb6992Smrgcpprog=${CPPROG-cp}
625bcb6992Smrgmkdirprog=${MKDIRPROG-mkdir}
635bcb6992Smrgmvprog=${MVPROG-mv}
645bcb6992Smrgrmprog=${RMPROG-rm}
655bcb6992Smrgstripprog=${STRIPPROG-strip}
665bcb6992Smrg
675bcb6992Smrgposix_mkdir=
685bcb6992Smrg
695bcb6992Smrg# Desired mode of installed file.
705bcb6992Smrgmode=0755
71bd1da9d7Smrg
72bd1da9d7Smrgchgrpcmd=
735bcb6992Smrgchmodcmd=$chmodprog
745bcb6992Smrgchowncmd=
755bcb6992Smrgmvcmd=$mvprog
76bd1da9d7Smrgrmcmd="$rmprog -f"
775bcb6992Smrgstripcmd=
785bcb6992Smrg
79bd1da9d7Smrgsrc=
80bd1da9d7Smrgdst=
81bd1da9d7Smrgdir_arg=
825bcb6992Smrgdst_arg=
835bcb6992Smrg
845bcb6992Smrgcopy_on_change=false
85e6d2e958Smrgis_target_a_directory=possibly
86bd1da9d7Smrg
875bcb6992Smrgusage="\
885bcb6992SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89bd1da9d7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90bd1da9d7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91bd1da9d7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92bd1da9d7Smrg
93bd1da9d7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94bd1da9d7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95bd1da9d7SmrgIn the 4th, create DIRECTORIES.
96bd1da9d7Smrg
97bd1da9d7SmrgOptions:
985bcb6992Smrg     --help     display this help and exit.
995bcb6992Smrg     --version  display version info and exit.
1005bcb6992Smrg
1015bcb6992Smrg  -c            (ignored)
1025bcb6992Smrg  -C            install only if different (preserve the last data modification time)
1035bcb6992Smrg  -d            create directories instead of installing files.
1045bcb6992Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1055bcb6992Smrg  -m MODE       $chmodprog installed files to MODE.
1065bcb6992Smrg  -o USER       $chownprog installed files to USER.
1075bcb6992Smrg  -s            $stripprog installed files.
1085bcb6992Smrg  -t DIRECTORY  install into DIRECTORY.
1095bcb6992Smrg  -T            report an error if DSTFILE is a directory.
110bd1da9d7Smrg
111bd1da9d7SmrgEnvironment variables override the default commands:
1125bcb6992Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1135bcb6992Smrg  RMPROG STRIPPROG
114bd1da9d7Smrg"
115bd1da9d7Smrg
1165bcb6992Smrgwhile test $# -ne 0; do
117bd1da9d7Smrg  case $1 in
1185bcb6992Smrg    -c) ;;
1195bcb6992Smrg
1205bcb6992Smrg    -C) copy_on_change=true;;
121bd1da9d7Smrg
1225bcb6992Smrg    -d) dir_arg=true;;
123bd1da9d7Smrg
124bd1da9d7Smrg    -g) chgrpcmd="$chgrpprog $2"
125e6d2e958Smrg        shift;;
126bd1da9d7Smrg
127bd1da9d7Smrg    --help) echo "$usage"; exit $?;;
128bd1da9d7Smrg
1295bcb6992Smrg    -m) mode=$2
130e6d2e958Smrg        case $mode in
131e6d2e958Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132e6d2e958Smrg            echo "$0: invalid mode: $mode" >&2
133e6d2e958Smrg            exit 1;;
134e6d2e958Smrg        esac
135e6d2e958Smrg        shift;;
136bd1da9d7Smrg
137bd1da9d7Smrg    -o) chowncmd="$chownprog $2"
138e6d2e958Smrg        shift;;
139bd1da9d7Smrg
1405bcb6992Smrg    -s) stripcmd=$stripprog;;
141bd1da9d7Smrg
142e6d2e958Smrg    -t)
143e6d2e958Smrg        is_target_a_directory=always
144e6d2e958Smrg        dst_arg=$2
145e6d2e958Smrg        # Protect names problematic for 'test' and other utilities.
146e6d2e958Smrg        case $dst_arg in
147e6d2e958Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148e6d2e958Smrg        esac
149e6d2e958Smrg        shift;;
150bd1da9d7Smrg
151e6d2e958Smrg    -T) is_target_a_directory=never;;
152bd1da9d7Smrg
153bd1da9d7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154bd1da9d7Smrg
155e6d2e958Smrg    --) shift
156e6d2e958Smrg        break;;
1575bcb6992Smrg
158e6d2e958Smrg    -*) echo "$0: invalid option: $1" >&2
159e6d2e958Smrg        exit 1;;
1605bcb6992Smrg
1615bcb6992Smrg    *)  break;;
162bd1da9d7Smrg  esac
1635bcb6992Smrg  shift
164bd1da9d7Smrgdone
165bd1da9d7Smrg
166e6d2e958Smrg# We allow the use of options -d and -T together, by making -d
167e6d2e958Smrg# take the precedence; this is for compatibility with GNU install.
168e6d2e958Smrg
169e6d2e958Smrgif test -n "$dir_arg"; then
170e6d2e958Smrg  if test -n "$dst_arg"; then
171e6d2e958Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172e6d2e958Smrg    exit 1
173e6d2e958Smrg  fi
174e6d2e958Smrgfi
175e6d2e958Smrg
1765bcb6992Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1775bcb6992Smrg  # When -d is used, all remaining arguments are directories to create.
1785bcb6992Smrg  # When -t is used, the destination is already specified.
1795bcb6992Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1805bcb6992Smrg  for arg
1815bcb6992Smrg  do
1825bcb6992Smrg    if test -n "$dst_arg"; then
1835bcb6992Smrg      # $@ is not empty: it contains at least $arg.
1845bcb6992Smrg      set fnord "$@" "$dst_arg"
1855bcb6992Smrg      shift # fnord
1865bcb6992Smrg    fi
1875bcb6992Smrg    shift # arg
1885bcb6992Smrg    dst_arg=$arg
189899129b3Smrg    # Protect names problematic for 'test' and other utilities.
190899129b3Smrg    case $dst_arg in
191899129b3Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192899129b3Smrg    esac
1935bcb6992Smrg  done
1945bcb6992Smrgfi
1955bcb6992Smrg
1965bcb6992Smrgif test $# -eq 0; then
197bd1da9d7Smrg  if test -z "$dir_arg"; then
198bd1da9d7Smrg    echo "$0: no input file specified." >&2
199bd1da9d7Smrg    exit 1
200bd1da9d7Smrg  fi
201899129b3Smrg  # It's OK to call 'install-sh -d' without argument.
202bd1da9d7Smrg  # This can happen when creating conditional directories.
203bd1da9d7Smrg  exit 0
204bd1da9d7Smrgfi
205bd1da9d7Smrg
206e6d2e958Smrgif test -z "$dir_arg"; then
207e6d2e958Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208e6d2e958Smrg    if test ! -d "$dst_arg"; then
209e6d2e958Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210e6d2e958Smrg      exit 1
211e6d2e958Smrg    fi
212e6d2e958Smrg  fi
213e6d2e958Smrgfi
214e6d2e958Smrg
2155bcb6992Smrgif test -z "$dir_arg"; then
216899129b3Smrg  do_exit='(exit $ret); exit $ret'
217899129b3Smrg  trap "ret=129; $do_exit" 1
218899129b3Smrg  trap "ret=130; $do_exit" 2
219899129b3Smrg  trap "ret=141; $do_exit" 13
220899129b3Smrg  trap "ret=143; $do_exit" 15
2215bcb6992Smrg
2225bcb6992Smrg  # Set umask so as not to create temps with too-generous modes.
2235bcb6992Smrg  # However, 'strip' requires both read and write access to temps.
2245bcb6992Smrg  case $mode in
2255bcb6992Smrg    # Optimize common cases.
2265bcb6992Smrg    *644) cp_umask=133;;
2275bcb6992Smrg    *755) cp_umask=22;;
2285bcb6992Smrg
2295bcb6992Smrg    *[0-7])
2305bcb6992Smrg      if test -z "$stripcmd"; then
231e6d2e958Smrg        u_plus_rw=
2325bcb6992Smrg      else
233e6d2e958Smrg        u_plus_rw='% 200'
2345bcb6992Smrg      fi
2355bcb6992Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2365bcb6992Smrg    *)
2375bcb6992Smrg      if test -z "$stripcmd"; then
238e6d2e958Smrg        u_plus_rw=
2395bcb6992Smrg      else
240e6d2e958Smrg        u_plus_rw=,u+rw
2415bcb6992Smrg      fi
2425bcb6992Smrg      cp_umask=$mode$u_plus_rw;;
2435bcb6992Smrg  esac
2445bcb6992Smrgfi
2455bcb6992Smrg
246bd1da9d7Smrgfor src
247bd1da9d7Smrgdo
248899129b3Smrg  # Protect names problematic for 'test' and other utilities.
249bd1da9d7Smrg  case $src in
250899129b3Smrg    -* | [=\(\)!]) src=./$src;;
251bd1da9d7Smrg  esac
252bd1da9d7Smrg
253bd1da9d7Smrg  if test -n "$dir_arg"; then
254bd1da9d7Smrg    dst=$src
2555bcb6992Smrg    dstdir=$dst
2565bcb6992Smrg    test -d "$dstdir"
2575bcb6992Smrg    dstdir_status=$?
258bd1da9d7Smrg  else
2595bcb6992Smrg
260bd1da9d7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261bd1da9d7Smrg    # might cause directories to be created, which would be especially bad
262bd1da9d7Smrg    # if $src (and thus $dsttmp) contains '*'.
263bd1da9d7Smrg    if test ! -f "$src" && test ! -d "$src"; then
264bd1da9d7Smrg      echo "$0: $src does not exist." >&2
265bd1da9d7Smrg      exit 1
266bd1da9d7Smrg    fi
267bd1da9d7Smrg
2685bcb6992Smrg    if test -z "$dst_arg"; then
269bd1da9d7Smrg      echo "$0: no destination specified." >&2
270bd1da9d7Smrg      exit 1
271bd1da9d7Smrg    fi
2725bcb6992Smrg    dst=$dst_arg
273bd1da9d7Smrg
274bd1da9d7Smrg    # If destination is a directory, append the input filename; won't work
275bd1da9d7Smrg    # if double slashes aren't ignored.
276bd1da9d7Smrg    if test -d "$dst"; then
277e6d2e958Smrg      if test "$is_target_a_directory" = never; then
278e6d2e958Smrg        echo "$0: $dst_arg: Is a directory" >&2
279e6d2e958Smrg        exit 1
280bd1da9d7Smrg      fi
2815bcb6992Smrg      dstdir=$dst
2825bcb6992Smrg      dst=$dstdir/`basename "$src"`
2835bcb6992Smrg      dstdir_status=0
2845bcb6992Smrg    else
285e6d2e958Smrg      dstdir=`dirname "$dst"`
2865bcb6992Smrg      test -d "$dstdir"
2875bcb6992Smrg      dstdir_status=$?
288bd1da9d7Smrg    fi
289bd1da9d7Smrg  fi
290bd1da9d7Smrg
2915bcb6992Smrg  obsolete_mkdir_used=false
2925bcb6992Smrg
2935bcb6992Smrg  if test $dstdir_status != 0; then
2945bcb6992Smrg    case $posix_mkdir in
2955bcb6992Smrg      '')
296e6d2e958Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297e6d2e958Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298e6d2e958Smrg        umask=`umask`
299e6d2e958Smrg        case $stripcmd.$umask in
300e6d2e958Smrg          # Optimize common cases.
301e6d2e958Smrg          *[2367][2367]) mkdir_umask=$umask;;
302e6d2e958Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303e6d2e958Smrg
304e6d2e958Smrg          *[0-7])
305e6d2e958Smrg            mkdir_umask=`expr $umask + 22 \
306e6d2e958Smrg              - $umask % 100 % 40 + $umask % 20 \
307e6d2e958Smrg              - $umask % 10 % 4 + $umask % 2
308e6d2e958Smrg            `;;
309e6d2e958Smrg          *) mkdir_umask=$umask,go-w;;
310e6d2e958Smrg        esac
311e6d2e958Smrg
312e6d2e958Smrg        # With -d, create the new directory with the user-specified mode.
313e6d2e958Smrg        # Otherwise, rely on $mkdir_umask.
314e6d2e958Smrg        if test -n "$dir_arg"; then
315e6d2e958Smrg          mkdir_mode=-m$mode
316e6d2e958Smrg        else
317e6d2e958Smrg          mkdir_mode=
318e6d2e958Smrg        fi
319e6d2e958Smrg
320e6d2e958Smrg        posix_mkdir=false
321e6d2e958Smrg        case $umask in
322e6d2e958Smrg          *[123567][0-7][0-7])
323e6d2e958Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324e6d2e958Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325e6d2e958Smrg            ;;
326e6d2e958Smrg          *)
327e6d2e958Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328e6d2e958Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329e6d2e958Smrg
330e6d2e958Smrg            if (umask $mkdir_umask &&
331e6d2e958Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332e6d2e958Smrg            then
333e6d2e958Smrg              if test -z "$dir_arg" || {
334e6d2e958Smrg                   # Check for POSIX incompatibilities with -m.
335e6d2e958Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336e6d2e958Smrg                   # other-writable bit of parent directory when it shouldn't.
337e6d2e958Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338e6d2e958Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339e6d2e958Smrg                   case $ls_ld_tmpdir in
340e6d2e958Smrg                     d????-?r-*) different_mode=700;;
341e6d2e958Smrg                     d????-?--*) different_mode=755;;
342e6d2e958Smrg                     *) false;;
343e6d2e958Smrg                   esac &&
344e6d2e958Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345e6d2e958Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346e6d2e958Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347e6d2e958Smrg                   }
348e6d2e958Smrg                 }
349e6d2e958Smrg              then posix_mkdir=:
350e6d2e958Smrg              fi
351e6d2e958Smrg              rmdir "$tmpdir/d" "$tmpdir"
352e6d2e958Smrg            else
353e6d2e958Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354e6d2e958Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355e6d2e958Smrg            fi
356e6d2e958Smrg            trap '' 0;;
357e6d2e958Smrg        esac;;
3585bcb6992Smrg    esac
359bd1da9d7Smrg
3605bcb6992Smrg    if
3615bcb6992Smrg      $posix_mkdir && (
362e6d2e958Smrg        umask $mkdir_umask &&
363e6d2e958Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3645bcb6992Smrg      )
3655bcb6992Smrg    then :
3665bcb6992Smrg    else
367bd1da9d7Smrg
3685bcb6992Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3695bcb6992Smrg      # or it failed possibly due to a race condition.  Create the
3705bcb6992Smrg      # directory the slow way, step by step, checking for races as we go.
371bd1da9d7Smrg
3725bcb6992Smrg      case $dstdir in
373e6d2e958Smrg        /*) prefix='/';;
374e6d2e958Smrg        [-=\(\)!]*) prefix='./';;
375e6d2e958Smrg        *)  prefix='';;
3765bcb6992Smrg      esac
377bd1da9d7Smrg
3785bcb6992Smrg      oIFS=$IFS
3795bcb6992Smrg      IFS=/
380e6d2e958Smrg      set -f
3815bcb6992Smrg      set fnord $dstdir
382bd1da9d7Smrg      shift
383e6d2e958Smrg      set +f
3845bcb6992Smrg      IFS=$oIFS
3855bcb6992Smrg
3865bcb6992Smrg      prefixes=
3875bcb6992Smrg
3885bcb6992Smrg      for d
3895bcb6992Smrg      do
390e6d2e958Smrg        test X"$d" = X && continue
391e6d2e958Smrg
392e6d2e958Smrg        prefix=$prefix$d
393e6d2e958Smrg        if test -d "$prefix"; then
394e6d2e958Smrg          prefixes=
395e6d2e958Smrg        else
396e6d2e958Smrg          if $posix_mkdir; then
397e6d2e958Smrg            (umask=$mkdir_umask &&
398e6d2e958Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399e6d2e958Smrg            # Don't fail if two instances are running concurrently.
400e6d2e958Smrg            test -d "$prefix" || exit 1
401e6d2e958Smrg          else
402e6d2e958Smrg            case $prefix in
403e6d2e958Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404e6d2e958Smrg              *) qprefix=$prefix;;
405e6d2e958Smrg            esac
406e6d2e958Smrg            prefixes="$prefixes '$qprefix'"
407e6d2e958Smrg          fi
408e6d2e958Smrg        fi
409e6d2e958Smrg        prefix=$prefix/
4105bcb6992Smrg      done
4115bcb6992Smrg
4125bcb6992Smrg      if test -n "$prefixes"; then
413e6d2e958Smrg        # Don't fail if two instances are running concurrently.
414e6d2e958Smrg        (umask $mkdir_umask &&
415e6d2e958Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416e6d2e958Smrg          test -d "$dstdir" || exit 1
417e6d2e958Smrg        obsolete_mkdir_used=true
418bd1da9d7Smrg      fi
4195bcb6992Smrg    fi
420bd1da9d7Smrg  fi
421bd1da9d7Smrg
422bd1da9d7Smrg  if test -n "$dir_arg"; then
4235bcb6992Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4245bcb6992Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4255bcb6992Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4265bcb6992Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427bd1da9d7Smrg  else
428bd1da9d7Smrg
429bd1da9d7Smrg    # Make a couple of temp file names in the proper directory.
430bd1da9d7Smrg    dsttmp=$dstdir/_inst.$$_
431bd1da9d7Smrg    rmtmp=$dstdir/_rm.$$_
432bd1da9d7Smrg
433bd1da9d7Smrg    # Trap to clean up those temp files at exit.
434bd1da9d7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435bd1da9d7Smrg
436bd1da9d7Smrg    # Copy the file name to the temp name.
4375bcb6992Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438bd1da9d7Smrg
439bd1da9d7Smrg    # and set any options; do chmod last to preserve setuid bits.
440bd1da9d7Smrg    #
441bd1da9d7Smrg    # If any of these fail, we abort the whole thing.  If we want to
442bd1da9d7Smrg    # ignore errors from any of these, just make sure not to ignore
443bd1da9d7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444bd1da9d7Smrg    #
4455bcb6992Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4465bcb6992Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4475bcb6992Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4485bcb6992Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4495bcb6992Smrg
4505bcb6992Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4515bcb6992Smrg    if $copy_on_change &&
452e6d2e958Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453e6d2e958Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454e6d2e958Smrg       set -f &&
4555bcb6992Smrg       set X $old && old=:$2:$4:$5:$6 &&
4565bcb6992Smrg       set X $new && new=:$2:$4:$5:$6 &&
457e6d2e958Smrg       set +f &&
4585bcb6992Smrg       test "$old" = "$new" &&
4595bcb6992Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4605bcb6992Smrg    then
4615bcb6992Smrg      rm -f "$dsttmp"
4625bcb6992Smrg    else
4635bcb6992Smrg      # Rename the file to the real destination.
4645bcb6992Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4655bcb6992Smrg
4665bcb6992Smrg      # The rename failed, perhaps because mv can't rename something else
4675bcb6992Smrg      # to itself, or perhaps because mv is so ancient that it does not
4685bcb6992Smrg      # support -f.
4695bcb6992Smrg      {
470e6d2e958Smrg        # Now remove or move aside any old file at destination location.
471e6d2e958Smrg        # We try this two ways since rm can't unlink itself on some
472e6d2e958Smrg        # systems and the destination file might be busy for other
473e6d2e958Smrg        # reasons.  In this case, the final cleanup might fail but the new
474e6d2e958Smrg        # file should still install successfully.
475e6d2e958Smrg        {
476e6d2e958Smrg          test ! -f "$dst" ||
477e6d2e958Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478e6d2e958Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479e6d2e958Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480e6d2e958Smrg          } ||
481e6d2e958Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482e6d2e958Smrg            (exit 1); exit 1
483e6d2e958Smrg          }
484e6d2e958Smrg        } &&
485e6d2e958Smrg
486e6d2e958Smrg        # Now rename the file to the real destination.
487e6d2e958Smrg        $doit $mvcmd "$dsttmp" "$dst"
4885bcb6992Smrg      }
4895bcb6992Smrg    fi || exit 1
4905bcb6992Smrg
4915bcb6992Smrg    trap '' 0
4925bcb6992Smrg  fi
493bd1da9d7Smrgdone
494bd1da9d7Smrg
495bd1da9d7Smrg# Local variables:
496bd1da9d7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497bd1da9d7Smrg# time-stamp-start: "scriptversion="
498bd1da9d7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4995bcb6992Smrg# time-stamp-time-zone: "UTC"
5005bcb6992Smrg# time-stamp-end: "; # UTC"
501bd1da9d7Smrg# End:
502