19fe995a9Smrg#!/bin/sh
29fe995a9Smrg# install - install a program, script, or datafile
39fe995a9Smrg
4c048b52eSmrgscriptversion=2020-11-14.01; # UTC
59fe995a9Smrg
69fe995a9Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79fe995a9Smrg# later released in X11R6 (xc/config/util/install.sh) with the
89fe995a9Smrg# following copyright and license.
99fe995a9Smrg#
109fe995a9Smrg# Copyright (C) 1994 X Consortium
119fe995a9Smrg#
129fe995a9Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139fe995a9Smrg# of this software and associated documentation files (the "Software"), to
149fe995a9Smrg# deal in the Software without restriction, including without limitation the
159fe995a9Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169fe995a9Smrg# sell copies of the Software, and to permit persons to whom the Software is
179fe995a9Smrg# furnished to do so, subject to the following conditions:
189fe995a9Smrg#
199fe995a9Smrg# The above copyright notice and this permission notice shall be included in
209fe995a9Smrg# all copies or substantial portions of the Software.
219fe995a9Smrg#
229fe995a9Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239fe995a9Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249fe995a9Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259fe995a9Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269fe995a9Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279fe995a9Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289fe995a9Smrg#
299fe995a9Smrg# Except as contained in this notice, the name of the X Consortium shall not
309fe995a9Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
319fe995a9Smrg# ings in this Software without prior written authorization from the X Consor-
329fe995a9Smrg# tium.
339fe995a9Smrg#
349fe995a9Smrg#
359fe995a9Smrg# FSF changes to this file are in the public domain.
369fe995a9Smrg#
379fe995a9Smrg# Calling this script install-sh is preferred over install.sh, to prevent
389d794632Smrg# 'make' implicit rules from creating a file called install from it
399fe995a9Smrg# when there is no Makefile.
409fe995a9Smrg#
419fe995a9Smrg# This script is compatible with the BSD install script, but was written
429fe995a9Smrg# from scratch.
439fe995a9Smrg
44e8ac26b0Smrgtab='	'
459fe995a9Smrgnl='
469fe995a9Smrg'
47e8ac26b0SmrgIFS=" $tab$nl"
489fe995a9Smrg
49e8ac26b0Smrg# Set DOITPROG to "echo" to test this script.
509fe995a9Smrg
51b62cc08cSmrgdoit=${DOITPROG-}
52e8ac26b0Smrgdoit_exec=${doit:-exec}
539fe995a9Smrg
549fe995a9Smrg# Put in absolute file names if you don't have them in your path;
559fe995a9Smrg# or use environment vars.
569fe995a9Smrg
57b62cc08cSmrgchgrpprog=${CHGRPPROG-chgrp}
58b62cc08cSmrgchmodprog=${CHMODPROG-chmod}
59b62cc08cSmrgchownprog=${CHOWNPROG-chown}
60b62cc08cSmrgcmpprog=${CMPPROG-cmp}
61b62cc08cSmrgcpprog=${CPPROG-cp}
62b62cc08cSmrgmkdirprog=${MKDIRPROG-mkdir}
63b62cc08cSmrgmvprog=${MVPROG-mv}
64b62cc08cSmrgrmprog=${RMPROG-rm}
65b62cc08cSmrgstripprog=${STRIPPROG-strip}
66b62cc08cSmrg
679fe995a9Smrgposix_mkdir=
689fe995a9Smrg
699fe995a9Smrg# Desired mode of installed file.
709fe995a9Smrgmode=0755
719fe995a9Smrg
72c048b52eSmrg# Create dirs (including intermediate dirs) using mode 755.
73c048b52eSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74c048b52eSmrgmkdir_umask=22
75c048b52eSmrg
76c048b52eSmrgbackupsuffix=
77b62cc08cSmrgchgrpcmd=
789fe995a9Smrgchmodcmd=$chmodprog
799fe995a9Smrgchowncmd=
80b62cc08cSmrgmvcmd=$mvprog
819fe995a9Smrgrmcmd="$rmprog -f"
82b62cc08cSmrgstripcmd=
83b62cc08cSmrg
849fe995a9Smrgsrc=
859fe995a9Smrgdst=
869fe995a9Smrgdir_arg=
87b62cc08cSmrgdst_arg=
88b62cc08cSmrg
89b62cc08cSmrgcopy_on_change=false
90e8ac26b0Smrgis_target_a_directory=possibly
919fe995a9Smrg
92b62cc08cSmrgusage="\
93b62cc08cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
949fe995a9Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
959fe995a9Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
969fe995a9Smrg   or: $0 [OPTION]... -d DIRECTORIES...
979fe995a9Smrg
989fe995a9SmrgIn the 1st form, copy SRCFILE to DSTFILE.
999fe995a9SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1009fe995a9SmrgIn the 4th, create DIRECTORIES.
1019fe995a9Smrg
1029fe995a9SmrgOptions:
103b62cc08cSmrg     --help     display this help and exit.
104b62cc08cSmrg     --version  display version info and exit.
105b62cc08cSmrg
106b62cc08cSmrg  -c            (ignored)
107c048b52eSmrg  -C            install only if different (preserve data modification time)
108b62cc08cSmrg  -d            create directories instead of installing files.
109b62cc08cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110b62cc08cSmrg  -m MODE       $chmodprog installed files to MODE.
111b62cc08cSmrg  -o USER       $chownprog installed files to USER.
112c048b52eSmrg  -p            pass -p to $cpprog.
113b62cc08cSmrg  -s            $stripprog installed files.
114c048b52eSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115b62cc08cSmrg  -t DIRECTORY  install into DIRECTORY.
116b62cc08cSmrg  -T            report an error if DSTFILE is a directory.
1179fe995a9Smrg
1189fe995a9SmrgEnvironment variables override the default commands:
119b62cc08cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120b62cc08cSmrg  RMPROG STRIPPROG
121c048b52eSmrg
122c048b52eSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123c048b52eSmrgit's up to you to specify -f if you want it.
124c048b52eSmrg
125c048b52eSmrgIf -S is not specified, no backups are attempted.
126c048b52eSmrg
127c048b52eSmrgEmail bug reports to bug-automake@gnu.org.
128c048b52eSmrgAutomake home page: https://www.gnu.org/software/automake/
1299fe995a9Smrg"
1309fe995a9Smrg
1319fe995a9Smrgwhile test $# -ne 0; do
1329fe995a9Smrg  case $1 in
133b62cc08cSmrg    -c) ;;
134b62cc08cSmrg
135b62cc08cSmrg    -C) copy_on_change=true;;
1369fe995a9Smrg
137b62cc08cSmrg    -d) dir_arg=true;;
1389fe995a9Smrg
1399fe995a9Smrg    -g) chgrpcmd="$chgrpprog $2"
140e8ac26b0Smrg        shift;;
1419fe995a9Smrg
1429fe995a9Smrg    --help) echo "$usage"; exit $?;;
1439fe995a9Smrg
1449fe995a9Smrg    -m) mode=$2
145e8ac26b0Smrg        case $mode in
146e8ac26b0Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147e8ac26b0Smrg            echo "$0: invalid mode: $mode" >&2
148e8ac26b0Smrg            exit 1;;
149e8ac26b0Smrg        esac
150e8ac26b0Smrg        shift;;
1519fe995a9Smrg
1529fe995a9Smrg    -o) chowncmd="$chownprog $2"
153e8ac26b0Smrg        shift;;
1549fe995a9Smrg
155c048b52eSmrg    -p) cpprog="$cpprog -p";;
156c048b52eSmrg
157b62cc08cSmrg    -s) stripcmd=$stripprog;;
1589fe995a9Smrg
159c048b52eSmrg    -S) backupsuffix="$2"
160c048b52eSmrg        shift;;
161c048b52eSmrg
162e8ac26b0Smrg    -t)
163e8ac26b0Smrg        is_target_a_directory=always
164e8ac26b0Smrg        dst_arg=$2
165e8ac26b0Smrg        # Protect names problematic for 'test' and other utilities.
166e8ac26b0Smrg        case $dst_arg in
167e8ac26b0Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168e8ac26b0Smrg        esac
169e8ac26b0Smrg        shift;;
1709fe995a9Smrg
171e8ac26b0Smrg    -T) is_target_a_directory=never;;
1729fe995a9Smrg
1739fe995a9Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1749fe995a9Smrg
175e8ac26b0Smrg    --) shift
176e8ac26b0Smrg        break;;
1779fe995a9Smrg
178e8ac26b0Smrg    -*) echo "$0: invalid option: $1" >&2
179e8ac26b0Smrg        exit 1;;
1809fe995a9Smrg
1819fe995a9Smrg    *)  break;;
1829fe995a9Smrg  esac
183b62cc08cSmrg  shift
1849fe995a9Smrgdone
1859fe995a9Smrg
186e8ac26b0Smrg# We allow the use of options -d and -T together, by making -d
187e8ac26b0Smrg# take the precedence; this is for compatibility with GNU install.
188e8ac26b0Smrg
189e8ac26b0Smrgif test -n "$dir_arg"; then
190e8ac26b0Smrg  if test -n "$dst_arg"; then
191e8ac26b0Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192e8ac26b0Smrg    exit 1
193e8ac26b0Smrg  fi
194e8ac26b0Smrgfi
195e8ac26b0Smrg
196b62cc08cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1979fe995a9Smrg  # When -d is used, all remaining arguments are directories to create.
1989fe995a9Smrg  # When -t is used, the destination is already specified.
1999fe995a9Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2009fe995a9Smrg  for arg
2019fe995a9Smrg  do
202b62cc08cSmrg    if test -n "$dst_arg"; then
2039fe995a9Smrg      # $@ is not empty: it contains at least $arg.
204b62cc08cSmrg      set fnord "$@" "$dst_arg"
2059fe995a9Smrg      shift # fnord
2069fe995a9Smrg    fi
2079fe995a9Smrg    shift # arg
208b62cc08cSmrg    dst_arg=$arg
2099d794632Smrg    # Protect names problematic for 'test' and other utilities.
2109d794632Smrg    case $dst_arg in
2119d794632Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2129d794632Smrg    esac
2139fe995a9Smrg  done
2149fe995a9Smrgfi
2159fe995a9Smrg
2169fe995a9Smrgif test $# -eq 0; then
2179fe995a9Smrg  if test -z "$dir_arg"; then
2189fe995a9Smrg    echo "$0: no input file specified." >&2
2199fe995a9Smrg    exit 1
2209fe995a9Smrg  fi
2219d794632Smrg  # It's OK to call 'install-sh -d' without argument.
2229fe995a9Smrg  # This can happen when creating conditional directories.
2239fe995a9Smrg  exit 0
2249fe995a9Smrgfi
2259fe995a9Smrg
226e8ac26b0Smrgif test -z "$dir_arg"; then
227e8ac26b0Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228e8ac26b0Smrg    if test ! -d "$dst_arg"; then
229e8ac26b0Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230e8ac26b0Smrg      exit 1
231e8ac26b0Smrg    fi
232e8ac26b0Smrg  fi
233e8ac26b0Smrgfi
234e8ac26b0Smrg
2359fe995a9Smrgif test -z "$dir_arg"; then
2369d794632Smrg  do_exit='(exit $ret); exit $ret'
2379d794632Smrg  trap "ret=129; $do_exit" 1
2389d794632Smrg  trap "ret=130; $do_exit" 2
2399d794632Smrg  trap "ret=141; $do_exit" 13
2409d794632Smrg  trap "ret=143; $do_exit" 15
2419fe995a9Smrg
2429fe995a9Smrg  # Set umask so as not to create temps with too-generous modes.
2439fe995a9Smrg  # However, 'strip' requires both read and write access to temps.
2449fe995a9Smrg  case $mode in
2459fe995a9Smrg    # Optimize common cases.
2469fe995a9Smrg    *644) cp_umask=133;;
2479fe995a9Smrg    *755) cp_umask=22;;
2489fe995a9Smrg
2499fe995a9Smrg    *[0-7])
2509fe995a9Smrg      if test -z "$stripcmd"; then
251e8ac26b0Smrg        u_plus_rw=
2529fe995a9Smrg      else
253e8ac26b0Smrg        u_plus_rw='% 200'
2549fe995a9Smrg      fi
2559fe995a9Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2569fe995a9Smrg    *)
2579fe995a9Smrg      if test -z "$stripcmd"; then
258e8ac26b0Smrg        u_plus_rw=
2599fe995a9Smrg      else
260e8ac26b0Smrg        u_plus_rw=,u+rw
2619fe995a9Smrg      fi
2629fe995a9Smrg      cp_umask=$mode$u_plus_rw;;
2639fe995a9Smrg  esac
2649fe995a9Smrgfi
2659fe995a9Smrg
2669fe995a9Smrgfor src
2679fe995a9Smrgdo
2689d794632Smrg  # Protect names problematic for 'test' and other utilities.
2699fe995a9Smrg  case $src in
2709d794632Smrg    -* | [=\(\)!]) src=./$src;;
2719fe995a9Smrg  esac
2729fe995a9Smrg
2739fe995a9Smrg  if test -n "$dir_arg"; then
2749fe995a9Smrg    dst=$src
2759fe995a9Smrg    dstdir=$dst
2769fe995a9Smrg    test -d "$dstdir"
2779fe995a9Smrg    dstdir_status=$?
278c048b52eSmrg    # Don't chown directories that already exist.
279c048b52eSmrg    if test $dstdir_status = 0; then
280c048b52eSmrg      chowncmd=""
281c048b52eSmrg    fi
2829fe995a9Smrg  else
2839fe995a9Smrg
2849fe995a9Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2859fe995a9Smrg    # might cause directories to be created, which would be especially bad
2869fe995a9Smrg    # if $src (and thus $dsttmp) contains '*'.
2879fe995a9Smrg    if test ! -f "$src" && test ! -d "$src"; then
2889fe995a9Smrg      echo "$0: $src does not exist." >&2
2899fe995a9Smrg      exit 1
2909fe995a9Smrg    fi
2919fe995a9Smrg
292b62cc08cSmrg    if test -z "$dst_arg"; then
2939fe995a9Smrg      echo "$0: no destination specified." >&2
2949fe995a9Smrg      exit 1
2959fe995a9Smrg    fi
296b62cc08cSmrg    dst=$dst_arg
2979fe995a9Smrg
298c048b52eSmrg    # If destination is a directory, append the input filename.
2999fe995a9Smrg    if test -d "$dst"; then
300e8ac26b0Smrg      if test "$is_target_a_directory" = never; then
301e8ac26b0Smrg        echo "$0: $dst_arg: Is a directory" >&2
302e8ac26b0Smrg        exit 1
3039fe995a9Smrg      fi
3049fe995a9Smrg      dstdir=$dst
305c048b52eSmrg      dstbase=`basename "$src"`
306c048b52eSmrg      case $dst in
307c048b52eSmrg	*/) dst=$dst$dstbase;;
308c048b52eSmrg	*)  dst=$dst/$dstbase;;
309c048b52eSmrg      esac
3109fe995a9Smrg      dstdir_status=0
3119fe995a9Smrg    else
312e8ac26b0Smrg      dstdir=`dirname "$dst"`
3139fe995a9Smrg      test -d "$dstdir"
3149fe995a9Smrg      dstdir_status=$?
3159fe995a9Smrg    fi
3169fe995a9Smrg  fi
3179fe995a9Smrg
318c048b52eSmrg  case $dstdir in
319c048b52eSmrg    */) dstdirslash=$dstdir;;
320c048b52eSmrg    *)  dstdirslash=$dstdir/;;
321c048b52eSmrg  esac
322c048b52eSmrg
3239fe995a9Smrg  obsolete_mkdir_used=false
3249fe995a9Smrg
3259fe995a9Smrg  if test $dstdir_status != 0; then
3269fe995a9Smrg    case $posix_mkdir in
3279fe995a9Smrg      '')
328e8ac26b0Smrg        # With -d, create the new directory with the user-specified mode.
329e8ac26b0Smrg        # Otherwise, rely on $mkdir_umask.
330e8ac26b0Smrg        if test -n "$dir_arg"; then
331e8ac26b0Smrg          mkdir_mode=-m$mode
332e8ac26b0Smrg        else
333e8ac26b0Smrg          mkdir_mode=
334e8ac26b0Smrg        fi
335e8ac26b0Smrg
336e8ac26b0Smrg        posix_mkdir=false
337c048b52eSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338c048b52eSmrg	# here however when possible just to lower collision chance.
339c048b52eSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340c048b52eSmrg
341c048b52eSmrg	trap '
342c048b52eSmrg	  ret=$?
343c048b52eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344c048b52eSmrg	  exit $ret
345c048b52eSmrg	' 0
346c048b52eSmrg
347c048b52eSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348c048b52eSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349c048b52eSmrg	# directory is successfully created first before we actually test
350c048b52eSmrg	# 'mkdir -p'.
351c048b52eSmrg	if (umask $mkdir_umask &&
352c048b52eSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353c048b52eSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354c048b52eSmrg	then
355c048b52eSmrg	  if test -z "$dir_arg" || {
356c048b52eSmrg	       # Check for POSIX incompatibilities with -m.
357c048b52eSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358c048b52eSmrg	       # other-writable bit of parent directory when it shouldn't.
359c048b52eSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360c048b52eSmrg	       test_tmpdir="$tmpdir/a"
361c048b52eSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362c048b52eSmrg	       case $ls_ld_tmpdir in
363c048b52eSmrg		 d????-?r-*) different_mode=700;;
364c048b52eSmrg		 d????-?--*) different_mode=755;;
365c048b52eSmrg		 *) false;;
366c048b52eSmrg	       esac &&
367c048b52eSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368c048b52eSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369c048b52eSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370c048b52eSmrg	       }
371c048b52eSmrg	     }
372c048b52eSmrg	  then posix_mkdir=:
373c048b52eSmrg	  fi
374c048b52eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375c048b52eSmrg	else
376c048b52eSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377c048b52eSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378c048b52eSmrg	fi
379c048b52eSmrg	trap '' 0;;
3809fe995a9Smrg    esac
3819fe995a9Smrg
3829fe995a9Smrg    if
3839fe995a9Smrg      $posix_mkdir && (
384e8ac26b0Smrg        umask $mkdir_umask &&
385e8ac26b0Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3869fe995a9Smrg      )
3879fe995a9Smrg    then :
3889fe995a9Smrg    else
3899fe995a9Smrg
390c048b52eSmrg      # mkdir does not conform to POSIX,
3919fe995a9Smrg      # or it failed possibly due to a race condition.  Create the
3929fe995a9Smrg      # directory the slow way, step by step, checking for races as we go.
3939fe995a9Smrg
3949fe995a9Smrg      case $dstdir in
395e8ac26b0Smrg        /*) prefix='/';;
396e8ac26b0Smrg        [-=\(\)!]*) prefix='./';;
397e8ac26b0Smrg        *)  prefix='';;
3989fe995a9Smrg      esac
3999fe995a9Smrg
4009fe995a9Smrg      oIFS=$IFS
4019fe995a9Smrg      IFS=/
402e8ac26b0Smrg      set -f
4039fe995a9Smrg      set fnord $dstdir
4049fe995a9Smrg      shift
405e8ac26b0Smrg      set +f
4069fe995a9Smrg      IFS=$oIFS
4079fe995a9Smrg
4089fe995a9Smrg      prefixes=
4099fe995a9Smrg
4109fe995a9Smrg      for d
4119fe995a9Smrg      do
412e8ac26b0Smrg        test X"$d" = X && continue
413e8ac26b0Smrg
414e8ac26b0Smrg        prefix=$prefix$d
415e8ac26b0Smrg        if test -d "$prefix"; then
416e8ac26b0Smrg          prefixes=
417e8ac26b0Smrg        else
418e8ac26b0Smrg          if $posix_mkdir; then
419c048b52eSmrg            (umask $mkdir_umask &&
420e8ac26b0Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421e8ac26b0Smrg            # Don't fail if two instances are running concurrently.
422e8ac26b0Smrg            test -d "$prefix" || exit 1
423e8ac26b0Smrg          else
424e8ac26b0Smrg            case $prefix in
425e8ac26b0Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426e8ac26b0Smrg              *) qprefix=$prefix;;
427e8ac26b0Smrg            esac
428e8ac26b0Smrg            prefixes="$prefixes '$qprefix'"
429e8ac26b0Smrg          fi
430e8ac26b0Smrg        fi
431e8ac26b0Smrg        prefix=$prefix/
4329fe995a9Smrg      done
4339fe995a9Smrg
4349fe995a9Smrg      if test -n "$prefixes"; then
435e8ac26b0Smrg        # Don't fail if two instances are running concurrently.
436e8ac26b0Smrg        (umask $mkdir_umask &&
437e8ac26b0Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438e8ac26b0Smrg          test -d "$dstdir" || exit 1
439e8ac26b0Smrg        obsolete_mkdir_used=true
4409fe995a9Smrg      fi
4419fe995a9Smrg    fi
4429fe995a9Smrg  fi
4439fe995a9Smrg
4449fe995a9Smrg  if test -n "$dir_arg"; then
4459fe995a9Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4469fe995a9Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4479fe995a9Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4489fe995a9Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4499fe995a9Smrg  else
4509fe995a9Smrg
4519fe995a9Smrg    # Make a couple of temp file names in the proper directory.
452c048b52eSmrg    dsttmp=${dstdirslash}_inst.$$_
453c048b52eSmrg    rmtmp=${dstdirslash}_rm.$$_
4549fe995a9Smrg
4559fe995a9Smrg    # Trap to clean up those temp files at exit.
4569fe995a9Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4579fe995a9Smrg
4589fe995a9Smrg    # Copy the file name to the temp name.
459c048b52eSmrg    (umask $cp_umask &&
460c048b52eSmrg     { test -z "$stripcmd" || {
461c048b52eSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462c048b52eSmrg	 # which would cause strip to fail.
463c048b52eSmrg	 if test -z "$doit"; then
464c048b52eSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465c048b52eSmrg	 else
466c048b52eSmrg	   $doit touch "$dsttmp"
467c048b52eSmrg	 fi
468c048b52eSmrg       }
469c048b52eSmrg     } &&
470c048b52eSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4719fe995a9Smrg
4729fe995a9Smrg    # and set any options; do chmod last to preserve setuid bits.
4739fe995a9Smrg    #
4749fe995a9Smrg    # If any of these fail, we abort the whole thing.  If we want to
4759fe995a9Smrg    # ignore errors from any of these, just make sure not to ignore
4769fe995a9Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4779fe995a9Smrg    #
478b62cc08cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479b62cc08cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480b62cc08cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481b62cc08cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482b62cc08cSmrg
483b62cc08cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484b62cc08cSmrg    if $copy_on_change &&
485e8ac26b0Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486e8ac26b0Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487e8ac26b0Smrg       set -f &&
488b62cc08cSmrg       set X $old && old=:$2:$4:$5:$6 &&
489b62cc08cSmrg       set X $new && new=:$2:$4:$5:$6 &&
490e8ac26b0Smrg       set +f &&
491b62cc08cSmrg       test "$old" = "$new" &&
492b62cc08cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493b62cc08cSmrg    then
494b62cc08cSmrg      rm -f "$dsttmp"
495b62cc08cSmrg    else
496c048b52eSmrg      # If $backupsuffix is set, and the file being installed
497c048b52eSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498c048b52eSmrg      # e.g., if mv doesn't support -f.
499c048b52eSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500c048b52eSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501c048b52eSmrg      fi
502c048b52eSmrg
503b62cc08cSmrg      # Rename the file to the real destination.
504b62cc08cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505b62cc08cSmrg
506b62cc08cSmrg      # The rename failed, perhaps because mv can't rename something else
507b62cc08cSmrg      # to itself, or perhaps because mv is so ancient that it does not
508b62cc08cSmrg      # support -f.
509b62cc08cSmrg      {
510e8ac26b0Smrg        # Now remove or move aside any old file at destination location.
511e8ac26b0Smrg        # We try this two ways since rm can't unlink itself on some
512e8ac26b0Smrg        # systems and the destination file might be busy for other
513e8ac26b0Smrg        # reasons.  In this case, the final cleanup might fail but the new
514e8ac26b0Smrg        # file should still install successfully.
515e8ac26b0Smrg        {
516e8ac26b0Smrg          test ! -f "$dst" ||
517c048b52eSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518e8ac26b0Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519c048b52eSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520e8ac26b0Smrg          } ||
521e8ac26b0Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522e8ac26b0Smrg            (exit 1); exit 1
523e8ac26b0Smrg          }
524e8ac26b0Smrg        } &&
525e8ac26b0Smrg
526e8ac26b0Smrg        # Now rename the file to the real destination.
527e8ac26b0Smrg        $doit $mvcmd "$dsttmp" "$dst"
528b62cc08cSmrg      }
529b62cc08cSmrg    fi || exit 1
5309fe995a9Smrg
5319fe995a9Smrg    trap '' 0
5329fe995a9Smrg  fi
5339fe995a9Smrgdone
5349fe995a9Smrg
5359fe995a9Smrg# Local variables:
536c048b52eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5379fe995a9Smrg# time-stamp-start: "scriptversion="
5389fe995a9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539c048b52eSmrg# time-stamp-time-zone: "UTC0"
540b62cc08cSmrg# time-stamp-end: "; # UTC"
5419fe995a9Smrg# End:
542