install-sh revision 7a5ec427
1c06b6b69Smrg#!/bin/sh
2c06b6b69Smrg# install - install a program, script, or datafile
3c06b6b69Smrg
47a5ec427Smrgscriptversion=2013-12-25.23; # UTC
5c06b6b69Smrg
6c06b6b69Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c06b6b69Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8c06b6b69Smrg# following copyright and license.
9c06b6b69Smrg#
10c06b6b69Smrg# Copyright (C) 1994 X Consortium
11c06b6b69Smrg#
12c06b6b69Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c06b6b69Smrg# of this software and associated documentation files (the "Software"), to
14c06b6b69Smrg# deal in the Software without restriction, including without limitation the
15c06b6b69Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c06b6b69Smrg# sell copies of the Software, and to permit persons to whom the Software is
17c06b6b69Smrg# furnished to do so, subject to the following conditions:
18c06b6b69Smrg#
19c06b6b69Smrg# The above copyright notice and this permission notice shall be included in
20c06b6b69Smrg# all copies or substantial portions of the Software.
21c06b6b69Smrg#
22c06b6b69Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c06b6b69Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c06b6b69Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c06b6b69Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c06b6b69Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c06b6b69Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c06b6b69Smrg#
29c06b6b69Smrg# Except as contained in this notice, the name of the X Consortium shall not
30c06b6b69Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c06b6b69Smrg# ings in this Software without prior written authorization from the X Consor-
32c06b6b69Smrg# tium.
33c06b6b69Smrg#
34c06b6b69Smrg#
35c06b6b69Smrg# FSF changes to this file are in the public domain.
36c06b6b69Smrg#
37c06b6b69Smrg# Calling this script install-sh is preferred over install.sh, to prevent
387a5ec427Smrg# 'make' implicit rules from creating a file called install from it
39c06b6b69Smrg# when there is no Makefile.
40c06b6b69Smrg#
41c06b6b69Smrg# This script is compatible with the BSD install script, but was written
429f4658d1Smrg# from scratch.
439f4658d1Smrg
447a5ec427Smrgtab='	'
459f4658d1Smrgnl='
469f4658d1Smrg'
477a5ec427SmrgIFS=" $tab$nl"
48c06b6b69Smrg
497a5ec427Smrg# Set DOITPROG to "echo" to test this script.
50c06b6b69Smrg
519f4658d1Smrgdoit=${DOITPROG-}
527a5ec427Smrgdoit_exec=${doit:-exec}
53c06b6b69Smrg
549f4658d1Smrg# Put in absolute file names if you don't have them in your path;
559f4658d1Smrg# or use environment vars.
569f4658d1Smrg
579f4658d1Smrgchgrpprog=${CHGRPPROG-chgrp}
589f4658d1Smrgchmodprog=${CHMODPROG-chmod}
599f4658d1Smrgchownprog=${CHOWNPROG-chown}
609f4658d1Smrgcmpprog=${CMPPROG-cmp}
619f4658d1Smrgcpprog=${CPPROG-cp}
629f4658d1Smrgmkdirprog=${MKDIRPROG-mkdir}
639f4658d1Smrgmvprog=${MVPROG-mv}
649f4658d1Smrgrmprog=${RMPROG-rm}
659f4658d1Smrgstripprog=${STRIPPROG-strip}
669f4658d1Smrg
679f4658d1Smrgposix_mkdir=
689f4658d1Smrg
699f4658d1Smrg# Desired mode of installed file.
709f4658d1Smrgmode=0755
71c06b6b69Smrg
72c06b6b69Smrgchgrpcmd=
739f4658d1Smrgchmodcmd=$chmodprog
749f4658d1Smrgchowncmd=
759f4658d1Smrgmvcmd=$mvprog
76c06b6b69Smrgrmcmd="$rmprog -f"
779f4658d1Smrgstripcmd=
789f4658d1Smrg
79c06b6b69Smrgsrc=
80c06b6b69Smrgdst=
81c06b6b69Smrgdir_arg=
829f4658d1Smrgdst_arg=
839f4658d1Smrg
849f4658d1Smrgcopy_on_change=false
857a5ec427Smrgis_target_a_directory=possibly
86c06b6b69Smrg
879f4658d1Smrgusage="\
889f4658d1SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89c06b6b69Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90c06b6b69Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91c06b6b69Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92c06b6b69Smrg
93c06b6b69SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94c06b6b69SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95c06b6b69SmrgIn the 4th, create DIRECTORIES.
96c06b6b69Smrg
97c06b6b69SmrgOptions:
989f4658d1Smrg     --help     display this help and exit.
999f4658d1Smrg     --version  display version info and exit.
1009f4658d1Smrg
1019f4658d1Smrg  -c            (ignored)
1029f4658d1Smrg  -C            install only if different (preserve the last data modification time)
1039f4658d1Smrg  -d            create directories instead of installing files.
1049f4658d1Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1059f4658d1Smrg  -m MODE       $chmodprog installed files to MODE.
1069f4658d1Smrg  -o USER       $chownprog installed files to USER.
1079f4658d1Smrg  -s            $stripprog installed files.
1089f4658d1Smrg  -t DIRECTORY  install into DIRECTORY.
1099f4658d1Smrg  -T            report an error if DSTFILE is a directory.
110c06b6b69Smrg
111c06b6b69SmrgEnvironment variables override the default commands:
1129f4658d1Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1139f4658d1Smrg  RMPROG STRIPPROG
114c06b6b69Smrg"
115c06b6b69Smrg
1169f4658d1Smrgwhile test $# -ne 0; do
117c06b6b69Smrg  case $1 in
1189f4658d1Smrg    -c) ;;
1199f4658d1Smrg
1209f4658d1Smrg    -C) copy_on_change=true;;
121c06b6b69Smrg
1229f4658d1Smrg    -d) dir_arg=true;;
123c06b6b69Smrg
124c06b6b69Smrg    -g) chgrpcmd="$chgrpprog $2"
1257a5ec427Smrg        shift;;
126c06b6b69Smrg
127c06b6b69Smrg    --help) echo "$usage"; exit $?;;
128c06b6b69Smrg
1299f4658d1Smrg    -m) mode=$2
1307a5ec427Smrg        case $mode in
1317a5ec427Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1327a5ec427Smrg            echo "$0: invalid mode: $mode" >&2
1337a5ec427Smrg            exit 1;;
1347a5ec427Smrg        esac
1357a5ec427Smrg        shift;;
136c06b6b69Smrg
137c06b6b69Smrg    -o) chowncmd="$chownprog $2"
1387a5ec427Smrg        shift;;
139c06b6b69Smrg
1409f4658d1Smrg    -s) stripcmd=$stripprog;;
141c06b6b69Smrg
1427a5ec427Smrg    -t)
1437a5ec427Smrg        is_target_a_directory=always
1447a5ec427Smrg        dst_arg=$2
1457a5ec427Smrg        # Protect names problematic for 'test' and other utilities.
1467a5ec427Smrg        case $dst_arg in
1477a5ec427Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1487a5ec427Smrg        esac
1497a5ec427Smrg        shift;;
150c06b6b69Smrg
1517a5ec427Smrg    -T) is_target_a_directory=never;;
152c06b6b69Smrg
153c06b6b69Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154c06b6b69Smrg
1557a5ec427Smrg    --) shift
1567a5ec427Smrg        break;;
1579f4658d1Smrg
1587a5ec427Smrg    -*) echo "$0: invalid option: $1" >&2
1597a5ec427Smrg        exit 1;;
1609f4658d1Smrg
1619f4658d1Smrg    *)  break;;
162c06b6b69Smrg  esac
1639f4658d1Smrg  shift
164c06b6b69Smrgdone
165c06b6b69Smrg
1667a5ec427Smrg# We allow the use of options -d and -T together, by making -d
1677a5ec427Smrg# take the precedence; this is for compatibility with GNU install.
1687a5ec427Smrg
1697a5ec427Smrgif test -n "$dir_arg"; then
1707a5ec427Smrg  if test -n "$dst_arg"; then
1717a5ec427Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1727a5ec427Smrg    exit 1
1737a5ec427Smrg  fi
1747a5ec427Smrgfi
1757a5ec427Smrg
1769f4658d1Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1779f4658d1Smrg  # When -d is used, all remaining arguments are directories to create.
1789f4658d1Smrg  # When -t is used, the destination is already specified.
1799f4658d1Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1809f4658d1Smrg  for arg
1819f4658d1Smrg  do
1829f4658d1Smrg    if test -n "$dst_arg"; then
1839f4658d1Smrg      # $@ is not empty: it contains at least $arg.
1849f4658d1Smrg      set fnord "$@" "$dst_arg"
1859f4658d1Smrg      shift # fnord
1869f4658d1Smrg    fi
1879f4658d1Smrg    shift # arg
1889f4658d1Smrg    dst_arg=$arg
1897a5ec427Smrg    # Protect names problematic for 'test' and other utilities.
190a1d73b4bSmrg    case $dst_arg in
191a1d73b4bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192a1d73b4bSmrg    esac
1939f4658d1Smrg  done
1949f4658d1Smrgfi
1959f4658d1Smrg
1969f4658d1Smrgif test $# -eq 0; then
197c06b6b69Smrg  if test -z "$dir_arg"; then
198c06b6b69Smrg    echo "$0: no input file specified." >&2
199c06b6b69Smrg    exit 1
200c06b6b69Smrg  fi
2017a5ec427Smrg  # It's OK to call 'install-sh -d' without argument.
202c06b6b69Smrg  # This can happen when creating conditional directories.
203c06b6b69Smrg  exit 0
204c06b6b69Smrgfi
205c06b6b69Smrg
2067a5ec427Smrgif test -z "$dir_arg"; then
2077a5ec427Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2087a5ec427Smrg    if test ! -d "$dst_arg"; then
2097a5ec427Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2107a5ec427Smrg      exit 1
2117a5ec427Smrg    fi
2127a5ec427Smrg  fi
2137a5ec427Smrgfi
2147a5ec427Smrg
2159f4658d1Smrgif test -z "$dir_arg"; then
216a1d73b4bSmrg  do_exit='(exit $ret); exit $ret'
217a1d73b4bSmrg  trap "ret=129; $do_exit" 1
218a1d73b4bSmrg  trap "ret=130; $do_exit" 2
219a1d73b4bSmrg  trap "ret=141; $do_exit" 13
220a1d73b4bSmrg  trap "ret=143; $do_exit" 15
2219f4658d1Smrg
2229f4658d1Smrg  # Set umask so as not to create temps with too-generous modes.
2239f4658d1Smrg  # However, 'strip' requires both read and write access to temps.
2249f4658d1Smrg  case $mode in
2259f4658d1Smrg    # Optimize common cases.
2269f4658d1Smrg    *644) cp_umask=133;;
2279f4658d1Smrg    *755) cp_umask=22;;
2289f4658d1Smrg
2299f4658d1Smrg    *[0-7])
2309f4658d1Smrg      if test -z "$stripcmd"; then
2317a5ec427Smrg        u_plus_rw=
2329f4658d1Smrg      else
2337a5ec427Smrg        u_plus_rw='% 200'
2349f4658d1Smrg      fi
2359f4658d1Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2369f4658d1Smrg    *)
2379f4658d1Smrg      if test -z "$stripcmd"; then
2387a5ec427Smrg        u_plus_rw=
2399f4658d1Smrg      else
2407a5ec427Smrg        u_plus_rw=,u+rw
2419f4658d1Smrg      fi
2429f4658d1Smrg      cp_umask=$mode$u_plus_rw;;
2439f4658d1Smrg  esac
2449f4658d1Smrgfi
2459f4658d1Smrg
246c06b6b69Smrgfor src
247c06b6b69Smrgdo
2487a5ec427Smrg  # Protect names problematic for 'test' and other utilities.
249c06b6b69Smrg  case $src in
250a1d73b4bSmrg    -* | [=\(\)!]) src=./$src;;
251c06b6b69Smrg  esac
252c06b6b69Smrg
253c06b6b69Smrg  if test -n "$dir_arg"; then
254c06b6b69Smrg    dst=$src
2559f4658d1Smrg    dstdir=$dst
2569f4658d1Smrg    test -d "$dstdir"
2579f4658d1Smrg    dstdir_status=$?
258c06b6b69Smrg  else
2599f4658d1Smrg
260c06b6b69Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261c06b6b69Smrg    # might cause directories to be created, which would be especially bad
262c06b6b69Smrg    # if $src (and thus $dsttmp) contains '*'.
263c06b6b69Smrg    if test ! -f "$src" && test ! -d "$src"; then
264c06b6b69Smrg      echo "$0: $src does not exist." >&2
265c06b6b69Smrg      exit 1
266c06b6b69Smrg    fi
267c06b6b69Smrg
2689f4658d1Smrg    if test -z "$dst_arg"; then
269c06b6b69Smrg      echo "$0: no destination specified." >&2
270c06b6b69Smrg      exit 1
271c06b6b69Smrg    fi
2729f4658d1Smrg    dst=$dst_arg
273c06b6b69Smrg
274c06b6b69Smrg    # If destination is a directory, append the input filename; won't work
275c06b6b69Smrg    # if double slashes aren't ignored.
276c06b6b69Smrg    if test -d "$dst"; then
2777a5ec427Smrg      if test "$is_target_a_directory" = never; then
2787a5ec427Smrg        echo "$0: $dst_arg: Is a directory" >&2
2797a5ec427Smrg        exit 1
280c06b6b69Smrg      fi
2819f4658d1Smrg      dstdir=$dst
2829f4658d1Smrg      dst=$dstdir/`basename "$src"`
2839f4658d1Smrg      dstdir_status=0
2849f4658d1Smrg    else
2857a5ec427Smrg      dstdir=`dirname "$dst"`
2869f4658d1Smrg      test -d "$dstdir"
2879f4658d1Smrg      dstdir_status=$?
288c06b6b69Smrg    fi
289c06b6b69Smrg  fi
290c06b6b69Smrg
2919f4658d1Smrg  obsolete_mkdir_used=false
2929f4658d1Smrg
2939f4658d1Smrg  if test $dstdir_status != 0; then
2949f4658d1Smrg    case $posix_mkdir in
2959f4658d1Smrg      '')
2967a5ec427Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2977a5ec427Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2987a5ec427Smrg        umask=`umask`
2997a5ec427Smrg        case $stripcmd.$umask in
3007a5ec427Smrg          # Optimize common cases.
3017a5ec427Smrg          *[2367][2367]) mkdir_umask=$umask;;
3027a5ec427Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3037a5ec427Smrg
3047a5ec427Smrg          *[0-7])
3057a5ec427Smrg            mkdir_umask=`expr $umask + 22 \
3067a5ec427Smrg              - $umask % 100 % 40 + $umask % 20 \
3077a5ec427Smrg              - $umask % 10 % 4 + $umask % 2
3087a5ec427Smrg            `;;
3097a5ec427Smrg          *) mkdir_umask=$umask,go-w;;
3107a5ec427Smrg        esac
3117a5ec427Smrg
3127a5ec427Smrg        # With -d, create the new directory with the user-specified mode.
3137a5ec427Smrg        # Otherwise, rely on $mkdir_umask.
3147a5ec427Smrg        if test -n "$dir_arg"; then
3157a5ec427Smrg          mkdir_mode=-m$mode
3167a5ec427Smrg        else
3177a5ec427Smrg          mkdir_mode=
3187a5ec427Smrg        fi
3197a5ec427Smrg
3207a5ec427Smrg        posix_mkdir=false
3217a5ec427Smrg        case $umask in
3227a5ec427Smrg          *[123567][0-7][0-7])
3237a5ec427Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3247a5ec427Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3257a5ec427Smrg            ;;
3267a5ec427Smrg          *)
3277a5ec427Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3287a5ec427Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3297a5ec427Smrg
3307a5ec427Smrg            if (umask $mkdir_umask &&
3317a5ec427Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3327a5ec427Smrg            then
3337a5ec427Smrg              if test -z "$dir_arg" || {
3347a5ec427Smrg                   # Check for POSIX incompatibilities with -m.
3357a5ec427Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3367a5ec427Smrg                   # other-writable bit of parent directory when it shouldn't.
3377a5ec427Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3387a5ec427Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3397a5ec427Smrg                   case $ls_ld_tmpdir in
3407a5ec427Smrg                     d????-?r-*) different_mode=700;;
3417a5ec427Smrg                     d????-?--*) different_mode=755;;
3427a5ec427Smrg                     *) false;;
3437a5ec427Smrg                   esac &&
3447a5ec427Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3457a5ec427Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3467a5ec427Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3477a5ec427Smrg                   }
3487a5ec427Smrg                 }
3497a5ec427Smrg              then posix_mkdir=:
3507a5ec427Smrg              fi
3517a5ec427Smrg              rmdir "$tmpdir/d" "$tmpdir"
3527a5ec427Smrg            else
3537a5ec427Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3547a5ec427Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3557a5ec427Smrg            fi
3567a5ec427Smrg            trap '' 0;;
3577a5ec427Smrg        esac;;
3589f4658d1Smrg    esac
359c06b6b69Smrg
3609f4658d1Smrg    if
3619f4658d1Smrg      $posix_mkdir && (
3627a5ec427Smrg        umask $mkdir_umask &&
3637a5ec427Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3649f4658d1Smrg      )
3659f4658d1Smrg    then :
3669f4658d1Smrg    else
367c06b6b69Smrg
3689f4658d1Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3699f4658d1Smrg      # or it failed possibly due to a race condition.  Create the
3709f4658d1Smrg      # directory the slow way, step by step, checking for races as we go.
371c06b6b69Smrg
3729f4658d1Smrg      case $dstdir in
3737a5ec427Smrg        /*) prefix='/';;
3747a5ec427Smrg        [-=\(\)!]*) prefix='./';;
3757a5ec427Smrg        *)  prefix='';;
3769f4658d1Smrg      esac
377c06b6b69Smrg
3789f4658d1Smrg      oIFS=$IFS
3799f4658d1Smrg      IFS=/
3807a5ec427Smrg      set -f
3819f4658d1Smrg      set fnord $dstdir
382c06b6b69Smrg      shift
3837a5ec427Smrg      set +f
3849f4658d1Smrg      IFS=$oIFS
3859f4658d1Smrg
3869f4658d1Smrg      prefixes=
3879f4658d1Smrg
3889f4658d1Smrg      for d
3899f4658d1Smrg      do
3907a5ec427Smrg        test X"$d" = X && continue
3917a5ec427Smrg
3927a5ec427Smrg        prefix=$prefix$d
3937a5ec427Smrg        if test -d "$prefix"; then
3947a5ec427Smrg          prefixes=
3957a5ec427Smrg        else
3967a5ec427Smrg          if $posix_mkdir; then
3977a5ec427Smrg            (umask=$mkdir_umask &&
3987a5ec427Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3997a5ec427Smrg            # Don't fail if two instances are running concurrently.
4007a5ec427Smrg            test -d "$prefix" || exit 1
4017a5ec427Smrg          else
4027a5ec427Smrg            case $prefix in
4037a5ec427Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4047a5ec427Smrg              *) qprefix=$prefix;;
4057a5ec427Smrg            esac
4067a5ec427Smrg            prefixes="$prefixes '$qprefix'"
4077a5ec427Smrg          fi
4087a5ec427Smrg        fi
4097a5ec427Smrg        prefix=$prefix/
4109f4658d1Smrg      done
4119f4658d1Smrg
4129f4658d1Smrg      if test -n "$prefixes"; then
4137a5ec427Smrg        # Don't fail if two instances are running concurrently.
4147a5ec427Smrg        (umask $mkdir_umask &&
4157a5ec427Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4167a5ec427Smrg          test -d "$dstdir" || exit 1
4177a5ec427Smrg        obsolete_mkdir_used=true
418c06b6b69Smrg      fi
4199f4658d1Smrg    fi
420c06b6b69Smrg  fi
421c06b6b69Smrg
422c06b6b69Smrg  if test -n "$dir_arg"; then
4239f4658d1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4249f4658d1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4259f4658d1Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4269f4658d1Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427c06b6b69Smrg  else
428c06b6b69Smrg
429c06b6b69Smrg    # Make a couple of temp file names in the proper directory.
430c06b6b69Smrg    dsttmp=$dstdir/_inst.$$_
431c06b6b69Smrg    rmtmp=$dstdir/_rm.$$_
432c06b6b69Smrg
433c06b6b69Smrg    # Trap to clean up those temp files at exit.
434c06b6b69Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435c06b6b69Smrg
436c06b6b69Smrg    # Copy the file name to the temp name.
4379f4658d1Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438c06b6b69Smrg
439c06b6b69Smrg    # and set any options; do chmod last to preserve setuid bits.
440c06b6b69Smrg    #
441c06b6b69Smrg    # If any of these fail, we abort the whole thing.  If we want to
442c06b6b69Smrg    # ignore errors from any of these, just make sure not to ignore
443c06b6b69Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444c06b6b69Smrg    #
4459f4658d1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4469f4658d1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4479f4658d1Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4489f4658d1Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4499f4658d1Smrg
4509f4658d1Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4519f4658d1Smrg    if $copy_on_change &&
4527a5ec427Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4537a5ec427Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4547a5ec427Smrg       set -f &&
4559f4658d1Smrg       set X $old && old=:$2:$4:$5:$6 &&
4569f4658d1Smrg       set X $new && new=:$2:$4:$5:$6 &&
4577a5ec427Smrg       set +f &&
4589f4658d1Smrg       test "$old" = "$new" &&
4599f4658d1Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4609f4658d1Smrg    then
4619f4658d1Smrg      rm -f "$dsttmp"
4629f4658d1Smrg    else
4639f4658d1Smrg      # Rename the file to the real destination.
4649f4658d1Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4659f4658d1Smrg
4669f4658d1Smrg      # The rename failed, perhaps because mv can't rename something else
4679f4658d1Smrg      # to itself, or perhaps because mv is so ancient that it does not
4689f4658d1Smrg      # support -f.
4699f4658d1Smrg      {
4707a5ec427Smrg        # Now remove or move aside any old file at destination location.
4717a5ec427Smrg        # We try this two ways since rm can't unlink itself on some
4727a5ec427Smrg        # systems and the destination file might be busy for other
4737a5ec427Smrg        # reasons.  In this case, the final cleanup might fail but the new
4747a5ec427Smrg        # file should still install successfully.
4757a5ec427Smrg        {
4767a5ec427Smrg          test ! -f "$dst" ||
4777a5ec427Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4787a5ec427Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4797a5ec427Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4807a5ec427Smrg          } ||
4817a5ec427Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4827a5ec427Smrg            (exit 1); exit 1
4837a5ec427Smrg          }
4847a5ec427Smrg        } &&
4857a5ec427Smrg
4867a5ec427Smrg        # Now rename the file to the real destination.
4877a5ec427Smrg        $doit $mvcmd "$dsttmp" "$dst"
4889f4658d1Smrg      }
4899f4658d1Smrg    fi || exit 1
4909f4658d1Smrg
4919f4658d1Smrg    trap '' 0
4929f4658d1Smrg  fi
493c06b6b69Smrgdone
494c06b6b69Smrg
495c06b6b69Smrg# Local variables:
496c06b6b69Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497c06b6b69Smrg# time-stamp-start: "scriptversion="
498c06b6b69Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4997d17d341Smrg# time-stamp-time-zone: "UTC"
5007d17d341Smrg# time-stamp-end: "; # UTC"
501c06b6b69Smrg# End:
502