1b3307321Smrg#!/bin/sh
2b3307321Smrg# install - install a program, script, or datafile
3afe13c8eSmrg
474b97a6cSmrgscriptversion=2020-11-14.01; # UTC
5afe13c8eSmrg
6b3307321Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7b3307321Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8b3307321Smrg# following copyright and license.
9b3307321Smrg#
10b3307321Smrg# Copyright (C) 1994 X Consortium
11b3307321Smrg#
12b3307321Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13b3307321Smrg# of this software and associated documentation files (the "Software"), to
14b3307321Smrg# deal in the Software without restriction, including without limitation the
15b3307321Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16b3307321Smrg# sell copies of the Software, and to permit persons to whom the Software is
17b3307321Smrg# furnished to do so, subject to the following conditions:
18b3307321Smrg#
19b3307321Smrg# The above copyright notice and this permission notice shall be included in
20b3307321Smrg# all copies or substantial portions of the Software.
21b3307321Smrg#
22b3307321Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23b3307321Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24b3307321Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25b3307321Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26b3307321Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27b3307321Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28b3307321Smrg#
29b3307321Smrg# Except as contained in this notice, the name of the X Consortium shall not
30b3307321Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31b3307321Smrg# ings in this Software without prior written authorization from the X Consor-
32b3307321Smrg# tium.
33b3307321Smrg#
34b3307321Smrg#
35b3307321Smrg# FSF changes to this file are in the public domain.
36b3307321Smrg#
37b3307321Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3874a3f230Smrg# 'make' implicit rules from creating a file called install from it
39b3307321Smrg# when there is no Makefile.
40b3307321Smrg#
41b3307321Smrg# This script is compatible with the BSD install script, but was written
42afe13c8eSmrg# from scratch.
43b3307321Smrg
4406ef0fecSmrgtab='	'
45afe13c8eSmrgnl='
46afe13c8eSmrg'
4706ef0fecSmrgIFS=" $tab$nl"
48b3307321Smrg
4906ef0fecSmrg# Set DOITPROG to "echo" to test this script.
50b3307321Smrg
51afe13c8eSmrgdoit=${DOITPROG-}
5206ef0fecSmrgdoit_exec=${doit:-exec}
53b3307321Smrg
54afe13c8eSmrg# Put in absolute file names if you don't have them in your path;
55afe13c8eSmrg# or use environment vars.
56afe13c8eSmrg
57afe13c8eSmrgchgrpprog=${CHGRPPROG-chgrp}
58afe13c8eSmrgchmodprog=${CHMODPROG-chmod}
59afe13c8eSmrgchownprog=${CHOWNPROG-chown}
60afe13c8eSmrgcmpprog=${CMPPROG-cmp}
61afe13c8eSmrgcpprog=${CPPROG-cp}
62afe13c8eSmrgmkdirprog=${MKDIRPROG-mkdir}
63afe13c8eSmrgmvprog=${MVPROG-mv}
64afe13c8eSmrgrmprog=${RMPROG-rm}
65afe13c8eSmrgstripprog=${STRIPPROG-strip}
66afe13c8eSmrg
67afe13c8eSmrgposix_mkdir=
68afe13c8eSmrg
69afe13c8eSmrg# Desired mode of installed file.
70afe13c8eSmrgmode=0755
71afe13c8eSmrg
7274b97a6cSmrg# Create dirs (including intermediate dirs) using mode 755.
7374b97a6cSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7474b97a6cSmrgmkdir_umask=22
7574b97a6cSmrg
7674b97a6cSmrgbackupsuffix=
77afe13c8eSmrgchgrpcmd=
78afe13c8eSmrgchmodcmd=$chmodprog
79afe13c8eSmrgchowncmd=
80afe13c8eSmrgmvcmd=$mvprog
81afe13c8eSmrgrmcmd="$rmprog -f"
82afe13c8eSmrgstripcmd=
83b3307321Smrg
84afe13c8eSmrgsrc=
85afe13c8eSmrgdst=
86afe13c8eSmrgdir_arg=
87afe13c8eSmrgdst_arg=
88b3307321Smrg
89afe13c8eSmrgcopy_on_change=false
9006ef0fecSmrgis_target_a_directory=possibly
91b3307321Smrg
92afe13c8eSmrgusage="\
93afe13c8eSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94afe13c8eSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95afe13c8eSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96afe13c8eSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97b3307321Smrg
98afe13c8eSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99afe13c8eSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100afe13c8eSmrgIn the 4th, create DIRECTORIES.
101b3307321Smrg
102afe13c8eSmrgOptions:
103afe13c8eSmrg     --help     display this help and exit.
104afe13c8eSmrg     --version  display version info and exit.
105b3307321Smrg
106afe13c8eSmrg  -c            (ignored)
10774b97a6cSmrg  -C            install only if different (preserve data modification time)
108afe13c8eSmrg  -d            create directories instead of installing files.
109afe13c8eSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110afe13c8eSmrg  -m MODE       $chmodprog installed files to MODE.
111afe13c8eSmrg  -o USER       $chownprog installed files to USER.
11274b97a6cSmrg  -p            pass -p to $cpprog.
113afe13c8eSmrg  -s            $stripprog installed files.
11474b97a6cSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115afe13c8eSmrg  -t DIRECTORY  install into DIRECTORY.
116afe13c8eSmrg  -T            report an error if DSTFILE is a directory.
117b3307321Smrg
118afe13c8eSmrgEnvironment variables override the default commands:
119afe13c8eSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120afe13c8eSmrg  RMPROG STRIPPROG
12174b97a6cSmrg
12274b97a6cSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12374b97a6cSmrgit's up to you to specify -f if you want it.
12474b97a6cSmrg
12574b97a6cSmrgIf -S is not specified, no backups are attempted.
12674b97a6cSmrg
12774b97a6cSmrgEmail bug reports to bug-automake@gnu.org.
12874b97a6cSmrgAutomake home page: https://www.gnu.org/software/automake/
129afe13c8eSmrg"
130b3307321Smrg
131afe13c8eSmrgwhile test $# -ne 0; do
132afe13c8eSmrg  case $1 in
133afe13c8eSmrg    -c) ;;
134b3307321Smrg
135afe13c8eSmrg    -C) copy_on_change=true;;
136b3307321Smrg
137afe13c8eSmrg    -d) dir_arg=true;;
138b3307321Smrg
139afe13c8eSmrg    -g) chgrpcmd="$chgrpprog $2"
14006ef0fecSmrg        shift;;
141b3307321Smrg
142afe13c8eSmrg    --help) echo "$usage"; exit $?;;
143b3307321Smrg
144afe13c8eSmrg    -m) mode=$2
14506ef0fecSmrg        case $mode in
14606ef0fecSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14706ef0fecSmrg            echo "$0: invalid mode: $mode" >&2
14806ef0fecSmrg            exit 1;;
14906ef0fecSmrg        esac
15006ef0fecSmrg        shift;;
151b3307321Smrg
152afe13c8eSmrg    -o) chowncmd="$chownprog $2"
15306ef0fecSmrg        shift;;
154b3307321Smrg
15574b97a6cSmrg    -p) cpprog="$cpprog -p";;
15674b97a6cSmrg
157afe13c8eSmrg    -s) stripcmd=$stripprog;;
158b3307321Smrg
15974b97a6cSmrg    -S) backupsuffix="$2"
16074b97a6cSmrg        shift;;
16174b97a6cSmrg
16206ef0fecSmrg    -t)
16306ef0fecSmrg        is_target_a_directory=always
16406ef0fecSmrg        dst_arg=$2
16506ef0fecSmrg        # Protect names problematic for 'test' and other utilities.
16606ef0fecSmrg        case $dst_arg in
16706ef0fecSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16806ef0fecSmrg        esac
16906ef0fecSmrg        shift;;
170b3307321Smrg
17106ef0fecSmrg    -T) is_target_a_directory=never;;
172b3307321Smrg
173afe13c8eSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174b3307321Smrg
17506ef0fecSmrg    --) shift
17606ef0fecSmrg        break;;
177b3307321Smrg
17806ef0fecSmrg    -*) echo "$0: invalid option: $1" >&2
17906ef0fecSmrg        exit 1;;
180b3307321Smrg
181afe13c8eSmrg    *)  break;;
182afe13c8eSmrg  esac
183afe13c8eSmrg  shift
184afe13c8eSmrgdone
185b3307321Smrg
18606ef0fecSmrg# We allow the use of options -d and -T together, by making -d
18706ef0fecSmrg# take the precedence; this is for compatibility with GNU install.
18806ef0fecSmrg
18906ef0fecSmrgif test -n "$dir_arg"; then
19006ef0fecSmrg  if test -n "$dst_arg"; then
19106ef0fecSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19206ef0fecSmrg    exit 1
19306ef0fecSmrg  fi
19406ef0fecSmrgfi
19506ef0fecSmrg
196afe13c8eSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197afe13c8eSmrg  # When -d is used, all remaining arguments are directories to create.
198afe13c8eSmrg  # When -t is used, the destination is already specified.
199afe13c8eSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200afe13c8eSmrg  for arg
201afe13c8eSmrg  do
202afe13c8eSmrg    if test -n "$dst_arg"; then
203afe13c8eSmrg      # $@ is not empty: it contains at least $arg.
204afe13c8eSmrg      set fnord "$@" "$dst_arg"
205afe13c8eSmrg      shift # fnord
206afe13c8eSmrg    fi
207afe13c8eSmrg    shift # arg
208afe13c8eSmrg    dst_arg=$arg
20974a3f230Smrg    # Protect names problematic for 'test' and other utilities.
21074a3f230Smrg    case $dst_arg in
21174a3f230Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21274a3f230Smrg    esac
213afe13c8eSmrg  done
214afe13c8eSmrgfi
215b3307321Smrg
216afe13c8eSmrgif test $# -eq 0; then
217afe13c8eSmrg  if test -z "$dir_arg"; then
218afe13c8eSmrg    echo "$0: no input file specified." >&2
219afe13c8eSmrg    exit 1
220afe13c8eSmrg  fi
22174a3f230Smrg  # It's OK to call 'install-sh -d' without argument.
222afe13c8eSmrg  # This can happen when creating conditional directories.
223afe13c8eSmrg  exit 0
224afe13c8eSmrgfi
225b3307321Smrg
22606ef0fecSmrgif test -z "$dir_arg"; then
22706ef0fecSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22806ef0fecSmrg    if test ! -d "$dst_arg"; then
22906ef0fecSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23006ef0fecSmrg      exit 1
23106ef0fecSmrg    fi
23206ef0fecSmrg  fi
23306ef0fecSmrgfi
23406ef0fecSmrg
235afe13c8eSmrgif test -z "$dir_arg"; then
23674a3f230Smrg  do_exit='(exit $ret); exit $ret'
23774a3f230Smrg  trap "ret=129; $do_exit" 1
23874a3f230Smrg  trap "ret=130; $do_exit" 2
23974a3f230Smrg  trap "ret=141; $do_exit" 13
24074a3f230Smrg  trap "ret=143; $do_exit" 15
241afe13c8eSmrg
242afe13c8eSmrg  # Set umask so as not to create temps with too-generous modes.
243afe13c8eSmrg  # However, 'strip' requires both read and write access to temps.
244afe13c8eSmrg  case $mode in
245afe13c8eSmrg    # Optimize common cases.
246afe13c8eSmrg    *644) cp_umask=133;;
247afe13c8eSmrg    *755) cp_umask=22;;
248afe13c8eSmrg
249afe13c8eSmrg    *[0-7])
250afe13c8eSmrg      if test -z "$stripcmd"; then
25106ef0fecSmrg        u_plus_rw=
252afe13c8eSmrg      else
25306ef0fecSmrg        u_plus_rw='% 200'
254afe13c8eSmrg      fi
255afe13c8eSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256afe13c8eSmrg    *)
257afe13c8eSmrg      if test -z "$stripcmd"; then
25806ef0fecSmrg        u_plus_rw=
259afe13c8eSmrg      else
26006ef0fecSmrg        u_plus_rw=,u+rw
261afe13c8eSmrg      fi
262afe13c8eSmrg      cp_umask=$mode$u_plus_rw;;
263afe13c8eSmrg  esac
264afe13c8eSmrgfi
265b3307321Smrg
266afe13c8eSmrgfor src
267afe13c8eSmrgdo
26874a3f230Smrg  # Protect names problematic for 'test' and other utilities.
269afe13c8eSmrg  case $src in
27074a3f230Smrg    -* | [=\(\)!]) src=./$src;;
271afe13c8eSmrg  esac
272afe13c8eSmrg
273afe13c8eSmrg  if test -n "$dir_arg"; then
274afe13c8eSmrg    dst=$src
275afe13c8eSmrg    dstdir=$dst
276afe13c8eSmrg    test -d "$dstdir"
277afe13c8eSmrg    dstdir_status=$?
27874b97a6cSmrg    # Don't chown directories that already exist.
27974b97a6cSmrg    if test $dstdir_status = 0; then
28074b97a6cSmrg      chowncmd=""
28174b97a6cSmrg    fi
282afe13c8eSmrg  else
283afe13c8eSmrg
284afe13c8eSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285afe13c8eSmrg    # might cause directories to be created, which would be especially bad
286afe13c8eSmrg    # if $src (and thus $dsttmp) contains '*'.
287afe13c8eSmrg    if test ! -f "$src" && test ! -d "$src"; then
288afe13c8eSmrg      echo "$0: $src does not exist." >&2
289afe13c8eSmrg      exit 1
290afe13c8eSmrg    fi
291afe13c8eSmrg
292afe13c8eSmrg    if test -z "$dst_arg"; then
293afe13c8eSmrg      echo "$0: no destination specified." >&2
294afe13c8eSmrg      exit 1
295afe13c8eSmrg    fi
296afe13c8eSmrg    dst=$dst_arg
297b3307321Smrg
29874b97a6cSmrg    # If destination is a directory, append the input filename.
299afe13c8eSmrg    if test -d "$dst"; then
30006ef0fecSmrg      if test "$is_target_a_directory" = never; then
30106ef0fecSmrg        echo "$0: $dst_arg: Is a directory" >&2
30206ef0fecSmrg        exit 1
303afe13c8eSmrg      fi
304afe13c8eSmrg      dstdir=$dst
30574b97a6cSmrg      dstbase=`basename "$src"`
30674b97a6cSmrg      case $dst in
30774b97a6cSmrg	*/) dst=$dst$dstbase;;
30874b97a6cSmrg	*)  dst=$dst/$dstbase;;
30974b97a6cSmrg      esac
310afe13c8eSmrg      dstdir_status=0
311afe13c8eSmrg    else
31206ef0fecSmrg      dstdir=`dirname "$dst"`
313afe13c8eSmrg      test -d "$dstdir"
314afe13c8eSmrg      dstdir_status=$?
315afe13c8eSmrg    fi
316afe13c8eSmrg  fi
317afe13c8eSmrg
31874b97a6cSmrg  case $dstdir in
31974b97a6cSmrg    */) dstdirslash=$dstdir;;
32074b97a6cSmrg    *)  dstdirslash=$dstdir/;;
32174b97a6cSmrg  esac
32274b97a6cSmrg
323afe13c8eSmrg  obsolete_mkdir_used=false
324afe13c8eSmrg
325afe13c8eSmrg  if test $dstdir_status != 0; then
326afe13c8eSmrg    case $posix_mkdir in
327afe13c8eSmrg      '')
32806ef0fecSmrg        # With -d, create the new directory with the user-specified mode.
32906ef0fecSmrg        # Otherwise, rely on $mkdir_umask.
33006ef0fecSmrg        if test -n "$dir_arg"; then
33106ef0fecSmrg          mkdir_mode=-m$mode
33206ef0fecSmrg        else
33306ef0fecSmrg          mkdir_mode=
33406ef0fecSmrg        fi
33506ef0fecSmrg
33606ef0fecSmrg        posix_mkdir=false
33774b97a6cSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33874b97a6cSmrg	# here however when possible just to lower collision chance.
33974b97a6cSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34074b97a6cSmrg
34174b97a6cSmrg	trap '
34274b97a6cSmrg	  ret=$?
34374b97a6cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34474b97a6cSmrg	  exit $ret
34574b97a6cSmrg	' 0
34674b97a6cSmrg
34774b97a6cSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34874b97a6cSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34974b97a6cSmrg	# directory is successfully created first before we actually test
35074b97a6cSmrg	# 'mkdir -p'.
35174b97a6cSmrg	if (umask $mkdir_umask &&
35274b97a6cSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35374b97a6cSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35474b97a6cSmrg	then
35574b97a6cSmrg	  if test -z "$dir_arg" || {
35674b97a6cSmrg	       # Check for POSIX incompatibilities with -m.
35774b97a6cSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35874b97a6cSmrg	       # other-writable bit of parent directory when it shouldn't.
35974b97a6cSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36074b97a6cSmrg	       test_tmpdir="$tmpdir/a"
36174b97a6cSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36274b97a6cSmrg	       case $ls_ld_tmpdir in
36374b97a6cSmrg		 d????-?r-*) different_mode=700;;
36474b97a6cSmrg		 d????-?--*) different_mode=755;;
36574b97a6cSmrg		 *) false;;
36674b97a6cSmrg	       esac &&
36774b97a6cSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36874b97a6cSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36974b97a6cSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37074b97a6cSmrg	       }
37174b97a6cSmrg	     }
37274b97a6cSmrg	  then posix_mkdir=:
37374b97a6cSmrg	  fi
37474b97a6cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37574b97a6cSmrg	else
37674b97a6cSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37774b97a6cSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37874b97a6cSmrg	fi
37974b97a6cSmrg	trap '' 0;;
380afe13c8eSmrg    esac
381b3307321Smrg
382afe13c8eSmrg    if
383afe13c8eSmrg      $posix_mkdir && (
38406ef0fecSmrg        umask $mkdir_umask &&
38506ef0fecSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386afe13c8eSmrg      )
387afe13c8eSmrg    then :
388afe13c8eSmrg    else
389afe13c8eSmrg
39074b97a6cSmrg      # mkdir does not conform to POSIX,
391afe13c8eSmrg      # or it failed possibly due to a race condition.  Create the
392afe13c8eSmrg      # directory the slow way, step by step, checking for races as we go.
393afe13c8eSmrg
394afe13c8eSmrg      case $dstdir in
39506ef0fecSmrg        /*) prefix='/';;
39606ef0fecSmrg        [-=\(\)!]*) prefix='./';;
39706ef0fecSmrg        *)  prefix='';;
398afe13c8eSmrg      esac
399afe13c8eSmrg
400afe13c8eSmrg      oIFS=$IFS
401afe13c8eSmrg      IFS=/
40206ef0fecSmrg      set -f
403afe13c8eSmrg      set fnord $dstdir
404afe13c8eSmrg      shift
40506ef0fecSmrg      set +f
406afe13c8eSmrg      IFS=$oIFS
407afe13c8eSmrg
408afe13c8eSmrg      prefixes=
409afe13c8eSmrg
410afe13c8eSmrg      for d
411afe13c8eSmrg      do
41206ef0fecSmrg        test X"$d" = X && continue
41306ef0fecSmrg
41406ef0fecSmrg        prefix=$prefix$d
41506ef0fecSmrg        if test -d "$prefix"; then
41606ef0fecSmrg          prefixes=
41706ef0fecSmrg        else
41806ef0fecSmrg          if $posix_mkdir; then
41974b97a6cSmrg            (umask $mkdir_umask &&
42006ef0fecSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42106ef0fecSmrg            # Don't fail if two instances are running concurrently.
42206ef0fecSmrg            test -d "$prefix" || exit 1
42306ef0fecSmrg          else
42406ef0fecSmrg            case $prefix in
42506ef0fecSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42606ef0fecSmrg              *) qprefix=$prefix;;
42706ef0fecSmrg            esac
42806ef0fecSmrg            prefixes="$prefixes '$qprefix'"
42906ef0fecSmrg          fi
43006ef0fecSmrg        fi
43106ef0fecSmrg        prefix=$prefix/
432afe13c8eSmrg      done
433afe13c8eSmrg
434afe13c8eSmrg      if test -n "$prefixes"; then
43506ef0fecSmrg        # Don't fail if two instances are running concurrently.
43606ef0fecSmrg        (umask $mkdir_umask &&
43706ef0fecSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43806ef0fecSmrg          test -d "$dstdir" || exit 1
43906ef0fecSmrg        obsolete_mkdir_used=true
440afe13c8eSmrg      fi
441afe13c8eSmrg    fi
442afe13c8eSmrg  fi
443afe13c8eSmrg
444afe13c8eSmrg  if test -n "$dir_arg"; then
445afe13c8eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446afe13c8eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447afe13c8eSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448afe13c8eSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449afe13c8eSmrg  else
450afe13c8eSmrg
451afe13c8eSmrg    # Make a couple of temp file names in the proper directory.
45274b97a6cSmrg    dsttmp=${dstdirslash}_inst.$$_
45374b97a6cSmrg    rmtmp=${dstdirslash}_rm.$$_
454afe13c8eSmrg
455afe13c8eSmrg    # Trap to clean up those temp files at exit.
456afe13c8eSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457afe13c8eSmrg
458afe13c8eSmrg    # Copy the file name to the temp name.
45974b97a6cSmrg    (umask $cp_umask &&
46074b97a6cSmrg     { test -z "$stripcmd" || {
46174b97a6cSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46274b97a6cSmrg	 # which would cause strip to fail.
46374b97a6cSmrg	 if test -z "$doit"; then
46474b97a6cSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46574b97a6cSmrg	 else
46674b97a6cSmrg	   $doit touch "$dsttmp"
46774b97a6cSmrg	 fi
46874b97a6cSmrg       }
46974b97a6cSmrg     } &&
47074b97a6cSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471afe13c8eSmrg
472afe13c8eSmrg    # and set any options; do chmod last to preserve setuid bits.
473afe13c8eSmrg    #
474afe13c8eSmrg    # If any of these fail, we abort the whole thing.  If we want to
475afe13c8eSmrg    # ignore errors from any of these, just make sure not to ignore
476afe13c8eSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477afe13c8eSmrg    #
478afe13c8eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479afe13c8eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480afe13c8eSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481afe13c8eSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482afe13c8eSmrg
483afe13c8eSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484afe13c8eSmrg    if $copy_on_change &&
48506ef0fecSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48606ef0fecSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48706ef0fecSmrg       set -f &&
488afe13c8eSmrg       set X $old && old=:$2:$4:$5:$6 &&
489afe13c8eSmrg       set X $new && new=:$2:$4:$5:$6 &&
49006ef0fecSmrg       set +f &&
491afe13c8eSmrg       test "$old" = "$new" &&
492afe13c8eSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493afe13c8eSmrg    then
494afe13c8eSmrg      rm -f "$dsttmp"
495afe13c8eSmrg    else
49674b97a6cSmrg      # If $backupsuffix is set, and the file being installed
49774b97a6cSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49874b97a6cSmrg      # e.g., if mv doesn't support -f.
49974b97a6cSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50074b97a6cSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50174b97a6cSmrg      fi
50274b97a6cSmrg
503afe13c8eSmrg      # Rename the file to the real destination.
504afe13c8eSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505afe13c8eSmrg
506afe13c8eSmrg      # The rename failed, perhaps because mv can't rename something else
507afe13c8eSmrg      # to itself, or perhaps because mv is so ancient that it does not
508afe13c8eSmrg      # support -f.
509afe13c8eSmrg      {
51006ef0fecSmrg        # Now remove or move aside any old file at destination location.
51106ef0fecSmrg        # We try this two ways since rm can't unlink itself on some
51206ef0fecSmrg        # systems and the destination file might be busy for other
51306ef0fecSmrg        # reasons.  In this case, the final cleanup might fail but the new
51406ef0fecSmrg        # file should still install successfully.
51506ef0fecSmrg        {
51606ef0fecSmrg          test ! -f "$dst" ||
51774b97a6cSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51806ef0fecSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51974b97a6cSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52006ef0fecSmrg          } ||
52106ef0fecSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52206ef0fecSmrg            (exit 1); exit 1
52306ef0fecSmrg          }
52406ef0fecSmrg        } &&
52506ef0fecSmrg
52606ef0fecSmrg        # Now rename the file to the real destination.
52706ef0fecSmrg        $doit $mvcmd "$dsttmp" "$dst"
528afe13c8eSmrg      }
529afe13c8eSmrg    fi || exit 1
530afe13c8eSmrg
531afe13c8eSmrg    trap '' 0
532afe13c8eSmrg  fi
533afe13c8eSmrgdone
534b3307321Smrg
535afe13c8eSmrg# Local variables:
53674b97a6cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537afe13c8eSmrg# time-stamp-start: "scriptversion="
538afe13c8eSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53974b97a6cSmrg# time-stamp-time-zone: "UTC0"
5405e358ecaSmrg# time-stamp-end: "; # UTC"
541afe13c8eSmrg# End:
542