16f03b1f6Smrg#!/bin/sh
26f03b1f6Smrg# install - install a program, script, or datafile
36f03b1f6Smrg
440613eb2Smrgscriptversion=2020-11-14.01; # UTC
56f03b1f6Smrg
66f03b1f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76f03b1f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86f03b1f6Smrg# following copyright and license.
96f03b1f6Smrg#
106f03b1f6Smrg# Copyright (C) 1994 X Consortium
116f03b1f6Smrg#
126f03b1f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136f03b1f6Smrg# of this software and associated documentation files (the "Software"), to
146f03b1f6Smrg# deal in the Software without restriction, including without limitation the
156f03b1f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166f03b1f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
176f03b1f6Smrg# furnished to do so, subject to the following conditions:
186f03b1f6Smrg#
196f03b1f6Smrg# The above copyright notice and this permission notice shall be included in
206f03b1f6Smrg# all copies or substantial portions of the Software.
216f03b1f6Smrg#
226f03b1f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236f03b1f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246f03b1f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256f03b1f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266f03b1f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276f03b1f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286f03b1f6Smrg#
296f03b1f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
306f03b1f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316f03b1f6Smrg# ings in this Software without prior written authorization from the X Consor-
326f03b1f6Smrg# tium.
336f03b1f6Smrg#
346f03b1f6Smrg#
356f03b1f6Smrg# FSF changes to this file are in the public domain.
366f03b1f6Smrg#
376f03b1f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3817caa701Smrg# 'make' implicit rules from creating a file called install from it
396f03b1f6Smrg# when there is no Makefile.
406f03b1f6Smrg#
416f03b1f6Smrg# This script is compatible with the BSD install script, but was written
4298af18c5Smrg# from scratch.
4398af18c5Smrg
44c1e3c5d0Smrgtab='	'
4598af18c5Smrgnl='
4698af18c5Smrg'
47c1e3c5d0SmrgIFS=" $tab$nl"
486f03b1f6Smrg
49c1e3c5d0Smrg# Set DOITPROG to "echo" to test this script.
506f03b1f6Smrg
5198af18c5Smrgdoit=${DOITPROG-}
52c1e3c5d0Smrgdoit_exec=${doit:-exec}
536f03b1f6Smrg
5498af18c5Smrg# Put in absolute file names if you don't have them in your path;
5598af18c5Smrg# or use environment vars.
5698af18c5Smrg
5798af18c5Smrgchgrpprog=${CHGRPPROG-chgrp}
5898af18c5Smrgchmodprog=${CHMODPROG-chmod}
5998af18c5Smrgchownprog=${CHOWNPROG-chown}
6098af18c5Smrgcmpprog=${CMPPROG-cmp}
6198af18c5Smrgcpprog=${CPPROG-cp}
6298af18c5Smrgmkdirprog=${MKDIRPROG-mkdir}
6398af18c5Smrgmvprog=${MVPROG-mv}
6498af18c5Smrgrmprog=${RMPROG-rm}
6598af18c5Smrgstripprog=${STRIPPROG-strip}
6698af18c5Smrg
6798af18c5Smrgposix_mkdir=
6898af18c5Smrg
6998af18c5Smrg# Desired mode of installed file.
7098af18c5Smrgmode=0755
716f03b1f6Smrg
7240613eb2Smrg# Create dirs (including intermediate dirs) using mode 755.
7340613eb2Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7440613eb2Smrgmkdir_umask=22
7540613eb2Smrg
7640613eb2Smrgbackupsuffix=
776f03b1f6Smrgchgrpcmd=
7898af18c5Smrgchmodcmd=$chmodprog
7998af18c5Smrgchowncmd=
8098af18c5Smrgmvcmd=$mvprog
816f03b1f6Smrgrmcmd="$rmprog -f"
8298af18c5Smrgstripcmd=
8398af18c5Smrg
846f03b1f6Smrgsrc=
856f03b1f6Smrgdst=
866f03b1f6Smrgdir_arg=
8798af18c5Smrgdst_arg=
8898af18c5Smrg
8998af18c5Smrgcopy_on_change=false
90c1e3c5d0Smrgis_target_a_directory=possibly
916f03b1f6Smrg
9298af18c5Smrgusage="\
9398af18c5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
946f03b1f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
956f03b1f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
966f03b1f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
976f03b1f6Smrg
986f03b1f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
996f03b1f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1006f03b1f6SmrgIn the 4th, create DIRECTORIES.
1016f03b1f6Smrg
1026f03b1f6SmrgOptions:
10398af18c5Smrg     --help     display this help and exit.
10498af18c5Smrg     --version  display version info and exit.
10598af18c5Smrg
10698af18c5Smrg  -c            (ignored)
10740613eb2Smrg  -C            install only if different (preserve data modification time)
10898af18c5Smrg  -d            create directories instead of installing files.
10998af18c5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11098af18c5Smrg  -m MODE       $chmodprog installed files to MODE.
11198af18c5Smrg  -o USER       $chownprog installed files to USER.
11240613eb2Smrg  -p            pass -p to $cpprog.
11398af18c5Smrg  -s            $stripprog installed files.
11440613eb2Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11598af18c5Smrg  -t DIRECTORY  install into DIRECTORY.
11698af18c5Smrg  -T            report an error if DSTFILE is a directory.
1176f03b1f6Smrg
1186f03b1f6SmrgEnvironment variables override the default commands:
11998af18c5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12098af18c5Smrg  RMPROG STRIPPROG
12140613eb2Smrg
12240613eb2SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12340613eb2Smrgit's up to you to specify -f if you want it.
12440613eb2Smrg
12540613eb2SmrgIf -S is not specified, no backups are attempted.
12640613eb2Smrg
12740613eb2SmrgEmail bug reports to bug-automake@gnu.org.
12840613eb2SmrgAutomake home page: https://www.gnu.org/software/automake/
1296f03b1f6Smrg"
1306f03b1f6Smrg
13198af18c5Smrgwhile test $# -ne 0; do
1326f03b1f6Smrg  case $1 in
13398af18c5Smrg    -c) ;;
13498af18c5Smrg
13598af18c5Smrg    -C) copy_on_change=true;;
1366f03b1f6Smrg
13798af18c5Smrg    -d) dir_arg=true;;
1386f03b1f6Smrg
1396f03b1f6Smrg    -g) chgrpcmd="$chgrpprog $2"
140c1e3c5d0Smrg        shift;;
1416f03b1f6Smrg
1426f03b1f6Smrg    --help) echo "$usage"; exit $?;;
1436f03b1f6Smrg
14498af18c5Smrg    -m) mode=$2
145c1e3c5d0Smrg        case $mode in
146c1e3c5d0Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147c1e3c5d0Smrg            echo "$0: invalid mode: $mode" >&2
148c1e3c5d0Smrg            exit 1;;
149c1e3c5d0Smrg        esac
150c1e3c5d0Smrg        shift;;
1516f03b1f6Smrg
1526f03b1f6Smrg    -o) chowncmd="$chownprog $2"
153c1e3c5d0Smrg        shift;;
1546f03b1f6Smrg
15540613eb2Smrg    -p) cpprog="$cpprog -p";;
15640613eb2Smrg
15798af18c5Smrg    -s) stripcmd=$stripprog;;
1586f03b1f6Smrg
15940613eb2Smrg    -S) backupsuffix="$2"
16040613eb2Smrg        shift;;
16140613eb2Smrg
162c1e3c5d0Smrg    -t)
163c1e3c5d0Smrg        is_target_a_directory=always
164c1e3c5d0Smrg        dst_arg=$2
165c1e3c5d0Smrg        # Protect names problematic for 'test' and other utilities.
166c1e3c5d0Smrg        case $dst_arg in
167c1e3c5d0Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168c1e3c5d0Smrg        esac
169c1e3c5d0Smrg        shift;;
1706f03b1f6Smrg
171c1e3c5d0Smrg    -T) is_target_a_directory=never;;
1726f03b1f6Smrg
1736f03b1f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1746f03b1f6Smrg
175c1e3c5d0Smrg    --) shift
176c1e3c5d0Smrg        break;;
17798af18c5Smrg
178c1e3c5d0Smrg    -*) echo "$0: invalid option: $1" >&2
179c1e3c5d0Smrg        exit 1;;
18098af18c5Smrg
18198af18c5Smrg    *)  break;;
1826f03b1f6Smrg  esac
18398af18c5Smrg  shift
1846f03b1f6Smrgdone
1856f03b1f6Smrg
186c1e3c5d0Smrg# We allow the use of options -d and -T together, by making -d
187c1e3c5d0Smrg# take the precedence; this is for compatibility with GNU install.
188c1e3c5d0Smrg
189c1e3c5d0Smrgif test -n "$dir_arg"; then
190c1e3c5d0Smrg  if test -n "$dst_arg"; then
191c1e3c5d0Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192c1e3c5d0Smrg    exit 1
193c1e3c5d0Smrg  fi
194c1e3c5d0Smrgfi
195c1e3c5d0Smrg
19698af18c5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19798af18c5Smrg  # When -d is used, all remaining arguments are directories to create.
19898af18c5Smrg  # When -t is used, the destination is already specified.
19998af18c5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20098af18c5Smrg  for arg
20198af18c5Smrg  do
20298af18c5Smrg    if test -n "$dst_arg"; then
20398af18c5Smrg      # $@ is not empty: it contains at least $arg.
20498af18c5Smrg      set fnord "$@" "$dst_arg"
20598af18c5Smrg      shift # fnord
20698af18c5Smrg    fi
20798af18c5Smrg    shift # arg
20898af18c5Smrg    dst_arg=$arg
20917caa701Smrg    # Protect names problematic for 'test' and other utilities.
21017caa701Smrg    case $dst_arg in
21117caa701Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21217caa701Smrg    esac
21398af18c5Smrg  done
21498af18c5Smrgfi
21598af18c5Smrg
21698af18c5Smrgif test $# -eq 0; then
2176f03b1f6Smrg  if test -z "$dir_arg"; then
2186f03b1f6Smrg    echo "$0: no input file specified." >&2
2196f03b1f6Smrg    exit 1
2206f03b1f6Smrg  fi
22117caa701Smrg  # It's OK to call 'install-sh -d' without argument.
2226f03b1f6Smrg  # This can happen when creating conditional directories.
2236f03b1f6Smrg  exit 0
2246f03b1f6Smrgfi
2256f03b1f6Smrg
226c1e3c5d0Smrgif test -z "$dir_arg"; then
227c1e3c5d0Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228c1e3c5d0Smrg    if test ! -d "$dst_arg"; then
229c1e3c5d0Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230c1e3c5d0Smrg      exit 1
231c1e3c5d0Smrg    fi
232c1e3c5d0Smrg  fi
233c1e3c5d0Smrgfi
234c1e3c5d0Smrg
23598af18c5Smrgif test -z "$dir_arg"; then
23617caa701Smrg  do_exit='(exit $ret); exit $ret'
23717caa701Smrg  trap "ret=129; $do_exit" 1
23817caa701Smrg  trap "ret=130; $do_exit" 2
23917caa701Smrg  trap "ret=141; $do_exit" 13
24017caa701Smrg  trap "ret=143; $do_exit" 15
24198af18c5Smrg
24298af18c5Smrg  # Set umask so as not to create temps with too-generous modes.
24398af18c5Smrg  # However, 'strip' requires both read and write access to temps.
24498af18c5Smrg  case $mode in
24598af18c5Smrg    # Optimize common cases.
24698af18c5Smrg    *644) cp_umask=133;;
24798af18c5Smrg    *755) cp_umask=22;;
24898af18c5Smrg
24998af18c5Smrg    *[0-7])
25098af18c5Smrg      if test -z "$stripcmd"; then
251c1e3c5d0Smrg        u_plus_rw=
25298af18c5Smrg      else
253c1e3c5d0Smrg        u_plus_rw='% 200'
25498af18c5Smrg      fi
25598af18c5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25698af18c5Smrg    *)
25798af18c5Smrg      if test -z "$stripcmd"; then
258c1e3c5d0Smrg        u_plus_rw=
25998af18c5Smrg      else
260c1e3c5d0Smrg        u_plus_rw=,u+rw
26198af18c5Smrg      fi
26298af18c5Smrg      cp_umask=$mode$u_plus_rw;;
26398af18c5Smrg  esac
26498af18c5Smrgfi
26598af18c5Smrg
2666f03b1f6Smrgfor src
2676f03b1f6Smrgdo
26817caa701Smrg  # Protect names problematic for 'test' and other utilities.
2696f03b1f6Smrg  case $src in
27017caa701Smrg    -* | [=\(\)!]) src=./$src;;
2716f03b1f6Smrg  esac
2726f03b1f6Smrg
2736f03b1f6Smrg  if test -n "$dir_arg"; then
2746f03b1f6Smrg    dst=$src
27598af18c5Smrg    dstdir=$dst
27698af18c5Smrg    test -d "$dstdir"
27798af18c5Smrg    dstdir_status=$?
27840613eb2Smrg    # Don't chown directories that already exist.
27940613eb2Smrg    if test $dstdir_status = 0; then
28040613eb2Smrg      chowncmd=""
28140613eb2Smrg    fi
2826f03b1f6Smrg  else
28398af18c5Smrg
2846f03b1f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2856f03b1f6Smrg    # might cause directories to be created, which would be especially bad
2866f03b1f6Smrg    # if $src (and thus $dsttmp) contains '*'.
2876f03b1f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
2886f03b1f6Smrg      echo "$0: $src does not exist." >&2
2896f03b1f6Smrg      exit 1
2906f03b1f6Smrg    fi
2916f03b1f6Smrg
29298af18c5Smrg    if test -z "$dst_arg"; then
2936f03b1f6Smrg      echo "$0: no destination specified." >&2
2946f03b1f6Smrg      exit 1
2956f03b1f6Smrg    fi
29698af18c5Smrg    dst=$dst_arg
2976f03b1f6Smrg
298cfcf9057Smrg    # If destination is a directory, append the input filename.
2996f03b1f6Smrg    if test -d "$dst"; then
300c1e3c5d0Smrg      if test "$is_target_a_directory" = never; then
301c1e3c5d0Smrg        echo "$0: $dst_arg: Is a directory" >&2
302c1e3c5d0Smrg        exit 1
3036f03b1f6Smrg      fi
30498af18c5Smrg      dstdir=$dst
305cfcf9057Smrg      dstbase=`basename "$src"`
306cfcf9057Smrg      case $dst in
307cfcf9057Smrg	*/) dst=$dst$dstbase;;
308cfcf9057Smrg	*)  dst=$dst/$dstbase;;
309cfcf9057Smrg      esac
31098af18c5Smrg      dstdir_status=0
31198af18c5Smrg    else
312c1e3c5d0Smrg      dstdir=`dirname "$dst"`
31398af18c5Smrg      test -d "$dstdir"
31498af18c5Smrg      dstdir_status=$?
3156f03b1f6Smrg    fi
3166f03b1f6Smrg  fi
3176f03b1f6Smrg
318cfcf9057Smrg  case $dstdir in
319cfcf9057Smrg    */) dstdirslash=$dstdir;;
320cfcf9057Smrg    *)  dstdirslash=$dstdir/;;
321cfcf9057Smrg  esac
322cfcf9057Smrg
32398af18c5Smrg  obsolete_mkdir_used=false
32498af18c5Smrg
32598af18c5Smrg  if test $dstdir_status != 0; then
32698af18c5Smrg    case $posix_mkdir in
32798af18c5Smrg      '')
328c1e3c5d0Smrg        # With -d, create the new directory with the user-specified mode.
329c1e3c5d0Smrg        # Otherwise, rely on $mkdir_umask.
330c1e3c5d0Smrg        if test -n "$dir_arg"; then
331c1e3c5d0Smrg          mkdir_mode=-m$mode
332c1e3c5d0Smrg        else
333c1e3c5d0Smrg          mkdir_mode=
334c1e3c5d0Smrg        fi
335c1e3c5d0Smrg
336c1e3c5d0Smrg        posix_mkdir=false
33740613eb2Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33840613eb2Smrg	# here however when possible just to lower collision chance.
33940613eb2Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34040613eb2Smrg
34140613eb2Smrg	trap '
34240613eb2Smrg	  ret=$?
34340613eb2Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34440613eb2Smrg	  exit $ret
34540613eb2Smrg	' 0
34640613eb2Smrg
34740613eb2Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34840613eb2Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34940613eb2Smrg	# directory is successfully created first before we actually test
35040613eb2Smrg	# 'mkdir -p'.
35140613eb2Smrg	if (umask $mkdir_umask &&
35240613eb2Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35340613eb2Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35440613eb2Smrg	then
35540613eb2Smrg	  if test -z "$dir_arg" || {
35640613eb2Smrg	       # Check for POSIX incompatibilities with -m.
35740613eb2Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35840613eb2Smrg	       # other-writable bit of parent directory when it shouldn't.
35940613eb2Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36040613eb2Smrg	       test_tmpdir="$tmpdir/a"
36140613eb2Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36240613eb2Smrg	       case $ls_ld_tmpdir in
36340613eb2Smrg		 d????-?r-*) different_mode=700;;
36440613eb2Smrg		 d????-?--*) different_mode=755;;
36540613eb2Smrg		 *) false;;
36640613eb2Smrg	       esac &&
36740613eb2Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36840613eb2Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36940613eb2Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37040613eb2Smrg	       }
37140613eb2Smrg	     }
37240613eb2Smrg	  then posix_mkdir=:
37340613eb2Smrg	  fi
37440613eb2Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37540613eb2Smrg	else
37640613eb2Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37740613eb2Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37840613eb2Smrg	fi
37940613eb2Smrg	trap '' 0;;
38098af18c5Smrg    esac
3816f03b1f6Smrg
38298af18c5Smrg    if
38398af18c5Smrg      $posix_mkdir && (
384c1e3c5d0Smrg        umask $mkdir_umask &&
385c1e3c5d0Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38698af18c5Smrg      )
38798af18c5Smrg    then :
38898af18c5Smrg    else
3896f03b1f6Smrg
39040613eb2Smrg      # mkdir does not conform to POSIX,
39198af18c5Smrg      # or it failed possibly due to a race condition.  Create the
39298af18c5Smrg      # directory the slow way, step by step, checking for races as we go.
3936f03b1f6Smrg
39498af18c5Smrg      case $dstdir in
395c1e3c5d0Smrg        /*) prefix='/';;
396c1e3c5d0Smrg        [-=\(\)!]*) prefix='./';;
397c1e3c5d0Smrg        *)  prefix='';;
39898af18c5Smrg      esac
3996f03b1f6Smrg
40098af18c5Smrg      oIFS=$IFS
40198af18c5Smrg      IFS=/
402c1e3c5d0Smrg      set -f
40398af18c5Smrg      set fnord $dstdir
4046f03b1f6Smrg      shift
405c1e3c5d0Smrg      set +f
40698af18c5Smrg      IFS=$oIFS
40798af18c5Smrg
40898af18c5Smrg      prefixes=
40998af18c5Smrg
41098af18c5Smrg      for d
41198af18c5Smrg      do
412c1e3c5d0Smrg        test X"$d" = X && continue
413c1e3c5d0Smrg
414c1e3c5d0Smrg        prefix=$prefix$d
415c1e3c5d0Smrg        if test -d "$prefix"; then
416c1e3c5d0Smrg          prefixes=
417c1e3c5d0Smrg        else
418c1e3c5d0Smrg          if $posix_mkdir; then
41940613eb2Smrg            (umask $mkdir_umask &&
420c1e3c5d0Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421c1e3c5d0Smrg            # Don't fail if two instances are running concurrently.
422c1e3c5d0Smrg            test -d "$prefix" || exit 1
423c1e3c5d0Smrg          else
424c1e3c5d0Smrg            case $prefix in
425c1e3c5d0Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426c1e3c5d0Smrg              *) qprefix=$prefix;;
427c1e3c5d0Smrg            esac
428c1e3c5d0Smrg            prefixes="$prefixes '$qprefix'"
429c1e3c5d0Smrg          fi
430c1e3c5d0Smrg        fi
431c1e3c5d0Smrg        prefix=$prefix/
43298af18c5Smrg      done
43398af18c5Smrg
43498af18c5Smrg      if test -n "$prefixes"; then
435c1e3c5d0Smrg        # Don't fail if two instances are running concurrently.
436c1e3c5d0Smrg        (umask $mkdir_umask &&
437c1e3c5d0Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438c1e3c5d0Smrg          test -d "$dstdir" || exit 1
439c1e3c5d0Smrg        obsolete_mkdir_used=true
4406f03b1f6Smrg      fi
44198af18c5Smrg    fi
4426f03b1f6Smrg  fi
4436f03b1f6Smrg
4446f03b1f6Smrg  if test -n "$dir_arg"; then
44598af18c5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44698af18c5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44798af18c5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44898af18c5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4496f03b1f6Smrg  else
4506f03b1f6Smrg
4516f03b1f6Smrg    # Make a couple of temp file names in the proper directory.
452cfcf9057Smrg    dsttmp=${dstdirslash}_inst.$$_
453cfcf9057Smrg    rmtmp=${dstdirslash}_rm.$$_
4546f03b1f6Smrg
4556f03b1f6Smrg    # Trap to clean up those temp files at exit.
4566f03b1f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4576f03b1f6Smrg
4586f03b1f6Smrg    # Copy the file name to the temp name.
45940613eb2Smrg    (umask $cp_umask &&
46040613eb2Smrg     { test -z "$stripcmd" || {
46140613eb2Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46240613eb2Smrg	 # which would cause strip to fail.
46340613eb2Smrg	 if test -z "$doit"; then
46440613eb2Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46540613eb2Smrg	 else
46640613eb2Smrg	   $doit touch "$dsttmp"
46740613eb2Smrg	 fi
46840613eb2Smrg       }
46940613eb2Smrg     } &&
47040613eb2Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4716f03b1f6Smrg
4726f03b1f6Smrg    # and set any options; do chmod last to preserve setuid bits.
4736f03b1f6Smrg    #
4746f03b1f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
4756f03b1f6Smrg    # ignore errors from any of these, just make sure not to ignore
4766f03b1f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4776f03b1f6Smrg    #
47898af18c5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47998af18c5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48098af18c5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48198af18c5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48298af18c5Smrg
48398af18c5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48498af18c5Smrg    if $copy_on_change &&
485c1e3c5d0Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486c1e3c5d0Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487c1e3c5d0Smrg       set -f &&
48898af18c5Smrg       set X $old && old=:$2:$4:$5:$6 &&
48998af18c5Smrg       set X $new && new=:$2:$4:$5:$6 &&
490c1e3c5d0Smrg       set +f &&
49198af18c5Smrg       test "$old" = "$new" &&
49298af18c5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49398af18c5Smrg    then
49498af18c5Smrg      rm -f "$dsttmp"
49598af18c5Smrg    else
49640613eb2Smrg      # If $backupsuffix is set, and the file being installed
49740613eb2Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49840613eb2Smrg      # e.g., if mv doesn't support -f.
49940613eb2Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50040613eb2Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50140613eb2Smrg      fi
50240613eb2Smrg
50398af18c5Smrg      # Rename the file to the real destination.
50498af18c5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50598af18c5Smrg
50698af18c5Smrg      # The rename failed, perhaps because mv can't rename something else
50798af18c5Smrg      # to itself, or perhaps because mv is so ancient that it does not
50898af18c5Smrg      # support -f.
50998af18c5Smrg      {
510c1e3c5d0Smrg        # Now remove or move aside any old file at destination location.
511c1e3c5d0Smrg        # We try this two ways since rm can't unlink itself on some
512c1e3c5d0Smrg        # systems and the destination file might be busy for other
513c1e3c5d0Smrg        # reasons.  In this case, the final cleanup might fail but the new
514c1e3c5d0Smrg        # file should still install successfully.
515c1e3c5d0Smrg        {
516c1e3c5d0Smrg          test ! -f "$dst" ||
51740613eb2Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518c1e3c5d0Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51940613eb2Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520c1e3c5d0Smrg          } ||
521c1e3c5d0Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522c1e3c5d0Smrg            (exit 1); exit 1
523c1e3c5d0Smrg          }
524c1e3c5d0Smrg        } &&
525c1e3c5d0Smrg
526c1e3c5d0Smrg        # Now rename the file to the real destination.
527c1e3c5d0Smrg        $doit $mvcmd "$dsttmp" "$dst"
52898af18c5Smrg      }
52998af18c5Smrg    fi || exit 1
53098af18c5Smrg
53198af18c5Smrg    trap '' 0
53298af18c5Smrg  fi
5336f03b1f6Smrgdone
5346f03b1f6Smrg
5356f03b1f6Smrg# Local variables:
536cfcf9057Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5376f03b1f6Smrg# time-stamp-start: "scriptversion="
5386f03b1f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539cfcf9057Smrg# time-stamp-time-zone: "UTC0"
54098af18c5Smrg# time-stamp-end: "; # UTC"
5416f03b1f6Smrg# End:
542