172b676d7Smrg#!/bin/sh
272b676d7Smrg# install - install a program, script, or datafile
372b676d7Smrg
474fcc364Smrgscriptversion=2014-09-12.12; # UTC
572b676d7Smrg
672b676d7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
772b676d7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
872b676d7Smrg# following copyright and license.
972b676d7Smrg#
1072b676d7Smrg# Copyright (C) 1994 X Consortium
1172b676d7Smrg#
1272b676d7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1372b676d7Smrg# of this software and associated documentation files (the "Software"), to
1472b676d7Smrg# deal in the Software without restriction, including without limitation the
1572b676d7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1672b676d7Smrg# sell copies of the Software, and to permit persons to whom the Software is
1772b676d7Smrg# furnished to do so, subject to the following conditions:
1872b676d7Smrg#
1972b676d7Smrg# The above copyright notice and this permission notice shall be included in
2072b676d7Smrg# all copies or substantial portions of the Software.
2172b676d7Smrg#
2272b676d7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2372b676d7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2472b676d7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2572b676d7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2672b676d7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2772b676d7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2872b676d7Smrg#
2972b676d7Smrg# Except as contained in this notice, the name of the X Consortium shall not
3072b676d7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3172b676d7Smrg# ings in this Software without prior written authorization from the X Consor-
3272b676d7Smrg# tium.
3372b676d7Smrg#
3472b676d7Smrg#
3572b676d7Smrg# FSF changes to this file are in the public domain.
3672b676d7Smrg#
3772b676d7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3893d9adc1Smrg# 'make' implicit rules from creating a file called install from it
3972b676d7Smrg# when there is no Makefile.
4072b676d7Smrg#
4172b676d7Smrg# This script is compatible with the BSD install script, but was written
421fd23544Smrg# from scratch.
431fd23544Smrg
4493d9adc1Smrgtab='	'
451fd23544Smrgnl='
461fd23544Smrg'
4793d9adc1SmrgIFS=" $tab$nl"
4872b676d7Smrg
4993d9adc1Smrg# Set DOITPROG to "echo" to test this script.
5072b676d7Smrg
511fd23544Smrgdoit=${DOITPROG-}
5293d9adc1Smrgdoit_exec=${doit:-exec}
5372b676d7Smrg
541fd23544Smrg# Put in absolute file names if you don't have them in your path;
551fd23544Smrg# or use environment vars.
561fd23544Smrg
571fd23544Smrgchgrpprog=${CHGRPPROG-chgrp}
581fd23544Smrgchmodprog=${CHMODPROG-chmod}
591fd23544Smrgchownprog=${CHOWNPROG-chown}
601fd23544Smrgcmpprog=${CMPPROG-cmp}
611fd23544Smrgcpprog=${CPPROG-cp}
621fd23544Smrgmkdirprog=${MKDIRPROG-mkdir}
631fd23544Smrgmvprog=${MVPROG-mv}
641fd23544Smrgrmprog=${RMPROG-rm}
651fd23544Smrgstripprog=${STRIPPROG-strip}
661fd23544Smrg
671fd23544Smrgposix_mkdir=
681fd23544Smrg
691fd23544Smrg# Desired mode of installed file.
701fd23544Smrgmode=0755
7172b676d7Smrg
7272b676d7Smrgchgrpcmd=
731fd23544Smrgchmodcmd=$chmodprog
741fd23544Smrgchowncmd=
751fd23544Smrgmvcmd=$mvprog
7672b676d7Smrgrmcmd="$rmprog -f"
771fd23544Smrgstripcmd=
781fd23544Smrg
7972b676d7Smrgsrc=
8072b676d7Smrgdst=
8172b676d7Smrgdir_arg=
821fd23544Smrgdst_arg=
831fd23544Smrg
841fd23544Smrgcopy_on_change=false
8593d9adc1Smrgis_target_a_directory=possibly
8672b676d7Smrg
871fd23544Smrgusage="\
881fd23544SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8972b676d7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9072b676d7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9172b676d7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9272b676d7Smrg
9372b676d7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9472b676d7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9572b676d7SmrgIn the 4th, create DIRECTORIES.
9672b676d7Smrg
9772b676d7SmrgOptions:
981fd23544Smrg     --help     display this help and exit.
991fd23544Smrg     --version  display version info and exit.
1001fd23544Smrg
1011fd23544Smrg  -c            (ignored)
1021fd23544Smrg  -C            install only if different (preserve the last data modification time)
1031fd23544Smrg  -d            create directories instead of installing files.
1041fd23544Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1051fd23544Smrg  -m MODE       $chmodprog installed files to MODE.
1061fd23544Smrg  -o USER       $chownprog installed files to USER.
1071fd23544Smrg  -s            $stripprog installed files.
1081fd23544Smrg  -t DIRECTORY  install into DIRECTORY.
1091fd23544Smrg  -T            report an error if DSTFILE is a directory.
11072b676d7Smrg
11172b676d7SmrgEnvironment variables override the default commands:
1121fd23544Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1131fd23544Smrg  RMPROG STRIPPROG
11472b676d7Smrg"
11572b676d7Smrg
1161fd23544Smrgwhile test $# -ne 0; do
11772b676d7Smrg  case $1 in
1181fd23544Smrg    -c) ;;
1191fd23544Smrg
1201fd23544Smrg    -C) copy_on_change=true;;
12172b676d7Smrg
1221fd23544Smrg    -d) dir_arg=true;;
12372b676d7Smrg
12472b676d7Smrg    -g) chgrpcmd="$chgrpprog $2"
12593d9adc1Smrg        shift;;
12672b676d7Smrg
12772b676d7Smrg    --help) echo "$usage"; exit $?;;
12872b676d7Smrg
1291fd23544Smrg    -m) mode=$2
13093d9adc1Smrg        case $mode in
13193d9adc1Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13293d9adc1Smrg            echo "$0: invalid mode: $mode" >&2
13393d9adc1Smrg            exit 1;;
13493d9adc1Smrg        esac
13593d9adc1Smrg        shift;;
13672b676d7Smrg
13772b676d7Smrg    -o) chowncmd="$chownprog $2"
13893d9adc1Smrg        shift;;
13972b676d7Smrg
1401fd23544Smrg    -s) stripcmd=$stripprog;;
14172b676d7Smrg
14293d9adc1Smrg    -t)
14393d9adc1Smrg        is_target_a_directory=always
14493d9adc1Smrg        dst_arg=$2
14593d9adc1Smrg        # Protect names problematic for 'test' and other utilities.
14693d9adc1Smrg        case $dst_arg in
14793d9adc1Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14893d9adc1Smrg        esac
14993d9adc1Smrg        shift;;
15072b676d7Smrg
15193d9adc1Smrg    -T) is_target_a_directory=never;;
15272b676d7Smrg
15372b676d7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15472b676d7Smrg
15593d9adc1Smrg    --) shift
15693d9adc1Smrg        break;;
1571fd23544Smrg
15893d9adc1Smrg    -*) echo "$0: invalid option: $1" >&2
15993d9adc1Smrg        exit 1;;
1601fd23544Smrg
1611fd23544Smrg    *)  break;;
16272b676d7Smrg  esac
1631fd23544Smrg  shift
16472b676d7Smrgdone
16572b676d7Smrg
16693d9adc1Smrg# We allow the use of options -d and -T together, by making -d
16793d9adc1Smrg# take the precedence; this is for compatibility with GNU install.
16893d9adc1Smrg
16993d9adc1Smrgif test -n "$dir_arg"; then
17093d9adc1Smrg  if test -n "$dst_arg"; then
17193d9adc1Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17293d9adc1Smrg    exit 1
17393d9adc1Smrg  fi
17493d9adc1Smrgfi
17593d9adc1Smrg
1761fd23544Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771fd23544Smrg  # When -d is used, all remaining arguments are directories to create.
1781fd23544Smrg  # When -t is used, the destination is already specified.
1791fd23544Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801fd23544Smrg  for arg
1811fd23544Smrg  do
1821fd23544Smrg    if test -n "$dst_arg"; then
1831fd23544Smrg      # $@ is not empty: it contains at least $arg.
1841fd23544Smrg      set fnord "$@" "$dst_arg"
1851fd23544Smrg      shift # fnord
1861fd23544Smrg    fi
1871fd23544Smrg    shift # arg
1881fd23544Smrg    dst_arg=$arg
18993d9adc1Smrg    # Protect names problematic for 'test' and other utilities.
19093d9adc1Smrg    case $dst_arg in
19193d9adc1Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19293d9adc1Smrg    esac
1931fd23544Smrg  done
1941fd23544Smrgfi
1951fd23544Smrg
1961fd23544Smrgif test $# -eq 0; then
19772b676d7Smrg  if test -z "$dir_arg"; then
19872b676d7Smrg    echo "$0: no input file specified." >&2
19972b676d7Smrg    exit 1
20072b676d7Smrg  fi
20193d9adc1Smrg  # It's OK to call 'install-sh -d' without argument.
20272b676d7Smrg  # This can happen when creating conditional directories.
20372b676d7Smrg  exit 0
20472b676d7Smrgfi
20572b676d7Smrg
2061fd23544Smrgif test -z "$dir_arg"; then
20793d9adc1Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20893d9adc1Smrg    if test ! -d "$dst_arg"; then
20993d9adc1Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21093d9adc1Smrg      exit 1
21193d9adc1Smrg    fi
21293d9adc1Smrg  fi
21393d9adc1Smrgfi
21493d9adc1Smrg
21593d9adc1Smrgif test -z "$dir_arg"; then
21693d9adc1Smrg  do_exit='(exit $ret); exit $ret'
21793d9adc1Smrg  trap "ret=129; $do_exit" 1
21893d9adc1Smrg  trap "ret=130; $do_exit" 2
21993d9adc1Smrg  trap "ret=141; $do_exit" 13
22093d9adc1Smrg  trap "ret=143; $do_exit" 15
2211fd23544Smrg
2221fd23544Smrg  # Set umask so as not to create temps with too-generous modes.
2231fd23544Smrg  # However, 'strip' requires both read and write access to temps.
2241fd23544Smrg  case $mode in
2251fd23544Smrg    # Optimize common cases.
2261fd23544Smrg    *644) cp_umask=133;;
2271fd23544Smrg    *755) cp_umask=22;;
2281fd23544Smrg
2291fd23544Smrg    *[0-7])
2301fd23544Smrg      if test -z "$stripcmd"; then
23193d9adc1Smrg        u_plus_rw=
2321fd23544Smrg      else
23393d9adc1Smrg        u_plus_rw='% 200'
2341fd23544Smrg      fi
2351fd23544Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2361fd23544Smrg    *)
2371fd23544Smrg      if test -z "$stripcmd"; then
23893d9adc1Smrg        u_plus_rw=
2391fd23544Smrg      else
24093d9adc1Smrg        u_plus_rw=,u+rw
2411fd23544Smrg      fi
2421fd23544Smrg      cp_umask=$mode$u_plus_rw;;
2431fd23544Smrg  esac
2441fd23544Smrgfi
2451fd23544Smrg
24672b676d7Smrgfor src
24772b676d7Smrgdo
24893d9adc1Smrg  # Protect names problematic for 'test' and other utilities.
24972b676d7Smrg  case $src in
25093d9adc1Smrg    -* | [=\(\)!]) src=./$src;;
25172b676d7Smrg  esac
25272b676d7Smrg
25372b676d7Smrg  if test -n "$dir_arg"; then
25472b676d7Smrg    dst=$src
2551fd23544Smrg    dstdir=$dst
2561fd23544Smrg    test -d "$dstdir"
2571fd23544Smrg    dstdir_status=$?
25872b676d7Smrg  else
2591fd23544Smrg
26072b676d7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26172b676d7Smrg    # might cause directories to be created, which would be especially bad
26272b676d7Smrg    # if $src (and thus $dsttmp) contains '*'.
26372b676d7Smrg    if test ! -f "$src" && test ! -d "$src"; then
26472b676d7Smrg      echo "$0: $src does not exist." >&2
26572b676d7Smrg      exit 1
26672b676d7Smrg    fi
26772b676d7Smrg
2681fd23544Smrg    if test -z "$dst_arg"; then
26972b676d7Smrg      echo "$0: no destination specified." >&2
27072b676d7Smrg      exit 1
27172b676d7Smrg    fi
2721fd23544Smrg    dst=$dst_arg
27372b676d7Smrg
27472b676d7Smrg    # If destination is a directory, append the input filename; won't work
27572b676d7Smrg    # if double slashes aren't ignored.
27672b676d7Smrg    if test -d "$dst"; then
27793d9adc1Smrg      if test "$is_target_a_directory" = never; then
27893d9adc1Smrg        echo "$0: $dst_arg: Is a directory" >&2
27993d9adc1Smrg        exit 1
28072b676d7Smrg      fi
2811fd23544Smrg      dstdir=$dst
2821fd23544Smrg      dst=$dstdir/`basename "$src"`
2831fd23544Smrg      dstdir_status=0
2841fd23544Smrg    else
28593d9adc1Smrg      dstdir=`dirname "$dst"`
2861fd23544Smrg      test -d "$dstdir"
2871fd23544Smrg      dstdir_status=$?
28872b676d7Smrg    fi
28972b676d7Smrg  fi
29072b676d7Smrg
2911fd23544Smrg  obsolete_mkdir_used=false
2921fd23544Smrg
2931fd23544Smrg  if test $dstdir_status != 0; then
2941fd23544Smrg    case $posix_mkdir in
2951fd23544Smrg      '')
29693d9adc1Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29793d9adc1Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29893d9adc1Smrg        umask=`umask`
29993d9adc1Smrg        case $stripcmd.$umask in
30093d9adc1Smrg          # Optimize common cases.
30193d9adc1Smrg          *[2367][2367]) mkdir_umask=$umask;;
30293d9adc1Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30393d9adc1Smrg
30493d9adc1Smrg          *[0-7])
30593d9adc1Smrg            mkdir_umask=`expr $umask + 22 \
30693d9adc1Smrg              - $umask % 100 % 40 + $umask % 20 \
30793d9adc1Smrg              - $umask % 10 % 4 + $umask % 2
30893d9adc1Smrg            `;;
30993d9adc1Smrg          *) mkdir_umask=$umask,go-w;;
31093d9adc1Smrg        esac
31193d9adc1Smrg
31293d9adc1Smrg        # With -d, create the new directory with the user-specified mode.
31393d9adc1Smrg        # Otherwise, rely on $mkdir_umask.
31493d9adc1Smrg        if test -n "$dir_arg"; then
31593d9adc1Smrg          mkdir_mode=-m$mode
31693d9adc1Smrg        else
31793d9adc1Smrg          mkdir_mode=
31893d9adc1Smrg        fi
31993d9adc1Smrg
32093d9adc1Smrg        posix_mkdir=false
32193d9adc1Smrg        case $umask in
32293d9adc1Smrg          *[123567][0-7][0-7])
32393d9adc1Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32493d9adc1Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32593d9adc1Smrg            ;;
32693d9adc1Smrg          *)
32774fcc364Smrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
32874fcc364Smrg            # lower collision chance
32993d9adc1Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33074fcc364Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
33193d9adc1Smrg
33274fcc364Smrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
33374fcc364Smrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
33474fcc364Smrg            # that nobody tries to guess the $tmpdir name.
33593d9adc1Smrg            if (umask $mkdir_umask &&
33674fcc364Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
33774fcc364Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
33893d9adc1Smrg            then
33993d9adc1Smrg              if test -z "$dir_arg" || {
34093d9adc1Smrg                   # Check for POSIX incompatibilities with -m.
34193d9adc1Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
34293d9adc1Smrg                   # other-writable bit of parent directory when it shouldn't.
34393d9adc1Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
34474fcc364Smrg                   test_tmpdir="$tmpdir/a"
34574fcc364Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
34693d9adc1Smrg                   case $ls_ld_tmpdir in
34793d9adc1Smrg                     d????-?r-*) different_mode=700;;
34893d9adc1Smrg                     d????-?--*) different_mode=755;;
34993d9adc1Smrg                     *) false;;
35093d9adc1Smrg                   esac &&
35174fcc364Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
35274fcc364Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
35393d9adc1Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
35493d9adc1Smrg                   }
35593d9adc1Smrg                 }
35693d9adc1Smrg              then posix_mkdir=:
35793d9adc1Smrg              fi
35874fcc364Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
35993d9adc1Smrg            else
36093d9adc1Smrg              # Remove any dirs left behind by ancient mkdir implementations.
36174fcc364Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
36293d9adc1Smrg            fi
36393d9adc1Smrg            trap '' 0;;
36493d9adc1Smrg        esac;;
3651fd23544Smrg    esac
36672b676d7Smrg
3671fd23544Smrg    if
3681fd23544Smrg      $posix_mkdir && (
36993d9adc1Smrg        umask $mkdir_umask &&
37093d9adc1Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3711fd23544Smrg      )
3721fd23544Smrg    then :
3731fd23544Smrg    else
37472b676d7Smrg
3751fd23544Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3761fd23544Smrg      # or it failed possibly due to a race condition.  Create the
3771fd23544Smrg      # directory the slow way, step by step, checking for races as we go.
37872b676d7Smrg
3791fd23544Smrg      case $dstdir in
38093d9adc1Smrg        /*) prefix='/';;
38193d9adc1Smrg        [-=\(\)!]*) prefix='./';;
38293d9adc1Smrg        *)  prefix='';;
3831fd23544Smrg      esac
38472b676d7Smrg
3851fd23544Smrg      oIFS=$IFS
3861fd23544Smrg      IFS=/
38793d9adc1Smrg      set -f
3881fd23544Smrg      set fnord $dstdir
38972b676d7Smrg      shift
39093d9adc1Smrg      set +f
3911fd23544Smrg      IFS=$oIFS
3921fd23544Smrg
3931fd23544Smrg      prefixes=
3941fd23544Smrg
3951fd23544Smrg      for d
3961fd23544Smrg      do
39793d9adc1Smrg        test X"$d" = X && continue
39893d9adc1Smrg
39993d9adc1Smrg        prefix=$prefix$d
40093d9adc1Smrg        if test -d "$prefix"; then
40193d9adc1Smrg          prefixes=
40293d9adc1Smrg        else
40393d9adc1Smrg          if $posix_mkdir; then
40493d9adc1Smrg            (umask=$mkdir_umask &&
40593d9adc1Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
40693d9adc1Smrg            # Don't fail if two instances are running concurrently.
40793d9adc1Smrg            test -d "$prefix" || exit 1
40893d9adc1Smrg          else
40993d9adc1Smrg            case $prefix in
41093d9adc1Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
41193d9adc1Smrg              *) qprefix=$prefix;;
41293d9adc1Smrg            esac
41393d9adc1Smrg            prefixes="$prefixes '$qprefix'"
41493d9adc1Smrg          fi
41593d9adc1Smrg        fi
41693d9adc1Smrg        prefix=$prefix/
4171fd23544Smrg      done
4181fd23544Smrg
4191fd23544Smrg      if test -n "$prefixes"; then
42093d9adc1Smrg        # Don't fail if two instances are running concurrently.
42193d9adc1Smrg        (umask $mkdir_umask &&
42293d9adc1Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
42393d9adc1Smrg          test -d "$dstdir" || exit 1
42493d9adc1Smrg        obsolete_mkdir_used=true
42572b676d7Smrg      fi
4261fd23544Smrg    fi
42772b676d7Smrg  fi
42872b676d7Smrg
42972b676d7Smrg  if test -n "$dir_arg"; then
4301fd23544Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4311fd23544Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4321fd23544Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4331fd23544Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
43472b676d7Smrg  else
43572b676d7Smrg
43672b676d7Smrg    # Make a couple of temp file names in the proper directory.
43772b676d7Smrg    dsttmp=$dstdir/_inst.$$_
43872b676d7Smrg    rmtmp=$dstdir/_rm.$$_
43972b676d7Smrg
44072b676d7Smrg    # Trap to clean up those temp files at exit.
44172b676d7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
44272b676d7Smrg
44372b676d7Smrg    # Copy the file name to the temp name.
4441fd23544Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
44572b676d7Smrg
44672b676d7Smrg    # and set any options; do chmod last to preserve setuid bits.
44772b676d7Smrg    #
44872b676d7Smrg    # If any of these fail, we abort the whole thing.  If we want to
44972b676d7Smrg    # ignore errors from any of these, just make sure not to ignore
45072b676d7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
45172b676d7Smrg    #
4521fd23544Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4531fd23544Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4541fd23544Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4551fd23544Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4561fd23544Smrg
4571fd23544Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4581fd23544Smrg    if $copy_on_change &&
45993d9adc1Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
46093d9adc1Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
46193d9adc1Smrg       set -f &&
4621fd23544Smrg       set X $old && old=:$2:$4:$5:$6 &&
4631fd23544Smrg       set X $new && new=:$2:$4:$5:$6 &&
46493d9adc1Smrg       set +f &&
4651fd23544Smrg       test "$old" = "$new" &&
4661fd23544Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4671fd23544Smrg    then
4681fd23544Smrg      rm -f "$dsttmp"
4691fd23544Smrg    else
4701fd23544Smrg      # Rename the file to the real destination.
4711fd23544Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4721fd23544Smrg
4731fd23544Smrg      # The rename failed, perhaps because mv can't rename something else
4741fd23544Smrg      # to itself, or perhaps because mv is so ancient that it does not
4751fd23544Smrg      # support -f.
4761fd23544Smrg      {
47793d9adc1Smrg        # Now remove or move aside any old file at destination location.
47893d9adc1Smrg        # We try this two ways since rm can't unlink itself on some
47993d9adc1Smrg        # systems and the destination file might be busy for other
48093d9adc1Smrg        # reasons.  In this case, the final cleanup might fail but the new
48193d9adc1Smrg        # file should still install successfully.
48293d9adc1Smrg        {
48393d9adc1Smrg          test ! -f "$dst" ||
48493d9adc1Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
48593d9adc1Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
48693d9adc1Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48793d9adc1Smrg          } ||
48893d9adc1Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48993d9adc1Smrg            (exit 1); exit 1
49093d9adc1Smrg          }
49193d9adc1Smrg        } &&
49293d9adc1Smrg
49393d9adc1Smrg        # Now rename the file to the real destination.
49493d9adc1Smrg        $doit $mvcmd "$dsttmp" "$dst"
4951fd23544Smrg      }
4961fd23544Smrg    fi || exit 1
4971fd23544Smrg
4981fd23544Smrg    trap '' 0
4991fd23544Smrg  fi
50072b676d7Smrgdone
50172b676d7Smrg
50272b676d7Smrg# Local variables:
50372b676d7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50472b676d7Smrg# time-stamp-start: "scriptversion="
50572b676d7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
50674c14cd6Smrg# time-stamp-time-zone: "UTC"
50774c14cd6Smrg# time-stamp-end: "; # UTC"
50872b676d7Smrg# End:
509