116910e25Smrg#!/bin/sh
216910e25Smrg# install - install a program, script, or datafile
39c125d91Smrg
4ed89697eSmrgscriptversion=2020-11-14.01; # UTC
59c125d91Smrg
616910e25Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
716910e25Smrg# later released in X11R6 (xc/config/util/install.sh) with the
816910e25Smrg# following copyright and license.
916910e25Smrg#
1016910e25Smrg# Copyright (C) 1994 X Consortium
1116910e25Smrg#
1216910e25Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1316910e25Smrg# of this software and associated documentation files (the "Software"), to
1416910e25Smrg# deal in the Software without restriction, including without limitation the
1516910e25Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1616910e25Smrg# sell copies of the Software, and to permit persons to whom the Software is
1716910e25Smrg# furnished to do so, subject to the following conditions:
1816910e25Smrg#
1916910e25Smrg# The above copyright notice and this permission notice shall be included in
2016910e25Smrg# all copies or substantial portions of the Software.
2116910e25Smrg#
2216910e25Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2316910e25Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2416910e25Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2516910e25Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2616910e25Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2716910e25Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2816910e25Smrg#
2916910e25Smrg# Except as contained in this notice, the name of the X Consortium shall not
3016910e25Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3116910e25Smrg# ings in this Software without prior written authorization from the X Consor-
3216910e25Smrg# tium.
3316910e25Smrg#
3416910e25Smrg#
3516910e25Smrg# FSF changes to this file are in the public domain.
3616910e25Smrg#
3716910e25Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3876c9c3baSmrg# 'make' implicit rules from creating a file called install from it
3916910e25Smrg# when there is no Makefile.
4016910e25Smrg#
4116910e25Smrg# This script is compatible with the BSD install script, but was written
429c125d91Smrg# from scratch.
4316910e25Smrg
4476c9c3baSmrgtab='	'
459c125d91Smrgnl='
469c125d91Smrg'
4776c9c3baSmrgIFS=" $tab$nl"
4816910e25Smrg
4976c9c3baSmrg# Set DOITPROG to "echo" to test this script.
5016910e25Smrg
519c125d91Smrgdoit=${DOITPROG-}
5276c9c3baSmrgdoit_exec=${doit:-exec}
5316910e25Smrg
549c125d91Smrg# Put in absolute file names if you don't have them in your path;
559c125d91Smrg# or use environment vars.
569c125d91Smrg
579c125d91Smrgchgrpprog=${CHGRPPROG-chgrp}
589c125d91Smrgchmodprog=${CHMODPROG-chmod}
599c125d91Smrgchownprog=${CHOWNPROG-chown}
609c125d91Smrgcmpprog=${CMPPROG-cmp}
619c125d91Smrgcpprog=${CPPROG-cp}
629c125d91Smrgmkdirprog=${MKDIRPROG-mkdir}
639c125d91Smrgmvprog=${MVPROG-mv}
649c125d91Smrgrmprog=${RMPROG-rm}
659c125d91Smrgstripprog=${STRIPPROG-strip}
669c125d91Smrg
679c125d91Smrgposix_mkdir=
689c125d91Smrg
699c125d91Smrg# Desired mode of installed file.
709c125d91Smrgmode=0755
719c125d91Smrg
72ed89697eSmrg# Create dirs (including intermediate dirs) using mode 755.
73ed89697eSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74ed89697eSmrgmkdir_umask=22
75ed89697eSmrg
76ed89697eSmrgbackupsuffix=
779c125d91Smrgchgrpcmd=
789c125d91Smrgchmodcmd=$chmodprog
799c125d91Smrgchowncmd=
809c125d91Smrgmvcmd=$mvprog
819c125d91Smrgrmcmd="$rmprog -f"
829c125d91Smrgstripcmd=
8316910e25Smrg
849c125d91Smrgsrc=
859c125d91Smrgdst=
869c125d91Smrgdir_arg=
879c125d91Smrgdst_arg=
8816910e25Smrg
899c125d91Smrgcopy_on_change=false
9076c9c3baSmrgis_target_a_directory=possibly
9116910e25Smrg
929c125d91Smrgusage="\
939c125d91SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
949c125d91Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
959c125d91Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
969c125d91Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9716910e25Smrg
989c125d91SmrgIn the 1st form, copy SRCFILE to DSTFILE.
999c125d91SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1009c125d91SmrgIn the 4th, create DIRECTORIES.
10116910e25Smrg
1029c125d91SmrgOptions:
1039c125d91Smrg     --help     display this help and exit.
1049c125d91Smrg     --version  display version info and exit.
10516910e25Smrg
1069c125d91Smrg  -c            (ignored)
107ed89697eSmrg  -C            install only if different (preserve data modification time)
1089c125d91Smrg  -d            create directories instead of installing files.
1099c125d91Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1109c125d91Smrg  -m MODE       $chmodprog installed files to MODE.
1119c125d91Smrg  -o USER       $chownprog installed files to USER.
112ed89697eSmrg  -p            pass -p to $cpprog.
1139c125d91Smrg  -s            $stripprog installed files.
114ed89697eSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1159c125d91Smrg  -t DIRECTORY  install into DIRECTORY.
1169c125d91Smrg  -T            report an error if DSTFILE is a directory.
11716910e25Smrg
1189c125d91SmrgEnvironment variables override the default commands:
1199c125d91Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1209c125d91Smrg  RMPROG STRIPPROG
121ed89697eSmrg
122ed89697eSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123ed89697eSmrgit's up to you to specify -f if you want it.
124ed89697eSmrg
125ed89697eSmrgIf -S is not specified, no backups are attempted.
126ed89697eSmrg
127ed89697eSmrgEmail bug reports to bug-automake@gnu.org.
128ed89697eSmrgAutomake home page: https://www.gnu.org/software/automake/
1299c125d91Smrg"
13016910e25Smrg
1319c125d91Smrgwhile test $# -ne 0; do
1329c125d91Smrg  case $1 in
1339c125d91Smrg    -c) ;;
13416910e25Smrg
1359c125d91Smrg    -C) copy_on_change=true;;
13616910e25Smrg
1379c125d91Smrg    -d) dir_arg=true;;
13816910e25Smrg
1399c125d91Smrg    -g) chgrpcmd="$chgrpprog $2"
14076c9c3baSmrg        shift;;
14116910e25Smrg
1429c125d91Smrg    --help) echo "$usage"; exit $?;;
14316910e25Smrg
1449c125d91Smrg    -m) mode=$2
14576c9c3baSmrg        case $mode in
14676c9c3baSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14776c9c3baSmrg            echo "$0: invalid mode: $mode" >&2
14876c9c3baSmrg            exit 1;;
14976c9c3baSmrg        esac
15076c9c3baSmrg        shift;;
15116910e25Smrg
1529c125d91Smrg    -o) chowncmd="$chownprog $2"
15376c9c3baSmrg        shift;;
15416910e25Smrg
155ed89697eSmrg    -p) cpprog="$cpprog -p";;
156ed89697eSmrg
1579c125d91Smrg    -s) stripcmd=$stripprog;;
15816910e25Smrg
159ed89697eSmrg    -S) backupsuffix="$2"
160ed89697eSmrg        shift;;
161ed89697eSmrg
16276c9c3baSmrg    -t)
16376c9c3baSmrg        is_target_a_directory=always
16476c9c3baSmrg        dst_arg=$2
16576c9c3baSmrg        # Protect names problematic for 'test' and other utilities.
16676c9c3baSmrg        case $dst_arg in
16776c9c3baSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16876c9c3baSmrg        esac
16976c9c3baSmrg        shift;;
17016910e25Smrg
17176c9c3baSmrg    -T) is_target_a_directory=never;;
17216910e25Smrg
1739c125d91Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17416910e25Smrg
17576c9c3baSmrg    --) shift
17676c9c3baSmrg        break;;
17716910e25Smrg
17876c9c3baSmrg    -*) echo "$0: invalid option: $1" >&2
17976c9c3baSmrg        exit 1;;
18016910e25Smrg
1819c125d91Smrg    *)  break;;
1829c125d91Smrg  esac
1839c125d91Smrg  shift
1849c125d91Smrgdone
18516910e25Smrg
18676c9c3baSmrg# We allow the use of options -d and -T together, by making -d
18776c9c3baSmrg# take the precedence; this is for compatibility with GNU install.
18876c9c3baSmrg
18976c9c3baSmrgif test -n "$dir_arg"; then
19076c9c3baSmrg  if test -n "$dst_arg"; then
19176c9c3baSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19276c9c3baSmrg    exit 1
19376c9c3baSmrg  fi
19476c9c3baSmrgfi
19576c9c3baSmrg
1969c125d91Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1979c125d91Smrg  # When -d is used, all remaining arguments are directories to create.
1989c125d91Smrg  # When -t is used, the destination is already specified.
1999c125d91Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2009c125d91Smrg  for arg
2019c125d91Smrg  do
2029c125d91Smrg    if test -n "$dst_arg"; then
2039c125d91Smrg      # $@ is not empty: it contains at least $arg.
2049c125d91Smrg      set fnord "$@" "$dst_arg"
2059c125d91Smrg      shift # fnord
2069c125d91Smrg    fi
2079c125d91Smrg    shift # arg
2089c125d91Smrg    dst_arg=$arg
20976c9c3baSmrg    # Protect names problematic for 'test' and other utilities.
21076c9c3baSmrg    case $dst_arg in
21176c9c3baSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21276c9c3baSmrg    esac
2139c125d91Smrg  done
2149c125d91Smrgfi
21516910e25Smrg
2169c125d91Smrgif test $# -eq 0; then
2179c125d91Smrg  if test -z "$dir_arg"; then
2189c125d91Smrg    echo "$0: no input file specified." >&2
2199c125d91Smrg    exit 1
2209c125d91Smrg  fi
22176c9c3baSmrg  # It's OK to call 'install-sh -d' without argument.
2229c125d91Smrg  # This can happen when creating conditional directories.
2239c125d91Smrg  exit 0
2249c125d91Smrgfi
22516910e25Smrg
2269c125d91Smrgif test -z "$dir_arg"; then
22776c9c3baSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22876c9c3baSmrg    if test ! -d "$dst_arg"; then
22976c9c3baSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23076c9c3baSmrg      exit 1
23176c9c3baSmrg    fi
23276c9c3baSmrg  fi
23376c9c3baSmrgfi
23476c9c3baSmrg
23576c9c3baSmrgif test -z "$dir_arg"; then
23676c9c3baSmrg  do_exit='(exit $ret); exit $ret'
23776c9c3baSmrg  trap "ret=129; $do_exit" 1
23876c9c3baSmrg  trap "ret=130; $do_exit" 2
23976c9c3baSmrg  trap "ret=141; $do_exit" 13
24076c9c3baSmrg  trap "ret=143; $do_exit" 15
2419c125d91Smrg
2429c125d91Smrg  # Set umask so as not to create temps with too-generous modes.
2439c125d91Smrg  # However, 'strip' requires both read and write access to temps.
2449c125d91Smrg  case $mode in
2459c125d91Smrg    # Optimize common cases.
2469c125d91Smrg    *644) cp_umask=133;;
2479c125d91Smrg    *755) cp_umask=22;;
2489c125d91Smrg
2499c125d91Smrg    *[0-7])
2509c125d91Smrg      if test -z "$stripcmd"; then
25176c9c3baSmrg        u_plus_rw=
2529c125d91Smrg      else
25376c9c3baSmrg        u_plus_rw='% 200'
2549c125d91Smrg      fi
2559c125d91Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2569c125d91Smrg    *)
2579c125d91Smrg      if test -z "$stripcmd"; then
25876c9c3baSmrg        u_plus_rw=
2599c125d91Smrg      else
26076c9c3baSmrg        u_plus_rw=,u+rw
2619c125d91Smrg      fi
2629c125d91Smrg      cp_umask=$mode$u_plus_rw;;
2639c125d91Smrg  esac
2649c125d91Smrgfi
26516910e25Smrg
2669c125d91Smrgfor src
2679c125d91Smrgdo
26876c9c3baSmrg  # Protect names problematic for 'test' and other utilities.
2699c125d91Smrg  case $src in
27076c9c3baSmrg    -* | [=\(\)!]) src=./$src;;
2719c125d91Smrg  esac
2729c125d91Smrg
2739c125d91Smrg  if test -n "$dir_arg"; then
2749c125d91Smrg    dst=$src
2759c125d91Smrg    dstdir=$dst
2769c125d91Smrg    test -d "$dstdir"
2779c125d91Smrg    dstdir_status=$?
278ed89697eSmrg    # Don't chown directories that already exist.
279ed89697eSmrg    if test $dstdir_status = 0; then
280ed89697eSmrg      chowncmd=""
281ed89697eSmrg    fi
2829c125d91Smrg  else
2839c125d91Smrg
2849c125d91Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2859c125d91Smrg    # might cause directories to be created, which would be especially bad
2869c125d91Smrg    # if $src (and thus $dsttmp) contains '*'.
2879c125d91Smrg    if test ! -f "$src" && test ! -d "$src"; then
2889c125d91Smrg      echo "$0: $src does not exist." >&2
2899c125d91Smrg      exit 1
2909c125d91Smrg    fi
2919c125d91Smrg
2929c125d91Smrg    if test -z "$dst_arg"; then
2939c125d91Smrg      echo "$0: no destination specified." >&2
2949c125d91Smrg      exit 1
2959c125d91Smrg    fi
2969c125d91Smrg    dst=$dst_arg
29716910e25Smrg
298ed89697eSmrg    # If destination is a directory, append the input filename.
2999c125d91Smrg    if test -d "$dst"; then
30076c9c3baSmrg      if test "$is_target_a_directory" = never; then
30176c9c3baSmrg        echo "$0: $dst_arg: Is a directory" >&2
30276c9c3baSmrg        exit 1
3039c125d91Smrg      fi
3049c125d91Smrg      dstdir=$dst
305ed89697eSmrg      dstbase=`basename "$src"`
306ed89697eSmrg      case $dst in
307ed89697eSmrg	*/) dst=$dst$dstbase;;
308ed89697eSmrg	*)  dst=$dst/$dstbase;;
309ed89697eSmrg      esac
3109c125d91Smrg      dstdir_status=0
3119c125d91Smrg    else
31276c9c3baSmrg      dstdir=`dirname "$dst"`
3139c125d91Smrg      test -d "$dstdir"
3149c125d91Smrg      dstdir_status=$?
3159c125d91Smrg    fi
3169c125d91Smrg  fi
3179c125d91Smrg
318ed89697eSmrg  case $dstdir in
319ed89697eSmrg    */) dstdirslash=$dstdir;;
320ed89697eSmrg    *)  dstdirslash=$dstdir/;;
321ed89697eSmrg  esac
322ed89697eSmrg
3239c125d91Smrg  obsolete_mkdir_used=false
3249c125d91Smrg
3259c125d91Smrg  if test $dstdir_status != 0; then
3269c125d91Smrg    case $posix_mkdir in
3279c125d91Smrg      '')
32876c9c3baSmrg        # With -d, create the new directory with the user-specified mode.
32976c9c3baSmrg        # Otherwise, rely on $mkdir_umask.
33076c9c3baSmrg        if test -n "$dir_arg"; then
33176c9c3baSmrg          mkdir_mode=-m$mode
33276c9c3baSmrg        else
33376c9c3baSmrg          mkdir_mode=
33476c9c3baSmrg        fi
33576c9c3baSmrg
33676c9c3baSmrg        posix_mkdir=false
337ed89697eSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338ed89697eSmrg	# here however when possible just to lower collision chance.
339ed89697eSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340ed89697eSmrg
341ed89697eSmrg	trap '
342ed89697eSmrg	  ret=$?
343ed89697eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344ed89697eSmrg	  exit $ret
345ed89697eSmrg	' 0
346ed89697eSmrg
347ed89697eSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348ed89697eSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349ed89697eSmrg	# directory is successfully created first before we actually test
350ed89697eSmrg	# 'mkdir -p'.
351ed89697eSmrg	if (umask $mkdir_umask &&
352ed89697eSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353ed89697eSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354ed89697eSmrg	then
355ed89697eSmrg	  if test -z "$dir_arg" || {
356ed89697eSmrg	       # Check for POSIX incompatibilities with -m.
357ed89697eSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358ed89697eSmrg	       # other-writable bit of parent directory when it shouldn't.
359ed89697eSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360ed89697eSmrg	       test_tmpdir="$tmpdir/a"
361ed89697eSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362ed89697eSmrg	       case $ls_ld_tmpdir in
363ed89697eSmrg		 d????-?r-*) different_mode=700;;
364ed89697eSmrg		 d????-?--*) different_mode=755;;
365ed89697eSmrg		 *) false;;
366ed89697eSmrg	       esac &&
367ed89697eSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368ed89697eSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369ed89697eSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370ed89697eSmrg	       }
371ed89697eSmrg	     }
372ed89697eSmrg	  then posix_mkdir=:
373ed89697eSmrg	  fi
374ed89697eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375ed89697eSmrg	else
376ed89697eSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377ed89697eSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378ed89697eSmrg	fi
379ed89697eSmrg	trap '' 0;;
3809c125d91Smrg    esac
38116910e25Smrg
3829c125d91Smrg    if
3839c125d91Smrg      $posix_mkdir && (
38476c9c3baSmrg        umask $mkdir_umask &&
38576c9c3baSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3869c125d91Smrg      )
3879c125d91Smrg    then :
3889c125d91Smrg    else
3899c125d91Smrg
390ed89697eSmrg      # mkdir does not conform to POSIX,
3919c125d91Smrg      # or it failed possibly due to a race condition.  Create the
3929c125d91Smrg      # directory the slow way, step by step, checking for races as we go.
3939c125d91Smrg
3949c125d91Smrg      case $dstdir in
39576c9c3baSmrg        /*) prefix='/';;
39676c9c3baSmrg        [-=\(\)!]*) prefix='./';;
39776c9c3baSmrg        *)  prefix='';;
3989c125d91Smrg      esac
3999c125d91Smrg
4009c125d91Smrg      oIFS=$IFS
4019c125d91Smrg      IFS=/
40276c9c3baSmrg      set -f
4039c125d91Smrg      set fnord $dstdir
4049c125d91Smrg      shift
40576c9c3baSmrg      set +f
4069c125d91Smrg      IFS=$oIFS
4079c125d91Smrg
4089c125d91Smrg      prefixes=
4099c125d91Smrg
4109c125d91Smrg      for d
4119c125d91Smrg      do
41276c9c3baSmrg        test X"$d" = X && continue
41376c9c3baSmrg
41476c9c3baSmrg        prefix=$prefix$d
41576c9c3baSmrg        if test -d "$prefix"; then
41676c9c3baSmrg          prefixes=
41776c9c3baSmrg        else
41876c9c3baSmrg          if $posix_mkdir; then
419ed89697eSmrg            (umask $mkdir_umask &&
42076c9c3baSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42176c9c3baSmrg            # Don't fail if two instances are running concurrently.
42276c9c3baSmrg            test -d "$prefix" || exit 1
42376c9c3baSmrg          else
42476c9c3baSmrg            case $prefix in
42576c9c3baSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42676c9c3baSmrg              *) qprefix=$prefix;;
42776c9c3baSmrg            esac
42876c9c3baSmrg            prefixes="$prefixes '$qprefix'"
42976c9c3baSmrg          fi
43076c9c3baSmrg        fi
43176c9c3baSmrg        prefix=$prefix/
4329c125d91Smrg      done
4339c125d91Smrg
4349c125d91Smrg      if test -n "$prefixes"; then
43576c9c3baSmrg        # Don't fail if two instances are running concurrently.
43676c9c3baSmrg        (umask $mkdir_umask &&
43776c9c3baSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43876c9c3baSmrg          test -d "$dstdir" || exit 1
43976c9c3baSmrg        obsolete_mkdir_used=true
4409c125d91Smrg      fi
4419c125d91Smrg    fi
4429c125d91Smrg  fi
4439c125d91Smrg
4449c125d91Smrg  if test -n "$dir_arg"; then
4459c125d91Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4469c125d91Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4479c125d91Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4489c125d91Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4499c125d91Smrg  else
4509c125d91Smrg
4519c125d91Smrg    # Make a couple of temp file names in the proper directory.
452ed89697eSmrg    dsttmp=${dstdirslash}_inst.$$_
453ed89697eSmrg    rmtmp=${dstdirslash}_rm.$$_
4549c125d91Smrg
4559c125d91Smrg    # Trap to clean up those temp files at exit.
4569c125d91Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4579c125d91Smrg
4589c125d91Smrg    # Copy the file name to the temp name.
459ed89697eSmrg    (umask $cp_umask &&
460ed89697eSmrg     { test -z "$stripcmd" || {
461ed89697eSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462ed89697eSmrg	 # which would cause strip to fail.
463ed89697eSmrg	 if test -z "$doit"; then
464ed89697eSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465ed89697eSmrg	 else
466ed89697eSmrg	   $doit touch "$dsttmp"
467ed89697eSmrg	 fi
468ed89697eSmrg       }
469ed89697eSmrg     } &&
470ed89697eSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4719c125d91Smrg
4729c125d91Smrg    # and set any options; do chmod last to preserve setuid bits.
4739c125d91Smrg    #
4749c125d91Smrg    # If any of these fail, we abort the whole thing.  If we want to
4759c125d91Smrg    # ignore errors from any of these, just make sure not to ignore
4769c125d91Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4779c125d91Smrg    #
4789c125d91Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4799c125d91Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4809c125d91Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4819c125d91Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4829c125d91Smrg
4839c125d91Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4849c125d91Smrg    if $copy_on_change &&
48576c9c3baSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48676c9c3baSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48776c9c3baSmrg       set -f &&
4889c125d91Smrg       set X $old && old=:$2:$4:$5:$6 &&
4899c125d91Smrg       set X $new && new=:$2:$4:$5:$6 &&
49076c9c3baSmrg       set +f &&
4919c125d91Smrg       test "$old" = "$new" &&
4929c125d91Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4939c125d91Smrg    then
4949c125d91Smrg      rm -f "$dsttmp"
4959c125d91Smrg    else
496ed89697eSmrg      # If $backupsuffix is set, and the file being installed
497ed89697eSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498ed89697eSmrg      # e.g., if mv doesn't support -f.
499ed89697eSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500ed89697eSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501ed89697eSmrg      fi
502ed89697eSmrg
5039c125d91Smrg      # Rename the file to the real destination.
5049c125d91Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5059c125d91Smrg
5069c125d91Smrg      # The rename failed, perhaps because mv can't rename something else
5079c125d91Smrg      # to itself, or perhaps because mv is so ancient that it does not
5089c125d91Smrg      # support -f.
5099c125d91Smrg      {
51076c9c3baSmrg        # Now remove or move aside any old file at destination location.
51176c9c3baSmrg        # We try this two ways since rm can't unlink itself on some
51276c9c3baSmrg        # systems and the destination file might be busy for other
51376c9c3baSmrg        # reasons.  In this case, the final cleanup might fail but the new
51476c9c3baSmrg        # file should still install successfully.
51576c9c3baSmrg        {
51676c9c3baSmrg          test ! -f "$dst" ||
517ed89697eSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51876c9c3baSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519ed89697eSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52076c9c3baSmrg          } ||
52176c9c3baSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52276c9c3baSmrg            (exit 1); exit 1
52376c9c3baSmrg          }
52476c9c3baSmrg        } &&
52576c9c3baSmrg
52676c9c3baSmrg        # Now rename the file to the real destination.
52776c9c3baSmrg        $doit $mvcmd "$dsttmp" "$dst"
5289c125d91Smrg      }
5299c125d91Smrg    fi || exit 1
5309c125d91Smrg
5319c125d91Smrg    trap '' 0
5329c125d91Smrg  fi
5339c125d91Smrgdone
53416910e25Smrg
5359c125d91Smrg# Local variables:
536ed89697eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5379c125d91Smrg# time-stamp-start: "scriptversion="
5389c125d91Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539ed89697eSmrg# time-stamp-time-zone: "UTC0"
5409c125d91Smrg# time-stamp-end: "; # UTC"
5419c125d91Smrg# End:
542