115fb4814Smrg#!/bin/sh
215fb4814Smrg# install - install a program, script, or datafile
315fb4814Smrg
4f010a93dSmrgscriptversion=2020-11-14.01; # UTC
515fb4814Smrg
615fb4814Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
715fb4814Smrg# later released in X11R6 (xc/config/util/install.sh) with the
815fb4814Smrg# following copyright and license.
915fb4814Smrg#
1015fb4814Smrg# Copyright (C) 1994 X Consortium
1115fb4814Smrg#
1215fb4814Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1315fb4814Smrg# of this software and associated documentation files (the "Software"), to
1415fb4814Smrg# deal in the Software without restriction, including without limitation the
1515fb4814Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1615fb4814Smrg# sell copies of the Software, and to permit persons to whom the Software is
1715fb4814Smrg# furnished to do so, subject to the following conditions:
1815fb4814Smrg#
1915fb4814Smrg# The above copyright notice and this permission notice shall be included in
2015fb4814Smrg# all copies or substantial portions of the Software.
2115fb4814Smrg#
2215fb4814Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2315fb4814Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2415fb4814Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2515fb4814Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2615fb4814Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2715fb4814Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2815fb4814Smrg#
2915fb4814Smrg# Except as contained in this notice, the name of the X Consortium shall not
3015fb4814Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3115fb4814Smrg# ings in this Software without prior written authorization from the X Consor-
3215fb4814Smrg# tium.
3315fb4814Smrg#
3415fb4814Smrg#
3515fb4814Smrg# FSF changes to this file are in the public domain.
3615fb4814Smrg#
3715fb4814Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38cf503b78Smrg# 'make' implicit rules from creating a file called install from it
3915fb4814Smrg# when there is no Makefile.
4015fb4814Smrg#
4115fb4814Smrg# This script is compatible with the BSD install script, but was written
42de78e416Smrg# from scratch.
43de78e416Smrg
44f010a93dSmrgtab='	'
45de78e416Smrgnl='
46de78e416Smrg'
47f010a93dSmrgIFS=" $tab$nl"
4815fb4814Smrg
49f010a93dSmrg# Set DOITPROG to "echo" to test this script.
5015fb4814Smrg
51b4d38c65Smrgdoit=${DOITPROG-}
52f010a93dSmrgdoit_exec=${doit:-exec}
5315fb4814Smrg
54de78e416Smrg# Put in absolute file names if you don't have them in your path;
55de78e416Smrg# or use environment vars.
5615fb4814Smrg
57b4d38c65Smrgchgrpprog=${CHGRPPROG-chgrp}
58b4d38c65Smrgchmodprog=${CHMODPROG-chmod}
59b4d38c65Smrgchownprog=${CHOWNPROG-chown}
60b4d38c65Smrgcmpprog=${CMPPROG-cmp}
61b4d38c65Smrgcpprog=${CPPROG-cp}
62b4d38c65Smrgmkdirprog=${MKDIRPROG-mkdir}
63b4d38c65Smrgmvprog=${MVPROG-mv}
64b4d38c65Smrgrmprog=${RMPROG-rm}
65b4d38c65Smrgstripprog=${STRIPPROG-strip}
66b4d38c65Smrg
67de78e416Smrgposix_mkdir=
68de78e416Smrg
69de78e416Smrg# Desired mode of installed file.
70de78e416Smrgmode=0755
71de78e416Smrg
72f010a93dSmrg# Create dirs (including intermediate dirs) using mode 755.
73f010a93dSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74f010a93dSmrgmkdir_umask=22
75f010a93dSmrg
76f010a93dSmrgbackupsuffix=
77b4d38c65Smrgchgrpcmd=
78de78e416Smrgchmodcmd=$chmodprog
7915fb4814Smrgchowncmd=
80b4d38c65Smrgmvcmd=$mvprog
81b534f209Smrgrmcmd="$rmprog -f"
82b4d38c65Smrgstripcmd=
83b4d38c65Smrg
8415fb4814Smrgsrc=
8515fb4814Smrgdst=
8615fb4814Smrgdir_arg=
87b4d38c65Smrgdst_arg=
88b4d38c65Smrg
89b4d38c65Smrgcopy_on_change=false
90f010a93dSmrgis_target_a_directory=possibly
9115fb4814Smrg
92b4d38c65Smrgusage="\
93b4d38c65SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9415fb4814Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9515fb4814Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9615fb4814Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9715fb4814Smrg
9815fb4814SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9915fb4814SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10015fb4814SmrgIn the 4th, create DIRECTORIES.
10115fb4814Smrg
10215fb4814SmrgOptions:
103b4d38c65Smrg     --help     display this help and exit.
104b4d38c65Smrg     --version  display version info and exit.
105b4d38c65Smrg
106b4d38c65Smrg  -c            (ignored)
107f010a93dSmrg  -C            install only if different (preserve data modification time)
108b4d38c65Smrg  -d            create directories instead of installing files.
109b4d38c65Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110b4d38c65Smrg  -m MODE       $chmodprog installed files to MODE.
111b4d38c65Smrg  -o USER       $chownprog installed files to USER.
112f010a93dSmrg  -p            pass -p to $cpprog.
113b4d38c65Smrg  -s            $stripprog installed files.
114f010a93dSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115b4d38c65Smrg  -t DIRECTORY  install into DIRECTORY.
116b4d38c65Smrg  -T            report an error if DSTFILE is a directory.
11715fb4814Smrg
11815fb4814SmrgEnvironment variables override the default commands:
119b4d38c65Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120b4d38c65Smrg  RMPROG STRIPPROG
121f010a93dSmrg
122f010a93dSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123f010a93dSmrgit's up to you to specify -f if you want it.
124f010a93dSmrg
125f010a93dSmrgIf -S is not specified, no backups are attempted.
126f010a93dSmrg
127f010a93dSmrgEmail bug reports to bug-automake@gnu.org.
128f010a93dSmrgAutomake home page: https://www.gnu.org/software/automake/
12915fb4814Smrg"
13015fb4814Smrg
131de78e416Smrgwhile test $# -ne 0; do
13215fb4814Smrg  case $1 in
133b4d38c65Smrg    -c) ;;
13415fb4814Smrg
135b4d38c65Smrg    -C) copy_on_change=true;;
136b4d38c65Smrg
137b4d38c65Smrg    -d) dir_arg=true;;
13815fb4814Smrg
13915fb4814Smrg    -g) chgrpcmd="$chgrpprog $2"
140f010a93dSmrg        shift;;
14115fb4814Smrg
14215fb4814Smrg    --help) echo "$usage"; exit $?;;
14315fb4814Smrg
144de78e416Smrg    -m) mode=$2
145f010a93dSmrg        case $mode in
146f010a93dSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147f010a93dSmrg            echo "$0: invalid mode: $mode" >&2
148f010a93dSmrg            exit 1;;
149f010a93dSmrg        esac
150f010a93dSmrg        shift;;
15115fb4814Smrg
15215fb4814Smrg    -o) chowncmd="$chownprog $2"
153f010a93dSmrg        shift;;
154f010a93dSmrg
155f010a93dSmrg    -p) cpprog="$cpprog -p";;
15615fb4814Smrg
157b4d38c65Smrg    -s) stripcmd=$stripprog;;
15815fb4814Smrg
159f010a93dSmrg    -S) backupsuffix="$2"
160f010a93dSmrg        shift;;
16115fb4814Smrg
162f010a93dSmrg    -t)
163f010a93dSmrg        is_target_a_directory=always
164f010a93dSmrg        dst_arg=$2
165f010a93dSmrg        # Protect names problematic for 'test' and other utilities.
166f010a93dSmrg        case $dst_arg in
167f010a93dSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168f010a93dSmrg        esac
169f010a93dSmrg        shift;;
170f010a93dSmrg
171f010a93dSmrg    -T) is_target_a_directory=never;;
17215fb4814Smrg
17315fb4814Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17415fb4814Smrg
175f010a93dSmrg    --) shift
176f010a93dSmrg        break;;
177de78e416Smrg
178f010a93dSmrg    -*) echo "$0: invalid option: $1" >&2
179f010a93dSmrg        exit 1;;
180de78e416Smrg
181de78e416Smrg    *)  break;;
18215fb4814Smrg  esac
183b4d38c65Smrg  shift
18415fb4814Smrgdone
18515fb4814Smrg
186f010a93dSmrg# We allow the use of options -d and -T together, by making -d
187f010a93dSmrg# take the precedence; this is for compatibility with GNU install.
188f010a93dSmrg
189f010a93dSmrgif test -n "$dir_arg"; then
190f010a93dSmrg  if test -n "$dst_arg"; then
191f010a93dSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192f010a93dSmrg    exit 1
193f010a93dSmrg  fi
194f010a93dSmrgfi
195f010a93dSmrg
196b4d38c65Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197de78e416Smrg  # When -d is used, all remaining arguments are directories to create.
198de78e416Smrg  # When -t is used, the destination is already specified.
199de78e416Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200de78e416Smrg  for arg
201de78e416Smrg  do
202b4d38c65Smrg    if test -n "$dst_arg"; then
203de78e416Smrg      # $@ is not empty: it contains at least $arg.
204b4d38c65Smrg      set fnord "$@" "$dst_arg"
205de78e416Smrg      shift # fnord
206de78e416Smrg    fi
207de78e416Smrg    shift # arg
208b4d38c65Smrg    dst_arg=$arg
209cf503b78Smrg    # Protect names problematic for 'test' and other utilities.
210cf503b78Smrg    case $dst_arg in
211cf503b78Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212cf503b78Smrg    esac
213de78e416Smrg  done
214de78e416Smrgfi
215de78e416Smrg
216de78e416Smrgif test $# -eq 0; then
21715fb4814Smrg  if test -z "$dir_arg"; then
21815fb4814Smrg    echo "$0: no input file specified." >&2
21915fb4814Smrg    exit 1
22015fb4814Smrg  fi
221cf503b78Smrg  # It's OK to call 'install-sh -d' without argument.
22215fb4814Smrg  # This can happen when creating conditional directories.
22315fb4814Smrg  exit 0
22415fb4814Smrgfi
22515fb4814Smrg
226f010a93dSmrgif test -z "$dir_arg"; then
227f010a93dSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228f010a93dSmrg    if test ! -d "$dst_arg"; then
229f010a93dSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230f010a93dSmrg      exit 1
231f010a93dSmrg    fi
232f010a93dSmrg  fi
233f010a93dSmrgfi
234f010a93dSmrg
235de78e416Smrgif test -z "$dir_arg"; then
236cf503b78Smrg  do_exit='(exit $ret); exit $ret'
237cf503b78Smrg  trap "ret=129; $do_exit" 1
238cf503b78Smrg  trap "ret=130; $do_exit" 2
239cf503b78Smrg  trap "ret=141; $do_exit" 13
240cf503b78Smrg  trap "ret=143; $do_exit" 15
241de78e416Smrg
242de78e416Smrg  # Set umask so as not to create temps with too-generous modes.
243de78e416Smrg  # However, 'strip' requires both read and write access to temps.
244de78e416Smrg  case $mode in
245de78e416Smrg    # Optimize common cases.
246de78e416Smrg    *644) cp_umask=133;;
247de78e416Smrg    *755) cp_umask=22;;
248de78e416Smrg
249de78e416Smrg    *[0-7])
250de78e416Smrg      if test -z "$stripcmd"; then
251f010a93dSmrg        u_plus_rw=
252de78e416Smrg      else
253f010a93dSmrg        u_plus_rw='% 200'
254de78e416Smrg      fi
255de78e416Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256de78e416Smrg    *)
257de78e416Smrg      if test -z "$stripcmd"; then
258f010a93dSmrg        u_plus_rw=
259de78e416Smrg      else
260f010a93dSmrg        u_plus_rw=,u+rw
261de78e416Smrg      fi
262de78e416Smrg      cp_umask=$mode$u_plus_rw;;
263de78e416Smrg  esac
264de78e416Smrgfi
265de78e416Smrg
26615fb4814Smrgfor src
26715fb4814Smrgdo
268cf503b78Smrg  # Protect names problematic for 'test' and other utilities.
26915fb4814Smrg  case $src in
270cf503b78Smrg    -* | [=\(\)!]) src=./$src;;
27115fb4814Smrg  esac
27215fb4814Smrg
27315fb4814Smrg  if test -n "$dir_arg"; then
27415fb4814Smrg    dst=$src
275de78e416Smrg    dstdir=$dst
276de78e416Smrg    test -d "$dstdir"
277de78e416Smrg    dstdir_status=$?
278f010a93dSmrg    # Don't chown directories that already exist.
279f010a93dSmrg    if test $dstdir_status = 0; then
280f010a93dSmrg      chowncmd=""
281f010a93dSmrg    fi
28215fb4814Smrg  else
283de78e416Smrg
28415fb4814Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28515fb4814Smrg    # might cause directories to be created, which would be especially bad
28615fb4814Smrg    # if $src (and thus $dsttmp) contains '*'.
28715fb4814Smrg    if test ! -f "$src" && test ! -d "$src"; then
28815fb4814Smrg      echo "$0: $src does not exist." >&2
28915fb4814Smrg      exit 1
29015fb4814Smrg    fi
29115fb4814Smrg
292b4d38c65Smrg    if test -z "$dst_arg"; then
29315fb4814Smrg      echo "$0: no destination specified." >&2
29415fb4814Smrg      exit 1
29515fb4814Smrg    fi
296b4d38c65Smrg    dst=$dst_arg
29715fb4814Smrg
298f010a93dSmrg    # If destination is a directory, append the input filename.
29915fb4814Smrg    if test -d "$dst"; then
300f010a93dSmrg      if test "$is_target_a_directory" = never; then
301f010a93dSmrg        echo "$0: $dst_arg: Is a directory" >&2
302f010a93dSmrg        exit 1
30315fb4814Smrg      fi
304de78e416Smrg      dstdir=$dst
305f010a93dSmrg      dstbase=`basename "$src"`
306f010a93dSmrg      case $dst in
307f010a93dSmrg	*/) dst=$dst$dstbase;;
308f010a93dSmrg	*)  dst=$dst/$dstbase;;
309f010a93dSmrg      esac
310de78e416Smrg      dstdir_status=0
311de78e416Smrg    else
312f010a93dSmrg      dstdir=`dirname "$dst"`
313de78e416Smrg      test -d "$dstdir"
314de78e416Smrg      dstdir_status=$?
31515fb4814Smrg    fi
31615fb4814Smrg  fi
31715fb4814Smrg
318f010a93dSmrg  case $dstdir in
319f010a93dSmrg    */) dstdirslash=$dstdir;;
320f010a93dSmrg    *)  dstdirslash=$dstdir/;;
321f010a93dSmrg  esac
322f010a93dSmrg
323de78e416Smrg  obsolete_mkdir_used=false
324de78e416Smrg
325de78e416Smrg  if test $dstdir_status != 0; then
326de78e416Smrg    case $posix_mkdir in
327de78e416Smrg      '')
328f010a93dSmrg        # With -d, create the new directory with the user-specified mode.
329f010a93dSmrg        # Otherwise, rely on $mkdir_umask.
330f010a93dSmrg        if test -n "$dir_arg"; then
331f010a93dSmrg          mkdir_mode=-m$mode
332f010a93dSmrg        else
333f010a93dSmrg          mkdir_mode=
334f010a93dSmrg        fi
335f010a93dSmrg
336f010a93dSmrg        posix_mkdir=false
337f010a93dSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338f010a93dSmrg	# here however when possible just to lower collision chance.
339f010a93dSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340f010a93dSmrg
341f010a93dSmrg	trap '
342f010a93dSmrg	  ret=$?
343f010a93dSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344f010a93dSmrg	  exit $ret
345f010a93dSmrg	' 0
346f010a93dSmrg
347f010a93dSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348f010a93dSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349f010a93dSmrg	# directory is successfully created first before we actually test
350f010a93dSmrg	# 'mkdir -p'.
351f010a93dSmrg	if (umask $mkdir_umask &&
352f010a93dSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353f010a93dSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354f010a93dSmrg	then
355f010a93dSmrg	  if test -z "$dir_arg" || {
356f010a93dSmrg	       # Check for POSIX incompatibilities with -m.
357f010a93dSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358f010a93dSmrg	       # other-writable bit of parent directory when it shouldn't.
359f010a93dSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360f010a93dSmrg	       test_tmpdir="$tmpdir/a"
361f010a93dSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362f010a93dSmrg	       case $ls_ld_tmpdir in
363f010a93dSmrg		 d????-?r-*) different_mode=700;;
364f010a93dSmrg		 d????-?--*) different_mode=755;;
365f010a93dSmrg		 *) false;;
366f010a93dSmrg	       esac &&
367f010a93dSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368f010a93dSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369f010a93dSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370f010a93dSmrg	       }
371f010a93dSmrg	     }
372f010a93dSmrg	  then posix_mkdir=:
373f010a93dSmrg	  fi
374f010a93dSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375de78e416Smrg	else
376f010a93dSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377f010a93dSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378de78e416Smrg	fi
379f010a93dSmrg	trap '' 0;;
380de78e416Smrg    esac
38115fb4814Smrg
382de78e416Smrg    if
383de78e416Smrg      $posix_mkdir && (
384f010a93dSmrg        umask $mkdir_umask &&
385f010a93dSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386de78e416Smrg      )
387de78e416Smrg    then :
388de78e416Smrg    else
38915fb4814Smrg
390f010a93dSmrg      # mkdir does not conform to POSIX,
391de78e416Smrg      # or it failed possibly due to a race condition.  Create the
392de78e416Smrg      # directory the slow way, step by step, checking for races as we go.
393de78e416Smrg
394de78e416Smrg      case $dstdir in
395f010a93dSmrg        /*) prefix='/';;
396f010a93dSmrg        [-=\(\)!]*) prefix='./';;
397f010a93dSmrg        *)  prefix='';;
398de78e416Smrg      esac
399de78e416Smrg
400de78e416Smrg      oIFS=$IFS
401de78e416Smrg      IFS=/
402f010a93dSmrg      set -f
403de78e416Smrg      set fnord $dstdir
40415fb4814Smrg      shift
405f010a93dSmrg      set +f
406de78e416Smrg      IFS=$oIFS
407de78e416Smrg
408de78e416Smrg      prefixes=
409de78e416Smrg
410de78e416Smrg      for d
411de78e416Smrg      do
412f010a93dSmrg        test X"$d" = X && continue
413f010a93dSmrg
414f010a93dSmrg        prefix=$prefix$d
415f010a93dSmrg        if test -d "$prefix"; then
416f010a93dSmrg          prefixes=
417f010a93dSmrg        else
418f010a93dSmrg          if $posix_mkdir; then
419f010a93dSmrg            (umask $mkdir_umask &&
420f010a93dSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421f010a93dSmrg            # Don't fail if two instances are running concurrently.
422f010a93dSmrg            test -d "$prefix" || exit 1
423f010a93dSmrg          else
424f010a93dSmrg            case $prefix in
425f010a93dSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426f010a93dSmrg              *) qprefix=$prefix;;
427f010a93dSmrg            esac
428f010a93dSmrg            prefixes="$prefixes '$qprefix'"
429f010a93dSmrg          fi
430f010a93dSmrg        fi
431f010a93dSmrg        prefix=$prefix/
432de78e416Smrg      done
433de78e416Smrg
434de78e416Smrg      if test -n "$prefixes"; then
435f010a93dSmrg        # Don't fail if two instances are running concurrently.
436f010a93dSmrg        (umask $mkdir_umask &&
437f010a93dSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438f010a93dSmrg          test -d "$dstdir" || exit 1
439f010a93dSmrg        obsolete_mkdir_used=true
44015fb4814Smrg      fi
441de78e416Smrg    fi
44215fb4814Smrg  fi
44315fb4814Smrg
44415fb4814Smrg  if test -n "$dir_arg"; then
445de78e416Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446de78e416Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447de78e416Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448de78e416Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44915fb4814Smrg  else
45015fb4814Smrg
45115fb4814Smrg    # Make a couple of temp file names in the proper directory.
452f010a93dSmrg    dsttmp=${dstdirslash}_inst.$$_
453f010a93dSmrg    rmtmp=${dstdirslash}_rm.$$_
45415fb4814Smrg
45515fb4814Smrg    # Trap to clean up those temp files at exit.
45615fb4814Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45715fb4814Smrg
45815fb4814Smrg    # Copy the file name to the temp name.
459f010a93dSmrg    (umask $cp_umask &&
460f010a93dSmrg     { test -z "$stripcmd" || {
461f010a93dSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462f010a93dSmrg	 # which would cause strip to fail.
463f010a93dSmrg	 if test -z "$doit"; then
464f010a93dSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465f010a93dSmrg	 else
466f010a93dSmrg	   $doit touch "$dsttmp"
467f010a93dSmrg	 fi
468f010a93dSmrg       }
469f010a93dSmrg     } &&
470f010a93dSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47115fb4814Smrg
47215fb4814Smrg    # and set any options; do chmod last to preserve setuid bits.
47315fb4814Smrg    #
47415fb4814Smrg    # If any of these fail, we abort the whole thing.  If we want to
47515fb4814Smrg    # ignore errors from any of these, just make sure not to ignore
47615fb4814Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47715fb4814Smrg    #
478b4d38c65Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479b4d38c65Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480b4d38c65Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481b4d38c65Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482b4d38c65Smrg
483b4d38c65Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484b4d38c65Smrg    if $copy_on_change &&
485f010a93dSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486f010a93dSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487f010a93dSmrg       set -f &&
488b4d38c65Smrg       set X $old && old=:$2:$4:$5:$6 &&
489b4d38c65Smrg       set X $new && new=:$2:$4:$5:$6 &&
490f010a93dSmrg       set +f &&
491b4d38c65Smrg       test "$old" = "$new" &&
492b4d38c65Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493b4d38c65Smrg    then
494b4d38c65Smrg      rm -f "$dsttmp"
495b4d38c65Smrg    else
496f010a93dSmrg      # If $backupsuffix is set, and the file being installed
497f010a93dSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498f010a93dSmrg      # e.g., if mv doesn't support -f.
499f010a93dSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500f010a93dSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501f010a93dSmrg      fi
502f010a93dSmrg
503b4d38c65Smrg      # Rename the file to the real destination.
504b4d38c65Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505b4d38c65Smrg
506b4d38c65Smrg      # The rename failed, perhaps because mv can't rename something else
507b4d38c65Smrg      # to itself, or perhaps because mv is so ancient that it does not
508b4d38c65Smrg      # support -f.
509b4d38c65Smrg      {
510f010a93dSmrg        # Now remove or move aside any old file at destination location.
511f010a93dSmrg        # We try this two ways since rm can't unlink itself on some
512f010a93dSmrg        # systems and the destination file might be busy for other
513f010a93dSmrg        # reasons.  In this case, the final cleanup might fail but the new
514f010a93dSmrg        # file should still install successfully.
515f010a93dSmrg        {
516f010a93dSmrg          test ! -f "$dst" ||
517f010a93dSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518f010a93dSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519f010a93dSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520f010a93dSmrg          } ||
521f010a93dSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522f010a93dSmrg            (exit 1); exit 1
523f010a93dSmrg          }
524f010a93dSmrg        } &&
525f010a93dSmrg
526f010a93dSmrg        # Now rename the file to the real destination.
527f010a93dSmrg        $doit $mvcmd "$dsttmp" "$dst"
528b4d38c65Smrg      }
529b4d38c65Smrg    fi || exit 1
53015fb4814Smrg
531de78e416Smrg    trap '' 0
532de78e416Smrg  fi
533de78e416Smrgdone
53415fb4814Smrg
53515fb4814Smrg# Local variables:
536f010a93dSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53715fb4814Smrg# time-stamp-start: "scriptversion="
53815fb4814Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539f010a93dSmrg# time-stamp-time-zone: "UTC0"
540b4d38c65Smrg# time-stamp-end: "; # UTC"
54115fb4814Smrg# End:
542