install-sh revision ec318dbf
1bccedf53Smrg#!/bin/sh
2bccedf53Smrg# install - install a program, script, or datafile
37c5f6000Smrg
4ec318dbfSmrgscriptversion=2020-11-14.01; # 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
72ec318dbfSmrg# Create dirs (including intermediate dirs) using mode 755.
73ec318dbfSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74ec318dbfSmrgmkdir_umask=22
75ec318dbfSmrg
76ec318dbfSmrgbackupsuffix=
777c5f6000Smrgchgrpcmd=
787c5f6000Smrgchmodcmd=$chmodprog
797c5f6000Smrgchowncmd=
807c5f6000Smrgmvcmd=$mvprog
817c5f6000Smrgrmcmd="$rmprog -f"
827c5f6000Smrgstripcmd=
83bccedf53Smrg
847c5f6000Smrgsrc=
857c5f6000Smrgdst=
867c5f6000Smrgdir_arg=
877c5f6000Smrgdst_arg=
88bccedf53Smrg
897c5f6000Smrgcopy_on_change=false
9089afc689Smrgis_target_a_directory=possibly
91bccedf53Smrg
927c5f6000Smrgusage="\
937c5f6000SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
947c5f6000Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
957c5f6000Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
967c5f6000Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97bccedf53Smrg
987c5f6000SmrgIn the 1st form, copy SRCFILE to DSTFILE.
997c5f6000SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1007c5f6000SmrgIn the 4th, create DIRECTORIES.
101bccedf53Smrg
1027c5f6000SmrgOptions:
1037c5f6000Smrg     --help     display this help and exit.
1047c5f6000Smrg     --version  display version info and exit.
105bccedf53Smrg
1067c5f6000Smrg  -c            (ignored)
107ec318dbfSmrg  -C            install only if different (preserve data modification time)
1087c5f6000Smrg  -d            create directories instead of installing files.
1097c5f6000Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1107c5f6000Smrg  -m MODE       $chmodprog installed files to MODE.
1117c5f6000Smrg  -o USER       $chownprog installed files to USER.
112ec318dbfSmrg  -p            pass -p to $cpprog.
1137c5f6000Smrg  -s            $stripprog installed files.
114ec318dbfSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1157c5f6000Smrg  -t DIRECTORY  install into DIRECTORY.
1167c5f6000Smrg  -T            report an error if DSTFILE is a directory.
117bccedf53Smrg
1187c5f6000SmrgEnvironment variables override the default commands:
1197c5f6000Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1207c5f6000Smrg  RMPROG STRIPPROG
121ec318dbfSmrg
122ec318dbfSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123ec318dbfSmrgit's up to you to specify -f if you want it.
124ec318dbfSmrg
125ec318dbfSmrgIf -S is not specified, no backups are attempted.
126ec318dbfSmrg
127ec318dbfSmrgEmail bug reports to bug-automake@gnu.org.
128ec318dbfSmrgAutomake home page: https://www.gnu.org/software/automake/
1297c5f6000Smrg"
130bccedf53Smrg
1317c5f6000Smrgwhile test $# -ne 0; do
1327c5f6000Smrg  case $1 in
1337c5f6000Smrg    -c) ;;
134bccedf53Smrg
1357c5f6000Smrg    -C) copy_on_change=true;;
136bccedf53Smrg
1377c5f6000Smrg    -d) dir_arg=true;;
138bccedf53Smrg
1397c5f6000Smrg    -g) chgrpcmd="$chgrpprog $2"
14089afc689Smrg        shift;;
141bccedf53Smrg
1427c5f6000Smrg    --help) echo "$usage"; exit $?;;
143bccedf53Smrg
1447c5f6000Smrg    -m) mode=$2
14589afc689Smrg        case $mode in
14689afc689Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14789afc689Smrg            echo "$0: invalid mode: $mode" >&2
14889afc689Smrg            exit 1;;
14989afc689Smrg        esac
15089afc689Smrg        shift;;
151bccedf53Smrg
1527c5f6000Smrg    -o) chowncmd="$chownprog $2"
15389afc689Smrg        shift;;
154bccedf53Smrg
155ec318dbfSmrg    -p) cpprog="$cpprog -p";;
156ec318dbfSmrg
1577c5f6000Smrg    -s) stripcmd=$stripprog;;
158bccedf53Smrg
159ec318dbfSmrg    -S) backupsuffix="$2"
160ec318dbfSmrg        shift;;
161ec318dbfSmrg
16289afc689Smrg    -t)
16389afc689Smrg        is_target_a_directory=always
16489afc689Smrg        dst_arg=$2
16589afc689Smrg        # Protect names problematic for 'test' and other utilities.
16689afc689Smrg        case $dst_arg in
16789afc689Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16889afc689Smrg        esac
16989afc689Smrg        shift;;
170bccedf53Smrg
17189afc689Smrg    -T) is_target_a_directory=never;;
172bccedf53Smrg
1737c5f6000Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174bccedf53Smrg
17589afc689Smrg    --) shift
17689afc689Smrg        break;;
177bccedf53Smrg
17889afc689Smrg    -*) echo "$0: invalid option: $1" >&2
17989afc689Smrg        exit 1;;
180bccedf53Smrg
1817c5f6000Smrg    *)  break;;
1827c5f6000Smrg  esac
1837c5f6000Smrg  shift
1847c5f6000Smrgdone
185bccedf53Smrg
18689afc689Smrg# We allow the use of options -d and -T together, by making -d
18789afc689Smrg# take the precedence; this is for compatibility with GNU install.
18889afc689Smrg
18989afc689Smrgif test -n "$dir_arg"; then
19089afc689Smrg  if test -n "$dst_arg"; then
19189afc689Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19289afc689Smrg    exit 1
19389afc689Smrg  fi
19489afc689Smrgfi
19589afc689Smrg
1967c5f6000Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1977c5f6000Smrg  # When -d is used, all remaining arguments are directories to create.
1987c5f6000Smrg  # When -t is used, the destination is already specified.
1997c5f6000Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2007c5f6000Smrg  for arg
2017c5f6000Smrg  do
2027c5f6000Smrg    if test -n "$dst_arg"; then
2037c5f6000Smrg      # $@ is not empty: it contains at least $arg.
2047c5f6000Smrg      set fnord "$@" "$dst_arg"
2057c5f6000Smrg      shift # fnord
2067c5f6000Smrg    fi
2077c5f6000Smrg    shift # arg
2087c5f6000Smrg    dst_arg=$arg
20989afc689Smrg    # Protect names problematic for 'test' and other utilities.
2101b1389eeSmrg    case $dst_arg in
2111b1389eeSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2121b1389eeSmrg    esac
2137c5f6000Smrg  done
2147c5f6000Smrgfi
215bccedf53Smrg
2167c5f6000Smrgif test $# -eq 0; then
2177c5f6000Smrg  if test -z "$dir_arg"; then
2187c5f6000Smrg    echo "$0: no input file specified." >&2
2197c5f6000Smrg    exit 1
2207c5f6000Smrg  fi
22189afc689Smrg  # It's OK to call 'install-sh -d' without argument.
2227c5f6000Smrg  # This can happen when creating conditional directories.
2237c5f6000Smrg  exit 0
2247c5f6000Smrgfi
225bccedf53Smrg
22689afc689Smrgif test -z "$dir_arg"; then
22789afc689Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22889afc689Smrg    if test ! -d "$dst_arg"; then
22989afc689Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23089afc689Smrg      exit 1
23189afc689Smrg    fi
23289afc689Smrg  fi
23389afc689Smrgfi
23489afc689Smrg
2357c5f6000Smrgif test -z "$dir_arg"; then
2361b1389eeSmrg  do_exit='(exit $ret); exit $ret'
2371b1389eeSmrg  trap "ret=129; $do_exit" 1
2381b1389eeSmrg  trap "ret=130; $do_exit" 2
2391b1389eeSmrg  trap "ret=141; $do_exit" 13
2401b1389eeSmrg  trap "ret=143; $do_exit" 15
2417c5f6000Smrg
2427c5f6000Smrg  # Set umask so as not to create temps with too-generous modes.
2437c5f6000Smrg  # However, 'strip' requires both read and write access to temps.
2447c5f6000Smrg  case $mode in
2457c5f6000Smrg    # Optimize common cases.
2467c5f6000Smrg    *644) cp_umask=133;;
2477c5f6000Smrg    *755) cp_umask=22;;
2487c5f6000Smrg
2497c5f6000Smrg    *[0-7])
2507c5f6000Smrg      if test -z "$stripcmd"; then
25189afc689Smrg        u_plus_rw=
2527c5f6000Smrg      else
25389afc689Smrg        u_plus_rw='% 200'
2547c5f6000Smrg      fi
2557c5f6000Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2567c5f6000Smrg    *)
2577c5f6000Smrg      if test -z "$stripcmd"; then
25889afc689Smrg        u_plus_rw=
2597c5f6000Smrg      else
26089afc689Smrg        u_plus_rw=,u+rw
2617c5f6000Smrg      fi
2627c5f6000Smrg      cp_umask=$mode$u_plus_rw;;
2637c5f6000Smrg  esac
2647c5f6000Smrgfi
265bccedf53Smrg
2667c5f6000Smrgfor src
2677c5f6000Smrgdo
26889afc689Smrg  # Protect names problematic for 'test' and other utilities.
2697c5f6000Smrg  case $src in
2701b1389eeSmrg    -* | [=\(\)!]) src=./$src;;
2717c5f6000Smrg  esac
2727c5f6000Smrg
2737c5f6000Smrg  if test -n "$dir_arg"; then
2747c5f6000Smrg    dst=$src
2757c5f6000Smrg    dstdir=$dst
2767c5f6000Smrg    test -d "$dstdir"
2777c5f6000Smrg    dstdir_status=$?
278ec318dbfSmrg    # Don't chown directories that already exist.
279ec318dbfSmrg    if test $dstdir_status = 0; then
280ec318dbfSmrg      chowncmd=""
281ec318dbfSmrg    fi
2827c5f6000Smrg  else
2837c5f6000Smrg
2847c5f6000Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2857c5f6000Smrg    # might cause directories to be created, which would be especially bad
2867c5f6000Smrg    # if $src (and thus $dsttmp) contains '*'.
2877c5f6000Smrg    if test ! -f "$src" && test ! -d "$src"; then
2887c5f6000Smrg      echo "$0: $src does not exist." >&2
2897c5f6000Smrg      exit 1
2907c5f6000Smrg    fi
2917c5f6000Smrg
2927c5f6000Smrg    if test -z "$dst_arg"; then
2937c5f6000Smrg      echo "$0: no destination specified." >&2
2947c5f6000Smrg      exit 1
2957c5f6000Smrg    fi
2967c5f6000Smrg    dst=$dst_arg
297bccedf53Smrg
298ec318dbfSmrg    # If destination is a directory, append the input filename.
2997c5f6000Smrg    if test -d "$dst"; then
30089afc689Smrg      if test "$is_target_a_directory" = never; then
30189afc689Smrg        echo "$0: $dst_arg: Is a directory" >&2
30289afc689Smrg        exit 1
3037c5f6000Smrg      fi
3047c5f6000Smrg      dstdir=$dst
305ec318dbfSmrg      dstbase=`basename "$src"`
306ec318dbfSmrg      case $dst in
307ec318dbfSmrg	*/) dst=$dst$dstbase;;
308ec318dbfSmrg	*)  dst=$dst/$dstbase;;
309ec318dbfSmrg      esac
3107c5f6000Smrg      dstdir_status=0
3117c5f6000Smrg    else
31289afc689Smrg      dstdir=`dirname "$dst"`
3137c5f6000Smrg      test -d "$dstdir"
3147c5f6000Smrg      dstdir_status=$?
3157c5f6000Smrg    fi
3167c5f6000Smrg  fi
3177c5f6000Smrg
318ec318dbfSmrg  case $dstdir in
319ec318dbfSmrg    */) dstdirslash=$dstdir;;
320ec318dbfSmrg    *)  dstdirslash=$dstdir/;;
321ec318dbfSmrg  esac
322ec318dbfSmrg
3237c5f6000Smrg  obsolete_mkdir_used=false
3247c5f6000Smrg
3257c5f6000Smrg  if test $dstdir_status != 0; then
3267c5f6000Smrg    case $posix_mkdir in
3277c5f6000Smrg      '')
32889afc689Smrg        # With -d, create the new directory with the user-specified mode.
32989afc689Smrg        # Otherwise, rely on $mkdir_umask.
33089afc689Smrg        if test -n "$dir_arg"; then
33189afc689Smrg          mkdir_mode=-m$mode
33289afc689Smrg        else
33389afc689Smrg          mkdir_mode=
33489afc689Smrg        fi
33589afc689Smrg
33689afc689Smrg        posix_mkdir=false
337ec318dbfSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338ec318dbfSmrg	# here however when possible just to lower collision chance.
339ec318dbfSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340ec318dbfSmrg
341ec318dbfSmrg	trap '
342ec318dbfSmrg	  ret=$?
343ec318dbfSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344ec318dbfSmrg	  exit $ret
345ec318dbfSmrg	' 0
346ec318dbfSmrg
347ec318dbfSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348ec318dbfSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349ec318dbfSmrg	# directory is successfully created first before we actually test
350ec318dbfSmrg	# 'mkdir -p'.
351ec318dbfSmrg	if (umask $mkdir_umask &&
352ec318dbfSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353ec318dbfSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354ec318dbfSmrg	then
355ec318dbfSmrg	  if test -z "$dir_arg" || {
356ec318dbfSmrg	       # Check for POSIX incompatibilities with -m.
357ec318dbfSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358ec318dbfSmrg	       # other-writable bit of parent directory when it shouldn't.
359ec318dbfSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360ec318dbfSmrg	       test_tmpdir="$tmpdir/a"
361ec318dbfSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362ec318dbfSmrg	       case $ls_ld_tmpdir in
363ec318dbfSmrg		 d????-?r-*) different_mode=700;;
364ec318dbfSmrg		 d????-?--*) different_mode=755;;
365ec318dbfSmrg		 *) false;;
366ec318dbfSmrg	       esac &&
367ec318dbfSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368ec318dbfSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369ec318dbfSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370ec318dbfSmrg	       }
371ec318dbfSmrg	     }
372ec318dbfSmrg	  then posix_mkdir=:
373ec318dbfSmrg	  fi
374ec318dbfSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375ec318dbfSmrg	else
376ec318dbfSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377ec318dbfSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378ec318dbfSmrg	fi
379ec318dbfSmrg	trap '' 0;;
3807c5f6000Smrg    esac
381bccedf53Smrg
3827c5f6000Smrg    if
3837c5f6000Smrg      $posix_mkdir && (
38489afc689Smrg        umask $mkdir_umask &&
38589afc689Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3867c5f6000Smrg      )
3877c5f6000Smrg    then :
3887c5f6000Smrg    else
3897c5f6000Smrg
390ec318dbfSmrg      # mkdir does not conform to POSIX,
3917c5f6000Smrg      # or it failed possibly due to a race condition.  Create the
3927c5f6000Smrg      # directory the slow way, step by step, checking for races as we go.
3937c5f6000Smrg
3947c5f6000Smrg      case $dstdir in
39589afc689Smrg        /*) prefix='/';;
39689afc689Smrg        [-=\(\)!]*) prefix='./';;
39789afc689Smrg        *)  prefix='';;
3987c5f6000Smrg      esac
3997c5f6000Smrg
4007c5f6000Smrg      oIFS=$IFS
4017c5f6000Smrg      IFS=/
40289afc689Smrg      set -f
4037c5f6000Smrg      set fnord $dstdir
4047c5f6000Smrg      shift
40589afc689Smrg      set +f
4067c5f6000Smrg      IFS=$oIFS
4077c5f6000Smrg
4087c5f6000Smrg      prefixes=
4097c5f6000Smrg
4107c5f6000Smrg      for d
4117c5f6000Smrg      do
41289afc689Smrg        test X"$d" = X && continue
41389afc689Smrg
41489afc689Smrg        prefix=$prefix$d
41589afc689Smrg        if test -d "$prefix"; then
41689afc689Smrg          prefixes=
41789afc689Smrg        else
41889afc689Smrg          if $posix_mkdir; then
419ec318dbfSmrg            (umask $mkdir_umask &&
42089afc689Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42189afc689Smrg            # Don't fail if two instances are running concurrently.
42289afc689Smrg            test -d "$prefix" || exit 1
42389afc689Smrg          else
42489afc689Smrg            case $prefix in
42589afc689Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42689afc689Smrg              *) qprefix=$prefix;;
42789afc689Smrg            esac
42889afc689Smrg            prefixes="$prefixes '$qprefix'"
42989afc689Smrg          fi
43089afc689Smrg        fi
43189afc689Smrg        prefix=$prefix/
4327c5f6000Smrg      done
4337c5f6000Smrg
4347c5f6000Smrg      if test -n "$prefixes"; then
43589afc689Smrg        # Don't fail if two instances are running concurrently.
43689afc689Smrg        (umask $mkdir_umask &&
43789afc689Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43889afc689Smrg          test -d "$dstdir" || exit 1
43989afc689Smrg        obsolete_mkdir_used=true
4407c5f6000Smrg      fi
4417c5f6000Smrg    fi
4427c5f6000Smrg  fi
4437c5f6000Smrg
4447c5f6000Smrg  if test -n "$dir_arg"; then
4457c5f6000Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4467c5f6000Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4477c5f6000Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4487c5f6000Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4497c5f6000Smrg  else
4507c5f6000Smrg
4517c5f6000Smrg    # Make a couple of temp file names in the proper directory.
452ec318dbfSmrg    dsttmp=${dstdirslash}_inst.$$_
453ec318dbfSmrg    rmtmp=${dstdirslash}_rm.$$_
4547c5f6000Smrg
4557c5f6000Smrg    # Trap to clean up those temp files at exit.
4567c5f6000Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4577c5f6000Smrg
4587c5f6000Smrg    # Copy the file name to the temp name.
459ec318dbfSmrg    (umask $cp_umask &&
460ec318dbfSmrg     { test -z "$stripcmd" || {
461ec318dbfSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462ec318dbfSmrg	 # which would cause strip to fail.
463ec318dbfSmrg	 if test -z "$doit"; then
464ec318dbfSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465ec318dbfSmrg	 else
466ec318dbfSmrg	   $doit touch "$dsttmp"
467ec318dbfSmrg	 fi
468ec318dbfSmrg       }
469ec318dbfSmrg     } &&
470ec318dbfSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4717c5f6000Smrg
4727c5f6000Smrg    # and set any options; do chmod last to preserve setuid bits.
4737c5f6000Smrg    #
4747c5f6000Smrg    # If any of these fail, we abort the whole thing.  If we want to
4757c5f6000Smrg    # ignore errors from any of these, just make sure not to ignore
4767c5f6000Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4777c5f6000Smrg    #
4787c5f6000Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4797c5f6000Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4807c5f6000Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4817c5f6000Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4827c5f6000Smrg
4837c5f6000Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4847c5f6000Smrg    if $copy_on_change &&
48589afc689Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48689afc689Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48789afc689Smrg       set -f &&
4887c5f6000Smrg       set X $old && old=:$2:$4:$5:$6 &&
4897c5f6000Smrg       set X $new && new=:$2:$4:$5:$6 &&
49089afc689Smrg       set +f &&
4917c5f6000Smrg       test "$old" = "$new" &&
4927c5f6000Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4937c5f6000Smrg    then
4947c5f6000Smrg      rm -f "$dsttmp"
4957c5f6000Smrg    else
496ec318dbfSmrg      # If $backupsuffix is set, and the file being installed
497ec318dbfSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498ec318dbfSmrg      # e.g., if mv doesn't support -f.
499ec318dbfSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500ec318dbfSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501ec318dbfSmrg      fi
502ec318dbfSmrg
5037c5f6000Smrg      # Rename the file to the real destination.
5047c5f6000Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5057c5f6000Smrg
5067c5f6000Smrg      # The rename failed, perhaps because mv can't rename something else
5077c5f6000Smrg      # to itself, or perhaps because mv is so ancient that it does not
5087c5f6000Smrg      # support -f.
5097c5f6000Smrg      {
51089afc689Smrg        # Now remove or move aside any old file at destination location.
51189afc689Smrg        # We try this two ways since rm can't unlink itself on some
51289afc689Smrg        # systems and the destination file might be busy for other
51389afc689Smrg        # reasons.  In this case, the final cleanup might fail but the new
51489afc689Smrg        # file should still install successfully.
51589afc689Smrg        {
51689afc689Smrg          test ! -f "$dst" ||
517ec318dbfSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51889afc689Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519ec318dbfSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52089afc689Smrg          } ||
52189afc689Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52289afc689Smrg            (exit 1); exit 1
52389afc689Smrg          }
52489afc689Smrg        } &&
52589afc689Smrg
52689afc689Smrg        # Now rename the file to the real destination.
52789afc689Smrg        $doit $mvcmd "$dsttmp" "$dst"
5287c5f6000Smrg      }
5297c5f6000Smrg    fi || exit 1
5307c5f6000Smrg
5317c5f6000Smrg    trap '' 0
5327c5f6000Smrg  fi
5337c5f6000Smrgdone
534bccedf53Smrg
5357c5f6000Smrg# Local variables:
536ec318dbfSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5377c5f6000Smrg# time-stamp-start: "scriptversion="
5387c5f6000Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539ec318dbfSmrg# time-stamp-time-zone: "UTC0"
5407c5f6000Smrg# time-stamp-end: "; # UTC"
5417c5f6000Smrg# End:
542