install-sh revision 89afc689
1bccedf53Smrg#!/bin/sh
2bccedf53Smrg# install - install a program, script, or datafile
37c5f6000Smrg
489afc689Smrgscriptversion=2013-12-25.23; # UTC
57c5f6000Smrg
6bccedf53Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7bccedf53Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8bccedf53Smrg# following copyright and license.
9bccedf53Smrg#
10bccedf53Smrg# Copyright (C) 1994 X Consortium
11bccedf53Smrg#
12bccedf53Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13bccedf53Smrg# of this software and associated documentation files (the "Software"), to
14bccedf53Smrg# deal in the Software without restriction, including without limitation the
15bccedf53Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16bccedf53Smrg# sell copies of the Software, and to permit persons to whom the Software is
17bccedf53Smrg# furnished to do so, subject to the following conditions:
18bccedf53Smrg#
19bccedf53Smrg# The above copyright notice and this permission notice shall be included in
20bccedf53Smrg# all copies or substantial portions of the Software.
21bccedf53Smrg#
22bccedf53Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23bccedf53Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24bccedf53Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25bccedf53Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26bccedf53Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27bccedf53Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28bccedf53Smrg#
29bccedf53Smrg# Except as contained in this notice, the name of the X Consortium shall not
30bccedf53Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31bccedf53Smrg# ings in this Software without prior written authorization from the X Consor-
32bccedf53Smrg# tium.
33bccedf53Smrg#
34bccedf53Smrg#
35bccedf53Smrg# FSF changes to this file are in the public domain.
36bccedf53Smrg#
37bccedf53Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3889afc689Smrg# 'make' implicit rules from creating a file called install from it
39bccedf53Smrg# when there is no Makefile.
40bccedf53Smrg#
41bccedf53Smrg# This script is compatible with the BSD install script, but was written
427c5f6000Smrg# from scratch.
43bccedf53Smrg
4489afc689Smrgtab='	'
457c5f6000Smrgnl='
467c5f6000Smrg'
4789afc689SmrgIFS=" $tab$nl"
48bccedf53Smrg
4989afc689Smrg# Set DOITPROG to "echo" to test this script.
50bccedf53Smrg
517c5f6000Smrgdoit=${DOITPROG-}
5289afc689Smrgdoit_exec=${doit:-exec}
53bccedf53Smrg
547c5f6000Smrg# Put in absolute file names if you don't have them in your path;
557c5f6000Smrg# or use environment vars.
567c5f6000Smrg
577c5f6000Smrgchgrpprog=${CHGRPPROG-chgrp}
587c5f6000Smrgchmodprog=${CHMODPROG-chmod}
597c5f6000Smrgchownprog=${CHOWNPROG-chown}
607c5f6000Smrgcmpprog=${CMPPROG-cmp}
617c5f6000Smrgcpprog=${CPPROG-cp}
627c5f6000Smrgmkdirprog=${MKDIRPROG-mkdir}
637c5f6000Smrgmvprog=${MVPROG-mv}
647c5f6000Smrgrmprog=${RMPROG-rm}
657c5f6000Smrgstripprog=${STRIPPROG-strip}
667c5f6000Smrg
677c5f6000Smrgposix_mkdir=
687c5f6000Smrg
697c5f6000Smrg# Desired mode of installed file.
707c5f6000Smrgmode=0755
717c5f6000Smrg
727c5f6000Smrgchgrpcmd=
737c5f6000Smrgchmodcmd=$chmodprog
747c5f6000Smrgchowncmd=
757c5f6000Smrgmvcmd=$mvprog
767c5f6000Smrgrmcmd="$rmprog -f"
777c5f6000Smrgstripcmd=
78bccedf53Smrg
797c5f6000Smrgsrc=
807c5f6000Smrgdst=
817c5f6000Smrgdir_arg=
827c5f6000Smrgdst_arg=
83bccedf53Smrg
847c5f6000Smrgcopy_on_change=false
8589afc689Smrgis_target_a_directory=possibly
86bccedf53Smrg
877c5f6000Smrgusage="\
887c5f6000SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
897c5f6000Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
907c5f6000Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
917c5f6000Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92bccedf53Smrg
937c5f6000SmrgIn the 1st form, copy SRCFILE to DSTFILE.
947c5f6000SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
957c5f6000SmrgIn the 4th, create DIRECTORIES.
96bccedf53Smrg
977c5f6000SmrgOptions:
987c5f6000Smrg     --help     display this help and exit.
997c5f6000Smrg     --version  display version info and exit.
100bccedf53Smrg
1017c5f6000Smrg  -c            (ignored)
1027c5f6000Smrg  -C            install only if different (preserve the last data modification time)
1037c5f6000Smrg  -d            create directories instead of installing files.
1047c5f6000Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1057c5f6000Smrg  -m MODE       $chmodprog installed files to MODE.
1067c5f6000Smrg  -o USER       $chownprog installed files to USER.
1077c5f6000Smrg  -s            $stripprog installed files.
1087c5f6000Smrg  -t DIRECTORY  install into DIRECTORY.
1097c5f6000Smrg  -T            report an error if DSTFILE is a directory.
110bccedf53Smrg
1117c5f6000SmrgEnvironment variables override the default commands:
1127c5f6000Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1137c5f6000Smrg  RMPROG STRIPPROG
1147c5f6000Smrg"
115bccedf53Smrg
1167c5f6000Smrgwhile test $# -ne 0; do
1177c5f6000Smrg  case $1 in
1187c5f6000Smrg    -c) ;;
119bccedf53Smrg
1207c5f6000Smrg    -C) copy_on_change=true;;
121bccedf53Smrg
1227c5f6000Smrg    -d) dir_arg=true;;
123bccedf53Smrg
1247c5f6000Smrg    -g) chgrpcmd="$chgrpprog $2"
12589afc689Smrg        shift;;
126bccedf53Smrg
1277c5f6000Smrg    --help) echo "$usage"; exit $?;;
128bccedf53Smrg
1297c5f6000Smrg    -m) mode=$2
13089afc689Smrg        case $mode in
13189afc689Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13289afc689Smrg            echo "$0: invalid mode: $mode" >&2
13389afc689Smrg            exit 1;;
13489afc689Smrg        esac
13589afc689Smrg        shift;;
136bccedf53Smrg
1377c5f6000Smrg    -o) chowncmd="$chownprog $2"
13889afc689Smrg        shift;;
139bccedf53Smrg
1407c5f6000Smrg    -s) stripcmd=$stripprog;;
141bccedf53Smrg
14289afc689Smrg    -t)
14389afc689Smrg        is_target_a_directory=always
14489afc689Smrg        dst_arg=$2
14589afc689Smrg        # Protect names problematic for 'test' and other utilities.
14689afc689Smrg        case $dst_arg in
14789afc689Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14889afc689Smrg        esac
14989afc689Smrg        shift;;
150bccedf53Smrg
15189afc689Smrg    -T) is_target_a_directory=never;;
152bccedf53Smrg
1537c5f6000Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154bccedf53Smrg
15589afc689Smrg    --) shift
15689afc689Smrg        break;;
157bccedf53Smrg
15889afc689Smrg    -*) echo "$0: invalid option: $1" >&2
15989afc689Smrg        exit 1;;
160bccedf53Smrg
1617c5f6000Smrg    *)  break;;
1627c5f6000Smrg  esac
1637c5f6000Smrg  shift
1647c5f6000Smrgdone
165bccedf53Smrg
16689afc689Smrg# We allow the use of options -d and -T together, by making -d
16789afc689Smrg# take the precedence; this is for compatibility with GNU install.
16889afc689Smrg
16989afc689Smrgif test -n "$dir_arg"; then
17089afc689Smrg  if test -n "$dst_arg"; then
17189afc689Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17289afc689Smrg    exit 1
17389afc689Smrg  fi
17489afc689Smrgfi
17589afc689Smrg
1767c5f6000Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777c5f6000Smrg  # When -d is used, all remaining arguments are directories to create.
1787c5f6000Smrg  # When -t is used, the destination is already specified.
1797c5f6000Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807c5f6000Smrg  for arg
1817c5f6000Smrg  do
1827c5f6000Smrg    if test -n "$dst_arg"; then
1837c5f6000Smrg      # $@ is not empty: it contains at least $arg.
1847c5f6000Smrg      set fnord "$@" "$dst_arg"
1857c5f6000Smrg      shift # fnord
1867c5f6000Smrg    fi
1877c5f6000Smrg    shift # arg
1887c5f6000Smrg    dst_arg=$arg
18989afc689Smrg    # Protect names problematic for 'test' and other utilities.
1901b1389eeSmrg    case $dst_arg in
1911b1389eeSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1921b1389eeSmrg    esac
1937c5f6000Smrg  done
1947c5f6000Smrgfi
195bccedf53Smrg
1967c5f6000Smrgif test $# -eq 0; then
1977c5f6000Smrg  if test -z "$dir_arg"; then
1987c5f6000Smrg    echo "$0: no input file specified." >&2
1997c5f6000Smrg    exit 1
2007c5f6000Smrg  fi
20189afc689Smrg  # It's OK to call 'install-sh -d' without argument.
2027c5f6000Smrg  # This can happen when creating conditional directories.
2037c5f6000Smrg  exit 0
2047c5f6000Smrgfi
205bccedf53Smrg
20689afc689Smrgif test -z "$dir_arg"; then
20789afc689Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20889afc689Smrg    if test ! -d "$dst_arg"; then
20989afc689Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21089afc689Smrg      exit 1
21189afc689Smrg    fi
21289afc689Smrg  fi
21389afc689Smrgfi
21489afc689Smrg
2157c5f6000Smrgif test -z "$dir_arg"; then
2161b1389eeSmrg  do_exit='(exit $ret); exit $ret'
2171b1389eeSmrg  trap "ret=129; $do_exit" 1
2181b1389eeSmrg  trap "ret=130; $do_exit" 2
2191b1389eeSmrg  trap "ret=141; $do_exit" 13
2201b1389eeSmrg  trap "ret=143; $do_exit" 15
2217c5f6000Smrg
2227c5f6000Smrg  # Set umask so as not to create temps with too-generous modes.
2237c5f6000Smrg  # However, 'strip' requires both read and write access to temps.
2247c5f6000Smrg  case $mode in
2257c5f6000Smrg    # Optimize common cases.
2267c5f6000Smrg    *644) cp_umask=133;;
2277c5f6000Smrg    *755) cp_umask=22;;
2287c5f6000Smrg
2297c5f6000Smrg    *[0-7])
2307c5f6000Smrg      if test -z "$stripcmd"; then
23189afc689Smrg        u_plus_rw=
2327c5f6000Smrg      else
23389afc689Smrg        u_plus_rw='% 200'
2347c5f6000Smrg      fi
2357c5f6000Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2367c5f6000Smrg    *)
2377c5f6000Smrg      if test -z "$stripcmd"; then
23889afc689Smrg        u_plus_rw=
2397c5f6000Smrg      else
24089afc689Smrg        u_plus_rw=,u+rw
2417c5f6000Smrg      fi
2427c5f6000Smrg      cp_umask=$mode$u_plus_rw;;
2437c5f6000Smrg  esac
2447c5f6000Smrgfi
245bccedf53Smrg
2467c5f6000Smrgfor src
2477c5f6000Smrgdo
24889afc689Smrg  # Protect names problematic for 'test' and other utilities.
2497c5f6000Smrg  case $src in
2501b1389eeSmrg    -* | [=\(\)!]) src=./$src;;
2517c5f6000Smrg  esac
2527c5f6000Smrg
2537c5f6000Smrg  if test -n "$dir_arg"; then
2547c5f6000Smrg    dst=$src
2557c5f6000Smrg    dstdir=$dst
2567c5f6000Smrg    test -d "$dstdir"
2577c5f6000Smrg    dstdir_status=$?
2587c5f6000Smrg  else
2597c5f6000Smrg
2607c5f6000Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2617c5f6000Smrg    # might cause directories to be created, which would be especially bad
2627c5f6000Smrg    # if $src (and thus $dsttmp) contains '*'.
2637c5f6000Smrg    if test ! -f "$src" && test ! -d "$src"; then
2647c5f6000Smrg      echo "$0: $src does not exist." >&2
2657c5f6000Smrg      exit 1
2667c5f6000Smrg    fi
2677c5f6000Smrg
2687c5f6000Smrg    if test -z "$dst_arg"; then
2697c5f6000Smrg      echo "$0: no destination specified." >&2
2707c5f6000Smrg      exit 1
2717c5f6000Smrg    fi
2727c5f6000Smrg    dst=$dst_arg
273bccedf53Smrg
2747c5f6000Smrg    # If destination is a directory, append the input filename; won't work
2757c5f6000Smrg    # if double slashes aren't ignored.
2767c5f6000Smrg    if test -d "$dst"; then
27789afc689Smrg      if test "$is_target_a_directory" = never; then
27889afc689Smrg        echo "$0: $dst_arg: Is a directory" >&2
27989afc689Smrg        exit 1
2807c5f6000Smrg      fi
2817c5f6000Smrg      dstdir=$dst
2827c5f6000Smrg      dst=$dstdir/`basename "$src"`
2837c5f6000Smrg      dstdir_status=0
2847c5f6000Smrg    else
28589afc689Smrg      dstdir=`dirname "$dst"`
2867c5f6000Smrg      test -d "$dstdir"
2877c5f6000Smrg      dstdir_status=$?
2887c5f6000Smrg    fi
2897c5f6000Smrg  fi
2907c5f6000Smrg
2917c5f6000Smrg  obsolete_mkdir_used=false
2927c5f6000Smrg
2937c5f6000Smrg  if test $dstdir_status != 0; then
2947c5f6000Smrg    case $posix_mkdir in
2957c5f6000Smrg      '')
29689afc689Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29789afc689Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29889afc689Smrg        umask=`umask`
29989afc689Smrg        case $stripcmd.$umask in
30089afc689Smrg          # Optimize common cases.
30189afc689Smrg          *[2367][2367]) mkdir_umask=$umask;;
30289afc689Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30389afc689Smrg
30489afc689Smrg          *[0-7])
30589afc689Smrg            mkdir_umask=`expr $umask + 22 \
30689afc689Smrg              - $umask % 100 % 40 + $umask % 20 \
30789afc689Smrg              - $umask % 10 % 4 + $umask % 2
30889afc689Smrg            `;;
30989afc689Smrg          *) mkdir_umask=$umask,go-w;;
31089afc689Smrg        esac
31189afc689Smrg
31289afc689Smrg        # With -d, create the new directory with the user-specified mode.
31389afc689Smrg        # Otherwise, rely on $mkdir_umask.
31489afc689Smrg        if test -n "$dir_arg"; then
31589afc689Smrg          mkdir_mode=-m$mode
31689afc689Smrg        else
31789afc689Smrg          mkdir_mode=
31889afc689Smrg        fi
31989afc689Smrg
32089afc689Smrg        posix_mkdir=false
32189afc689Smrg        case $umask in
32289afc689Smrg          *[123567][0-7][0-7])
32389afc689Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32489afc689Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32589afc689Smrg            ;;
32689afc689Smrg          *)
32789afc689Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32889afc689Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32989afc689Smrg
33089afc689Smrg            if (umask $mkdir_umask &&
33189afc689Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33289afc689Smrg            then
33389afc689Smrg              if test -z "$dir_arg" || {
33489afc689Smrg                   # Check for POSIX incompatibilities with -m.
33589afc689Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33689afc689Smrg                   # other-writable bit of parent directory when it shouldn't.
33789afc689Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33889afc689Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33989afc689Smrg                   case $ls_ld_tmpdir in
34089afc689Smrg                     d????-?r-*) different_mode=700;;
34189afc689Smrg                     d????-?--*) different_mode=755;;
34289afc689Smrg                     *) false;;
34389afc689Smrg                   esac &&
34489afc689Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34589afc689Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34689afc689Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34789afc689Smrg                   }
34889afc689Smrg                 }
34989afc689Smrg              then posix_mkdir=:
35089afc689Smrg              fi
35189afc689Smrg              rmdir "$tmpdir/d" "$tmpdir"
35289afc689Smrg            else
35389afc689Smrg              # Remove any dirs left behind by ancient mkdir implementations.
35489afc689Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35589afc689Smrg            fi
35689afc689Smrg            trap '' 0;;
35789afc689Smrg        esac;;
3587c5f6000Smrg    esac
359bccedf53Smrg
3607c5f6000Smrg    if
3617c5f6000Smrg      $posix_mkdir && (
36289afc689Smrg        umask $mkdir_umask &&
36389afc689Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3647c5f6000Smrg      )
3657c5f6000Smrg    then :
3667c5f6000Smrg    else
3677c5f6000Smrg
3687c5f6000Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3697c5f6000Smrg      # or it failed possibly due to a race condition.  Create the
3707c5f6000Smrg      # directory the slow way, step by step, checking for races as we go.
3717c5f6000Smrg
3727c5f6000Smrg      case $dstdir in
37389afc689Smrg        /*) prefix='/';;
37489afc689Smrg        [-=\(\)!]*) prefix='./';;
37589afc689Smrg        *)  prefix='';;
3767c5f6000Smrg      esac
3777c5f6000Smrg
3787c5f6000Smrg      oIFS=$IFS
3797c5f6000Smrg      IFS=/
38089afc689Smrg      set -f
3817c5f6000Smrg      set fnord $dstdir
3827c5f6000Smrg      shift
38389afc689Smrg      set +f
3847c5f6000Smrg      IFS=$oIFS
3857c5f6000Smrg
3867c5f6000Smrg      prefixes=
3877c5f6000Smrg
3887c5f6000Smrg      for d
3897c5f6000Smrg      do
39089afc689Smrg        test X"$d" = X && continue
39189afc689Smrg
39289afc689Smrg        prefix=$prefix$d
39389afc689Smrg        if test -d "$prefix"; then
39489afc689Smrg          prefixes=
39589afc689Smrg        else
39689afc689Smrg          if $posix_mkdir; then
39789afc689Smrg            (umask=$mkdir_umask &&
39889afc689Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39989afc689Smrg            # Don't fail if two instances are running concurrently.
40089afc689Smrg            test -d "$prefix" || exit 1
40189afc689Smrg          else
40289afc689Smrg            case $prefix in
40389afc689Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40489afc689Smrg              *) qprefix=$prefix;;
40589afc689Smrg            esac
40689afc689Smrg            prefixes="$prefixes '$qprefix'"
40789afc689Smrg          fi
40889afc689Smrg        fi
40989afc689Smrg        prefix=$prefix/
4107c5f6000Smrg      done
4117c5f6000Smrg
4127c5f6000Smrg      if test -n "$prefixes"; then
41389afc689Smrg        # Don't fail if two instances are running concurrently.
41489afc689Smrg        (umask $mkdir_umask &&
41589afc689Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41689afc689Smrg          test -d "$dstdir" || exit 1
41789afc689Smrg        obsolete_mkdir_used=true
4187c5f6000Smrg      fi
4197c5f6000Smrg    fi
4207c5f6000Smrg  fi
4217c5f6000Smrg
4227c5f6000Smrg  if test -n "$dir_arg"; then
4237c5f6000Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4247c5f6000Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4257c5f6000Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4267c5f6000Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4277c5f6000Smrg  else
4287c5f6000Smrg
4297c5f6000Smrg    # Make a couple of temp file names in the proper directory.
4307c5f6000Smrg    dsttmp=$dstdir/_inst.$$_
4317c5f6000Smrg    rmtmp=$dstdir/_rm.$$_
4327c5f6000Smrg
4337c5f6000Smrg    # Trap to clean up those temp files at exit.
4347c5f6000Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4357c5f6000Smrg
4367c5f6000Smrg    # Copy the file name to the temp name.
4377c5f6000Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4387c5f6000Smrg
4397c5f6000Smrg    # and set any options; do chmod last to preserve setuid bits.
4407c5f6000Smrg    #
4417c5f6000Smrg    # If any of these fail, we abort the whole thing.  If we want to
4427c5f6000Smrg    # ignore errors from any of these, just make sure not to ignore
4437c5f6000Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4447c5f6000Smrg    #
4457c5f6000Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4467c5f6000Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4477c5f6000Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4487c5f6000Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4497c5f6000Smrg
4507c5f6000Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4517c5f6000Smrg    if $copy_on_change &&
45289afc689Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45389afc689Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45489afc689Smrg       set -f &&
4557c5f6000Smrg       set X $old && old=:$2:$4:$5:$6 &&
4567c5f6000Smrg       set X $new && new=:$2:$4:$5:$6 &&
45789afc689Smrg       set +f &&
4587c5f6000Smrg       test "$old" = "$new" &&
4597c5f6000Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4607c5f6000Smrg    then
4617c5f6000Smrg      rm -f "$dsttmp"
4627c5f6000Smrg    else
4637c5f6000Smrg      # Rename the file to the real destination.
4647c5f6000Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4657c5f6000Smrg
4667c5f6000Smrg      # The rename failed, perhaps because mv can't rename something else
4677c5f6000Smrg      # to itself, or perhaps because mv is so ancient that it does not
4687c5f6000Smrg      # support -f.
4697c5f6000Smrg      {
47089afc689Smrg        # Now remove or move aside any old file at destination location.
47189afc689Smrg        # We try this two ways since rm can't unlink itself on some
47289afc689Smrg        # systems and the destination file might be busy for other
47389afc689Smrg        # reasons.  In this case, the final cleanup might fail but the new
47489afc689Smrg        # file should still install successfully.
47589afc689Smrg        {
47689afc689Smrg          test ! -f "$dst" ||
47789afc689Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47889afc689Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47989afc689Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48089afc689Smrg          } ||
48189afc689Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48289afc689Smrg            (exit 1); exit 1
48389afc689Smrg          }
48489afc689Smrg        } &&
48589afc689Smrg
48689afc689Smrg        # Now rename the file to the real destination.
48789afc689Smrg        $doit $mvcmd "$dsttmp" "$dst"
4887c5f6000Smrg      }
4897c5f6000Smrg    fi || exit 1
4907c5f6000Smrg
4917c5f6000Smrg    trap '' 0
4927c5f6000Smrg  fi
4937c5f6000Smrgdone
494bccedf53Smrg
4957c5f6000Smrg# Local variables:
4967c5f6000Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4977c5f6000Smrg# time-stamp-start: "scriptversion="
4987c5f6000Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4997c5f6000Smrg# time-stamp-time-zone: "UTC"
5007c5f6000Smrg# time-stamp-end: "; # UTC"
5017c5f6000Smrg# End:
502