install-sh revision 5ec34c4c
17a84e134Smrg#!/bin/sh
27a84e134Smrg# install - install a program, script, or datafile
37a84e134Smrg
45ec34c4cSmrgscriptversion=2018-03-11.20; # UTC
57a84e134Smrg
67a84e134Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a84e134Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a84e134Smrg# following copyright and license.
97a84e134Smrg#
107a84e134Smrg# Copyright (C) 1994 X Consortium
117a84e134Smrg#
127a84e134Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a84e134Smrg# of this software and associated documentation files (the "Software"), to
147a84e134Smrg# deal in the Software without restriction, including without limitation the
157a84e134Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a84e134Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a84e134Smrg# furnished to do so, subject to the following conditions:
187a84e134Smrg#
197a84e134Smrg# The above copyright notice and this permission notice shall be included in
207a84e134Smrg# all copies or substantial portions of the Software.
217a84e134Smrg#
227a84e134Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a84e134Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a84e134Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a84e134Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a84e134Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a84e134Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a84e134Smrg#
297a84e134Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a84e134Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a84e134Smrg# ings in this Software without prior written authorization from the X Consor-
327a84e134Smrg# tium.
337a84e134Smrg#
347a84e134Smrg#
357a84e134Smrg# FSF changes to this file are in the public domain.
367a84e134Smrg#
377a84e134Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c889a3bfSmrg# 'make' implicit rules from creating a file called install from it
397a84e134Smrg# when there is no Makefile.
407a84e134Smrg#
417a84e134Smrg# This script is compatible with the BSD install script, but was written
427a84e134Smrg# from scratch.
437a84e134Smrg
44c8571806Smrgtab='	'
457a84e134Smrgnl='
467a84e134Smrg'
47c8571806SmrgIFS=" $tab$nl"
487a84e134Smrg
49c8571806Smrg# Set DOITPROG to "echo" to test this script.
507a84e134Smrg
51ab902922Smrgdoit=${DOITPROG-}
52c8571806Smrgdoit_exec=${doit:-exec}
537a84e134Smrg
547a84e134Smrg# Put in absolute file names if you don't have them in your path;
557a84e134Smrg# or use environment vars.
567a84e134Smrg
57ab902922Smrgchgrpprog=${CHGRPPROG-chgrp}
58ab902922Smrgchmodprog=${CHMODPROG-chmod}
59ab902922Smrgchownprog=${CHOWNPROG-chown}
60ab902922Smrgcmpprog=${CMPPROG-cmp}
61ab902922Smrgcpprog=${CPPROG-cp}
62ab902922Smrgmkdirprog=${MKDIRPROG-mkdir}
63ab902922Smrgmvprog=${MVPROG-mv}
64ab902922Smrgrmprog=${RMPROG-rm}
65ab902922Smrgstripprog=${STRIPPROG-strip}
66ab902922Smrg
677a84e134Smrgposix_mkdir=
687a84e134Smrg
697a84e134Smrg# Desired mode of installed file.
707a84e134Smrgmode=0755
717a84e134Smrg
72ab902922Smrgchgrpcmd=
737a84e134Smrgchmodcmd=$chmodprog
747a84e134Smrgchowncmd=
75ab902922Smrgmvcmd=$mvprog
767a84e134Smrgrmcmd="$rmprog -f"
77ab902922Smrgstripcmd=
78ab902922Smrg
797a84e134Smrgsrc=
807a84e134Smrgdst=
817a84e134Smrgdir_arg=
82ab902922Smrgdst_arg=
83ab902922Smrg
84ab902922Smrgcopy_on_change=false
85c8571806Smrgis_target_a_directory=possibly
867a84e134Smrg
87ab902922Smrgusage="\
88ab902922SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
897a84e134Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
907a84e134Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
917a84e134Smrg   or: $0 [OPTION]... -d DIRECTORIES...
927a84e134Smrg
937a84e134SmrgIn the 1st form, copy SRCFILE to DSTFILE.
947a84e134SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
957a84e134SmrgIn the 4th, create DIRECTORIES.
967a84e134Smrg
977a84e134SmrgOptions:
98ab902922Smrg     --help     display this help and exit.
99ab902922Smrg     --version  display version info and exit.
100ab902922Smrg
101ab902922Smrg  -c            (ignored)
102ab902922Smrg  -C            install only if different (preserve the last data modification time)
103ab902922Smrg  -d            create directories instead of installing files.
104ab902922Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105ab902922Smrg  -m MODE       $chmodprog installed files to MODE.
106ab902922Smrg  -o USER       $chownprog installed files to USER.
107ab902922Smrg  -s            $stripprog installed files.
108ab902922Smrg  -t DIRECTORY  install into DIRECTORY.
109ab902922Smrg  -T            report an error if DSTFILE is a directory.
1107a84e134Smrg
1117a84e134SmrgEnvironment variables override the default commands:
112ab902922Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113ab902922Smrg  RMPROG STRIPPROG
1147a84e134Smrg"
1157a84e134Smrg
1167a84e134Smrgwhile test $# -ne 0; do
1177a84e134Smrg  case $1 in
118ab902922Smrg    -c) ;;
119ab902922Smrg
120ab902922Smrg    -C) copy_on_change=true;;
1217a84e134Smrg
122ab902922Smrg    -d) dir_arg=true;;
1237a84e134Smrg
1247a84e134Smrg    -g) chgrpcmd="$chgrpprog $2"
125c8571806Smrg        shift;;
1267a84e134Smrg
1277a84e134Smrg    --help) echo "$usage"; exit $?;;
1287a84e134Smrg
1297a84e134Smrg    -m) mode=$2
130c8571806Smrg        case $mode in
131c8571806Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132c8571806Smrg            echo "$0: invalid mode: $mode" >&2
133c8571806Smrg            exit 1;;
134c8571806Smrg        esac
135c8571806Smrg        shift;;
1367a84e134Smrg
1377a84e134Smrg    -o) chowncmd="$chownprog $2"
138c8571806Smrg        shift;;
1397a84e134Smrg
140ab902922Smrg    -s) stripcmd=$stripprog;;
1417a84e134Smrg
142c8571806Smrg    -t)
143c8571806Smrg        is_target_a_directory=always
144c8571806Smrg        dst_arg=$2
145c8571806Smrg        # Protect names problematic for 'test' and other utilities.
146c8571806Smrg        case $dst_arg in
147c8571806Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148c8571806Smrg        esac
149c8571806Smrg        shift;;
1507a84e134Smrg
151c8571806Smrg    -T) is_target_a_directory=never;;
1527a84e134Smrg
1537a84e134Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1547a84e134Smrg
155c8571806Smrg    --) shift
156c8571806Smrg        break;;
1577a84e134Smrg
158c8571806Smrg    -*) echo "$0: invalid option: $1" >&2
159c8571806Smrg        exit 1;;
1607a84e134Smrg
1617a84e134Smrg    *)  break;;
1627a84e134Smrg  esac
163ab902922Smrg  shift
1647a84e134Smrgdone
1657a84e134Smrg
166c8571806Smrg# We allow the use of options -d and -T together, by making -d
167c8571806Smrg# take the precedence; this is for compatibility with GNU install.
168c8571806Smrg
169c8571806Smrgif test -n "$dir_arg"; then
170c8571806Smrg  if test -n "$dst_arg"; then
171c8571806Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172c8571806Smrg    exit 1
173c8571806Smrg  fi
174c8571806Smrgfi
175c8571806Smrg
176ab902922Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777a84e134Smrg  # When -d is used, all remaining arguments are directories to create.
1787a84e134Smrg  # When -t is used, the destination is already specified.
1797a84e134Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807a84e134Smrg  for arg
1817a84e134Smrg  do
182ab902922Smrg    if test -n "$dst_arg"; then
1837a84e134Smrg      # $@ is not empty: it contains at least $arg.
184ab902922Smrg      set fnord "$@" "$dst_arg"
1857a84e134Smrg      shift # fnord
1867a84e134Smrg    fi
1877a84e134Smrg    shift # arg
188ab902922Smrg    dst_arg=$arg
189c889a3bfSmrg    # Protect names problematic for 'test' and other utilities.
190421c997bSmrg    case $dst_arg in
191421c997bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192421c997bSmrg    esac
1937a84e134Smrg  done
1947a84e134Smrgfi
1957a84e134Smrg
1967a84e134Smrgif test $# -eq 0; then
1977a84e134Smrg  if test -z "$dir_arg"; then
1987a84e134Smrg    echo "$0: no input file specified." >&2
1997a84e134Smrg    exit 1
2007a84e134Smrg  fi
201c889a3bfSmrg  # It's OK to call 'install-sh -d' without argument.
2027a84e134Smrg  # This can happen when creating conditional directories.
2037a84e134Smrg  exit 0
2047a84e134Smrgfi
2057a84e134Smrg
206c8571806Smrgif test -z "$dir_arg"; then
207c8571806Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208c8571806Smrg    if test ! -d "$dst_arg"; then
209c8571806Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210c8571806Smrg      exit 1
211c8571806Smrg    fi
212c8571806Smrg  fi
213c8571806Smrgfi
214c8571806Smrg
2157a84e134Smrgif test -z "$dir_arg"; then
216421c997bSmrg  do_exit='(exit $ret); exit $ret'
217421c997bSmrg  trap "ret=129; $do_exit" 1
218421c997bSmrg  trap "ret=130; $do_exit" 2
219421c997bSmrg  trap "ret=141; $do_exit" 13
220421c997bSmrg  trap "ret=143; $do_exit" 15
2217a84e134Smrg
2227a84e134Smrg  # Set umask so as not to create temps with too-generous modes.
2237a84e134Smrg  # However, 'strip' requires both read and write access to temps.
2247a84e134Smrg  case $mode in
2257a84e134Smrg    # Optimize common cases.
2267a84e134Smrg    *644) cp_umask=133;;
2277a84e134Smrg    *755) cp_umask=22;;
2287a84e134Smrg
2297a84e134Smrg    *[0-7])
2307a84e134Smrg      if test -z "$stripcmd"; then
231c8571806Smrg        u_plus_rw=
2327a84e134Smrg      else
233c8571806Smrg        u_plus_rw='% 200'
2347a84e134Smrg      fi
2357a84e134Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2367a84e134Smrg    *)
2377a84e134Smrg      if test -z "$stripcmd"; then
238c8571806Smrg        u_plus_rw=
2397a84e134Smrg      else
240c8571806Smrg        u_plus_rw=,u+rw
2417a84e134Smrg      fi
2427a84e134Smrg      cp_umask=$mode$u_plus_rw;;
2437a84e134Smrg  esac
2447a84e134Smrgfi
2457a84e134Smrg
2467a84e134Smrgfor src
2477a84e134Smrgdo
248c889a3bfSmrg  # Protect names problematic for 'test' and other utilities.
2497a84e134Smrg  case $src in
250421c997bSmrg    -* | [=\(\)!]) src=./$src;;
2517a84e134Smrg  esac
2527a84e134Smrg
2537a84e134Smrg  if test -n "$dir_arg"; then
2547a84e134Smrg    dst=$src
2557a84e134Smrg    dstdir=$dst
2567a84e134Smrg    test -d "$dstdir"
2577a84e134Smrg    dstdir_status=$?
2587a84e134Smrg  else
2597a84e134Smrg
2607a84e134Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2617a84e134Smrg    # might cause directories to be created, which would be especially bad
2627a84e134Smrg    # if $src (and thus $dsttmp) contains '*'.
2637a84e134Smrg    if test ! -f "$src" && test ! -d "$src"; then
2647a84e134Smrg      echo "$0: $src does not exist." >&2
2657a84e134Smrg      exit 1
2667a84e134Smrg    fi
2677a84e134Smrg
268ab902922Smrg    if test -z "$dst_arg"; then
2697a84e134Smrg      echo "$0: no destination specified." >&2
2707a84e134Smrg      exit 1
2717a84e134Smrg    fi
272ab902922Smrg    dst=$dst_arg
2737a84e134Smrg
2745ec34c4cSmrg    # If destination is a directory, append the input filename.
2757a84e134Smrg    if test -d "$dst"; then
276c8571806Smrg      if test "$is_target_a_directory" = never; then
277c8571806Smrg        echo "$0: $dst_arg: Is a directory" >&2
278c8571806Smrg        exit 1
2797a84e134Smrg      fi
2807a84e134Smrg      dstdir=$dst
2815ec34c4cSmrg      dstbase=`basename "$src"`
2825ec34c4cSmrg      case $dst in
2835ec34c4cSmrg	*/) dst=$dst$dstbase;;
2845ec34c4cSmrg	*)  dst=$dst/$dstbase;;
2855ec34c4cSmrg      esac
2867a84e134Smrg      dstdir_status=0
2877a84e134Smrg    else
288c8571806Smrg      dstdir=`dirname "$dst"`
2897a84e134Smrg      test -d "$dstdir"
2907a84e134Smrg      dstdir_status=$?
2917a84e134Smrg    fi
2927a84e134Smrg  fi
2937a84e134Smrg
2945ec34c4cSmrg  case $dstdir in
2955ec34c4cSmrg    */) dstdirslash=$dstdir;;
2965ec34c4cSmrg    *)  dstdirslash=$dstdir/;;
2975ec34c4cSmrg  esac
2985ec34c4cSmrg
2997a84e134Smrg  obsolete_mkdir_used=false
3007a84e134Smrg
3017a84e134Smrg  if test $dstdir_status != 0; then
3027a84e134Smrg    case $posix_mkdir in
3037a84e134Smrg      '')
304c8571806Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
305c8571806Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
306c8571806Smrg        umask=`umask`
307c8571806Smrg        case $stripcmd.$umask in
308c8571806Smrg          # Optimize common cases.
309c8571806Smrg          *[2367][2367]) mkdir_umask=$umask;;
310c8571806Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311c8571806Smrg
312c8571806Smrg          *[0-7])
313c8571806Smrg            mkdir_umask=`expr $umask + 22 \
314c8571806Smrg              - $umask % 100 % 40 + $umask % 20 \
315c8571806Smrg              - $umask % 10 % 4 + $umask % 2
316c8571806Smrg            `;;
317c8571806Smrg          *) mkdir_umask=$umask,go-w;;
318c8571806Smrg        esac
319c8571806Smrg
320c8571806Smrg        # With -d, create the new directory with the user-specified mode.
321c8571806Smrg        # Otherwise, rely on $mkdir_umask.
322c8571806Smrg        if test -n "$dir_arg"; then
323c8571806Smrg          mkdir_mode=-m$mode
324c8571806Smrg        else
325c8571806Smrg          mkdir_mode=
326c8571806Smrg        fi
327c8571806Smrg
328c8571806Smrg        posix_mkdir=false
329c8571806Smrg        case $umask in
330c8571806Smrg          *[123567][0-7][0-7])
331c8571806Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
332c8571806Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333c8571806Smrg            ;;
334c8571806Smrg          *)
3355ec34c4cSmrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
3365ec34c4cSmrg            # here however when possible just to lower collision chance.
337c8571806Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
338c8571806Smrg
3395ec34c4cSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
3405ec34c4cSmrg
3415ec34c4cSmrg            # Because "mkdir -p" follows existing symlinks and we likely work
3425ec34c4cSmrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
3435ec34c4cSmrg            # directory is successfully created first before we actually test
3445ec34c4cSmrg            # 'mkdir -p' feature.
345c8571806Smrg            if (umask $mkdir_umask &&
3465ec34c4cSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
3475ec34c4cSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
348c8571806Smrg            then
349c8571806Smrg              if test -z "$dir_arg" || {
350c8571806Smrg                   # Check for POSIX incompatibilities with -m.
351c8571806Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
352c8571806Smrg                   # other-writable bit of parent directory when it shouldn't.
353c8571806Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3545ec34c4cSmrg                   test_tmpdir="$tmpdir/a"
3555ec34c4cSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
356c8571806Smrg                   case $ls_ld_tmpdir in
357c8571806Smrg                     d????-?r-*) different_mode=700;;
358c8571806Smrg                     d????-?--*) different_mode=755;;
359c8571806Smrg                     *) false;;
360c8571806Smrg                   esac &&
3615ec34c4cSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3625ec34c4cSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
363c8571806Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
364c8571806Smrg                   }
365c8571806Smrg                 }
366c8571806Smrg              then posix_mkdir=:
367c8571806Smrg              fi
3685ec34c4cSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
369c8571806Smrg            else
370c8571806Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3715ec34c4cSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
372c8571806Smrg            fi
373c8571806Smrg            trap '' 0;;
374c8571806Smrg        esac;;
3757a84e134Smrg    esac
3767a84e134Smrg
3777a84e134Smrg    if
3787a84e134Smrg      $posix_mkdir && (
379c8571806Smrg        umask $mkdir_umask &&
380c8571806Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3817a84e134Smrg      )
3827a84e134Smrg    then :
3837a84e134Smrg    else
3847a84e134Smrg
3857a84e134Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3867a84e134Smrg      # or it failed possibly due to a race condition.  Create the
3877a84e134Smrg      # directory the slow way, step by step, checking for races as we go.
3887a84e134Smrg
3897a84e134Smrg      case $dstdir in
390c8571806Smrg        /*) prefix='/';;
391c8571806Smrg        [-=\(\)!]*) prefix='./';;
392c8571806Smrg        *)  prefix='';;
3937a84e134Smrg      esac
3947a84e134Smrg
3957a84e134Smrg      oIFS=$IFS
3967a84e134Smrg      IFS=/
397c8571806Smrg      set -f
3987a84e134Smrg      set fnord $dstdir
3997a84e134Smrg      shift
400c8571806Smrg      set +f
4017a84e134Smrg      IFS=$oIFS
4027a84e134Smrg
4037a84e134Smrg      prefixes=
4047a84e134Smrg
4057a84e134Smrg      for d
4067a84e134Smrg      do
407c8571806Smrg        test X"$d" = X && continue
408c8571806Smrg
409c8571806Smrg        prefix=$prefix$d
410c8571806Smrg        if test -d "$prefix"; then
411c8571806Smrg          prefixes=
412c8571806Smrg        else
413c8571806Smrg          if $posix_mkdir; then
414c8571806Smrg            (umask=$mkdir_umask &&
415c8571806Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416c8571806Smrg            # Don't fail if two instances are running concurrently.
417c8571806Smrg            test -d "$prefix" || exit 1
418c8571806Smrg          else
419c8571806Smrg            case $prefix in
420c8571806Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421c8571806Smrg              *) qprefix=$prefix;;
422c8571806Smrg            esac
423c8571806Smrg            prefixes="$prefixes '$qprefix'"
424c8571806Smrg          fi
425c8571806Smrg        fi
426c8571806Smrg        prefix=$prefix/
4277a84e134Smrg      done
4287a84e134Smrg
4297a84e134Smrg      if test -n "$prefixes"; then
430c8571806Smrg        # Don't fail if two instances are running concurrently.
431c8571806Smrg        (umask $mkdir_umask &&
432c8571806Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
433c8571806Smrg          test -d "$dstdir" || exit 1
434c8571806Smrg        obsolete_mkdir_used=true
4357a84e134Smrg      fi
4367a84e134Smrg    fi
4377a84e134Smrg  fi
4387a84e134Smrg
4397a84e134Smrg  if test -n "$dir_arg"; then
4407a84e134Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4417a84e134Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4427a84e134Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4437a84e134Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4447a84e134Smrg  else
4457a84e134Smrg
4467a84e134Smrg    # Make a couple of temp file names in the proper directory.
4475ec34c4cSmrg    dsttmp=${dstdirslash}_inst.$$_
4485ec34c4cSmrg    rmtmp=${dstdirslash}_rm.$$_
4497a84e134Smrg
4507a84e134Smrg    # Trap to clean up those temp files at exit.
4517a84e134Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4527a84e134Smrg
4537a84e134Smrg    # Copy the file name to the temp name.
4545ec34c4cSmrg    (umask $cp_umask &&
4555ec34c4cSmrg     { test -z "$stripcmd" || {
4565ec34c4cSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4575ec34c4cSmrg	 # which would cause strip to fail.
4585ec34c4cSmrg	 if test -z "$doit"; then
4595ec34c4cSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4605ec34c4cSmrg	 else
4615ec34c4cSmrg	   $doit touch "$dsttmp"
4625ec34c4cSmrg	 fi
4635ec34c4cSmrg       }
4645ec34c4cSmrg     } &&
4655ec34c4cSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4667a84e134Smrg
4677a84e134Smrg    # and set any options; do chmod last to preserve setuid bits.
4687a84e134Smrg    #
4697a84e134Smrg    # If any of these fail, we abort the whole thing.  If we want to
4707a84e134Smrg    # ignore errors from any of these, just make sure not to ignore
4717a84e134Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4727a84e134Smrg    #
473ab902922Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
474ab902922Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
475ab902922Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
476ab902922Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
477ab902922Smrg
478ab902922Smrg    # If -C, don't bother to copy if it wouldn't change the file.
479ab902922Smrg    if $copy_on_change &&
480c8571806Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
481c8571806Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
482c8571806Smrg       set -f &&
483ab902922Smrg       set X $old && old=:$2:$4:$5:$6 &&
484ab902922Smrg       set X $new && new=:$2:$4:$5:$6 &&
485c8571806Smrg       set +f &&
486ab902922Smrg       test "$old" = "$new" &&
487ab902922Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
488ab902922Smrg    then
489ab902922Smrg      rm -f "$dsttmp"
490ab902922Smrg    else
491ab902922Smrg      # Rename the file to the real destination.
492ab902922Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
493ab902922Smrg
494ab902922Smrg      # The rename failed, perhaps because mv can't rename something else
495ab902922Smrg      # to itself, or perhaps because mv is so ancient that it does not
496ab902922Smrg      # support -f.
497ab902922Smrg      {
498c8571806Smrg        # Now remove or move aside any old file at destination location.
499c8571806Smrg        # We try this two ways since rm can't unlink itself on some
500c8571806Smrg        # systems and the destination file might be busy for other
501c8571806Smrg        # reasons.  In this case, the final cleanup might fail but the new
502c8571806Smrg        # file should still install successfully.
503c8571806Smrg        {
504c8571806Smrg          test ! -f "$dst" ||
505c8571806Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
506c8571806Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
507c8571806Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
508c8571806Smrg          } ||
509c8571806Smrg          { echo "$0: cannot unlink or rename $dst" >&2
510c8571806Smrg            (exit 1); exit 1
511c8571806Smrg          }
512c8571806Smrg        } &&
513c8571806Smrg
514c8571806Smrg        # Now rename the file to the real destination.
515c8571806Smrg        $doit $mvcmd "$dsttmp" "$dst"
516ab902922Smrg      }
517ab902922Smrg    fi || exit 1
5187a84e134Smrg
5197a84e134Smrg    trap '' 0
5207a84e134Smrg  fi
5217a84e134Smrgdone
5227a84e134Smrg
5237a84e134Smrg# Local variables:
5245ec34c4cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5257a84e134Smrg# time-stamp-start: "scriptversion="
5267a84e134Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5275ec34c4cSmrg# time-stamp-time-zone: "UTC0"
528e1e1713cSmrg# time-stamp-end: "; # UTC"
5297a84e134Smrg# End:
530