195b296d0Smrg#!/bin/sh
295b296d0Smrg# install - install a program, script, or datafile
395b296d0Smrg
4d87a3195Smrgscriptversion=2020-11-14.01; # UTC
595b296d0Smrg
695b296d0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
795b296d0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
895b296d0Smrg# following copyright and license.
995b296d0Smrg#
1095b296d0Smrg# Copyright (C) 1994 X Consortium
1195b296d0Smrg#
1295b296d0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1395b296d0Smrg# of this software and associated documentation files (the "Software"), to
1495b296d0Smrg# deal in the Software without restriction, including without limitation the
1595b296d0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1695b296d0Smrg# sell copies of the Software, and to permit persons to whom the Software is
1795b296d0Smrg# furnished to do so, subject to the following conditions:
1895b296d0Smrg#
1995b296d0Smrg# The above copyright notice and this permission notice shall be included in
2095b296d0Smrg# all copies or substantial portions of the Software.
2195b296d0Smrg#
2295b296d0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2395b296d0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2495b296d0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2595b296d0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2695b296d0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2795b296d0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2895b296d0Smrg#
2995b296d0Smrg# Except as contained in this notice, the name of the X Consortium shall not
3095b296d0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3195b296d0Smrg# ings in this Software without prior written authorization from the X Consor-
3295b296d0Smrg# tium.
3395b296d0Smrg#
3495b296d0Smrg#
3595b296d0Smrg# FSF changes to this file are in the public domain.
3695b296d0Smrg#
3795b296d0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38eca46af7Smrg# 'make' implicit rules from creating a file called install from it
3995b296d0Smrg# when there is no Makefile.
4095b296d0Smrg#
4195b296d0Smrg# This script is compatible with the BSD install script, but was written
42ff89ac2bSmrg# from scratch.
43ff89ac2bSmrg
4489b0dc75Smrgtab='	'
45ff89ac2bSmrgnl='
46ff89ac2bSmrg'
4789b0dc75SmrgIFS=" $tab$nl"
4895b296d0Smrg
4989b0dc75Smrg# Set DOITPROG to "echo" to test this script.
5095b296d0Smrg
51ff89ac2bSmrgdoit=${DOITPROG-}
5289b0dc75Smrgdoit_exec=${doit:-exec}
5395b296d0Smrg
54ff89ac2bSmrg# Put in absolute file names if you don't have them in your path;
55ff89ac2bSmrg# or use environment vars.
56ff89ac2bSmrg
57ff89ac2bSmrgchgrpprog=${CHGRPPROG-chgrp}
58ff89ac2bSmrgchmodprog=${CHMODPROG-chmod}
59ff89ac2bSmrgchownprog=${CHOWNPROG-chown}
60ff89ac2bSmrgcmpprog=${CMPPROG-cmp}
61ff89ac2bSmrgcpprog=${CPPROG-cp}
62ff89ac2bSmrgmkdirprog=${MKDIRPROG-mkdir}
63ff89ac2bSmrgmvprog=${MVPROG-mv}
64ff89ac2bSmrgrmprog=${RMPROG-rm}
65ff89ac2bSmrgstripprog=${STRIPPROG-strip}
66ff89ac2bSmrg
67ff89ac2bSmrgposix_mkdir=
68ff89ac2bSmrg
69ff89ac2bSmrg# Desired mode of installed file.
70ff89ac2bSmrgmode=0755
7195b296d0Smrg
72d87a3195Smrg# Create dirs (including intermediate dirs) using mode 755.
73d87a3195Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74d87a3195Smrgmkdir_umask=22
75d87a3195Smrg
76d87a3195Smrgbackupsuffix=
7795b296d0Smrgchgrpcmd=
78ff89ac2bSmrgchmodcmd=$chmodprog
79ff89ac2bSmrgchowncmd=
80ff89ac2bSmrgmvcmd=$mvprog
8195b296d0Smrgrmcmd="$rmprog -f"
82ff89ac2bSmrgstripcmd=
83ff89ac2bSmrg
8495b296d0Smrgsrc=
8595b296d0Smrgdst=
8695b296d0Smrgdir_arg=
87ff89ac2bSmrgdst_arg=
88ff89ac2bSmrg
89ff89ac2bSmrgcopy_on_change=false
9089b0dc75Smrgis_target_a_directory=possibly
9195b296d0Smrg
92ff89ac2bSmrgusage="\
93ff89ac2bSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9495b296d0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9595b296d0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9695b296d0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9795b296d0Smrg
9895b296d0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9995b296d0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10095b296d0SmrgIn the 4th, create DIRECTORIES.
10195b296d0Smrg
10295b296d0SmrgOptions:
103ff89ac2bSmrg     --help     display this help and exit.
104ff89ac2bSmrg     --version  display version info and exit.
105ff89ac2bSmrg
106ff89ac2bSmrg  -c            (ignored)
107d87a3195Smrg  -C            install only if different (preserve data modification time)
108ff89ac2bSmrg  -d            create directories instead of installing files.
109ff89ac2bSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110ff89ac2bSmrg  -m MODE       $chmodprog installed files to MODE.
111ff89ac2bSmrg  -o USER       $chownprog installed files to USER.
112d87a3195Smrg  -p            pass -p to $cpprog.
113ff89ac2bSmrg  -s            $stripprog installed files.
114d87a3195Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115ff89ac2bSmrg  -t DIRECTORY  install into DIRECTORY.
116ff89ac2bSmrg  -T            report an error if DSTFILE is a directory.
11795b296d0Smrg
11895b296d0SmrgEnvironment variables override the default commands:
119ff89ac2bSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120ff89ac2bSmrg  RMPROG STRIPPROG
121d87a3195Smrg
122d87a3195SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123d87a3195Smrgit's up to you to specify -f if you want it.
124d87a3195Smrg
125d87a3195SmrgIf -S is not specified, no backups are attempted.
126d87a3195Smrg
127d87a3195SmrgEmail bug reports to bug-automake@gnu.org.
128d87a3195SmrgAutomake home page: https://www.gnu.org/software/automake/
12995b296d0Smrg"
13095b296d0Smrg
131ff89ac2bSmrgwhile test $# -ne 0; do
13295b296d0Smrg  case $1 in
133ff89ac2bSmrg    -c) ;;
134ff89ac2bSmrg
135ff89ac2bSmrg    -C) copy_on_change=true;;
13695b296d0Smrg
137ff89ac2bSmrg    -d) dir_arg=true;;
13895b296d0Smrg
13995b296d0Smrg    -g) chgrpcmd="$chgrpprog $2"
14089b0dc75Smrg        shift;;
14195b296d0Smrg
14295b296d0Smrg    --help) echo "$usage"; exit $?;;
14395b296d0Smrg
144ff89ac2bSmrg    -m) mode=$2
14589b0dc75Smrg        case $mode in
14689b0dc75Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14789b0dc75Smrg            echo "$0: invalid mode: $mode" >&2
14889b0dc75Smrg            exit 1;;
14989b0dc75Smrg        esac
15089b0dc75Smrg        shift;;
15195b296d0Smrg
15295b296d0Smrg    -o) chowncmd="$chownprog $2"
15389b0dc75Smrg        shift;;
15495b296d0Smrg
155d87a3195Smrg    -p) cpprog="$cpprog -p";;
156d87a3195Smrg
157ff89ac2bSmrg    -s) stripcmd=$stripprog;;
15895b296d0Smrg
159d87a3195Smrg    -S) backupsuffix="$2"
160d87a3195Smrg        shift;;
161d87a3195Smrg
16289b0dc75Smrg    -t)
16389b0dc75Smrg        is_target_a_directory=always
16489b0dc75Smrg        dst_arg=$2
16589b0dc75Smrg        # Protect names problematic for 'test' and other utilities.
16689b0dc75Smrg        case $dst_arg in
16789b0dc75Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16889b0dc75Smrg        esac
16989b0dc75Smrg        shift;;
17095b296d0Smrg
17189b0dc75Smrg    -T) is_target_a_directory=never;;
17295b296d0Smrg
17395b296d0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17495b296d0Smrg
17589b0dc75Smrg    --) shift
17689b0dc75Smrg        break;;
177ff89ac2bSmrg
17889b0dc75Smrg    -*) echo "$0: invalid option: $1" >&2
17989b0dc75Smrg        exit 1;;
180ff89ac2bSmrg
181ff89ac2bSmrg    *)  break;;
18295b296d0Smrg  esac
183ff89ac2bSmrg  shift
18495b296d0Smrgdone
18595b296d0Smrg
18689b0dc75Smrg# We allow the use of options -d and -T together, by making -d
18789b0dc75Smrg# take the precedence; this is for compatibility with GNU install.
18889b0dc75Smrg
18989b0dc75Smrgif test -n "$dir_arg"; then
19089b0dc75Smrg  if test -n "$dst_arg"; then
19189b0dc75Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19289b0dc75Smrg    exit 1
19389b0dc75Smrg  fi
19489b0dc75Smrgfi
19589b0dc75Smrg
196ff89ac2bSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197ff89ac2bSmrg  # When -d is used, all remaining arguments are directories to create.
198ff89ac2bSmrg  # When -t is used, the destination is already specified.
199ff89ac2bSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200ff89ac2bSmrg  for arg
201ff89ac2bSmrg  do
202ff89ac2bSmrg    if test -n "$dst_arg"; then
203ff89ac2bSmrg      # $@ is not empty: it contains at least $arg.
204ff89ac2bSmrg      set fnord "$@" "$dst_arg"
205ff89ac2bSmrg      shift # fnord
206ff89ac2bSmrg    fi
207ff89ac2bSmrg    shift # arg
208ff89ac2bSmrg    dst_arg=$arg
209eca46af7Smrg    # Protect names problematic for 'test' and other utilities.
210eca46af7Smrg    case $dst_arg in
211eca46af7Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212eca46af7Smrg    esac
213ff89ac2bSmrg  done
214ff89ac2bSmrgfi
215ff89ac2bSmrg
216ff89ac2bSmrgif test $# -eq 0; then
21795b296d0Smrg  if test -z "$dir_arg"; then
21895b296d0Smrg    echo "$0: no input file specified." >&2
21995b296d0Smrg    exit 1
22095b296d0Smrg  fi
221eca46af7Smrg  # It's OK to call 'install-sh -d' without argument.
22295b296d0Smrg  # This can happen when creating conditional directories.
22395b296d0Smrg  exit 0
22495b296d0Smrgfi
22595b296d0Smrg
22689b0dc75Smrgif test -z "$dir_arg"; then
22789b0dc75Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22889b0dc75Smrg    if test ! -d "$dst_arg"; then
22989b0dc75Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23089b0dc75Smrg      exit 1
23189b0dc75Smrg    fi
23289b0dc75Smrg  fi
23389b0dc75Smrgfi
23489b0dc75Smrg
235ff89ac2bSmrgif test -z "$dir_arg"; then
236eca46af7Smrg  do_exit='(exit $ret); exit $ret'
237eca46af7Smrg  trap "ret=129; $do_exit" 1
238eca46af7Smrg  trap "ret=130; $do_exit" 2
239eca46af7Smrg  trap "ret=141; $do_exit" 13
240eca46af7Smrg  trap "ret=143; $do_exit" 15
241ff89ac2bSmrg
242ff89ac2bSmrg  # Set umask so as not to create temps with too-generous modes.
243ff89ac2bSmrg  # However, 'strip' requires both read and write access to temps.
244ff89ac2bSmrg  case $mode in
245ff89ac2bSmrg    # Optimize common cases.
246ff89ac2bSmrg    *644) cp_umask=133;;
247ff89ac2bSmrg    *755) cp_umask=22;;
248ff89ac2bSmrg
249ff89ac2bSmrg    *[0-7])
250ff89ac2bSmrg      if test -z "$stripcmd"; then
25189b0dc75Smrg        u_plus_rw=
252ff89ac2bSmrg      else
25389b0dc75Smrg        u_plus_rw='% 200'
254ff89ac2bSmrg      fi
255ff89ac2bSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256ff89ac2bSmrg    *)
257ff89ac2bSmrg      if test -z "$stripcmd"; then
25889b0dc75Smrg        u_plus_rw=
259ff89ac2bSmrg      else
26089b0dc75Smrg        u_plus_rw=,u+rw
261ff89ac2bSmrg      fi
262ff89ac2bSmrg      cp_umask=$mode$u_plus_rw;;
263ff89ac2bSmrg  esac
264ff89ac2bSmrgfi
265ff89ac2bSmrg
26695b296d0Smrgfor src
26795b296d0Smrgdo
268eca46af7Smrg  # Protect names problematic for 'test' and other utilities.
26995b296d0Smrg  case $src in
270eca46af7Smrg    -* | [=\(\)!]) src=./$src;;
27195b296d0Smrg  esac
27295b296d0Smrg
27395b296d0Smrg  if test -n "$dir_arg"; then
27495b296d0Smrg    dst=$src
275ff89ac2bSmrg    dstdir=$dst
276ff89ac2bSmrg    test -d "$dstdir"
277ff89ac2bSmrg    dstdir_status=$?
278d87a3195Smrg    # Don't chown directories that already exist.
279d87a3195Smrg    if test $dstdir_status = 0; then
280d87a3195Smrg      chowncmd=""
281d87a3195Smrg    fi
28295b296d0Smrg  else
283ff89ac2bSmrg
28495b296d0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28595b296d0Smrg    # might cause directories to be created, which would be especially bad
28695b296d0Smrg    # if $src (and thus $dsttmp) contains '*'.
28795b296d0Smrg    if test ! -f "$src" && test ! -d "$src"; then
28895b296d0Smrg      echo "$0: $src does not exist." >&2
28995b296d0Smrg      exit 1
29095b296d0Smrg    fi
29195b296d0Smrg
292ff89ac2bSmrg    if test -z "$dst_arg"; then
29395b296d0Smrg      echo "$0: no destination specified." >&2
29495b296d0Smrg      exit 1
29595b296d0Smrg    fi
296ff89ac2bSmrg    dst=$dst_arg
29795b296d0Smrg
298d87a3195Smrg    # If destination is a directory, append the input filename.
29995b296d0Smrg    if test -d "$dst"; then
30089b0dc75Smrg      if test "$is_target_a_directory" = never; then
30189b0dc75Smrg        echo "$0: $dst_arg: Is a directory" >&2
30289b0dc75Smrg        exit 1
30395b296d0Smrg      fi
304ff89ac2bSmrg      dstdir=$dst
305d87a3195Smrg      dstbase=`basename "$src"`
306d87a3195Smrg      case $dst in
307d87a3195Smrg	*/) dst=$dst$dstbase;;
308d87a3195Smrg	*)  dst=$dst/$dstbase;;
309d87a3195Smrg      esac
310ff89ac2bSmrg      dstdir_status=0
311ff89ac2bSmrg    else
31289b0dc75Smrg      dstdir=`dirname "$dst"`
313ff89ac2bSmrg      test -d "$dstdir"
314ff89ac2bSmrg      dstdir_status=$?
31595b296d0Smrg    fi
31695b296d0Smrg  fi
31795b296d0Smrg
318d87a3195Smrg  case $dstdir in
319d87a3195Smrg    */) dstdirslash=$dstdir;;
320d87a3195Smrg    *)  dstdirslash=$dstdir/;;
321d87a3195Smrg  esac
322d87a3195Smrg
323ff89ac2bSmrg  obsolete_mkdir_used=false
324ff89ac2bSmrg
325ff89ac2bSmrg  if test $dstdir_status != 0; then
326ff89ac2bSmrg    case $posix_mkdir in
327ff89ac2bSmrg      '')
32889b0dc75Smrg        # With -d, create the new directory with the user-specified mode.
32989b0dc75Smrg        # Otherwise, rely on $mkdir_umask.
33089b0dc75Smrg        if test -n "$dir_arg"; then
33189b0dc75Smrg          mkdir_mode=-m$mode
33289b0dc75Smrg        else
33389b0dc75Smrg          mkdir_mode=
33489b0dc75Smrg        fi
33589b0dc75Smrg
33689b0dc75Smrg        posix_mkdir=false
337d87a3195Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338d87a3195Smrg	# here however when possible just to lower collision chance.
339d87a3195Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340d87a3195Smrg
341d87a3195Smrg	trap '
342d87a3195Smrg	  ret=$?
343d87a3195Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344d87a3195Smrg	  exit $ret
345d87a3195Smrg	' 0
346d87a3195Smrg
347d87a3195Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348d87a3195Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349d87a3195Smrg	# directory is successfully created first before we actually test
350d87a3195Smrg	# 'mkdir -p'.
351d87a3195Smrg	if (umask $mkdir_umask &&
352d87a3195Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353d87a3195Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354d87a3195Smrg	then
355d87a3195Smrg	  if test -z "$dir_arg" || {
356d87a3195Smrg	       # Check for POSIX incompatibilities with -m.
357d87a3195Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358d87a3195Smrg	       # other-writable bit of parent directory when it shouldn't.
359d87a3195Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360d87a3195Smrg	       test_tmpdir="$tmpdir/a"
361d87a3195Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362d87a3195Smrg	       case $ls_ld_tmpdir in
363d87a3195Smrg		 d????-?r-*) different_mode=700;;
364d87a3195Smrg		 d????-?--*) different_mode=755;;
365d87a3195Smrg		 *) false;;
366d87a3195Smrg	       esac &&
367d87a3195Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368d87a3195Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369d87a3195Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370d87a3195Smrg	       }
371d87a3195Smrg	     }
372d87a3195Smrg	  then posix_mkdir=:
373d87a3195Smrg	  fi
374d87a3195Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375d87a3195Smrg	else
376d87a3195Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377d87a3195Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378d87a3195Smrg	fi
379d87a3195Smrg	trap '' 0;;
380ff89ac2bSmrg    esac
38195b296d0Smrg
382ff89ac2bSmrg    if
383ff89ac2bSmrg      $posix_mkdir && (
38489b0dc75Smrg        umask $mkdir_umask &&
38589b0dc75Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386ff89ac2bSmrg      )
387ff89ac2bSmrg    then :
388ff89ac2bSmrg    else
38995b296d0Smrg
390d87a3195Smrg      # mkdir does not conform to POSIX,
391ff89ac2bSmrg      # or it failed possibly due to a race condition.  Create the
392ff89ac2bSmrg      # directory the slow way, step by step, checking for races as we go.
39395b296d0Smrg
394ff89ac2bSmrg      case $dstdir in
39589b0dc75Smrg        /*) prefix='/';;
39689b0dc75Smrg        [-=\(\)!]*) prefix='./';;
39789b0dc75Smrg        *)  prefix='';;
398ff89ac2bSmrg      esac
39995b296d0Smrg
400ff89ac2bSmrg      oIFS=$IFS
401ff89ac2bSmrg      IFS=/
40289b0dc75Smrg      set -f
403ff89ac2bSmrg      set fnord $dstdir
40495b296d0Smrg      shift
40589b0dc75Smrg      set +f
406ff89ac2bSmrg      IFS=$oIFS
407ff89ac2bSmrg
408ff89ac2bSmrg      prefixes=
409ff89ac2bSmrg
410ff89ac2bSmrg      for d
411ff89ac2bSmrg      do
41289b0dc75Smrg        test X"$d" = X && continue
41389b0dc75Smrg
41489b0dc75Smrg        prefix=$prefix$d
41589b0dc75Smrg        if test -d "$prefix"; then
41689b0dc75Smrg          prefixes=
41789b0dc75Smrg        else
41889b0dc75Smrg          if $posix_mkdir; then
419d87a3195Smrg            (umask $mkdir_umask &&
42089b0dc75Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42189b0dc75Smrg            # Don't fail if two instances are running concurrently.
42289b0dc75Smrg            test -d "$prefix" || exit 1
42389b0dc75Smrg          else
42489b0dc75Smrg            case $prefix in
42589b0dc75Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42689b0dc75Smrg              *) qprefix=$prefix;;
42789b0dc75Smrg            esac
42889b0dc75Smrg            prefixes="$prefixes '$qprefix'"
42989b0dc75Smrg          fi
43089b0dc75Smrg        fi
43189b0dc75Smrg        prefix=$prefix/
432ff89ac2bSmrg      done
433ff89ac2bSmrg
434ff89ac2bSmrg      if test -n "$prefixes"; then
43589b0dc75Smrg        # Don't fail if two instances are running concurrently.
43689b0dc75Smrg        (umask $mkdir_umask &&
43789b0dc75Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43889b0dc75Smrg          test -d "$dstdir" || exit 1
43989b0dc75Smrg        obsolete_mkdir_used=true
44095b296d0Smrg      fi
441ff89ac2bSmrg    fi
44295b296d0Smrg  fi
44395b296d0Smrg
44495b296d0Smrg  if test -n "$dir_arg"; then
445ff89ac2bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446ff89ac2bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447ff89ac2bSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448ff89ac2bSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44995b296d0Smrg  else
45095b296d0Smrg
45195b296d0Smrg    # Make a couple of temp file names in the proper directory.
452d87a3195Smrg    dsttmp=${dstdirslash}_inst.$$_
453d87a3195Smrg    rmtmp=${dstdirslash}_rm.$$_
45495b296d0Smrg
45595b296d0Smrg    # Trap to clean up those temp files at exit.
45695b296d0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45795b296d0Smrg
45895b296d0Smrg    # Copy the file name to the temp name.
459d87a3195Smrg    (umask $cp_umask &&
460d87a3195Smrg     { test -z "$stripcmd" || {
461d87a3195Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462d87a3195Smrg	 # which would cause strip to fail.
463d87a3195Smrg	 if test -z "$doit"; then
464d87a3195Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465d87a3195Smrg	 else
466d87a3195Smrg	   $doit touch "$dsttmp"
467d87a3195Smrg	 fi
468d87a3195Smrg       }
469d87a3195Smrg     } &&
470d87a3195Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47195b296d0Smrg
47295b296d0Smrg    # and set any options; do chmod last to preserve setuid bits.
47395b296d0Smrg    #
47495b296d0Smrg    # If any of these fail, we abort the whole thing.  If we want to
47595b296d0Smrg    # ignore errors from any of these, just make sure not to ignore
47695b296d0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47795b296d0Smrg    #
478ff89ac2bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479ff89ac2bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480ff89ac2bSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481ff89ac2bSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482ff89ac2bSmrg
483ff89ac2bSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484ff89ac2bSmrg    if $copy_on_change &&
48589b0dc75Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48689b0dc75Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48789b0dc75Smrg       set -f &&
488ff89ac2bSmrg       set X $old && old=:$2:$4:$5:$6 &&
489ff89ac2bSmrg       set X $new && new=:$2:$4:$5:$6 &&
49089b0dc75Smrg       set +f &&
491ff89ac2bSmrg       test "$old" = "$new" &&
492ff89ac2bSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493ff89ac2bSmrg    then
494ff89ac2bSmrg      rm -f "$dsttmp"
495ff89ac2bSmrg    else
496d87a3195Smrg      # If $backupsuffix is set, and the file being installed
497d87a3195Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498d87a3195Smrg      # e.g., if mv doesn't support -f.
499d87a3195Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500d87a3195Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501d87a3195Smrg      fi
502d87a3195Smrg
503ff89ac2bSmrg      # Rename the file to the real destination.
504ff89ac2bSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505ff89ac2bSmrg
506ff89ac2bSmrg      # The rename failed, perhaps because mv can't rename something else
507ff89ac2bSmrg      # to itself, or perhaps because mv is so ancient that it does not
508ff89ac2bSmrg      # support -f.
509ff89ac2bSmrg      {
51089b0dc75Smrg        # Now remove or move aside any old file at destination location.
51189b0dc75Smrg        # We try this two ways since rm can't unlink itself on some
51289b0dc75Smrg        # systems and the destination file might be busy for other
51389b0dc75Smrg        # reasons.  In this case, the final cleanup might fail but the new
51489b0dc75Smrg        # file should still install successfully.
51589b0dc75Smrg        {
51689b0dc75Smrg          test ! -f "$dst" ||
517d87a3195Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51889b0dc75Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519d87a3195Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52089b0dc75Smrg          } ||
52189b0dc75Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52289b0dc75Smrg            (exit 1); exit 1
52389b0dc75Smrg          }
52489b0dc75Smrg        } &&
52589b0dc75Smrg
52689b0dc75Smrg        # Now rename the file to the real destination.
52789b0dc75Smrg        $doit $mvcmd "$dsttmp" "$dst"
528ff89ac2bSmrg      }
529ff89ac2bSmrg    fi || exit 1
530ff89ac2bSmrg
531ff89ac2bSmrg    trap '' 0
532ff89ac2bSmrg  fi
53395b296d0Smrgdone
53495b296d0Smrg
53595b296d0Smrg# Local variables:
536d87a3195Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53795b296d0Smrg# time-stamp-start: "scriptversion="
53895b296d0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539d87a3195Smrg# time-stamp-time-zone: "UTC0"
54014330f12Smrg# time-stamp-end: "; # UTC"
54195b296d0Smrg# End:
542