117a48c7cSmrg#!/bin/sh
217a48c7cSmrg# install - install a program, script, or datafile
317a48c7cSmrg
436e956c5Smrgscriptversion=2014-09-12.12; # UTC
517a48c7cSmrg
617a48c7cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
717a48c7cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
817a48c7cSmrg# following copyright and license.
917a48c7cSmrg#
1017a48c7cSmrg# Copyright (C) 1994 X Consortium
1117a48c7cSmrg#
1217a48c7cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1317a48c7cSmrg# of this software and associated documentation files (the "Software"), to
1417a48c7cSmrg# deal in the Software without restriction, including without limitation the
1517a48c7cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1617a48c7cSmrg# sell copies of the Software, and to permit persons to whom the Software is
1717a48c7cSmrg# furnished to do so, subject to the following conditions:
1817a48c7cSmrg#
1917a48c7cSmrg# The above copyright notice and this permission notice shall be included in
2017a48c7cSmrg# all copies or substantial portions of the Software.
2117a48c7cSmrg#
2217a48c7cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2317a48c7cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2417a48c7cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2517a48c7cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2617a48c7cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2717a48c7cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2817a48c7cSmrg#
2917a48c7cSmrg# Except as contained in this notice, the name of the X Consortium shall not
3017a48c7cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3117a48c7cSmrg# ings in this Software without prior written authorization from the X Consor-
3217a48c7cSmrg# tium.
3317a48c7cSmrg#
3417a48c7cSmrg#
3517a48c7cSmrg# FSF changes to this file are in the public domain.
3617a48c7cSmrg#
3717a48c7cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3836e956c5Smrg# 'make' implicit rules from creating a file called install from it
3917a48c7cSmrg# when there is no Makefile.
4017a48c7cSmrg#
4117a48c7cSmrg# This script is compatible with the BSD install script, but was written
428e0ed500Smrg# from scratch.
438e0ed500Smrg
4436e956c5Smrgtab='	'
458e0ed500Smrgnl='
468e0ed500Smrg'
4736e956c5SmrgIFS=" $tab$nl"
4817a48c7cSmrg
4936e956c5Smrg# Set DOITPROG to "echo" to test this script.
5017a48c7cSmrg
51fbed5abfSmrgdoit=${DOITPROG-}
5236e956c5Smrgdoit_exec=${doit:-exec}
5317a48c7cSmrg
548e0ed500Smrg# Put in absolute file names if you don't have them in your path;
558e0ed500Smrg# or use environment vars.
5617a48c7cSmrg
57fbed5abfSmrgchgrpprog=${CHGRPPROG-chgrp}
58fbed5abfSmrgchmodprog=${CHMODPROG-chmod}
59fbed5abfSmrgchownprog=${CHOWNPROG-chown}
60fbed5abfSmrgcmpprog=${CMPPROG-cmp}
61fbed5abfSmrgcpprog=${CPPROG-cp}
62fbed5abfSmrgmkdirprog=${MKDIRPROG-mkdir}
63fbed5abfSmrgmvprog=${MVPROG-mv}
64fbed5abfSmrgrmprog=${RMPROG-rm}
65fbed5abfSmrgstripprog=${STRIPPROG-strip}
66fbed5abfSmrg
678e0ed500Smrgposix_mkdir=
688e0ed500Smrg
698e0ed500Smrg# Desired mode of installed file.
708e0ed500Smrgmode=0755
718e0ed500Smrg
72fbed5abfSmrgchgrpcmd=
738e0ed500Smrgchmodcmd=$chmodprog
7417a48c7cSmrgchowncmd=
75fbed5abfSmrgmvcmd=$mvprog
7617a48c7cSmrgrmcmd="$rmprog -f"
77fbed5abfSmrgstripcmd=
78fbed5abfSmrg
7917a48c7cSmrgsrc=
8017a48c7cSmrgdst=
8117a48c7cSmrgdir_arg=
82fbed5abfSmrgdst_arg=
83fbed5abfSmrg
84fbed5abfSmrgcopy_on_change=false
8536e956c5Smrgis_target_a_directory=possibly
8617a48c7cSmrg
87fbed5abfSmrgusage="\
88fbed5abfSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8917a48c7cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9017a48c7cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9117a48c7cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9217a48c7cSmrg
9317a48c7cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9417a48c7cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9517a48c7cSmrgIn the 4th, create DIRECTORIES.
9617a48c7cSmrg
9717a48c7cSmrgOptions:
98fbed5abfSmrg     --help     display this help and exit.
99fbed5abfSmrg     --version  display version info and exit.
100fbed5abfSmrg
101fbed5abfSmrg  -c            (ignored)
102fbed5abfSmrg  -C            install only if different (preserve the last data modification time)
103fbed5abfSmrg  -d            create directories instead of installing files.
104fbed5abfSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105fbed5abfSmrg  -m MODE       $chmodprog installed files to MODE.
106fbed5abfSmrg  -o USER       $chownprog installed files to USER.
107fbed5abfSmrg  -s            $stripprog installed files.
108fbed5abfSmrg  -t DIRECTORY  install into DIRECTORY.
109fbed5abfSmrg  -T            report an error if DSTFILE is a directory.
11017a48c7cSmrg
11117a48c7cSmrgEnvironment variables override the default commands:
112fbed5abfSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113fbed5abfSmrg  RMPROG STRIPPROG
11417a48c7cSmrg"
11517a48c7cSmrg
1168e0ed500Smrgwhile test $# -ne 0; do
11717a48c7cSmrg  case $1 in
118fbed5abfSmrg    -c) ;;
119fbed5abfSmrg
120fbed5abfSmrg    -C) copy_on_change=true;;
12117a48c7cSmrg
122fbed5abfSmrg    -d) dir_arg=true;;
12317a48c7cSmrg
12417a48c7cSmrg    -g) chgrpcmd="$chgrpprog $2"
12536e956c5Smrg        shift;;
12617a48c7cSmrg
12717a48c7cSmrg    --help) echo "$usage"; exit $?;;
12817a48c7cSmrg
1298e0ed500Smrg    -m) mode=$2
13036e956c5Smrg        case $mode in
13136e956c5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13236e956c5Smrg            echo "$0: invalid mode: $mode" >&2
13336e956c5Smrg            exit 1;;
13436e956c5Smrg        esac
13536e956c5Smrg        shift;;
13617a48c7cSmrg
13717a48c7cSmrg    -o) chowncmd="$chownprog $2"
13836e956c5Smrg        shift;;
13917a48c7cSmrg
140fbed5abfSmrg    -s) stripcmd=$stripprog;;
14117a48c7cSmrg
14236e956c5Smrg    -t)
14336e956c5Smrg        is_target_a_directory=always
14436e956c5Smrg        dst_arg=$2
14536e956c5Smrg        # Protect names problematic for 'test' and other utilities.
14636e956c5Smrg        case $dst_arg in
14736e956c5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14836e956c5Smrg        esac
14936e956c5Smrg        shift;;
15017a48c7cSmrg
15136e956c5Smrg    -T) is_target_a_directory=never;;
15217a48c7cSmrg
15317a48c7cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
15417a48c7cSmrg
15536e956c5Smrg    --) shift
15636e956c5Smrg        break;;
1578e0ed500Smrg
15836e956c5Smrg    -*) echo "$0: invalid option: $1" >&2
15936e956c5Smrg        exit 1;;
1608e0ed500Smrg
1618e0ed500Smrg    *)  break;;
16217a48c7cSmrg  esac
163fbed5abfSmrg  shift
16417a48c7cSmrgdone
16517a48c7cSmrg
16636e956c5Smrg# We allow the use of options -d and -T together, by making -d
16736e956c5Smrg# take the precedence; this is for compatibility with GNU install.
16836e956c5Smrg
16936e956c5Smrgif test -n "$dir_arg"; then
17036e956c5Smrg  if test -n "$dst_arg"; then
17136e956c5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17236e956c5Smrg    exit 1
17336e956c5Smrg  fi
17436e956c5Smrgfi
17536e956c5Smrg
176fbed5abfSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1778e0ed500Smrg  # When -d is used, all remaining arguments are directories to create.
1788e0ed500Smrg  # When -t is used, the destination is already specified.
1798e0ed500Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1808e0ed500Smrg  for arg
1818e0ed500Smrg  do
182fbed5abfSmrg    if test -n "$dst_arg"; then
1838e0ed500Smrg      # $@ is not empty: it contains at least $arg.
184fbed5abfSmrg      set fnord "$@" "$dst_arg"
1858e0ed500Smrg      shift # fnord
1868e0ed500Smrg    fi
1878e0ed500Smrg    shift # arg
188fbed5abfSmrg    dst_arg=$arg
18936e956c5Smrg    # Protect names problematic for 'test' and other utilities.
1900dd80ee0Smrg    case $dst_arg in
1910dd80ee0Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1920dd80ee0Smrg    esac
1938e0ed500Smrg  done
1948e0ed500Smrgfi
1958e0ed500Smrg
1968e0ed500Smrgif test $# -eq 0; then
19717a48c7cSmrg  if test -z "$dir_arg"; then
19817a48c7cSmrg    echo "$0: no input file specified." >&2
19917a48c7cSmrg    exit 1
20017a48c7cSmrg  fi
20136e956c5Smrg  # It's OK to call 'install-sh -d' without argument.
20217a48c7cSmrg  # This can happen when creating conditional directories.
20317a48c7cSmrg  exit 0
20417a48c7cSmrgfi
20517a48c7cSmrg
20636e956c5Smrgif test -z "$dir_arg"; then
20736e956c5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20836e956c5Smrg    if test ! -d "$dst_arg"; then
20936e956c5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21036e956c5Smrg      exit 1
21136e956c5Smrg    fi
21236e956c5Smrg  fi
21336e956c5Smrgfi
21436e956c5Smrg
2158e0ed500Smrgif test -z "$dir_arg"; then
2160dd80ee0Smrg  do_exit='(exit $ret); exit $ret'
2170dd80ee0Smrg  trap "ret=129; $do_exit" 1
2180dd80ee0Smrg  trap "ret=130; $do_exit" 2
2190dd80ee0Smrg  trap "ret=141; $do_exit" 13
2200dd80ee0Smrg  trap "ret=143; $do_exit" 15
2218e0ed500Smrg
2228e0ed500Smrg  # Set umask so as not to create temps with too-generous modes.
2238e0ed500Smrg  # However, 'strip' requires both read and write access to temps.
2248e0ed500Smrg  case $mode in
2258e0ed500Smrg    # Optimize common cases.
2268e0ed500Smrg    *644) cp_umask=133;;
2278e0ed500Smrg    *755) cp_umask=22;;
2288e0ed500Smrg
2298e0ed500Smrg    *[0-7])
2308e0ed500Smrg      if test -z "$stripcmd"; then
23136e956c5Smrg        u_plus_rw=
2328e0ed500Smrg      else
23336e956c5Smrg        u_plus_rw='% 200'
2348e0ed500Smrg      fi
2358e0ed500Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2368e0ed500Smrg    *)
2378e0ed500Smrg      if test -z "$stripcmd"; then
23836e956c5Smrg        u_plus_rw=
2398e0ed500Smrg      else
24036e956c5Smrg        u_plus_rw=,u+rw
2418e0ed500Smrg      fi
2428e0ed500Smrg      cp_umask=$mode$u_plus_rw;;
2438e0ed500Smrg  esac
2448e0ed500Smrgfi
2458e0ed500Smrg
24617a48c7cSmrgfor src
24717a48c7cSmrgdo
24836e956c5Smrg  # Protect names problematic for 'test' and other utilities.
24917a48c7cSmrg  case $src in
2500dd80ee0Smrg    -* | [=\(\)!]) src=./$src;;
25117a48c7cSmrg  esac
25217a48c7cSmrg
25317a48c7cSmrg  if test -n "$dir_arg"; then
25417a48c7cSmrg    dst=$src
2558e0ed500Smrg    dstdir=$dst
2568e0ed500Smrg    test -d "$dstdir"
2578e0ed500Smrg    dstdir_status=$?
25817a48c7cSmrg  else
2598e0ed500Smrg
26017a48c7cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26117a48c7cSmrg    # might cause directories to be created, which would be especially bad
26217a48c7cSmrg    # if $src (and thus $dsttmp) contains '*'.
26317a48c7cSmrg    if test ! -f "$src" && test ! -d "$src"; then
26417a48c7cSmrg      echo "$0: $src does not exist." >&2
26517a48c7cSmrg      exit 1
26617a48c7cSmrg    fi
26717a48c7cSmrg
268fbed5abfSmrg    if test -z "$dst_arg"; then
26917a48c7cSmrg      echo "$0: no destination specified." >&2
27017a48c7cSmrg      exit 1
27117a48c7cSmrg    fi
272fbed5abfSmrg    dst=$dst_arg
27317a48c7cSmrg
27417a48c7cSmrg    # If destination is a directory, append the input filename; won't work
27517a48c7cSmrg    # if double slashes aren't ignored.
27617a48c7cSmrg    if test -d "$dst"; then
27736e956c5Smrg      if test "$is_target_a_directory" = never; then
27836e956c5Smrg        echo "$0: $dst_arg: Is a directory" >&2
27936e956c5Smrg        exit 1
28017a48c7cSmrg      fi
2818e0ed500Smrg      dstdir=$dst
2828e0ed500Smrg      dst=$dstdir/`basename "$src"`
2838e0ed500Smrg      dstdir_status=0
2848e0ed500Smrg    else
28536e956c5Smrg      dstdir=`dirname "$dst"`
2868e0ed500Smrg      test -d "$dstdir"
2878e0ed500Smrg      dstdir_status=$?
28817a48c7cSmrg    fi
28917a48c7cSmrg  fi
29017a48c7cSmrg
2918e0ed500Smrg  obsolete_mkdir_used=false
2928e0ed500Smrg
2938e0ed500Smrg  if test $dstdir_status != 0; then
2948e0ed500Smrg    case $posix_mkdir in
2958e0ed500Smrg      '')
29636e956c5Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29736e956c5Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29836e956c5Smrg        umask=`umask`
29936e956c5Smrg        case $stripcmd.$umask in
30036e956c5Smrg          # Optimize common cases.
30136e956c5Smrg          *[2367][2367]) mkdir_umask=$umask;;
30236e956c5Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30336e956c5Smrg
30436e956c5Smrg          *[0-7])
30536e956c5Smrg            mkdir_umask=`expr $umask + 22 \
30636e956c5Smrg              - $umask % 100 % 40 + $umask % 20 \
30736e956c5Smrg              - $umask % 10 % 4 + $umask % 2
30836e956c5Smrg            `;;
30936e956c5Smrg          *) mkdir_umask=$umask,go-w;;
31036e956c5Smrg        esac
31136e956c5Smrg
31236e956c5Smrg        # With -d, create the new directory with the user-specified mode.
31336e956c5Smrg        # Otherwise, rely on $mkdir_umask.
31436e956c5Smrg        if test -n "$dir_arg"; then
31536e956c5Smrg          mkdir_mode=-m$mode
31636e956c5Smrg        else
31736e956c5Smrg          mkdir_mode=
31836e956c5Smrg        fi
31936e956c5Smrg
32036e956c5Smrg        posix_mkdir=false
32136e956c5Smrg        case $umask in
32236e956c5Smrg          *[123567][0-7][0-7])
32336e956c5Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32436e956c5Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32536e956c5Smrg            ;;
32636e956c5Smrg          *)
32736e956c5Smrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
32836e956c5Smrg            # lower collision chance
32936e956c5Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33036e956c5Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
33136e956c5Smrg
33236e956c5Smrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
33336e956c5Smrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
33436e956c5Smrg            # that nobody tries to guess the $tmpdir name.
33536e956c5Smrg            if (umask $mkdir_umask &&
33636e956c5Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
33736e956c5Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
33836e956c5Smrg            then
33936e956c5Smrg              if test -z "$dir_arg" || {
34036e956c5Smrg                   # Check for POSIX incompatibilities with -m.
34136e956c5Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
34236e956c5Smrg                   # other-writable bit of parent directory when it shouldn't.
34336e956c5Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
34436e956c5Smrg                   test_tmpdir="$tmpdir/a"
34536e956c5Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
34636e956c5Smrg                   case $ls_ld_tmpdir in
34736e956c5Smrg                     d????-?r-*) different_mode=700;;
34836e956c5Smrg                     d????-?--*) different_mode=755;;
34936e956c5Smrg                     *) false;;
35036e956c5Smrg                   esac &&
35136e956c5Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
35236e956c5Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
35336e956c5Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
35436e956c5Smrg                   }
35536e956c5Smrg                 }
35636e956c5Smrg              then posix_mkdir=:
35736e956c5Smrg              fi
35836e956c5Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
35936e956c5Smrg            else
36036e956c5Smrg              # Remove any dirs left behind by ancient mkdir implementations.
36136e956c5Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
36236e956c5Smrg            fi
36336e956c5Smrg            trap '' 0;;
36436e956c5Smrg        esac;;
3658e0ed500Smrg    esac
36617a48c7cSmrg
3678e0ed500Smrg    if
3688e0ed500Smrg      $posix_mkdir && (
36936e956c5Smrg        umask $mkdir_umask &&
37036e956c5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3718e0ed500Smrg      )
3728e0ed500Smrg    then :
3738e0ed500Smrg    else
37417a48c7cSmrg
3758e0ed500Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3768e0ed500Smrg      # or it failed possibly due to a race condition.  Create the
3778e0ed500Smrg      # directory the slow way, step by step, checking for races as we go.
3788e0ed500Smrg
3798e0ed500Smrg      case $dstdir in
38036e956c5Smrg        /*) prefix='/';;
38136e956c5Smrg        [-=\(\)!]*) prefix='./';;
38236e956c5Smrg        *)  prefix='';;
3838e0ed500Smrg      esac
3848e0ed500Smrg
3858e0ed500Smrg      oIFS=$IFS
3868e0ed500Smrg      IFS=/
38736e956c5Smrg      set -f
3888e0ed500Smrg      set fnord $dstdir
38917a48c7cSmrg      shift
39036e956c5Smrg      set +f
3918e0ed500Smrg      IFS=$oIFS
3928e0ed500Smrg
3938e0ed500Smrg      prefixes=
3948e0ed500Smrg
3958e0ed500Smrg      for d
3968e0ed500Smrg      do
39736e956c5Smrg        test X"$d" = X && continue
39836e956c5Smrg
39936e956c5Smrg        prefix=$prefix$d
40036e956c5Smrg        if test -d "$prefix"; then
40136e956c5Smrg          prefixes=
40236e956c5Smrg        else
40336e956c5Smrg          if $posix_mkdir; then
40436e956c5Smrg            (umask=$mkdir_umask &&
40536e956c5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
40636e956c5Smrg            # Don't fail if two instances are running concurrently.
40736e956c5Smrg            test -d "$prefix" || exit 1
40836e956c5Smrg          else
40936e956c5Smrg            case $prefix in
41036e956c5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
41136e956c5Smrg              *) qprefix=$prefix;;
41236e956c5Smrg            esac
41336e956c5Smrg            prefixes="$prefixes '$qprefix'"
41436e956c5Smrg          fi
41536e956c5Smrg        fi
41636e956c5Smrg        prefix=$prefix/
4178e0ed500Smrg      done
4188e0ed500Smrg
4198e0ed500Smrg      if test -n "$prefixes"; then
42036e956c5Smrg        # Don't fail if two instances are running concurrently.
42136e956c5Smrg        (umask $mkdir_umask &&
42236e956c5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
42336e956c5Smrg          test -d "$dstdir" || exit 1
42436e956c5Smrg        obsolete_mkdir_used=true
42517a48c7cSmrg      fi
4268e0ed500Smrg    fi
42717a48c7cSmrg  fi
42817a48c7cSmrg
42917a48c7cSmrg  if test -n "$dir_arg"; then
4308e0ed500Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4318e0ed500Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4328e0ed500Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4338e0ed500Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
43417a48c7cSmrg  else
43517a48c7cSmrg
43617a48c7cSmrg    # Make a couple of temp file names in the proper directory.
43717a48c7cSmrg    dsttmp=$dstdir/_inst.$$_
43817a48c7cSmrg    rmtmp=$dstdir/_rm.$$_
43917a48c7cSmrg
44017a48c7cSmrg    # Trap to clean up those temp files at exit.
44117a48c7cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
44217a48c7cSmrg
44317a48c7cSmrg    # Copy the file name to the temp name.
4448e0ed500Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
44517a48c7cSmrg
44617a48c7cSmrg    # and set any options; do chmod last to preserve setuid bits.
44717a48c7cSmrg    #
44817a48c7cSmrg    # If any of these fail, we abort the whole thing.  If we want to
44917a48c7cSmrg    # ignore errors from any of these, just make sure not to ignore
45017a48c7cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
45117a48c7cSmrg    #
452fbed5abfSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
453fbed5abfSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
454fbed5abfSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
455fbed5abfSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
456fbed5abfSmrg
457fbed5abfSmrg    # If -C, don't bother to copy if it wouldn't change the file.
458fbed5abfSmrg    if $copy_on_change &&
45936e956c5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
46036e956c5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
46136e956c5Smrg       set -f &&
462fbed5abfSmrg       set X $old && old=:$2:$4:$5:$6 &&
463fbed5abfSmrg       set X $new && new=:$2:$4:$5:$6 &&
46436e956c5Smrg       set +f &&
465fbed5abfSmrg       test "$old" = "$new" &&
466fbed5abfSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
467fbed5abfSmrg    then
468fbed5abfSmrg      rm -f "$dsttmp"
469fbed5abfSmrg    else
470fbed5abfSmrg      # Rename the file to the real destination.
471fbed5abfSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
472fbed5abfSmrg
473fbed5abfSmrg      # The rename failed, perhaps because mv can't rename something else
474fbed5abfSmrg      # to itself, or perhaps because mv is so ancient that it does not
475fbed5abfSmrg      # support -f.
476fbed5abfSmrg      {
47736e956c5Smrg        # Now remove or move aside any old file at destination location.
47836e956c5Smrg        # We try this two ways since rm can't unlink itself on some
47936e956c5Smrg        # systems and the destination file might be busy for other
48036e956c5Smrg        # reasons.  In this case, the final cleanup might fail but the new
48136e956c5Smrg        # file should still install successfully.
48236e956c5Smrg        {
48336e956c5Smrg          test ! -f "$dst" ||
48436e956c5Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
48536e956c5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
48636e956c5Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48736e956c5Smrg          } ||
48836e956c5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48936e956c5Smrg            (exit 1); exit 1
49036e956c5Smrg          }
49136e956c5Smrg        } &&
49236e956c5Smrg
49336e956c5Smrg        # Now rename the file to the real destination.
49436e956c5Smrg        $doit $mvcmd "$dsttmp" "$dst"
495fbed5abfSmrg      }
496fbed5abfSmrg    fi || exit 1
49717a48c7cSmrg
4988e0ed500Smrg    trap '' 0
4998e0ed500Smrg  fi
5008e0ed500Smrgdone
50117a48c7cSmrg
50217a48c7cSmrg# Local variables:
50317a48c7cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50417a48c7cSmrg# time-stamp-start: "scriptversion="
50517a48c7cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506fbed5abfSmrg# time-stamp-time-zone: "UTC"
507fbed5abfSmrg# time-stamp-end: "; # UTC"
50817a48c7cSmrg# End:
509