install-sh revision 4a8d91dc
150806d53Smrg#!/bin/sh
250806d53Smrg# install - install a program, script, or datafile
350806d53Smrg
44a8d91dcSmrgscriptversion=2014-09-12.12; # UTC
550806d53Smrg
650806d53Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
750806d53Smrg# later released in X11R6 (xc/config/util/install.sh) with the
850806d53Smrg# following copyright and license.
950806d53Smrg#
1050806d53Smrg# Copyright (C) 1994 X Consortium
1150806d53Smrg#
1250806d53Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1350806d53Smrg# of this software and associated documentation files (the "Software"), to
1450806d53Smrg# deal in the Software without restriction, including without limitation the
1550806d53Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1650806d53Smrg# sell copies of the Software, and to permit persons to whom the Software is
1750806d53Smrg# furnished to do so, subject to the following conditions:
1850806d53Smrg#
1950806d53Smrg# The above copyright notice and this permission notice shall be included in
2050806d53Smrg# all copies or substantial portions of the Software.
2150806d53Smrg#
2250806d53Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2350806d53Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2450806d53Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2550806d53Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2650806d53Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2750806d53Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2850806d53Smrg#
2950806d53Smrg# Except as contained in this notice, the name of the X Consortium shall not
3050806d53Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3150806d53Smrg# ings in this Software without prior written authorization from the X Consor-
3250806d53Smrg# tium.
3350806d53Smrg#
3450806d53Smrg#
3550806d53Smrg# FSF changes to this file are in the public domain.
3650806d53Smrg#
3750806d53Smrg# Calling this script install-sh is preferred over install.sh, to prevent
384a8d91dcSmrg# 'make' implicit rules from creating a file called install from it
3950806d53Smrg# when there is no Makefile.
4050806d53Smrg#
4150806d53Smrg# This script is compatible with the BSD install script, but was written
427965d9acSmrg# from scratch.
437965d9acSmrg
444a8d91dcSmrgtab='	'
457965d9acSmrgnl='
467965d9acSmrg'
474a8d91dcSmrgIFS=" $tab$nl"
4850806d53Smrg
494a8d91dcSmrg# Set DOITPROG to "echo" to test this script.
5050806d53Smrg
517965d9acSmrgdoit=${DOITPROG-}
524a8d91dcSmrgdoit_exec=${doit:-exec}
5350806d53Smrg
547965d9acSmrg# Put in absolute file names if you don't have them in your path;
557965d9acSmrg# or use environment vars.
567965d9acSmrg
577965d9acSmrgchgrpprog=${CHGRPPROG-chgrp}
587965d9acSmrgchmodprog=${CHMODPROG-chmod}
597965d9acSmrgchownprog=${CHOWNPROG-chown}
607965d9acSmrgcmpprog=${CMPPROG-cmp}
617965d9acSmrgcpprog=${CPPROG-cp}
627965d9acSmrgmkdirprog=${MKDIRPROG-mkdir}
637965d9acSmrgmvprog=${MVPROG-mv}
647965d9acSmrgrmprog=${RMPROG-rm}
657965d9acSmrgstripprog=${STRIPPROG-strip}
667965d9acSmrg
677965d9acSmrgposix_mkdir=
687965d9acSmrg
697965d9acSmrg# Desired mode of installed file.
707965d9acSmrgmode=0755
7150806d53Smrg
7250806d53Smrgchgrpcmd=
737965d9acSmrgchmodcmd=$chmodprog
747965d9acSmrgchowncmd=
757965d9acSmrgmvcmd=$mvprog
7650806d53Smrgrmcmd="$rmprog -f"
777965d9acSmrgstripcmd=
787965d9acSmrg
7950806d53Smrgsrc=
8050806d53Smrgdst=
8150806d53Smrgdir_arg=
827965d9acSmrgdst_arg=
837965d9acSmrg
847965d9acSmrgcopy_on_change=false
854a8d91dcSmrgis_target_a_directory=possibly
8650806d53Smrg
877965d9acSmrgusage="\
887965d9acSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8950806d53Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9050806d53Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9150806d53Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9250806d53Smrg
9350806d53SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9450806d53SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9550806d53SmrgIn the 4th, create DIRECTORIES.
9650806d53Smrg
9750806d53SmrgOptions:
987965d9acSmrg     --help     display this help and exit.
997965d9acSmrg     --version  display version info and exit.
1007965d9acSmrg
1017965d9acSmrg  -c            (ignored)
1027965d9acSmrg  -C            install only if different (preserve the last data modification time)
1037965d9acSmrg  -d            create directories instead of installing files.
1047965d9acSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1057965d9acSmrg  -m MODE       $chmodprog installed files to MODE.
1067965d9acSmrg  -o USER       $chownprog installed files to USER.
1077965d9acSmrg  -s            $stripprog installed files.
1087965d9acSmrg  -t DIRECTORY  install into DIRECTORY.
1097965d9acSmrg  -T            report an error if DSTFILE is a directory.
11050806d53Smrg
11150806d53SmrgEnvironment variables override the default commands:
1127965d9acSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1137965d9acSmrg  RMPROG STRIPPROG
11450806d53Smrg"
11550806d53Smrg
1167965d9acSmrgwhile test $# -ne 0; do
11750806d53Smrg  case $1 in
1187965d9acSmrg    -c) ;;
1197965d9acSmrg
1207965d9acSmrg    -C) copy_on_change=true;;
12150806d53Smrg
1227965d9acSmrg    -d) dir_arg=true;;
12350806d53Smrg
12450806d53Smrg    -g) chgrpcmd="$chgrpprog $2"
1254a8d91dcSmrg        shift;;
12650806d53Smrg
12750806d53Smrg    --help) echo "$usage"; exit $?;;
12850806d53Smrg
1297965d9acSmrg    -m) mode=$2
1304a8d91dcSmrg        case $mode in
1314a8d91dcSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1324a8d91dcSmrg            echo "$0: invalid mode: $mode" >&2
1334a8d91dcSmrg            exit 1;;
1344a8d91dcSmrg        esac
1354a8d91dcSmrg        shift;;
13650806d53Smrg
13750806d53Smrg    -o) chowncmd="$chownprog $2"
1384a8d91dcSmrg        shift;;
13950806d53Smrg
1407965d9acSmrg    -s) stripcmd=$stripprog;;
14150806d53Smrg
1424a8d91dcSmrg    -t)
1434a8d91dcSmrg        is_target_a_directory=always
1444a8d91dcSmrg        dst_arg=$2
1454a8d91dcSmrg        # Protect names problematic for 'test' and other utilities.
1464a8d91dcSmrg        case $dst_arg in
1474a8d91dcSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1484a8d91dcSmrg        esac
1494a8d91dcSmrg        shift;;
15050806d53Smrg
1514a8d91dcSmrg    -T) is_target_a_directory=never;;
15250806d53Smrg
15350806d53Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15450806d53Smrg
1554a8d91dcSmrg    --) shift
1564a8d91dcSmrg        break;;
1577965d9acSmrg
1584a8d91dcSmrg    -*) echo "$0: invalid option: $1" >&2
1594a8d91dcSmrg        exit 1;;
1607965d9acSmrg
1617965d9acSmrg    *)  break;;
16250806d53Smrg  esac
1637965d9acSmrg  shift
16450806d53Smrgdone
16550806d53Smrg
1664a8d91dcSmrg# We allow the use of options -d and -T together, by making -d
1674a8d91dcSmrg# take the precedence; this is for compatibility with GNU install.
1684a8d91dcSmrg
1694a8d91dcSmrgif test -n "$dir_arg"; then
1704a8d91dcSmrg  if test -n "$dst_arg"; then
1714a8d91dcSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1724a8d91dcSmrg    exit 1
1734a8d91dcSmrg  fi
1744a8d91dcSmrgfi
1754a8d91dcSmrg
1767965d9acSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777965d9acSmrg  # When -d is used, all remaining arguments are directories to create.
1787965d9acSmrg  # When -t is used, the destination is already specified.
1797965d9acSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807965d9acSmrg  for arg
1817965d9acSmrg  do
1827965d9acSmrg    if test -n "$dst_arg"; then
1837965d9acSmrg      # $@ is not empty: it contains at least $arg.
1847965d9acSmrg      set fnord "$@" "$dst_arg"
1857965d9acSmrg      shift # fnord
1867965d9acSmrg    fi
1877965d9acSmrg    shift # arg
1887965d9acSmrg    dst_arg=$arg
1894a8d91dcSmrg    # Protect names problematic for 'test' and other utilities.
190a73423d7Smrg    case $dst_arg in
191a73423d7Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192a73423d7Smrg    esac
1937965d9acSmrg  done
1947965d9acSmrgfi
1957965d9acSmrg
1967965d9acSmrgif test $# -eq 0; then
19750806d53Smrg  if test -z "$dir_arg"; then
19850806d53Smrg    echo "$0: no input file specified." >&2
19950806d53Smrg    exit 1
20050806d53Smrg  fi
2014a8d91dcSmrg  # It's OK to call 'install-sh -d' without argument.
20250806d53Smrg  # This can happen when creating conditional directories.
20350806d53Smrg  exit 0
20450806d53Smrgfi
20550806d53Smrg
2064a8d91dcSmrgif test -z "$dir_arg"; then
2074a8d91dcSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2084a8d91dcSmrg    if test ! -d "$dst_arg"; then
2094a8d91dcSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2104a8d91dcSmrg      exit 1
2114a8d91dcSmrg    fi
2124a8d91dcSmrg  fi
2134a8d91dcSmrgfi
2144a8d91dcSmrg
2157965d9acSmrgif test -z "$dir_arg"; then
216a73423d7Smrg  do_exit='(exit $ret); exit $ret'
217a73423d7Smrg  trap "ret=129; $do_exit" 1
218a73423d7Smrg  trap "ret=130; $do_exit" 2
219a73423d7Smrg  trap "ret=141; $do_exit" 13
220a73423d7Smrg  trap "ret=143; $do_exit" 15
2217965d9acSmrg
2227965d9acSmrg  # Set umask so as not to create temps with too-generous modes.
2237965d9acSmrg  # However, 'strip' requires both read and write access to temps.
2247965d9acSmrg  case $mode in
2257965d9acSmrg    # Optimize common cases.
2267965d9acSmrg    *644) cp_umask=133;;
2277965d9acSmrg    *755) cp_umask=22;;
2287965d9acSmrg
2297965d9acSmrg    *[0-7])
2307965d9acSmrg      if test -z "$stripcmd"; then
2314a8d91dcSmrg        u_plus_rw=
2327965d9acSmrg      else
2334a8d91dcSmrg        u_plus_rw='% 200'
2347965d9acSmrg      fi
2357965d9acSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2367965d9acSmrg    *)
2377965d9acSmrg      if test -z "$stripcmd"; then
2384a8d91dcSmrg        u_plus_rw=
2397965d9acSmrg      else
2404a8d91dcSmrg        u_plus_rw=,u+rw
2417965d9acSmrg      fi
2427965d9acSmrg      cp_umask=$mode$u_plus_rw;;
2437965d9acSmrg  esac
2447965d9acSmrgfi
2457965d9acSmrg
24650806d53Smrgfor src
24750806d53Smrgdo
2484a8d91dcSmrg  # Protect names problematic for 'test' and other utilities.
24950806d53Smrg  case $src in
250a73423d7Smrg    -* | [=\(\)!]) src=./$src;;
25150806d53Smrg  esac
25250806d53Smrg
25350806d53Smrg  if test -n "$dir_arg"; then
25450806d53Smrg    dst=$src
2557965d9acSmrg    dstdir=$dst
2567965d9acSmrg    test -d "$dstdir"
2577965d9acSmrg    dstdir_status=$?
25850806d53Smrg  else
2597965d9acSmrg
26050806d53Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26150806d53Smrg    # might cause directories to be created, which would be especially bad
26250806d53Smrg    # if $src (and thus $dsttmp) contains '*'.
26350806d53Smrg    if test ! -f "$src" && test ! -d "$src"; then
26450806d53Smrg      echo "$0: $src does not exist." >&2
26550806d53Smrg      exit 1
26650806d53Smrg    fi
26750806d53Smrg
2687965d9acSmrg    if test -z "$dst_arg"; then
26950806d53Smrg      echo "$0: no destination specified." >&2
27050806d53Smrg      exit 1
27150806d53Smrg    fi
2727965d9acSmrg    dst=$dst_arg
27350806d53Smrg
27450806d53Smrg    # If destination is a directory, append the input filename; won't work
27550806d53Smrg    # if double slashes aren't ignored.
27650806d53Smrg    if test -d "$dst"; then
2774a8d91dcSmrg      if test "$is_target_a_directory" = never; then
2784a8d91dcSmrg        echo "$0: $dst_arg: Is a directory" >&2
2794a8d91dcSmrg        exit 1
28050806d53Smrg      fi
2817965d9acSmrg      dstdir=$dst
2827965d9acSmrg      dst=$dstdir/`basename "$src"`
2837965d9acSmrg      dstdir_status=0
2847965d9acSmrg    else
2854a8d91dcSmrg      dstdir=`dirname "$dst"`
2867965d9acSmrg      test -d "$dstdir"
2877965d9acSmrg      dstdir_status=$?
28850806d53Smrg    fi
28950806d53Smrg  fi
29050806d53Smrg
2917965d9acSmrg  obsolete_mkdir_used=false
2927965d9acSmrg
2937965d9acSmrg  if test $dstdir_status != 0; then
2947965d9acSmrg    case $posix_mkdir in
2957965d9acSmrg      '')
2964a8d91dcSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
2974a8d91dcSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
2984a8d91dcSmrg        umask=`umask`
2994a8d91dcSmrg        case $stripcmd.$umask in
3004a8d91dcSmrg          # Optimize common cases.
3014a8d91dcSmrg          *[2367][2367]) mkdir_umask=$umask;;
3024a8d91dcSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3034a8d91dcSmrg
3044a8d91dcSmrg          *[0-7])
3054a8d91dcSmrg            mkdir_umask=`expr $umask + 22 \
3064a8d91dcSmrg              - $umask % 100 % 40 + $umask % 20 \
3074a8d91dcSmrg              - $umask % 10 % 4 + $umask % 2
3084a8d91dcSmrg            `;;
3094a8d91dcSmrg          *) mkdir_umask=$umask,go-w;;
3104a8d91dcSmrg        esac
3114a8d91dcSmrg
3124a8d91dcSmrg        # With -d, create the new directory with the user-specified mode.
3134a8d91dcSmrg        # Otherwise, rely on $mkdir_umask.
3144a8d91dcSmrg        if test -n "$dir_arg"; then
3154a8d91dcSmrg          mkdir_mode=-m$mode
3164a8d91dcSmrg        else
3174a8d91dcSmrg          mkdir_mode=
3184a8d91dcSmrg        fi
3194a8d91dcSmrg
3204a8d91dcSmrg        posix_mkdir=false
3214a8d91dcSmrg        case $umask in
3224a8d91dcSmrg          *[123567][0-7][0-7])
3234a8d91dcSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3244a8d91dcSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3254a8d91dcSmrg            ;;
3264a8d91dcSmrg          *)
3274a8d91dcSmrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
3284a8d91dcSmrg            # lower collision chance
3294a8d91dcSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3304a8d91dcSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
3314a8d91dcSmrg
3324a8d91dcSmrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
3334a8d91dcSmrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
3344a8d91dcSmrg            # that nobody tries to guess the $tmpdir name.
3354a8d91dcSmrg            if (umask $mkdir_umask &&
3364a8d91dcSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
3374a8d91dcSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3384a8d91dcSmrg            then
3394a8d91dcSmrg              if test -z "$dir_arg" || {
3404a8d91dcSmrg                   # Check for POSIX incompatibilities with -m.
3414a8d91dcSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3424a8d91dcSmrg                   # other-writable bit of parent directory when it shouldn't.
3434a8d91dcSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3444a8d91dcSmrg                   test_tmpdir="$tmpdir/a"
3454a8d91dcSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3464a8d91dcSmrg                   case $ls_ld_tmpdir in
3474a8d91dcSmrg                     d????-?r-*) different_mode=700;;
3484a8d91dcSmrg                     d????-?--*) different_mode=755;;
3494a8d91dcSmrg                     *) false;;
3504a8d91dcSmrg                   esac &&
3514a8d91dcSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3524a8d91dcSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3534a8d91dcSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3544a8d91dcSmrg                   }
3554a8d91dcSmrg                 }
3564a8d91dcSmrg              then posix_mkdir=:
3574a8d91dcSmrg              fi
3584a8d91dcSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3594a8d91dcSmrg            else
3604a8d91dcSmrg              # Remove any dirs left behind by ancient mkdir implementations.
3614a8d91dcSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3624a8d91dcSmrg            fi
3634a8d91dcSmrg            trap '' 0;;
3644a8d91dcSmrg        esac;;
3657965d9acSmrg    esac
36650806d53Smrg
3677965d9acSmrg    if
3687965d9acSmrg      $posix_mkdir && (
3694a8d91dcSmrg        umask $mkdir_umask &&
3704a8d91dcSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3717965d9acSmrg      )
3727965d9acSmrg    then :
3737965d9acSmrg    else
37450806d53Smrg
3757965d9acSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3767965d9acSmrg      # or it failed possibly due to a race condition.  Create the
3777965d9acSmrg      # directory the slow way, step by step, checking for races as we go.
37850806d53Smrg
3797965d9acSmrg      case $dstdir in
3804a8d91dcSmrg        /*) prefix='/';;
3814a8d91dcSmrg        [-=\(\)!]*) prefix='./';;
3824a8d91dcSmrg        *)  prefix='';;
3837965d9acSmrg      esac
38450806d53Smrg
3857965d9acSmrg      oIFS=$IFS
3867965d9acSmrg      IFS=/
3874a8d91dcSmrg      set -f
3887965d9acSmrg      set fnord $dstdir
38950806d53Smrg      shift
3904a8d91dcSmrg      set +f
3917965d9acSmrg      IFS=$oIFS
3927965d9acSmrg
3937965d9acSmrg      prefixes=
3947965d9acSmrg
3957965d9acSmrg      for d
3967965d9acSmrg      do
3974a8d91dcSmrg        test X"$d" = X && continue
3984a8d91dcSmrg
3994a8d91dcSmrg        prefix=$prefix$d
4004a8d91dcSmrg        if test -d "$prefix"; then
4014a8d91dcSmrg          prefixes=
4024a8d91dcSmrg        else
4034a8d91dcSmrg          if $posix_mkdir; then
4044a8d91dcSmrg            (umask=$mkdir_umask &&
4054a8d91dcSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4064a8d91dcSmrg            # Don't fail if two instances are running concurrently.
4074a8d91dcSmrg            test -d "$prefix" || exit 1
4084a8d91dcSmrg          else
4094a8d91dcSmrg            case $prefix in
4104a8d91dcSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4114a8d91dcSmrg              *) qprefix=$prefix;;
4124a8d91dcSmrg            esac
4134a8d91dcSmrg            prefixes="$prefixes '$qprefix'"
4144a8d91dcSmrg          fi
4154a8d91dcSmrg        fi
4164a8d91dcSmrg        prefix=$prefix/
4177965d9acSmrg      done
4187965d9acSmrg
4197965d9acSmrg      if test -n "$prefixes"; then
4204a8d91dcSmrg        # Don't fail if two instances are running concurrently.
4214a8d91dcSmrg        (umask $mkdir_umask &&
4224a8d91dcSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4234a8d91dcSmrg          test -d "$dstdir" || exit 1
4244a8d91dcSmrg        obsolete_mkdir_used=true
42550806d53Smrg      fi
4267965d9acSmrg    fi
42750806d53Smrg  fi
42850806d53Smrg
42950806d53Smrg  if test -n "$dir_arg"; then
4307965d9acSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4317965d9acSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4327965d9acSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4337965d9acSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
43450806d53Smrg  else
43550806d53Smrg
43650806d53Smrg    # Make a couple of temp file names in the proper directory.
43750806d53Smrg    dsttmp=$dstdir/_inst.$$_
43850806d53Smrg    rmtmp=$dstdir/_rm.$$_
43950806d53Smrg
44050806d53Smrg    # Trap to clean up those temp files at exit.
44150806d53Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
44250806d53Smrg
44350806d53Smrg    # Copy the file name to the temp name.
4447965d9acSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
44550806d53Smrg
44650806d53Smrg    # and set any options; do chmod last to preserve setuid bits.
44750806d53Smrg    #
44850806d53Smrg    # If any of these fail, we abort the whole thing.  If we want to
44950806d53Smrg    # ignore errors from any of these, just make sure not to ignore
45050806d53Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
45150806d53Smrg    #
4527965d9acSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4537965d9acSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4547965d9acSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4557965d9acSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4567965d9acSmrg
4577965d9acSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4587965d9acSmrg    if $copy_on_change &&
4594a8d91dcSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4604a8d91dcSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4614a8d91dcSmrg       set -f &&
4627965d9acSmrg       set X $old && old=:$2:$4:$5:$6 &&
4637965d9acSmrg       set X $new && new=:$2:$4:$5:$6 &&
4644a8d91dcSmrg       set +f &&
4657965d9acSmrg       test "$old" = "$new" &&
4667965d9acSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4677965d9acSmrg    then
4687965d9acSmrg      rm -f "$dsttmp"
4697965d9acSmrg    else
4707965d9acSmrg      # Rename the file to the real destination.
4717965d9acSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4727965d9acSmrg
4737965d9acSmrg      # The rename failed, perhaps because mv can't rename something else
4747965d9acSmrg      # to itself, or perhaps because mv is so ancient that it does not
4757965d9acSmrg      # support -f.
4767965d9acSmrg      {
4774a8d91dcSmrg        # Now remove or move aside any old file at destination location.
4784a8d91dcSmrg        # We try this two ways since rm can't unlink itself on some
4794a8d91dcSmrg        # systems and the destination file might be busy for other
4804a8d91dcSmrg        # reasons.  In this case, the final cleanup might fail but the new
4814a8d91dcSmrg        # file should still install successfully.
4824a8d91dcSmrg        {
4834a8d91dcSmrg          test ! -f "$dst" ||
4844a8d91dcSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4854a8d91dcSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4864a8d91dcSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4874a8d91dcSmrg          } ||
4884a8d91dcSmrg          { echo "$0: cannot unlink or rename $dst" >&2
4894a8d91dcSmrg            (exit 1); exit 1
4904a8d91dcSmrg          }
4914a8d91dcSmrg        } &&
4924a8d91dcSmrg
4934a8d91dcSmrg        # Now rename the file to the real destination.
4944a8d91dcSmrg        $doit $mvcmd "$dsttmp" "$dst"
4957965d9acSmrg      }
4967965d9acSmrg    fi || exit 1
4977965d9acSmrg
4987965d9acSmrg    trap '' 0
4997965d9acSmrg  fi
50050806d53Smrgdone
50150806d53Smrg
50250806d53Smrg# Local variables:
50350806d53Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50450806d53Smrg# time-stamp-start: "scriptversion="
50550806d53Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506a73423d7Smrg# time-stamp-time-zone: "UTC"
507a73423d7Smrg# time-stamp-end: "; # UTC"
50850806d53Smrg# End:
509