127702724Smrg#!/bin/sh
227702724Smrg# install - install a program, script, or datafile
327702724Smrg
4ae545d91Smrgscriptversion=2024-06-19.01; # UTC
527702724Smrg
627702724Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
727702724Smrg# later released in X11R6 (xc/config/util/install.sh) with the
827702724Smrg# following copyright and license.
927702724Smrg#
1027702724Smrg# Copyright (C) 1994 X Consortium
1127702724Smrg#
1227702724Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1327702724Smrg# of this software and associated documentation files (the "Software"), to
1427702724Smrg# deal in the Software without restriction, including without limitation the
1527702724Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1627702724Smrg# sell copies of the Software, and to permit persons to whom the Software is
1727702724Smrg# furnished to do so, subject to the following conditions:
1827702724Smrg#
1927702724Smrg# The above copyright notice and this permission notice shall be included in
2027702724Smrg# all copies or substantial portions of the Software.
2127702724Smrg#
2227702724Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2327702724Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2427702724Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2527702724Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2627702724Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2727702724Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2827702724Smrg#
2927702724Smrg# Except as contained in this notice, the name of the X Consortium shall not
3027702724Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3127702724Smrg# ings in this Software without prior written authorization from the X Consor-
3227702724Smrg# tium.
3327702724Smrg#
3427702724Smrg#
3527702724Smrg# FSF changes to this file are in the public domain.
3627702724Smrg#
3727702724Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38313a12fdSmrg# 'make' implicit rules from creating a file called install from it
3927702724Smrg# when there is no Makefile.
4027702724Smrg#
4127702724Smrg# This script is compatible with the BSD install script, but was written
42e19dfac4Smrg# from scratch.
43e19dfac4Smrg
447cea3689Smrgtab='	'
45e19dfac4Smrgnl='
46e19dfac4Smrg'
477cea3689SmrgIFS=" $tab$nl"
4827702724Smrg
497cea3689Smrg# Set DOITPROG to "echo" to test this script.
5027702724Smrg
51e19dfac4Smrgdoit=${DOITPROG-}
527cea3689Smrgdoit_exec=${doit:-exec}
5327702724Smrg
54e19dfac4Smrg# Put in absolute file names if you don't have them in your path;
55e19dfac4Smrg# or use environment vars.
56e19dfac4Smrg
57e19dfac4Smrgchgrpprog=${CHGRPPROG-chgrp}
58e19dfac4Smrgchmodprog=${CHMODPROG-chmod}
59e19dfac4Smrgchownprog=${CHOWNPROG-chown}
60e19dfac4Smrgcmpprog=${CMPPROG-cmp}
61e19dfac4Smrgcpprog=${CPPROG-cp}
62e19dfac4Smrgmkdirprog=${MKDIRPROG-mkdir}
63e19dfac4Smrgmvprog=${MVPROG-mv}
64e19dfac4Smrgrmprog=${RMPROG-rm}
65e19dfac4Smrgstripprog=${STRIPPROG-strip}
66e19dfac4Smrg
67e19dfac4Smrgposix_mkdir=
68e19dfac4Smrg
69e19dfac4Smrg# Desired mode of installed file.
70e19dfac4Smrgmode=0755
7127702724Smrg
7295b7a5c8Smrg# Create dirs (including intermediate dirs) using mode 755.
7395b7a5c8Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7495b7a5c8Smrgmkdir_umask=22
7595b7a5c8Smrg
7695b7a5c8Smrgbackupsuffix=
7727702724Smrgchgrpcmd=
78e19dfac4Smrgchmodcmd=$chmodprog
79e19dfac4Smrgchowncmd=
80e19dfac4Smrgmvcmd=$mvprog
8127702724Smrgrmcmd="$rmprog -f"
82e19dfac4Smrgstripcmd=
83e19dfac4Smrg
8427702724Smrgsrc=
8527702724Smrgdst=
8627702724Smrgdir_arg=
87e19dfac4Smrgdst_arg=
88e19dfac4Smrg
89e19dfac4Smrgcopy_on_change=false
907cea3689Smrgis_target_a_directory=possibly
9127702724Smrg
92e19dfac4Smrgusage="\
93e19dfac4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9427702724Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9527702724Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9627702724Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9727702724Smrg
9827702724SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9927702724SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10027702724SmrgIn the 4th, create DIRECTORIES.
10127702724Smrg
10227702724SmrgOptions:
103e19dfac4Smrg     --help     display this help and exit.
104e19dfac4Smrg     --version  display version info and exit.
105e19dfac4Smrg
106e19dfac4Smrg  -c            (ignored)
10795b7a5c8Smrg  -C            install only if different (preserve data modification time)
108e19dfac4Smrg  -d            create directories instead of installing files.
109e19dfac4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110e19dfac4Smrg  -m MODE       $chmodprog installed files to MODE.
111e19dfac4Smrg  -o USER       $chownprog installed files to USER.
11295b7a5c8Smrg  -p            pass -p to $cpprog.
113e19dfac4Smrg  -s            $stripprog installed files.
11495b7a5c8Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115e19dfac4Smrg  -t DIRECTORY  install into DIRECTORY.
116e19dfac4Smrg  -T            report an error if DSTFILE is a directory.
11727702724Smrg
11827702724SmrgEnvironment variables override the default commands:
119e19dfac4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120e19dfac4Smrg  RMPROG STRIPPROG
12195b7a5c8Smrg
12295b7a5c8SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12395b7a5c8Smrgit's up to you to specify -f if you want it.
12495b7a5c8Smrg
12595b7a5c8SmrgIf -S is not specified, no backups are attempted.
12695b7a5c8Smrg
127ae545d91SmrgReport bugs to <bug-automake@gnu.org>.
128ae545d91SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
129ae545d91SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
13027702724Smrg
131e19dfac4Smrgwhile test $# -ne 0; do
13227702724Smrg  case $1 in
133e19dfac4Smrg    -c) ;;
134e19dfac4Smrg
135e19dfac4Smrg    -C) copy_on_change=true;;
13627702724Smrg
137e19dfac4Smrg    -d) dir_arg=true;;
13827702724Smrg
13927702724Smrg    -g) chgrpcmd="$chgrpprog $2"
1407cea3689Smrg        shift;;
14127702724Smrg
14227702724Smrg    --help) echo "$usage"; exit $?;;
14327702724Smrg
144e19dfac4Smrg    -m) mode=$2
1457cea3689Smrg        case $mode in
1467cea3689Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1477cea3689Smrg            echo "$0: invalid mode: $mode" >&2
1487cea3689Smrg            exit 1;;
1497cea3689Smrg        esac
1507cea3689Smrg        shift;;
15127702724Smrg
15227702724Smrg    -o) chowncmd="$chownprog $2"
1537cea3689Smrg        shift;;
15427702724Smrg
15595b7a5c8Smrg    -p) cpprog="$cpprog -p";;
15695b7a5c8Smrg
157e19dfac4Smrg    -s) stripcmd=$stripprog;;
15827702724Smrg
15995b7a5c8Smrg    -S) backupsuffix="$2"
16095b7a5c8Smrg        shift;;
16195b7a5c8Smrg
1627cea3689Smrg    -t)
1637cea3689Smrg        is_target_a_directory=always
1647cea3689Smrg        dst_arg=$2
1657cea3689Smrg        # Protect names problematic for 'test' and other utilities.
1667cea3689Smrg        case $dst_arg in
1677cea3689Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1687cea3689Smrg        esac
1697cea3689Smrg        shift;;
17027702724Smrg
1717cea3689Smrg    -T) is_target_a_directory=never;;
17227702724Smrg
173ae545d91Smrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
17427702724Smrg
1757cea3689Smrg    --) shift
1767cea3689Smrg        break;;
177e19dfac4Smrg
1787cea3689Smrg    -*) echo "$0: invalid option: $1" >&2
1797cea3689Smrg        exit 1;;
180e19dfac4Smrg
181e19dfac4Smrg    *)  break;;
18227702724Smrg  esac
183e19dfac4Smrg  shift
18427702724Smrgdone
18527702724Smrg
1867cea3689Smrg# We allow the use of options -d and -T together, by making -d
1877cea3689Smrg# take the precedence; this is for compatibility with GNU install.
1887cea3689Smrg
1897cea3689Smrgif test -n "$dir_arg"; then
1907cea3689Smrg  if test -n "$dst_arg"; then
1917cea3689Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1927cea3689Smrg    exit 1
1937cea3689Smrg  fi
1947cea3689Smrgfi
1957cea3689Smrg
196e19dfac4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197e19dfac4Smrg  # When -d is used, all remaining arguments are directories to create.
198e19dfac4Smrg  # When -t is used, the destination is already specified.
199e19dfac4Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200e19dfac4Smrg  for arg
201e19dfac4Smrg  do
202e19dfac4Smrg    if test -n "$dst_arg"; then
203e19dfac4Smrg      # $@ is not empty: it contains at least $arg.
204e19dfac4Smrg      set fnord "$@" "$dst_arg"
205e19dfac4Smrg      shift # fnord
206e19dfac4Smrg    fi
207e19dfac4Smrg    shift # arg
208e19dfac4Smrg    dst_arg=$arg
209313a12fdSmrg    # Protect names problematic for 'test' and other utilities.
210313a12fdSmrg    case $dst_arg in
211313a12fdSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212313a12fdSmrg    esac
213e19dfac4Smrg  done
214e19dfac4Smrgfi
215e19dfac4Smrg
216e19dfac4Smrgif test $# -eq 0; then
21727702724Smrg  if test -z "$dir_arg"; then
21827702724Smrg    echo "$0: no input file specified." >&2
21927702724Smrg    exit 1
22027702724Smrg  fi
221313a12fdSmrg  # It's OK to call 'install-sh -d' without argument.
22227702724Smrg  # This can happen when creating conditional directories.
22327702724Smrg  exit 0
22427702724Smrgfi
22527702724Smrg
2267cea3689Smrgif test -z "$dir_arg"; then
2277cea3689Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2287cea3689Smrg    if test ! -d "$dst_arg"; then
2297cea3689Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2307cea3689Smrg      exit 1
2317cea3689Smrg    fi
2327cea3689Smrg  fi
2337cea3689Smrgfi
2347cea3689Smrg
235e19dfac4Smrgif test -z "$dir_arg"; then
236313a12fdSmrg  do_exit='(exit $ret); exit $ret'
237313a12fdSmrg  trap "ret=129; $do_exit" 1
238313a12fdSmrg  trap "ret=130; $do_exit" 2
239313a12fdSmrg  trap "ret=141; $do_exit" 13
240313a12fdSmrg  trap "ret=143; $do_exit" 15
241e19dfac4Smrg
242e19dfac4Smrg  # Set umask so as not to create temps with too-generous modes.
243e19dfac4Smrg  # However, 'strip' requires both read and write access to temps.
244e19dfac4Smrg  case $mode in
245e19dfac4Smrg    # Optimize common cases.
246e19dfac4Smrg    *644) cp_umask=133;;
247e19dfac4Smrg    *755) cp_umask=22;;
248e19dfac4Smrg
249e19dfac4Smrg    *[0-7])
250e19dfac4Smrg      if test -z "$stripcmd"; then
2517cea3689Smrg        u_plus_rw=
252e19dfac4Smrg      else
2537cea3689Smrg        u_plus_rw='% 200'
254e19dfac4Smrg      fi
255e19dfac4Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256e19dfac4Smrg    *)
257e19dfac4Smrg      if test -z "$stripcmd"; then
2587cea3689Smrg        u_plus_rw=
259e19dfac4Smrg      else
2607cea3689Smrg        u_plus_rw=,u+rw
261e19dfac4Smrg      fi
262e19dfac4Smrg      cp_umask=$mode$u_plus_rw;;
263e19dfac4Smrg  esac
264e19dfac4Smrgfi
265e19dfac4Smrg
26627702724Smrgfor src
26727702724Smrgdo
268313a12fdSmrg  # Protect names problematic for 'test' and other utilities.
26927702724Smrg  case $src in
270313a12fdSmrg    -* | [=\(\)!]) src=./$src;;
27127702724Smrg  esac
27227702724Smrg
27327702724Smrg  if test -n "$dir_arg"; then
27427702724Smrg    dst=$src
275e19dfac4Smrg    dstdir=$dst
276e19dfac4Smrg    test -d "$dstdir"
277e19dfac4Smrg    dstdir_status=$?
27895b7a5c8Smrg    # Don't chown directories that already exist.
27995b7a5c8Smrg    if test $dstdir_status = 0; then
28095b7a5c8Smrg      chowncmd=""
28195b7a5c8Smrg    fi
28227702724Smrg  else
283e19dfac4Smrg
28427702724Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28527702724Smrg    # might cause directories to be created, which would be especially bad
28627702724Smrg    # if $src (and thus $dsttmp) contains '*'.
28727702724Smrg    if test ! -f "$src" && test ! -d "$src"; then
28827702724Smrg      echo "$0: $src does not exist." >&2
28927702724Smrg      exit 1
29027702724Smrg    fi
29127702724Smrg
292e19dfac4Smrg    if test -z "$dst_arg"; then
29327702724Smrg      echo "$0: no destination specified." >&2
29427702724Smrg      exit 1
29527702724Smrg    fi
296e19dfac4Smrg    dst=$dst_arg
29727702724Smrg
29895b7a5c8Smrg    # If destination is a directory, append the input filename.
29927702724Smrg    if test -d "$dst"; then
3007cea3689Smrg      if test "$is_target_a_directory" = never; then
3017cea3689Smrg        echo "$0: $dst_arg: Is a directory" >&2
3027cea3689Smrg        exit 1
30327702724Smrg      fi
304e19dfac4Smrg      dstdir=$dst
30595b7a5c8Smrg      dstbase=`basename "$src"`
30695b7a5c8Smrg      case $dst in
30795b7a5c8Smrg	*/) dst=$dst$dstbase;;
30895b7a5c8Smrg	*)  dst=$dst/$dstbase;;
30995b7a5c8Smrg      esac
310e19dfac4Smrg      dstdir_status=0
311e19dfac4Smrg    else
3127cea3689Smrg      dstdir=`dirname "$dst"`
313e19dfac4Smrg      test -d "$dstdir"
314e19dfac4Smrg      dstdir_status=$?
31527702724Smrg    fi
31627702724Smrg  fi
31727702724Smrg
31895b7a5c8Smrg  case $dstdir in
31995b7a5c8Smrg    */) dstdirslash=$dstdir;;
32095b7a5c8Smrg    *)  dstdirslash=$dstdir/;;
32195b7a5c8Smrg  esac
32295b7a5c8Smrg
323e19dfac4Smrg  obsolete_mkdir_used=false
324e19dfac4Smrg
325e19dfac4Smrg  if test $dstdir_status != 0; then
326e19dfac4Smrg    case $posix_mkdir in
327e19dfac4Smrg      '')
3287cea3689Smrg        # With -d, create the new directory with the user-specified mode.
3297cea3689Smrg        # Otherwise, rely on $mkdir_umask.
3307cea3689Smrg        if test -n "$dir_arg"; then
3317cea3689Smrg          mkdir_mode=-m$mode
3327cea3689Smrg        else
3337cea3689Smrg          mkdir_mode=
3347cea3689Smrg        fi
3357cea3689Smrg
3367cea3689Smrg        posix_mkdir=false
33795b7a5c8Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33895b7a5c8Smrg	# here however when possible just to lower collision chance.
33995b7a5c8Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34095b7a5c8Smrg
34195b7a5c8Smrg	trap '
34295b7a5c8Smrg	  ret=$?
34395b7a5c8Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34495b7a5c8Smrg	  exit $ret
34595b7a5c8Smrg	' 0
34695b7a5c8Smrg
34795b7a5c8Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348ae545d91Smrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
34995b7a5c8Smrg	# directory is successfully created first before we actually test
35095b7a5c8Smrg	# 'mkdir -p'.
35195b7a5c8Smrg	if (umask $mkdir_umask &&
35295b7a5c8Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35395b7a5c8Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35495b7a5c8Smrg	then
35595b7a5c8Smrg	  if test -z "$dir_arg" || {
356ae545d91Smrg	       # Check for POSIX incompatibility with -m.
35795b7a5c8Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35895b7a5c8Smrg	       # other-writable bit of parent directory when it shouldn't.
35995b7a5c8Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36095b7a5c8Smrg	       test_tmpdir="$tmpdir/a"
36195b7a5c8Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36295b7a5c8Smrg	       case $ls_ld_tmpdir in
36395b7a5c8Smrg		 d????-?r-*) different_mode=700;;
36495b7a5c8Smrg		 d????-?--*) different_mode=755;;
36595b7a5c8Smrg		 *) false;;
36695b7a5c8Smrg	       esac &&
36795b7a5c8Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36895b7a5c8Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36995b7a5c8Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37095b7a5c8Smrg	       }
37195b7a5c8Smrg	     }
37295b7a5c8Smrg	  then posix_mkdir=:
37395b7a5c8Smrg	  fi
37495b7a5c8Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37595b7a5c8Smrg	else
37695b7a5c8Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37795b7a5c8Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37895b7a5c8Smrg	fi
37995b7a5c8Smrg	trap '' 0;;
380e19dfac4Smrg    esac
38127702724Smrg
382e19dfac4Smrg    if
383e19dfac4Smrg      $posix_mkdir && (
3847cea3689Smrg        umask $mkdir_umask &&
3857cea3689Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386e19dfac4Smrg      )
387e19dfac4Smrg    then :
388e19dfac4Smrg    else
38927702724Smrg
39095b7a5c8Smrg      # mkdir does not conform to POSIX,
391e19dfac4Smrg      # or it failed possibly due to a race condition.  Create the
392e19dfac4Smrg      # directory the slow way, step by step, checking for races as we go.
39327702724Smrg
394e19dfac4Smrg      case $dstdir in
3957cea3689Smrg        /*) prefix='/';;
3967cea3689Smrg        [-=\(\)!]*) prefix='./';;
3977cea3689Smrg        *)  prefix='';;
398e19dfac4Smrg      esac
39927702724Smrg
400e19dfac4Smrg      oIFS=$IFS
401e19dfac4Smrg      IFS=/
4027cea3689Smrg      set -f
403e19dfac4Smrg      set fnord $dstdir
40427702724Smrg      shift
4057cea3689Smrg      set +f
406e19dfac4Smrg      IFS=$oIFS
407e19dfac4Smrg
408e19dfac4Smrg      prefixes=
409e19dfac4Smrg
410e19dfac4Smrg      for d
411e19dfac4Smrg      do
4127cea3689Smrg        test X"$d" = X && continue
4137cea3689Smrg
4147cea3689Smrg        prefix=$prefix$d
4157cea3689Smrg        if test -d "$prefix"; then
4167cea3689Smrg          prefixes=
4177cea3689Smrg        else
4187cea3689Smrg          if $posix_mkdir; then
41995b7a5c8Smrg            (umask $mkdir_umask &&
4207cea3689Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4217cea3689Smrg            # Don't fail if two instances are running concurrently.
4227cea3689Smrg            test -d "$prefix" || exit 1
4237cea3689Smrg          else
4247cea3689Smrg            case $prefix in
4257cea3689Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4267cea3689Smrg              *) qprefix=$prefix;;
4277cea3689Smrg            esac
4287cea3689Smrg            prefixes="$prefixes '$qprefix'"
4297cea3689Smrg          fi
4307cea3689Smrg        fi
4317cea3689Smrg        prefix=$prefix/
432e19dfac4Smrg      done
433e19dfac4Smrg
434e19dfac4Smrg      if test -n "$prefixes"; then
4357cea3689Smrg        # Don't fail if two instances are running concurrently.
4367cea3689Smrg        (umask $mkdir_umask &&
4377cea3689Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4387cea3689Smrg          test -d "$dstdir" || exit 1
4397cea3689Smrg        obsolete_mkdir_used=true
44027702724Smrg      fi
441e19dfac4Smrg    fi
44227702724Smrg  fi
44327702724Smrg
44427702724Smrg  if test -n "$dir_arg"; then
445e19dfac4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446e19dfac4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447e19dfac4Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448e19dfac4Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44927702724Smrg  else
45027702724Smrg
45127702724Smrg    # Make a couple of temp file names in the proper directory.
45295b7a5c8Smrg    dsttmp=${dstdirslash}_inst.$$_
45395b7a5c8Smrg    rmtmp=${dstdirslash}_rm.$$_
45427702724Smrg
45527702724Smrg    # Trap to clean up those temp files at exit.
45627702724Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45727702724Smrg
45827702724Smrg    # Copy the file name to the temp name.
45995b7a5c8Smrg    (umask $cp_umask &&
46095b7a5c8Smrg     { test -z "$stripcmd" || {
46195b7a5c8Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46295b7a5c8Smrg	 # which would cause strip to fail.
46395b7a5c8Smrg	 if test -z "$doit"; then
46495b7a5c8Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46595b7a5c8Smrg	 else
46695b7a5c8Smrg	   $doit touch "$dsttmp"
46795b7a5c8Smrg	 fi
46895b7a5c8Smrg       }
46995b7a5c8Smrg     } &&
47095b7a5c8Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47127702724Smrg
47227702724Smrg    # and set any options; do chmod last to preserve setuid bits.
47327702724Smrg    #
47427702724Smrg    # If any of these fail, we abort the whole thing.  If we want to
47527702724Smrg    # ignore errors from any of these, just make sure not to ignore
47627702724Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47727702724Smrg    #
478e19dfac4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479e19dfac4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480e19dfac4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481e19dfac4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482e19dfac4Smrg
483e19dfac4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484e19dfac4Smrg    if $copy_on_change &&
4857cea3689Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4867cea3689Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4877cea3689Smrg       set -f &&
488e19dfac4Smrg       set X $old && old=:$2:$4:$5:$6 &&
489e19dfac4Smrg       set X $new && new=:$2:$4:$5:$6 &&
4907cea3689Smrg       set +f &&
491e19dfac4Smrg       test "$old" = "$new" &&
492e19dfac4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493e19dfac4Smrg    then
494e19dfac4Smrg      rm -f "$dsttmp"
495e19dfac4Smrg    else
49695b7a5c8Smrg      # If $backupsuffix is set, and the file being installed
49795b7a5c8Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49895b7a5c8Smrg      # e.g., if mv doesn't support -f.
49995b7a5c8Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50095b7a5c8Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50195b7a5c8Smrg      fi
50295b7a5c8Smrg
503e19dfac4Smrg      # Rename the file to the real destination.
504e19dfac4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505e19dfac4Smrg
506e19dfac4Smrg      # The rename failed, perhaps because mv can't rename something else
507e19dfac4Smrg      # to itself, or perhaps because mv is so ancient that it does not
508e19dfac4Smrg      # support -f.
509e19dfac4Smrg      {
5107cea3689Smrg        # Now remove or move aside any old file at destination location.
5117cea3689Smrg        # We try this two ways since rm can't unlink itself on some
5127cea3689Smrg        # systems and the destination file might be busy for other
5137cea3689Smrg        # reasons.  In this case, the final cleanup might fail but the new
5147cea3689Smrg        # file should still install successfully.
5157cea3689Smrg        {
5167cea3689Smrg          test ! -f "$dst" ||
51795b7a5c8Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5187cea3689Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51995b7a5c8Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5207cea3689Smrg          } ||
5217cea3689Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5227cea3689Smrg            (exit 1); exit 1
5237cea3689Smrg          }
5247cea3689Smrg        } &&
5257cea3689Smrg
5267cea3689Smrg        # Now rename the file to the real destination.
5277cea3689Smrg        $doit $mvcmd "$dsttmp" "$dst"
528e19dfac4Smrg      }
529e19dfac4Smrg    fi || exit 1
530e19dfac4Smrg
531e19dfac4Smrg    trap '' 0
532e19dfac4Smrg  fi
53327702724Smrgdone
53427702724Smrg
53527702724Smrg# Local variables:
53695b7a5c8Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53727702724Smrg# time-stamp-start: "scriptversion="
53827702724Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53995b7a5c8Smrg# time-stamp-time-zone: "UTC0"
54000084f2cSmrg# time-stamp-end: "; # UTC"
54127702724Smrg# End:
542