install-sh revision 0a392d7e
14a041c5bSmacallan#!/bin/sh
24a041c5bSmacallan# install - install a program, script, or datafile
34a041c5bSmacallan
40a392d7eSmrgscriptversion=2013-12-25.23; # UTC
54a041c5bSmacallan
64a041c5bSmacallan# This originates from X11R5 (mit/util/scripts/install.sh), which was
74a041c5bSmacallan# later released in X11R6 (xc/config/util/install.sh) with the
84a041c5bSmacallan# following copyright and license.
94a041c5bSmacallan#
104a041c5bSmacallan# Copyright (C) 1994 X Consortium
114a041c5bSmacallan#
124a041c5bSmacallan# Permission is hereby granted, free of charge, to any person obtaining a copy
134a041c5bSmacallan# of this software and associated documentation files (the "Software"), to
144a041c5bSmacallan# deal in the Software without restriction, including without limitation the
154a041c5bSmacallan# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164a041c5bSmacallan# sell copies of the Software, and to permit persons to whom the Software is
174a041c5bSmacallan# furnished to do so, subject to the following conditions:
184a041c5bSmacallan#
194a041c5bSmacallan# The above copyright notice and this permission notice shall be included in
204a041c5bSmacallan# all copies or substantial portions of the Software.
214a041c5bSmacallan#
224a041c5bSmacallan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234a041c5bSmacallan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244a041c5bSmacallan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254a041c5bSmacallan# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264a041c5bSmacallan# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274a041c5bSmacallan# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284a041c5bSmacallan#
294a041c5bSmacallan# Except as contained in this notice, the name of the X Consortium shall not
304a041c5bSmacallan# be used in advertising or otherwise to promote the sale, use or other deal-
314a041c5bSmacallan# ings in this Software without prior written authorization from the X Consor-
324a041c5bSmacallan# tium.
334a041c5bSmacallan#
344a041c5bSmacallan#
354a041c5bSmacallan# FSF changes to this file are in the public domain.
364a041c5bSmacallan#
374a041c5bSmacallan# Calling this script install-sh is preferred over install.sh, to prevent
3854b44505Smrg# 'make' implicit rules from creating a file called install from it
394a041c5bSmacallan# when there is no Makefile.
404a041c5bSmacallan#
414a041c5bSmacallan# This script is compatible with the BSD install script, but was written
424a041c5bSmacallan# from scratch.
434a041c5bSmacallan
440a392d7eSmrgtab='	'
454a041c5bSmacallannl='
464a041c5bSmacallan'
470a392d7eSmrgIFS=" $tab$nl"
484a041c5bSmacallan
490a392d7eSmrg# Set DOITPROG to "echo" to test this script.
504a041c5bSmacallan
514a041c5bSmacallandoit=${DOITPROG-}
520a392d7eSmrgdoit_exec=${doit:-exec}
534a041c5bSmacallan
544a041c5bSmacallan# Put in absolute file names if you don't have them in your path;
554a041c5bSmacallan# or use environment vars.
564a041c5bSmacallan
574a041c5bSmacallanchgrpprog=${CHGRPPROG-chgrp}
584a041c5bSmacallanchmodprog=${CHMODPROG-chmod}
594a041c5bSmacallanchownprog=${CHOWNPROG-chown}
604a041c5bSmacallancmpprog=${CMPPROG-cmp}
614a041c5bSmacallancpprog=${CPPROG-cp}
624a041c5bSmacallanmkdirprog=${MKDIRPROG-mkdir}
634a041c5bSmacallanmvprog=${MVPROG-mv}
644a041c5bSmacallanrmprog=${RMPROG-rm}
654a041c5bSmacallanstripprog=${STRIPPROG-strip}
664a041c5bSmacallan
674a041c5bSmacallanposix_mkdir=
684a041c5bSmacallan
694a041c5bSmacallan# Desired mode of installed file.
704a041c5bSmacallanmode=0755
714a041c5bSmacallan
724a041c5bSmacallanchgrpcmd=
734a041c5bSmacallanchmodcmd=$chmodprog
744a041c5bSmacallanchowncmd=
754a041c5bSmacallanmvcmd=$mvprog
764a041c5bSmacallanrmcmd="$rmprog -f"
774a041c5bSmacallanstripcmd=
784a041c5bSmacallan
794a041c5bSmacallansrc=
804a041c5bSmacallandst=
814a041c5bSmacallandir_arg=
824a041c5bSmacallandst_arg=
834a041c5bSmacallan
844a041c5bSmacallancopy_on_change=false
850a392d7eSmrgis_target_a_directory=possibly
864a041c5bSmacallan
874a041c5bSmacallanusage="\
884a041c5bSmacallanUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
894a041c5bSmacallan   or: $0 [OPTION]... SRCFILES... DIRECTORY
904a041c5bSmacallan   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
914a041c5bSmacallan   or: $0 [OPTION]... -d DIRECTORIES...
924a041c5bSmacallan
934a041c5bSmacallanIn the 1st form, copy SRCFILE to DSTFILE.
944a041c5bSmacallanIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
954a041c5bSmacallanIn the 4th, create DIRECTORIES.
964a041c5bSmacallan
974a041c5bSmacallanOptions:
984a041c5bSmacallan     --help     display this help and exit.
994a041c5bSmacallan     --version  display version info and exit.
1004a041c5bSmacallan
1014a041c5bSmacallan  -c            (ignored)
1024a041c5bSmacallan  -C            install only if different (preserve the last data modification time)
1034a041c5bSmacallan  -d            create directories instead of installing files.
1044a041c5bSmacallan  -g GROUP      $chgrpprog installed files to GROUP.
1054a041c5bSmacallan  -m MODE       $chmodprog installed files to MODE.
1064a041c5bSmacallan  -o USER       $chownprog installed files to USER.
1074a041c5bSmacallan  -s            $stripprog installed files.
1084a041c5bSmacallan  -t DIRECTORY  install into DIRECTORY.
1094a041c5bSmacallan  -T            report an error if DSTFILE is a directory.
1104a041c5bSmacallan
1114a041c5bSmacallanEnvironment variables override the default commands:
1124a041c5bSmacallan  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1134a041c5bSmacallan  RMPROG STRIPPROG
1144a041c5bSmacallan"
1154a041c5bSmacallan
1164a041c5bSmacallanwhile test $# -ne 0; do
1174a041c5bSmacallan  case $1 in
1184a041c5bSmacallan    -c) ;;
1194a041c5bSmacallan
1204a041c5bSmacallan    -C) copy_on_change=true;;
1214a041c5bSmacallan
1224a041c5bSmacallan    -d) dir_arg=true;;
1234a041c5bSmacallan
1244a041c5bSmacallan    -g) chgrpcmd="$chgrpprog $2"
1250a392d7eSmrg        shift;;
1264a041c5bSmacallan
1274a041c5bSmacallan    --help) echo "$usage"; exit $?;;
1284a041c5bSmacallan
1294a041c5bSmacallan    -m) mode=$2
1300a392d7eSmrg        case $mode in
1310a392d7eSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1320a392d7eSmrg            echo "$0: invalid mode: $mode" >&2
1330a392d7eSmrg            exit 1;;
1340a392d7eSmrg        esac
1350a392d7eSmrg        shift;;
1364a041c5bSmacallan
1374a041c5bSmacallan    -o) chowncmd="$chownprog $2"
1380a392d7eSmrg        shift;;
1394a041c5bSmacallan
1404a041c5bSmacallan    -s) stripcmd=$stripprog;;
1414a041c5bSmacallan
1420a392d7eSmrg    -t)
1430a392d7eSmrg        is_target_a_directory=always
1440a392d7eSmrg        dst_arg=$2
1450a392d7eSmrg        # Protect names problematic for 'test' and other utilities.
1460a392d7eSmrg        case $dst_arg in
1470a392d7eSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1480a392d7eSmrg        esac
1490a392d7eSmrg        shift;;
1504a041c5bSmacallan
1510a392d7eSmrg    -T) is_target_a_directory=never;;
1524a041c5bSmacallan
1534a041c5bSmacallan    --version) echo "$0 $scriptversion"; exit $?;;
1544a041c5bSmacallan
1550a392d7eSmrg    --) shift
1560a392d7eSmrg        break;;
1574a041c5bSmacallan
1580a392d7eSmrg    -*) echo "$0: invalid option: $1" >&2
1590a392d7eSmrg        exit 1;;
1604a041c5bSmacallan
1614a041c5bSmacallan    *)  break;;
1624a041c5bSmacallan  esac
1634a041c5bSmacallan  shift
1644a041c5bSmacallandone
1654a041c5bSmacallan
1660a392d7eSmrg# We allow the use of options -d and -T together, by making -d
1670a392d7eSmrg# take the precedence; this is for compatibility with GNU install.
1680a392d7eSmrg
1690a392d7eSmrgif test -n "$dir_arg"; then
1700a392d7eSmrg  if test -n "$dst_arg"; then
1710a392d7eSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1720a392d7eSmrg    exit 1
1730a392d7eSmrg  fi
1740a392d7eSmrgfi
1750a392d7eSmrg
1764a041c5bSmacallanif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1774a041c5bSmacallan  # When -d is used, all remaining arguments are directories to create.
1784a041c5bSmacallan  # When -t is used, the destination is already specified.
1794a041c5bSmacallan  # Otherwise, the last argument is the destination.  Remove it from $@.
1804a041c5bSmacallan  for arg
1814a041c5bSmacallan  do
1824a041c5bSmacallan    if test -n "$dst_arg"; then
1834a041c5bSmacallan      # $@ is not empty: it contains at least $arg.
1844a041c5bSmacallan      set fnord "$@" "$dst_arg"
1854a041c5bSmacallan      shift # fnord
1864a041c5bSmacallan    fi
1874a041c5bSmacallan    shift # arg
1884a041c5bSmacallan    dst_arg=$arg
18954b44505Smrg    # Protect names problematic for 'test' and other utilities.
19054b44505Smrg    case $dst_arg in
19154b44505Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19254b44505Smrg    esac
1934a041c5bSmacallan  done
1944a041c5bSmacallanfi
1954a041c5bSmacallan
1964a041c5bSmacallanif test $# -eq 0; then
1974a041c5bSmacallan  if test -z "$dir_arg"; then
1984a041c5bSmacallan    echo "$0: no input file specified." >&2
1994a041c5bSmacallan    exit 1
2004a041c5bSmacallan  fi
20154b44505Smrg  # It's OK to call 'install-sh -d' without argument.
2024a041c5bSmacallan  # This can happen when creating conditional directories.
2034a041c5bSmacallan  exit 0
2044a041c5bSmacallanfi
2054a041c5bSmacallan
2060a392d7eSmrgif test -z "$dir_arg"; then
2070a392d7eSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2080a392d7eSmrg    if test ! -d "$dst_arg"; then
2090a392d7eSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2100a392d7eSmrg      exit 1
2110a392d7eSmrg    fi
2120a392d7eSmrg  fi
2130a392d7eSmrgfi
2140a392d7eSmrg
2154a041c5bSmacallanif test -z "$dir_arg"; then
21654b44505Smrg  do_exit='(exit $ret); exit $ret'
21754b44505Smrg  trap "ret=129; $do_exit" 1
21854b44505Smrg  trap "ret=130; $do_exit" 2
21954b44505Smrg  trap "ret=141; $do_exit" 13
22054b44505Smrg  trap "ret=143; $do_exit" 15
2214a041c5bSmacallan
2224a041c5bSmacallan  # Set umask so as not to create temps with too-generous modes.
2234a041c5bSmacallan  # However, 'strip' requires both read and write access to temps.
2244a041c5bSmacallan  case $mode in
2254a041c5bSmacallan    # Optimize common cases.
2264a041c5bSmacallan    *644) cp_umask=133;;
2274a041c5bSmacallan    *755) cp_umask=22;;
2284a041c5bSmacallan
2294a041c5bSmacallan    *[0-7])
2304a041c5bSmacallan      if test -z "$stripcmd"; then
2310a392d7eSmrg        u_plus_rw=
2324a041c5bSmacallan      else
2330a392d7eSmrg        u_plus_rw='% 200'
2344a041c5bSmacallan      fi
2354a041c5bSmacallan      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2364a041c5bSmacallan    *)
2374a041c5bSmacallan      if test -z "$stripcmd"; then
2380a392d7eSmrg        u_plus_rw=
2394a041c5bSmacallan      else
2400a392d7eSmrg        u_plus_rw=,u+rw
2414a041c5bSmacallan      fi
2424a041c5bSmacallan      cp_umask=$mode$u_plus_rw;;
2434a041c5bSmacallan  esac
2444a041c5bSmacallanfi
2454a041c5bSmacallan
2464a041c5bSmacallanfor src
2474a041c5bSmacallando
24854b44505Smrg  # Protect names problematic for 'test' and other utilities.
2494a041c5bSmacallan  case $src in
25054b44505Smrg    -* | [=\(\)!]) src=./$src;;
2514a041c5bSmacallan  esac
2524a041c5bSmacallan
2534a041c5bSmacallan  if test -n "$dir_arg"; then
2544a041c5bSmacallan    dst=$src
2554a041c5bSmacallan    dstdir=$dst
2564a041c5bSmacallan    test -d "$dstdir"
2574a041c5bSmacallan    dstdir_status=$?
2584a041c5bSmacallan  else
2594a041c5bSmacallan
2604a041c5bSmacallan    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2614a041c5bSmacallan    # might cause directories to be created, which would be especially bad
2624a041c5bSmacallan    # if $src (and thus $dsttmp) contains '*'.
2634a041c5bSmacallan    if test ! -f "$src" && test ! -d "$src"; then
2644a041c5bSmacallan      echo "$0: $src does not exist." >&2
2654a041c5bSmacallan      exit 1
2664a041c5bSmacallan    fi
2674a041c5bSmacallan
2684a041c5bSmacallan    if test -z "$dst_arg"; then
2694a041c5bSmacallan      echo "$0: no destination specified." >&2
2704a041c5bSmacallan      exit 1
2714a041c5bSmacallan    fi
2724a041c5bSmacallan    dst=$dst_arg
2734a041c5bSmacallan
2744a041c5bSmacallan    # If destination is a directory, append the input filename; won't work
2754a041c5bSmacallan    # if double slashes aren't ignored.
2764a041c5bSmacallan    if test -d "$dst"; then
2770a392d7eSmrg      if test "$is_target_a_directory" = never; then
2780a392d7eSmrg        echo "$0: $dst_arg: Is a directory" >&2
2790a392d7eSmrg        exit 1
2804a041c5bSmacallan      fi
2814a041c5bSmacallan      dstdir=$dst
2824a041c5bSmacallan      dst=$dstdir/`basename "$src"`
2834a041c5bSmacallan      dstdir_status=0
2844a041c5bSmacallan    else
2850a392d7eSmrg      dstdir=`dirname "$dst"`
2864a041c5bSmacallan      test -d "$dstdir"
2874a041c5bSmacallan      dstdir_status=$?
2884a041c5bSmacallan    fi
2894a041c5bSmacallan  fi
2904a041c5bSmacallan
2914a041c5bSmacallan  obsolete_mkdir_used=false
2924a041c5bSmacallan
2934a041c5bSmacallan  if test $dstdir_status != 0; then
2944a041c5bSmacallan    case $posix_mkdir in
2954a041c5bSmacallan      '')
2960a392d7eSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
2970a392d7eSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
2980a392d7eSmrg        umask=`umask`
2990a392d7eSmrg        case $stripcmd.$umask in
3000a392d7eSmrg          # Optimize common cases.
3010a392d7eSmrg          *[2367][2367]) mkdir_umask=$umask;;
3020a392d7eSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3030a392d7eSmrg
3040a392d7eSmrg          *[0-7])
3050a392d7eSmrg            mkdir_umask=`expr $umask + 22 \
3060a392d7eSmrg              - $umask % 100 % 40 + $umask % 20 \
3070a392d7eSmrg              - $umask % 10 % 4 + $umask % 2
3080a392d7eSmrg            `;;
3090a392d7eSmrg          *) mkdir_umask=$umask,go-w;;
3100a392d7eSmrg        esac
3110a392d7eSmrg
3120a392d7eSmrg        # With -d, create the new directory with the user-specified mode.
3130a392d7eSmrg        # Otherwise, rely on $mkdir_umask.
3140a392d7eSmrg        if test -n "$dir_arg"; then
3150a392d7eSmrg          mkdir_mode=-m$mode
3160a392d7eSmrg        else
3170a392d7eSmrg          mkdir_mode=
3180a392d7eSmrg        fi
3190a392d7eSmrg
3200a392d7eSmrg        posix_mkdir=false
3210a392d7eSmrg        case $umask in
3220a392d7eSmrg          *[123567][0-7][0-7])
3230a392d7eSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3240a392d7eSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3250a392d7eSmrg            ;;
3260a392d7eSmrg          *)
3270a392d7eSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3280a392d7eSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3290a392d7eSmrg
3300a392d7eSmrg            if (umask $mkdir_umask &&
3310a392d7eSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3320a392d7eSmrg            then
3330a392d7eSmrg              if test -z "$dir_arg" || {
3340a392d7eSmrg                   # Check for POSIX incompatibilities with -m.
3350a392d7eSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3360a392d7eSmrg                   # other-writable bit of parent directory when it shouldn't.
3370a392d7eSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3380a392d7eSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3390a392d7eSmrg                   case $ls_ld_tmpdir in
3400a392d7eSmrg                     d????-?r-*) different_mode=700;;
3410a392d7eSmrg                     d????-?--*) different_mode=755;;
3420a392d7eSmrg                     *) false;;
3430a392d7eSmrg                   esac &&
3440a392d7eSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3450a392d7eSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3460a392d7eSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3470a392d7eSmrg                   }
3480a392d7eSmrg                 }
3490a392d7eSmrg              then posix_mkdir=:
3500a392d7eSmrg              fi
3510a392d7eSmrg              rmdir "$tmpdir/d" "$tmpdir"
3520a392d7eSmrg            else
3530a392d7eSmrg              # Remove any dirs left behind by ancient mkdir implementations.
3540a392d7eSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3550a392d7eSmrg            fi
3560a392d7eSmrg            trap '' 0;;
3570a392d7eSmrg        esac;;
3584a041c5bSmacallan    esac
3594a041c5bSmacallan
3604a041c5bSmacallan    if
3614a041c5bSmacallan      $posix_mkdir && (
3620a392d7eSmrg        umask $mkdir_umask &&
3630a392d7eSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3644a041c5bSmacallan      )
3654a041c5bSmacallan    then :
3664a041c5bSmacallan    else
3674a041c5bSmacallan
3684a041c5bSmacallan      # The umask is ridiculous, or mkdir does not conform to POSIX,
3694a041c5bSmacallan      # or it failed possibly due to a race condition.  Create the
3704a041c5bSmacallan      # directory the slow way, step by step, checking for races as we go.
3714a041c5bSmacallan
3724a041c5bSmacallan      case $dstdir in
3730a392d7eSmrg        /*) prefix='/';;
3740a392d7eSmrg        [-=\(\)!]*) prefix='./';;
3750a392d7eSmrg        *)  prefix='';;
3764a041c5bSmacallan      esac
3774a041c5bSmacallan
3784a041c5bSmacallan      oIFS=$IFS
3794a041c5bSmacallan      IFS=/
3800a392d7eSmrg      set -f
3814a041c5bSmacallan      set fnord $dstdir
3824a041c5bSmacallan      shift
3830a392d7eSmrg      set +f
3844a041c5bSmacallan      IFS=$oIFS
3854a041c5bSmacallan
3864a041c5bSmacallan      prefixes=
3874a041c5bSmacallan
3884a041c5bSmacallan      for d
3894a041c5bSmacallan      do
3900a392d7eSmrg        test X"$d" = X && continue
3910a392d7eSmrg
3920a392d7eSmrg        prefix=$prefix$d
3930a392d7eSmrg        if test -d "$prefix"; then
3940a392d7eSmrg          prefixes=
3950a392d7eSmrg        else
3960a392d7eSmrg          if $posix_mkdir; then
3970a392d7eSmrg            (umask=$mkdir_umask &&
3980a392d7eSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3990a392d7eSmrg            # Don't fail if two instances are running concurrently.
4000a392d7eSmrg            test -d "$prefix" || exit 1
4010a392d7eSmrg          else
4020a392d7eSmrg            case $prefix in
4030a392d7eSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4040a392d7eSmrg              *) qprefix=$prefix;;
4050a392d7eSmrg            esac
4060a392d7eSmrg            prefixes="$prefixes '$qprefix'"
4070a392d7eSmrg          fi
4080a392d7eSmrg        fi
4090a392d7eSmrg        prefix=$prefix/
4104a041c5bSmacallan      done
4114a041c5bSmacallan
4124a041c5bSmacallan      if test -n "$prefixes"; then
4130a392d7eSmrg        # Don't fail if two instances are running concurrently.
4140a392d7eSmrg        (umask $mkdir_umask &&
4150a392d7eSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4160a392d7eSmrg          test -d "$dstdir" || exit 1
4170a392d7eSmrg        obsolete_mkdir_used=true
4184a041c5bSmacallan      fi
4194a041c5bSmacallan    fi
4204a041c5bSmacallan  fi
4214a041c5bSmacallan
4224a041c5bSmacallan  if test -n "$dir_arg"; then
4234a041c5bSmacallan    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4244a041c5bSmacallan    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4254a041c5bSmacallan    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4264a041c5bSmacallan      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4274a041c5bSmacallan  else
4284a041c5bSmacallan
4294a041c5bSmacallan    # Make a couple of temp file names in the proper directory.
4304a041c5bSmacallan    dsttmp=$dstdir/_inst.$$_
4314a041c5bSmacallan    rmtmp=$dstdir/_rm.$$_
4324a041c5bSmacallan
4334a041c5bSmacallan    # Trap to clean up those temp files at exit.
4344a041c5bSmacallan    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4354a041c5bSmacallan
4364a041c5bSmacallan    # Copy the file name to the temp name.
4374a041c5bSmacallan    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4384a041c5bSmacallan
4394a041c5bSmacallan    # and set any options; do chmod last to preserve setuid bits.
4404a041c5bSmacallan    #
4414a041c5bSmacallan    # If any of these fail, we abort the whole thing.  If we want to
4424a041c5bSmacallan    # ignore errors from any of these, just make sure not to ignore
4434a041c5bSmacallan    # errors from the above "$doit $cpprog $src $dsttmp" command.
4444a041c5bSmacallan    #
4454a041c5bSmacallan    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4464a041c5bSmacallan    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4474a041c5bSmacallan    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4484a041c5bSmacallan    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4494a041c5bSmacallan
4504a041c5bSmacallan    # If -C, don't bother to copy if it wouldn't change the file.
4514a041c5bSmacallan    if $copy_on_change &&
4520a392d7eSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4530a392d7eSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4540a392d7eSmrg       set -f &&
4554a041c5bSmacallan       set X $old && old=:$2:$4:$5:$6 &&
4564a041c5bSmacallan       set X $new && new=:$2:$4:$5:$6 &&
4570a392d7eSmrg       set +f &&
4584a041c5bSmacallan       test "$old" = "$new" &&
4594a041c5bSmacallan       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4604a041c5bSmacallan    then
4614a041c5bSmacallan      rm -f "$dsttmp"
4624a041c5bSmacallan    else
4634a041c5bSmacallan      # Rename the file to the real destination.
4644a041c5bSmacallan      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4654a041c5bSmacallan
4664a041c5bSmacallan      # The rename failed, perhaps because mv can't rename something else
4674a041c5bSmacallan      # to itself, or perhaps because mv is so ancient that it does not
4684a041c5bSmacallan      # support -f.
4694a041c5bSmacallan      {
4700a392d7eSmrg        # Now remove or move aside any old file at destination location.
4710a392d7eSmrg        # We try this two ways since rm can't unlink itself on some
4720a392d7eSmrg        # systems and the destination file might be busy for other
4730a392d7eSmrg        # reasons.  In this case, the final cleanup might fail but the new
4740a392d7eSmrg        # file should still install successfully.
4750a392d7eSmrg        {
4760a392d7eSmrg          test ! -f "$dst" ||
4770a392d7eSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4780a392d7eSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4790a392d7eSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4800a392d7eSmrg          } ||
4810a392d7eSmrg          { echo "$0: cannot unlink or rename $dst" >&2
4820a392d7eSmrg            (exit 1); exit 1
4830a392d7eSmrg          }
4840a392d7eSmrg        } &&
4850a392d7eSmrg
4860a392d7eSmrg        # Now rename the file to the real destination.
4870a392d7eSmrg        $doit $mvcmd "$dsttmp" "$dst"
4884a041c5bSmacallan      }
4894a041c5bSmacallan    fi || exit 1
4904a041c5bSmacallan
4914a041c5bSmacallan    trap '' 0
4924a041c5bSmacallan  fi
4934a041c5bSmacallandone
4944a041c5bSmacallan
4954a041c5bSmacallan# Local variables:
4964a041c5bSmacallan# eval: (add-hook 'write-file-hooks 'time-stamp)
4974a041c5bSmacallan# time-stamp-start: "scriptversion="
4984a041c5bSmacallan# time-stamp-format: "%:y-%02m-%02d.%02H"
49954b44505Smrg# time-stamp-time-zone: "UTC"
50054b44505Smrg# time-stamp-end: "; # UTC"
5014a041c5bSmacallan# End:
502