16c321187Smrg#!/bin/sh
26c321187Smrg# install - install a program, script, or datafile
36c321187Smrg
49dedec0cSmrgscriptversion=2020-11-14.01; # UTC
56c321187Smrg
66c321187Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76c321187Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86c321187Smrg# following copyright and license.
96c321187Smrg#
106c321187Smrg# Copyright (C) 1994 X Consortium
116c321187Smrg#
126c321187Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136c321187Smrg# of this software and associated documentation files (the "Software"), to
146c321187Smrg# deal in the Software without restriction, including without limitation the
156c321187Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166c321187Smrg# sell copies of the Software, and to permit persons to whom the Software is
176c321187Smrg# furnished to do so, subject to the following conditions:
186c321187Smrg#
196c321187Smrg# The above copyright notice and this permission notice shall be included in
206c321187Smrg# all copies or substantial portions of the Software.
216c321187Smrg#
226c321187Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236c321187Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246c321187Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256c321187Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266c321187Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276c321187Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286c321187Smrg#
296c321187Smrg# Except as contained in this notice, the name of the X Consortium shall not
306c321187Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316c321187Smrg# ings in this Software without prior written authorization from the X Consor-
326c321187Smrg# tium.
336c321187Smrg#
346c321187Smrg#
356c321187Smrg# FSF changes to this file are in the public domain.
366c321187Smrg#
376c321187Smrg# Calling this script install-sh is preferred over install.sh, to prevent
389d0b5e55Smrg# 'make' implicit rules from creating a file called install from it
396c321187Smrg# when there is no Makefile.
406c321187Smrg#
416c321187Smrg# This script is compatible with the BSD install script, but was written
426c321187Smrg# from scratch.
436c321187Smrg
44198e4c3cSmrgtab='	'
456c321187Smrgnl='
466c321187Smrg'
47198e4c3cSmrgIFS=" $tab$nl"
486c321187Smrg
49198e4c3cSmrg# Set DOITPROG to "echo" to test this script.
506c321187Smrg
5193493779Smrgdoit=${DOITPROG-}
52198e4c3cSmrgdoit_exec=${doit:-exec}
536c321187Smrg
546c321187Smrg# Put in absolute file names if you don't have them in your path;
556c321187Smrg# or use environment vars.
566c321187Smrg
5793493779Smrgchgrpprog=${CHGRPPROG-chgrp}
5893493779Smrgchmodprog=${CHMODPROG-chmod}
5993493779Smrgchownprog=${CHOWNPROG-chown}
6093493779Smrgcmpprog=${CMPPROG-cmp}
6193493779Smrgcpprog=${CPPROG-cp}
6293493779Smrgmkdirprog=${MKDIRPROG-mkdir}
6393493779Smrgmvprog=${MVPROG-mv}
6493493779Smrgrmprog=${RMPROG-rm}
6593493779Smrgstripprog=${STRIPPROG-strip}
6693493779Smrg
676c321187Smrgposix_mkdir=
686c321187Smrg
696c321187Smrg# Desired mode of installed file.
706c321187Smrgmode=0755
716c321187Smrg
729dedec0cSmrg# Create dirs (including intermediate dirs) using mode 755.
739dedec0cSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
749dedec0cSmrgmkdir_umask=22
759dedec0cSmrg
769dedec0cSmrgbackupsuffix=
7793493779Smrgchgrpcmd=
786c321187Smrgchmodcmd=$chmodprog
796c321187Smrgchowncmd=
8093493779Smrgmvcmd=$mvprog
816c321187Smrgrmcmd="$rmprog -f"
8293493779Smrgstripcmd=
8393493779Smrg
846c321187Smrgsrc=
856c321187Smrgdst=
866c321187Smrgdir_arg=
8793493779Smrgdst_arg=
8893493779Smrg
8993493779Smrgcopy_on_change=false
90198e4c3cSmrgis_target_a_directory=possibly
916c321187Smrg
9293493779Smrgusage="\
9393493779SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
946c321187Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
956c321187Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
966c321187Smrg   or: $0 [OPTION]... -d DIRECTORIES...
976c321187Smrg
986c321187SmrgIn the 1st form, copy SRCFILE to DSTFILE.
996c321187SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1006c321187SmrgIn the 4th, create DIRECTORIES.
1016c321187Smrg
1026c321187SmrgOptions:
10393493779Smrg     --help     display this help and exit.
10493493779Smrg     --version  display version info and exit.
10593493779Smrg
10693493779Smrg  -c            (ignored)
1079dedec0cSmrg  -C            install only if different (preserve data modification time)
10893493779Smrg  -d            create directories instead of installing files.
10993493779Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11093493779Smrg  -m MODE       $chmodprog installed files to MODE.
11193493779Smrg  -o USER       $chownprog installed files to USER.
1129dedec0cSmrg  -p            pass -p to $cpprog.
11393493779Smrg  -s            $stripprog installed files.
1149dedec0cSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11593493779Smrg  -t DIRECTORY  install into DIRECTORY.
11693493779Smrg  -T            report an error if DSTFILE is a directory.
1176c321187Smrg
1186c321187SmrgEnvironment variables override the default commands:
11993493779Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12093493779Smrg  RMPROG STRIPPROG
1219dedec0cSmrg
1229dedec0cSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1239dedec0cSmrgit's up to you to specify -f if you want it.
1249dedec0cSmrg
1259dedec0cSmrgIf -S is not specified, no backups are attempted.
1269dedec0cSmrg
1279dedec0cSmrgEmail bug reports to bug-automake@gnu.org.
1289dedec0cSmrgAutomake home page: https://www.gnu.org/software/automake/
1296c321187Smrg"
1306c321187Smrg
1316c321187Smrgwhile test $# -ne 0; do
1326c321187Smrg  case $1 in
13393493779Smrg    -c) ;;
13493493779Smrg
13593493779Smrg    -C) copy_on_change=true;;
1366c321187Smrg
13793493779Smrg    -d) dir_arg=true;;
1386c321187Smrg
1396c321187Smrg    -g) chgrpcmd="$chgrpprog $2"
140198e4c3cSmrg        shift;;
1416c321187Smrg
1426c321187Smrg    --help) echo "$usage"; exit $?;;
1436c321187Smrg
1446c321187Smrg    -m) mode=$2
145198e4c3cSmrg        case $mode in
146198e4c3cSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147198e4c3cSmrg            echo "$0: invalid mode: $mode" >&2
148198e4c3cSmrg            exit 1;;
149198e4c3cSmrg        esac
150198e4c3cSmrg        shift;;
1516c321187Smrg
1526c321187Smrg    -o) chowncmd="$chownprog $2"
153198e4c3cSmrg        shift;;
1546c321187Smrg
1559dedec0cSmrg    -p) cpprog="$cpprog -p";;
1569dedec0cSmrg
15793493779Smrg    -s) stripcmd=$stripprog;;
1586c321187Smrg
1599dedec0cSmrg    -S) backupsuffix="$2"
1609dedec0cSmrg        shift;;
1619dedec0cSmrg
162198e4c3cSmrg    -t)
163198e4c3cSmrg        is_target_a_directory=always
164198e4c3cSmrg        dst_arg=$2
165198e4c3cSmrg        # Protect names problematic for 'test' and other utilities.
166198e4c3cSmrg        case $dst_arg in
167198e4c3cSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168198e4c3cSmrg        esac
169198e4c3cSmrg        shift;;
1706c321187Smrg
171198e4c3cSmrg    -T) is_target_a_directory=never;;
1726c321187Smrg
1736c321187Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1746c321187Smrg
175198e4c3cSmrg    --) shift
176198e4c3cSmrg        break;;
1776c321187Smrg
178198e4c3cSmrg    -*) echo "$0: invalid option: $1" >&2
179198e4c3cSmrg        exit 1;;
1806c321187Smrg
1816c321187Smrg    *)  break;;
1826c321187Smrg  esac
18393493779Smrg  shift
1846c321187Smrgdone
1856c321187Smrg
186198e4c3cSmrg# We allow the use of options -d and -T together, by making -d
187198e4c3cSmrg# take the precedence; this is for compatibility with GNU install.
188198e4c3cSmrg
189198e4c3cSmrgif test -n "$dir_arg"; then
190198e4c3cSmrg  if test -n "$dst_arg"; then
191198e4c3cSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192198e4c3cSmrg    exit 1
193198e4c3cSmrg  fi
194198e4c3cSmrgfi
195198e4c3cSmrg
19693493779Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1976c321187Smrg  # When -d is used, all remaining arguments are directories to create.
1986c321187Smrg  # When -t is used, the destination is already specified.
1996c321187Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2006c321187Smrg  for arg
2016c321187Smrg  do
20293493779Smrg    if test -n "$dst_arg"; then
2036c321187Smrg      # $@ is not empty: it contains at least $arg.
20493493779Smrg      set fnord "$@" "$dst_arg"
2056c321187Smrg      shift # fnord
2066c321187Smrg    fi
2076c321187Smrg    shift # arg
20893493779Smrg    dst_arg=$arg
2099d0b5e55Smrg    # Protect names problematic for 'test' and other utilities.
2109d0b5e55Smrg    case $dst_arg in
2119d0b5e55Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2129d0b5e55Smrg    esac
2136c321187Smrg  done
2146c321187Smrgfi
2156c321187Smrg
2166c321187Smrgif test $# -eq 0; then
2176c321187Smrg  if test -z "$dir_arg"; then
2186c321187Smrg    echo "$0: no input file specified." >&2
2196c321187Smrg    exit 1
2206c321187Smrg  fi
2219d0b5e55Smrg  # It's OK to call 'install-sh -d' without argument.
2226c321187Smrg  # This can happen when creating conditional directories.
2236c321187Smrg  exit 0
2246c321187Smrgfi
2256c321187Smrg
226198e4c3cSmrgif test -z "$dir_arg"; then
227198e4c3cSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228198e4c3cSmrg    if test ! -d "$dst_arg"; then
229198e4c3cSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230198e4c3cSmrg      exit 1
231198e4c3cSmrg    fi
232198e4c3cSmrg  fi
233198e4c3cSmrgfi
234198e4c3cSmrg
2356c321187Smrgif test -z "$dir_arg"; then
2369d0b5e55Smrg  do_exit='(exit $ret); exit $ret'
2379d0b5e55Smrg  trap "ret=129; $do_exit" 1
2389d0b5e55Smrg  trap "ret=130; $do_exit" 2
2399d0b5e55Smrg  trap "ret=141; $do_exit" 13
2409d0b5e55Smrg  trap "ret=143; $do_exit" 15
2416c321187Smrg
2426c321187Smrg  # Set umask so as not to create temps with too-generous modes.
2436c321187Smrg  # However, 'strip' requires both read and write access to temps.
2446c321187Smrg  case $mode in
2456c321187Smrg    # Optimize common cases.
2466c321187Smrg    *644) cp_umask=133;;
2476c321187Smrg    *755) cp_umask=22;;
2486c321187Smrg
2496c321187Smrg    *[0-7])
2506c321187Smrg      if test -z "$stripcmd"; then
251198e4c3cSmrg        u_plus_rw=
2526c321187Smrg      else
253198e4c3cSmrg        u_plus_rw='% 200'
2546c321187Smrg      fi
2556c321187Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2566c321187Smrg    *)
2576c321187Smrg      if test -z "$stripcmd"; then
258198e4c3cSmrg        u_plus_rw=
2596c321187Smrg      else
260198e4c3cSmrg        u_plus_rw=,u+rw
2616c321187Smrg      fi
2626c321187Smrg      cp_umask=$mode$u_plus_rw;;
2636c321187Smrg  esac
2646c321187Smrgfi
2656c321187Smrg
2666c321187Smrgfor src
2676c321187Smrgdo
2689d0b5e55Smrg  # Protect names problematic for 'test' and other utilities.
2696c321187Smrg  case $src in
2709d0b5e55Smrg    -* | [=\(\)!]) src=./$src;;
2716c321187Smrg  esac
2726c321187Smrg
2736c321187Smrg  if test -n "$dir_arg"; then
2746c321187Smrg    dst=$src
2756c321187Smrg    dstdir=$dst
2766c321187Smrg    test -d "$dstdir"
2776c321187Smrg    dstdir_status=$?
2789dedec0cSmrg    # Don't chown directories that already exist.
2799dedec0cSmrg    if test $dstdir_status = 0; then
2809dedec0cSmrg      chowncmd=""
2819dedec0cSmrg    fi
2826c321187Smrg  else
2836c321187Smrg
2846c321187Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2856c321187Smrg    # might cause directories to be created, which would be especially bad
2866c321187Smrg    # if $src (and thus $dsttmp) contains '*'.
2876c321187Smrg    if test ! -f "$src" && test ! -d "$src"; then
2886c321187Smrg      echo "$0: $src does not exist." >&2
2896c321187Smrg      exit 1
2906c321187Smrg    fi
2916c321187Smrg
29293493779Smrg    if test -z "$dst_arg"; then
2936c321187Smrg      echo "$0: no destination specified." >&2
2946c321187Smrg      exit 1
2956c321187Smrg    fi
29693493779Smrg    dst=$dst_arg
2976c321187Smrg
2989dedec0cSmrg    # If destination is a directory, append the input filename.
2996c321187Smrg    if test -d "$dst"; then
300198e4c3cSmrg      if test "$is_target_a_directory" = never; then
301198e4c3cSmrg        echo "$0: $dst_arg: Is a directory" >&2
302198e4c3cSmrg        exit 1
3036c321187Smrg      fi
3046c321187Smrg      dstdir=$dst
3059dedec0cSmrg      dstbase=`basename "$src"`
3069dedec0cSmrg      case $dst in
3079dedec0cSmrg	*/) dst=$dst$dstbase;;
3089dedec0cSmrg	*)  dst=$dst/$dstbase;;
3099dedec0cSmrg      esac
3106c321187Smrg      dstdir_status=0
3116c321187Smrg    else
312198e4c3cSmrg      dstdir=`dirname "$dst"`
3136c321187Smrg      test -d "$dstdir"
3146c321187Smrg      dstdir_status=$?
3156c321187Smrg    fi
3166c321187Smrg  fi
3176c321187Smrg
3189dedec0cSmrg  case $dstdir in
3199dedec0cSmrg    */) dstdirslash=$dstdir;;
3209dedec0cSmrg    *)  dstdirslash=$dstdir/;;
3219dedec0cSmrg  esac
3229dedec0cSmrg
3236c321187Smrg  obsolete_mkdir_used=false
3246c321187Smrg
3256c321187Smrg  if test $dstdir_status != 0; then
3266c321187Smrg    case $posix_mkdir in
3276c321187Smrg      '')
328198e4c3cSmrg        # With -d, create the new directory with the user-specified mode.
329198e4c3cSmrg        # Otherwise, rely on $mkdir_umask.
330198e4c3cSmrg        if test -n "$dir_arg"; then
331198e4c3cSmrg          mkdir_mode=-m$mode
332198e4c3cSmrg        else
333198e4c3cSmrg          mkdir_mode=
334198e4c3cSmrg        fi
335198e4c3cSmrg
336198e4c3cSmrg        posix_mkdir=false
3379dedec0cSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3389dedec0cSmrg	# here however when possible just to lower collision chance.
3399dedec0cSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3409dedec0cSmrg
3419dedec0cSmrg	trap '
3429dedec0cSmrg	  ret=$?
3439dedec0cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3449dedec0cSmrg	  exit $ret
3459dedec0cSmrg	' 0
3469dedec0cSmrg
3479dedec0cSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3489dedec0cSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3499dedec0cSmrg	# directory is successfully created first before we actually test
3509dedec0cSmrg	# 'mkdir -p'.
3519dedec0cSmrg	if (umask $mkdir_umask &&
3529dedec0cSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3539dedec0cSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3549dedec0cSmrg	then
3559dedec0cSmrg	  if test -z "$dir_arg" || {
3569dedec0cSmrg	       # Check for POSIX incompatibilities with -m.
3579dedec0cSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3589dedec0cSmrg	       # other-writable bit of parent directory when it shouldn't.
3599dedec0cSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3609dedec0cSmrg	       test_tmpdir="$tmpdir/a"
3619dedec0cSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3629dedec0cSmrg	       case $ls_ld_tmpdir in
3639dedec0cSmrg		 d????-?r-*) different_mode=700;;
3649dedec0cSmrg		 d????-?--*) different_mode=755;;
3659dedec0cSmrg		 *) false;;
3669dedec0cSmrg	       esac &&
3679dedec0cSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3689dedec0cSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3699dedec0cSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3709dedec0cSmrg	       }
3719dedec0cSmrg	     }
3729dedec0cSmrg	  then posix_mkdir=:
3739dedec0cSmrg	  fi
3749dedec0cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3759dedec0cSmrg	else
3769dedec0cSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3779dedec0cSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3789dedec0cSmrg	fi
3799dedec0cSmrg	trap '' 0;;
3806c321187Smrg    esac
3816c321187Smrg
3826c321187Smrg    if
3836c321187Smrg      $posix_mkdir && (
384198e4c3cSmrg        umask $mkdir_umask &&
385198e4c3cSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3866c321187Smrg      )
3876c321187Smrg    then :
3886c321187Smrg    else
3896c321187Smrg
3909dedec0cSmrg      # mkdir does not conform to POSIX,
3916c321187Smrg      # or it failed possibly due to a race condition.  Create the
3926c321187Smrg      # directory the slow way, step by step, checking for races as we go.
3936c321187Smrg
3946c321187Smrg      case $dstdir in
395198e4c3cSmrg        /*) prefix='/';;
396198e4c3cSmrg        [-=\(\)!]*) prefix='./';;
397198e4c3cSmrg        *)  prefix='';;
3986c321187Smrg      esac
3996c321187Smrg
4006c321187Smrg      oIFS=$IFS
4016c321187Smrg      IFS=/
402198e4c3cSmrg      set -f
4036c321187Smrg      set fnord $dstdir
4046c321187Smrg      shift
405198e4c3cSmrg      set +f
4066c321187Smrg      IFS=$oIFS
4076c321187Smrg
4086c321187Smrg      prefixes=
4096c321187Smrg
4106c321187Smrg      for d
4116c321187Smrg      do
412198e4c3cSmrg        test X"$d" = X && continue
413198e4c3cSmrg
414198e4c3cSmrg        prefix=$prefix$d
415198e4c3cSmrg        if test -d "$prefix"; then
416198e4c3cSmrg          prefixes=
417198e4c3cSmrg        else
418198e4c3cSmrg          if $posix_mkdir; then
4199dedec0cSmrg            (umask $mkdir_umask &&
420198e4c3cSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421198e4c3cSmrg            # Don't fail if two instances are running concurrently.
422198e4c3cSmrg            test -d "$prefix" || exit 1
423198e4c3cSmrg          else
424198e4c3cSmrg            case $prefix in
425198e4c3cSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426198e4c3cSmrg              *) qprefix=$prefix;;
427198e4c3cSmrg            esac
428198e4c3cSmrg            prefixes="$prefixes '$qprefix'"
429198e4c3cSmrg          fi
430198e4c3cSmrg        fi
431198e4c3cSmrg        prefix=$prefix/
4326c321187Smrg      done
4336c321187Smrg
4346c321187Smrg      if test -n "$prefixes"; then
435198e4c3cSmrg        # Don't fail if two instances are running concurrently.
436198e4c3cSmrg        (umask $mkdir_umask &&
437198e4c3cSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438198e4c3cSmrg          test -d "$dstdir" || exit 1
439198e4c3cSmrg        obsolete_mkdir_used=true
4406c321187Smrg      fi
4416c321187Smrg    fi
4426c321187Smrg  fi
4436c321187Smrg
4446c321187Smrg  if test -n "$dir_arg"; then
4456c321187Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4466c321187Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4476c321187Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4486c321187Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4496c321187Smrg  else
4506c321187Smrg
4516c321187Smrg    # Make a couple of temp file names in the proper directory.
4529dedec0cSmrg    dsttmp=${dstdirslash}_inst.$$_
4539dedec0cSmrg    rmtmp=${dstdirslash}_rm.$$_
4546c321187Smrg
4556c321187Smrg    # Trap to clean up those temp files at exit.
4566c321187Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4576c321187Smrg
4586c321187Smrg    # Copy the file name to the temp name.
4599dedec0cSmrg    (umask $cp_umask &&
4609dedec0cSmrg     { test -z "$stripcmd" || {
4619dedec0cSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4629dedec0cSmrg	 # which would cause strip to fail.
4639dedec0cSmrg	 if test -z "$doit"; then
4649dedec0cSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4659dedec0cSmrg	 else
4669dedec0cSmrg	   $doit touch "$dsttmp"
4679dedec0cSmrg	 fi
4689dedec0cSmrg       }
4699dedec0cSmrg     } &&
4709dedec0cSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4716c321187Smrg
4726c321187Smrg    # and set any options; do chmod last to preserve setuid bits.
4736c321187Smrg    #
4746c321187Smrg    # If any of these fail, we abort the whole thing.  If we want to
4756c321187Smrg    # ignore errors from any of these, just make sure not to ignore
4766c321187Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4776c321187Smrg    #
47893493779Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47993493779Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48093493779Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48193493779Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48293493779Smrg
48393493779Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48493493779Smrg    if $copy_on_change &&
485198e4c3cSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486198e4c3cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487198e4c3cSmrg       set -f &&
48893493779Smrg       set X $old && old=:$2:$4:$5:$6 &&
48993493779Smrg       set X $new && new=:$2:$4:$5:$6 &&
490198e4c3cSmrg       set +f &&
49193493779Smrg       test "$old" = "$new" &&
49293493779Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49393493779Smrg    then
49493493779Smrg      rm -f "$dsttmp"
49593493779Smrg    else
4969dedec0cSmrg      # If $backupsuffix is set, and the file being installed
4979dedec0cSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4989dedec0cSmrg      # e.g., if mv doesn't support -f.
4999dedec0cSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5009dedec0cSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5019dedec0cSmrg      fi
5029dedec0cSmrg
50393493779Smrg      # Rename the file to the real destination.
50493493779Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50593493779Smrg
50693493779Smrg      # The rename failed, perhaps because mv can't rename something else
50793493779Smrg      # to itself, or perhaps because mv is so ancient that it does not
50893493779Smrg      # support -f.
50993493779Smrg      {
510198e4c3cSmrg        # Now remove or move aside any old file at destination location.
511198e4c3cSmrg        # We try this two ways since rm can't unlink itself on some
512198e4c3cSmrg        # systems and the destination file might be busy for other
513198e4c3cSmrg        # reasons.  In this case, the final cleanup might fail but the new
514198e4c3cSmrg        # file should still install successfully.
515198e4c3cSmrg        {
516198e4c3cSmrg          test ! -f "$dst" ||
5179dedec0cSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518198e4c3cSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5199dedec0cSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520198e4c3cSmrg          } ||
521198e4c3cSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522198e4c3cSmrg            (exit 1); exit 1
523198e4c3cSmrg          }
524198e4c3cSmrg        } &&
525198e4c3cSmrg
526198e4c3cSmrg        # Now rename the file to the real destination.
527198e4c3cSmrg        $doit $mvcmd "$dsttmp" "$dst"
52893493779Smrg      }
52993493779Smrg    fi || exit 1
5306c321187Smrg
5316c321187Smrg    trap '' 0
5326c321187Smrg  fi
5336c321187Smrgdone
5346c321187Smrg
5356c321187Smrg# Local variables:
5369dedec0cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5376c321187Smrg# time-stamp-start: "scriptversion="
5386c321187Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5399dedec0cSmrg# time-stamp-time-zone: "UTC0"
54093493779Smrg# time-stamp-end: "; # UTC"
5416c321187Smrg# End:
542