17da8b7e3Smrg#!/bin/sh
27da8b7e3Smrg# install - install a program, script, or datafile
37da8b7e3Smrg
4e4b60806Smrgscriptversion=2020-11-14.01; # UTC
57da8b7e3Smrg
67da8b7e3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77da8b7e3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87da8b7e3Smrg# following copyright and license.
97da8b7e3Smrg#
107da8b7e3Smrg# Copyright (C) 1994 X Consortium
117da8b7e3Smrg#
127da8b7e3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137da8b7e3Smrg# of this software and associated documentation files (the "Software"), to
147da8b7e3Smrg# deal in the Software without restriction, including without limitation the
157da8b7e3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167da8b7e3Smrg# sell copies of the Software, and to permit persons to whom the Software is
177da8b7e3Smrg# furnished to do so, subject to the following conditions:
187da8b7e3Smrg#
197da8b7e3Smrg# The above copyright notice and this permission notice shall be included in
207da8b7e3Smrg# all copies or substantial portions of the Software.
217da8b7e3Smrg#
227da8b7e3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237da8b7e3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247da8b7e3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257da8b7e3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267da8b7e3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277da8b7e3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287da8b7e3Smrg#
297da8b7e3Smrg# Except as contained in this notice, the name of the X Consortium shall not
307da8b7e3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317da8b7e3Smrg# ings in this Software without prior written authorization from the X Consor-
327da8b7e3Smrg# tium.
337da8b7e3Smrg#
347da8b7e3Smrg#
357da8b7e3Smrg# FSF changes to this file are in the public domain.
367da8b7e3Smrg#
377da8b7e3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c3d5982aSmrg# 'make' implicit rules from creating a file called install from it
397da8b7e3Smrg# when there is no Makefile.
407da8b7e3Smrg#
417da8b7e3Smrg# This script is compatible with the BSD install script, but was written
42123e2cc7Smrg# from scratch.
43123e2cc7Smrg
44ff143803Smrgtab='	'
45123e2cc7Smrgnl='
46123e2cc7Smrg'
47ff143803SmrgIFS=" $tab$nl"
487da8b7e3Smrg
49ff143803Smrg# Set DOITPROG to "echo" to test this script.
507da8b7e3Smrg
51123e2cc7Smrgdoit=${DOITPROG-}
52ff143803Smrgdoit_exec=${doit:-exec}
537da8b7e3Smrg
54123e2cc7Smrg# Put in absolute file names if you don't have them in your path;
55123e2cc7Smrg# or use environment vars.
56123e2cc7Smrg
57123e2cc7Smrgchgrpprog=${CHGRPPROG-chgrp}
58123e2cc7Smrgchmodprog=${CHMODPROG-chmod}
59123e2cc7Smrgchownprog=${CHOWNPROG-chown}
60123e2cc7Smrgcmpprog=${CMPPROG-cmp}
61123e2cc7Smrgcpprog=${CPPROG-cp}
62123e2cc7Smrgmkdirprog=${MKDIRPROG-mkdir}
63123e2cc7Smrgmvprog=${MVPROG-mv}
64123e2cc7Smrgrmprog=${RMPROG-rm}
65123e2cc7Smrgstripprog=${STRIPPROG-strip}
66123e2cc7Smrg
67123e2cc7Smrgposix_mkdir=
68123e2cc7Smrg
69123e2cc7Smrg# Desired mode of installed file.
70123e2cc7Smrgmode=0755
717da8b7e3Smrg
72e4b60806Smrg# Create dirs (including intermediate dirs) using mode 755.
73e4b60806Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74e4b60806Smrgmkdir_umask=22
75e4b60806Smrg
76e4b60806Smrgbackupsuffix=
777da8b7e3Smrgchgrpcmd=
78123e2cc7Smrgchmodcmd=$chmodprog
79123e2cc7Smrgchowncmd=
80123e2cc7Smrgmvcmd=$mvprog
817da8b7e3Smrgrmcmd="$rmprog -f"
82123e2cc7Smrgstripcmd=
83123e2cc7Smrg
847da8b7e3Smrgsrc=
857da8b7e3Smrgdst=
867da8b7e3Smrgdir_arg=
87123e2cc7Smrgdst_arg=
88123e2cc7Smrg
89123e2cc7Smrgcopy_on_change=false
90ff143803Smrgis_target_a_directory=possibly
917da8b7e3Smrg
92123e2cc7Smrgusage="\
93123e2cc7SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
947da8b7e3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
957da8b7e3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
967da8b7e3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
977da8b7e3Smrg
987da8b7e3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
997da8b7e3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1007da8b7e3SmrgIn the 4th, create DIRECTORIES.
1017da8b7e3Smrg
1027da8b7e3SmrgOptions:
103123e2cc7Smrg     --help     display this help and exit.
104123e2cc7Smrg     --version  display version info and exit.
105123e2cc7Smrg
106123e2cc7Smrg  -c            (ignored)
107e4b60806Smrg  -C            install only if different (preserve data modification time)
108123e2cc7Smrg  -d            create directories instead of installing files.
109123e2cc7Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110123e2cc7Smrg  -m MODE       $chmodprog installed files to MODE.
111123e2cc7Smrg  -o USER       $chownprog installed files to USER.
112e4b60806Smrg  -p            pass -p to $cpprog.
113123e2cc7Smrg  -s            $stripprog installed files.
114e4b60806Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115123e2cc7Smrg  -t DIRECTORY  install into DIRECTORY.
116123e2cc7Smrg  -T            report an error if DSTFILE is a directory.
1177da8b7e3Smrg
1187da8b7e3SmrgEnvironment variables override the default commands:
119123e2cc7Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120123e2cc7Smrg  RMPROG STRIPPROG
121e4b60806Smrg
122e4b60806SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123e4b60806Smrgit's up to you to specify -f if you want it.
124e4b60806Smrg
125e4b60806SmrgIf -S is not specified, no backups are attempted.
126e4b60806Smrg
127e4b60806SmrgEmail bug reports to bug-automake@gnu.org.
128e4b60806SmrgAutomake home page: https://www.gnu.org/software/automake/
1297da8b7e3Smrg"
1307da8b7e3Smrg
131123e2cc7Smrgwhile test $# -ne 0; do
1327da8b7e3Smrg  case $1 in
133123e2cc7Smrg    -c) ;;
134123e2cc7Smrg
135123e2cc7Smrg    -C) copy_on_change=true;;
1367da8b7e3Smrg
137123e2cc7Smrg    -d) dir_arg=true;;
1387da8b7e3Smrg
1397da8b7e3Smrg    -g) chgrpcmd="$chgrpprog $2"
140ff143803Smrg        shift;;
1417da8b7e3Smrg
1427da8b7e3Smrg    --help) echo "$usage"; exit $?;;
1437da8b7e3Smrg
144123e2cc7Smrg    -m) mode=$2
145ff143803Smrg        case $mode in
146ff143803Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147ff143803Smrg            echo "$0: invalid mode: $mode" >&2
148ff143803Smrg            exit 1;;
149ff143803Smrg        esac
150ff143803Smrg        shift;;
1517da8b7e3Smrg
1527da8b7e3Smrg    -o) chowncmd="$chownprog $2"
153ff143803Smrg        shift;;
1547da8b7e3Smrg
155e4b60806Smrg    -p) cpprog="$cpprog -p";;
156e4b60806Smrg
157123e2cc7Smrg    -s) stripcmd=$stripprog;;
1587da8b7e3Smrg
159e4b60806Smrg    -S) backupsuffix="$2"
160e4b60806Smrg        shift;;
161e4b60806Smrg
162ff143803Smrg    -t)
163ff143803Smrg        is_target_a_directory=always
164ff143803Smrg        dst_arg=$2
165ff143803Smrg        # Protect names problematic for 'test' and other utilities.
166ff143803Smrg        case $dst_arg in
167ff143803Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168ff143803Smrg        esac
169ff143803Smrg        shift;;
1707da8b7e3Smrg
171ff143803Smrg    -T) is_target_a_directory=never;;
1727da8b7e3Smrg
1737da8b7e3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1747da8b7e3Smrg
175ff143803Smrg    --) shift
176ff143803Smrg        break;;
177123e2cc7Smrg
178ff143803Smrg    -*) echo "$0: invalid option: $1" >&2
179ff143803Smrg        exit 1;;
180123e2cc7Smrg
181123e2cc7Smrg    *)  break;;
1827da8b7e3Smrg  esac
183123e2cc7Smrg  shift
1847da8b7e3Smrgdone
1857da8b7e3Smrg
186ff143803Smrg# We allow the use of options -d and -T together, by making -d
187ff143803Smrg# take the precedence; this is for compatibility with GNU install.
188ff143803Smrg
189ff143803Smrgif test -n "$dir_arg"; then
190ff143803Smrg  if test -n "$dst_arg"; then
191ff143803Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192ff143803Smrg    exit 1
193ff143803Smrg  fi
194ff143803Smrgfi
195ff143803Smrg
196123e2cc7Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197123e2cc7Smrg  # When -d is used, all remaining arguments are directories to create.
198123e2cc7Smrg  # When -t is used, the destination is already specified.
199123e2cc7Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200123e2cc7Smrg  for arg
201123e2cc7Smrg  do
202123e2cc7Smrg    if test -n "$dst_arg"; then
203123e2cc7Smrg      # $@ is not empty: it contains at least $arg.
204123e2cc7Smrg      set fnord "$@" "$dst_arg"
205123e2cc7Smrg      shift # fnord
206123e2cc7Smrg    fi
207123e2cc7Smrg    shift # arg
208123e2cc7Smrg    dst_arg=$arg
209c3d5982aSmrg    # Protect names problematic for 'test' and other utilities.
210c3d5982aSmrg    case $dst_arg in
211c3d5982aSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212c3d5982aSmrg    esac
213123e2cc7Smrg  done
214123e2cc7Smrgfi
215123e2cc7Smrg
216123e2cc7Smrgif test $# -eq 0; then
2177da8b7e3Smrg  if test -z "$dir_arg"; then
2187da8b7e3Smrg    echo "$0: no input file specified." >&2
2197da8b7e3Smrg    exit 1
2207da8b7e3Smrg  fi
221c3d5982aSmrg  # It's OK to call 'install-sh -d' without argument.
2227da8b7e3Smrg  # This can happen when creating conditional directories.
2237da8b7e3Smrg  exit 0
2247da8b7e3Smrgfi
2257da8b7e3Smrg
226ff143803Smrgif test -z "$dir_arg"; then
227ff143803Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228ff143803Smrg    if test ! -d "$dst_arg"; then
229ff143803Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230ff143803Smrg      exit 1
231ff143803Smrg    fi
232ff143803Smrg  fi
233ff143803Smrgfi
234ff143803Smrg
235123e2cc7Smrgif test -z "$dir_arg"; then
236c3d5982aSmrg  do_exit='(exit $ret); exit $ret'
237c3d5982aSmrg  trap "ret=129; $do_exit" 1
238c3d5982aSmrg  trap "ret=130; $do_exit" 2
239c3d5982aSmrg  trap "ret=141; $do_exit" 13
240c3d5982aSmrg  trap "ret=143; $do_exit" 15
241123e2cc7Smrg
242123e2cc7Smrg  # Set umask so as not to create temps with too-generous modes.
243123e2cc7Smrg  # However, 'strip' requires both read and write access to temps.
244123e2cc7Smrg  case $mode in
245123e2cc7Smrg    # Optimize common cases.
246123e2cc7Smrg    *644) cp_umask=133;;
247123e2cc7Smrg    *755) cp_umask=22;;
248123e2cc7Smrg
249123e2cc7Smrg    *[0-7])
250123e2cc7Smrg      if test -z "$stripcmd"; then
251ff143803Smrg        u_plus_rw=
252123e2cc7Smrg      else
253ff143803Smrg        u_plus_rw='% 200'
254123e2cc7Smrg      fi
255123e2cc7Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256123e2cc7Smrg    *)
257123e2cc7Smrg      if test -z "$stripcmd"; then
258ff143803Smrg        u_plus_rw=
259123e2cc7Smrg      else
260ff143803Smrg        u_plus_rw=,u+rw
261123e2cc7Smrg      fi
262123e2cc7Smrg      cp_umask=$mode$u_plus_rw;;
263123e2cc7Smrg  esac
264123e2cc7Smrgfi
265123e2cc7Smrg
2667da8b7e3Smrgfor src
2677da8b7e3Smrgdo
268c3d5982aSmrg  # Protect names problematic for 'test' and other utilities.
2697da8b7e3Smrg  case $src in
270c3d5982aSmrg    -* | [=\(\)!]) src=./$src;;
2717da8b7e3Smrg  esac
2727da8b7e3Smrg
2737da8b7e3Smrg  if test -n "$dir_arg"; then
2747da8b7e3Smrg    dst=$src
275123e2cc7Smrg    dstdir=$dst
276123e2cc7Smrg    test -d "$dstdir"
277123e2cc7Smrg    dstdir_status=$?
278e4b60806Smrg    # Don't chown directories that already exist.
279e4b60806Smrg    if test $dstdir_status = 0; then
280e4b60806Smrg      chowncmd=""
281e4b60806Smrg    fi
2827da8b7e3Smrg  else
283123e2cc7Smrg
2847da8b7e3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2857da8b7e3Smrg    # might cause directories to be created, which would be especially bad
2867da8b7e3Smrg    # if $src (and thus $dsttmp) contains '*'.
2877da8b7e3Smrg    if test ! -f "$src" && test ! -d "$src"; then
2887da8b7e3Smrg      echo "$0: $src does not exist." >&2
2897da8b7e3Smrg      exit 1
2907da8b7e3Smrg    fi
2917da8b7e3Smrg
292123e2cc7Smrg    if test -z "$dst_arg"; then
2937da8b7e3Smrg      echo "$0: no destination specified." >&2
2947da8b7e3Smrg      exit 1
2957da8b7e3Smrg    fi
296123e2cc7Smrg    dst=$dst_arg
2977da8b7e3Smrg
298e4b60806Smrg    # If destination is a directory, append the input filename.
2997da8b7e3Smrg    if test -d "$dst"; then
300ff143803Smrg      if test "$is_target_a_directory" = never; then
301ff143803Smrg        echo "$0: $dst_arg: Is a directory" >&2
302ff143803Smrg        exit 1
3037da8b7e3Smrg      fi
304123e2cc7Smrg      dstdir=$dst
305e4b60806Smrg      dstbase=`basename "$src"`
306e4b60806Smrg      case $dst in
307e4b60806Smrg	*/) dst=$dst$dstbase;;
308e4b60806Smrg	*)  dst=$dst/$dstbase;;
309e4b60806Smrg      esac
310123e2cc7Smrg      dstdir_status=0
311123e2cc7Smrg    else
312ff143803Smrg      dstdir=`dirname "$dst"`
313123e2cc7Smrg      test -d "$dstdir"
314123e2cc7Smrg      dstdir_status=$?
3157da8b7e3Smrg    fi
3167da8b7e3Smrg  fi
3177da8b7e3Smrg
318e4b60806Smrg  case $dstdir in
319e4b60806Smrg    */) dstdirslash=$dstdir;;
320e4b60806Smrg    *)  dstdirslash=$dstdir/;;
321e4b60806Smrg  esac
322e4b60806Smrg
323123e2cc7Smrg  obsolete_mkdir_used=false
324123e2cc7Smrg
325123e2cc7Smrg  if test $dstdir_status != 0; then
326123e2cc7Smrg    case $posix_mkdir in
327123e2cc7Smrg      '')
328ff143803Smrg        # With -d, create the new directory with the user-specified mode.
329ff143803Smrg        # Otherwise, rely on $mkdir_umask.
330ff143803Smrg        if test -n "$dir_arg"; then
331ff143803Smrg          mkdir_mode=-m$mode
332ff143803Smrg        else
333ff143803Smrg          mkdir_mode=
334ff143803Smrg        fi
335ff143803Smrg
336ff143803Smrg        posix_mkdir=false
337e4b60806Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338e4b60806Smrg	# here however when possible just to lower collision chance.
339e4b60806Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340e4b60806Smrg
341e4b60806Smrg	trap '
342e4b60806Smrg	  ret=$?
343e4b60806Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344e4b60806Smrg	  exit $ret
345e4b60806Smrg	' 0
346e4b60806Smrg
347e4b60806Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348e4b60806Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349e4b60806Smrg	# directory is successfully created first before we actually test
350e4b60806Smrg	# 'mkdir -p'.
351e4b60806Smrg	if (umask $mkdir_umask &&
352e4b60806Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353e4b60806Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354e4b60806Smrg	then
355e4b60806Smrg	  if test -z "$dir_arg" || {
356e4b60806Smrg	       # Check for POSIX incompatibilities with -m.
357e4b60806Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358e4b60806Smrg	       # other-writable bit of parent directory when it shouldn't.
359e4b60806Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360e4b60806Smrg	       test_tmpdir="$tmpdir/a"
361e4b60806Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362e4b60806Smrg	       case $ls_ld_tmpdir in
363e4b60806Smrg		 d????-?r-*) different_mode=700;;
364e4b60806Smrg		 d????-?--*) different_mode=755;;
365e4b60806Smrg		 *) false;;
366e4b60806Smrg	       esac &&
367e4b60806Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368e4b60806Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369e4b60806Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370e4b60806Smrg	       }
371e4b60806Smrg	     }
372e4b60806Smrg	  then posix_mkdir=:
373e4b60806Smrg	  fi
374e4b60806Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375e4b60806Smrg	else
376e4b60806Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377e4b60806Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378e4b60806Smrg	fi
379e4b60806Smrg	trap '' 0;;
380123e2cc7Smrg    esac
3817da8b7e3Smrg
382123e2cc7Smrg    if
383123e2cc7Smrg      $posix_mkdir && (
384ff143803Smrg        umask $mkdir_umask &&
385ff143803Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386123e2cc7Smrg      )
387123e2cc7Smrg    then :
388123e2cc7Smrg    else
3897da8b7e3Smrg
390e4b60806Smrg      # mkdir does not conform to POSIX,
391123e2cc7Smrg      # or it failed possibly due to a race condition.  Create the
392123e2cc7Smrg      # directory the slow way, step by step, checking for races as we go.
3937da8b7e3Smrg
394123e2cc7Smrg      case $dstdir in
395ff143803Smrg        /*) prefix='/';;
396ff143803Smrg        [-=\(\)!]*) prefix='./';;
397ff143803Smrg        *)  prefix='';;
398123e2cc7Smrg      esac
3997da8b7e3Smrg
400123e2cc7Smrg      oIFS=$IFS
401123e2cc7Smrg      IFS=/
402ff143803Smrg      set -f
403123e2cc7Smrg      set fnord $dstdir
4047da8b7e3Smrg      shift
405ff143803Smrg      set +f
406123e2cc7Smrg      IFS=$oIFS
407123e2cc7Smrg
408123e2cc7Smrg      prefixes=
409123e2cc7Smrg
410123e2cc7Smrg      for d
411123e2cc7Smrg      do
412ff143803Smrg        test X"$d" = X && continue
413ff143803Smrg
414ff143803Smrg        prefix=$prefix$d
415ff143803Smrg        if test -d "$prefix"; then
416ff143803Smrg          prefixes=
417ff143803Smrg        else
418ff143803Smrg          if $posix_mkdir; then
419e4b60806Smrg            (umask $mkdir_umask &&
420ff143803Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421ff143803Smrg            # Don't fail if two instances are running concurrently.
422ff143803Smrg            test -d "$prefix" || exit 1
423ff143803Smrg          else
424ff143803Smrg            case $prefix in
425ff143803Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426ff143803Smrg              *) qprefix=$prefix;;
427ff143803Smrg            esac
428ff143803Smrg            prefixes="$prefixes '$qprefix'"
429ff143803Smrg          fi
430ff143803Smrg        fi
431ff143803Smrg        prefix=$prefix/
432123e2cc7Smrg      done
433123e2cc7Smrg
434123e2cc7Smrg      if test -n "$prefixes"; then
435ff143803Smrg        # Don't fail if two instances are running concurrently.
436ff143803Smrg        (umask $mkdir_umask &&
437ff143803Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438ff143803Smrg          test -d "$dstdir" || exit 1
439ff143803Smrg        obsolete_mkdir_used=true
4407da8b7e3Smrg      fi
441123e2cc7Smrg    fi
4427da8b7e3Smrg  fi
4437da8b7e3Smrg
4447da8b7e3Smrg  if test -n "$dir_arg"; then
445123e2cc7Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446123e2cc7Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447123e2cc7Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448123e2cc7Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4497da8b7e3Smrg  else
4507da8b7e3Smrg
4517da8b7e3Smrg    # Make a couple of temp file names in the proper directory.
452e4b60806Smrg    dsttmp=${dstdirslash}_inst.$$_
453e4b60806Smrg    rmtmp=${dstdirslash}_rm.$$_
4547da8b7e3Smrg
4557da8b7e3Smrg    # Trap to clean up those temp files at exit.
4567da8b7e3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4577da8b7e3Smrg
4587da8b7e3Smrg    # Copy the file name to the temp name.
459e4b60806Smrg    (umask $cp_umask &&
460e4b60806Smrg     { test -z "$stripcmd" || {
461e4b60806Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462e4b60806Smrg	 # which would cause strip to fail.
463e4b60806Smrg	 if test -z "$doit"; then
464e4b60806Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465e4b60806Smrg	 else
466e4b60806Smrg	   $doit touch "$dsttmp"
467e4b60806Smrg	 fi
468e4b60806Smrg       }
469e4b60806Smrg     } &&
470e4b60806Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4717da8b7e3Smrg
4727da8b7e3Smrg    # and set any options; do chmod last to preserve setuid bits.
4737da8b7e3Smrg    #
4747da8b7e3Smrg    # If any of these fail, we abort the whole thing.  If we want to
4757da8b7e3Smrg    # ignore errors from any of these, just make sure not to ignore
4767da8b7e3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4777da8b7e3Smrg    #
478123e2cc7Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479123e2cc7Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480123e2cc7Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481123e2cc7Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482123e2cc7Smrg
483123e2cc7Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484123e2cc7Smrg    if $copy_on_change &&
485ff143803Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486ff143803Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487ff143803Smrg       set -f &&
488123e2cc7Smrg       set X $old && old=:$2:$4:$5:$6 &&
489123e2cc7Smrg       set X $new && new=:$2:$4:$5:$6 &&
490ff143803Smrg       set +f &&
491123e2cc7Smrg       test "$old" = "$new" &&
492123e2cc7Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493123e2cc7Smrg    then
494123e2cc7Smrg      rm -f "$dsttmp"
495123e2cc7Smrg    else
496e4b60806Smrg      # If $backupsuffix is set, and the file being installed
497e4b60806Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498e4b60806Smrg      # e.g., if mv doesn't support -f.
499e4b60806Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500e4b60806Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501e4b60806Smrg      fi
502e4b60806Smrg
503123e2cc7Smrg      # Rename the file to the real destination.
504123e2cc7Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505123e2cc7Smrg
506123e2cc7Smrg      # The rename failed, perhaps because mv can't rename something else
507123e2cc7Smrg      # to itself, or perhaps because mv is so ancient that it does not
508123e2cc7Smrg      # support -f.
509123e2cc7Smrg      {
510ff143803Smrg        # Now remove or move aside any old file at destination location.
511ff143803Smrg        # We try this two ways since rm can't unlink itself on some
512ff143803Smrg        # systems and the destination file might be busy for other
513ff143803Smrg        # reasons.  In this case, the final cleanup might fail but the new
514ff143803Smrg        # file should still install successfully.
515ff143803Smrg        {
516ff143803Smrg          test ! -f "$dst" ||
517e4b60806Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518ff143803Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519e4b60806Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520ff143803Smrg          } ||
521ff143803Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522ff143803Smrg            (exit 1); exit 1
523ff143803Smrg          }
524ff143803Smrg        } &&
525ff143803Smrg
526ff143803Smrg        # Now rename the file to the real destination.
527ff143803Smrg        $doit $mvcmd "$dsttmp" "$dst"
528123e2cc7Smrg      }
529123e2cc7Smrg    fi || exit 1
530123e2cc7Smrg
531123e2cc7Smrg    trap '' 0
532123e2cc7Smrg  fi
5337da8b7e3Smrgdone
5347da8b7e3Smrg
5357da8b7e3Smrg# Local variables:
536e4b60806Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5377da8b7e3Smrg# time-stamp-start: "scriptversion="
5387da8b7e3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539e4b60806Smrg# time-stamp-time-zone: "UTC0"
540123e2cc7Smrg# time-stamp-end: "; # UTC"
5417da8b7e3Smrg# End:
542