18ae04fa6Smrg#!/bin/sh
28ae04fa6Smrg# install - install a program, script, or datafile
38ae04fa6Smrg
4138a9f8aSmrgscriptversion=2020-11-14.01; # UTC
58ae04fa6Smrg
68ae04fa6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
78ae04fa6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
88ae04fa6Smrg# following copyright and license.
98ae04fa6Smrg#
108ae04fa6Smrg# Copyright (C) 1994 X Consortium
118ae04fa6Smrg#
128ae04fa6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
138ae04fa6Smrg# of this software and associated documentation files (the "Software"), to
148ae04fa6Smrg# deal in the Software without restriction, including without limitation the
158ae04fa6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168ae04fa6Smrg# sell copies of the Software, and to permit persons to whom the Software is
178ae04fa6Smrg# furnished to do so, subject to the following conditions:
188ae04fa6Smrg#
198ae04fa6Smrg# The above copyright notice and this permission notice shall be included in
208ae04fa6Smrg# all copies or substantial portions of the Software.
218ae04fa6Smrg#
228ae04fa6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238ae04fa6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248ae04fa6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258ae04fa6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268ae04fa6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
278ae04fa6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288ae04fa6Smrg#
298ae04fa6Smrg# Except as contained in this notice, the name of the X Consortium shall not
308ae04fa6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
318ae04fa6Smrg# ings in this Software without prior written authorization from the X Consor-
328ae04fa6Smrg# tium.
338ae04fa6Smrg#
348ae04fa6Smrg#
358ae04fa6Smrg# FSF changes to this file are in the public domain.
368ae04fa6Smrg#
378ae04fa6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3880f56f3aSmrg# 'make' implicit rules from creating a file called install from it
398ae04fa6Smrg# when there is no Makefile.
408ae04fa6Smrg#
418ae04fa6Smrg# This script is compatible with the BSD install script, but was written
428ae04fa6Smrg# from scratch.
438ae04fa6Smrg
4480f56f3aSmrgtab='	'
458ae04fa6Smrgnl='
468ae04fa6Smrg'
4780f56f3aSmrgIFS=" $tab$nl"
488ae04fa6Smrg
4980f56f3aSmrg# Set DOITPROG to "echo" to test this script.
508ae04fa6Smrg
5144af7230Smrgdoit=${DOITPROG-}
5280f56f3aSmrgdoit_exec=${doit:-exec}
538ae04fa6Smrg
548ae04fa6Smrg# Put in absolute file names if you don't have them in your path;
558ae04fa6Smrg# or use environment vars.
568ae04fa6Smrg
5744af7230Smrgchgrpprog=${CHGRPPROG-chgrp}
5844af7230Smrgchmodprog=${CHMODPROG-chmod}
5944af7230Smrgchownprog=${CHOWNPROG-chown}
6044af7230Smrgcmpprog=${CMPPROG-cmp}
6144af7230Smrgcpprog=${CPPROG-cp}
6244af7230Smrgmkdirprog=${MKDIRPROG-mkdir}
6344af7230Smrgmvprog=${MVPROG-mv}
6444af7230Smrgrmprog=${RMPROG-rm}
6544af7230Smrgstripprog=${STRIPPROG-strip}
6644af7230Smrg
678ae04fa6Smrgposix_mkdir=
688ae04fa6Smrg
698ae04fa6Smrg# Desired mode of installed file.
708ae04fa6Smrgmode=0755
718ae04fa6Smrg
72138a9f8aSmrg# Create dirs (including intermediate dirs) using mode 755.
73138a9f8aSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74138a9f8aSmrgmkdir_umask=22
75138a9f8aSmrg
76138a9f8aSmrgbackupsuffix=
7744af7230Smrgchgrpcmd=
788ae04fa6Smrgchmodcmd=$chmodprog
798ae04fa6Smrgchowncmd=
8044af7230Smrgmvcmd=$mvprog
818ae04fa6Smrgrmcmd="$rmprog -f"
8244af7230Smrgstripcmd=
8344af7230Smrg
848ae04fa6Smrgsrc=
858ae04fa6Smrgdst=
868ae04fa6Smrgdir_arg=
8744af7230Smrgdst_arg=
8844af7230Smrg
8944af7230Smrgcopy_on_change=false
9080f56f3aSmrgis_target_a_directory=possibly
918ae04fa6Smrg
9244af7230Smrgusage="\
9344af7230SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
948ae04fa6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
958ae04fa6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
968ae04fa6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
978ae04fa6Smrg
988ae04fa6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
998ae04fa6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1008ae04fa6SmrgIn the 4th, create DIRECTORIES.
1018ae04fa6Smrg
1028ae04fa6SmrgOptions:
10344af7230Smrg     --help     display this help and exit.
10444af7230Smrg     --version  display version info and exit.
10544af7230Smrg
10644af7230Smrg  -c            (ignored)
107138a9f8aSmrg  -C            install only if different (preserve data modification time)
10844af7230Smrg  -d            create directories instead of installing files.
10944af7230Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11044af7230Smrg  -m MODE       $chmodprog installed files to MODE.
11144af7230Smrg  -o USER       $chownprog installed files to USER.
112138a9f8aSmrg  -p            pass -p to $cpprog.
11344af7230Smrg  -s            $stripprog installed files.
114138a9f8aSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11544af7230Smrg  -t DIRECTORY  install into DIRECTORY.
11644af7230Smrg  -T            report an error if DSTFILE is a directory.
1178ae04fa6Smrg
1188ae04fa6SmrgEnvironment variables override the default commands:
11944af7230Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12044af7230Smrg  RMPROG STRIPPROG
121138a9f8aSmrg
122138a9f8aSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123138a9f8aSmrgit's up to you to specify -f if you want it.
124138a9f8aSmrg
125138a9f8aSmrgIf -S is not specified, no backups are attempted.
126138a9f8aSmrg
127138a9f8aSmrgEmail bug reports to bug-automake@gnu.org.
128138a9f8aSmrgAutomake home page: https://www.gnu.org/software/automake/
1298ae04fa6Smrg"
1308ae04fa6Smrg
1318ae04fa6Smrgwhile test $# -ne 0; do
1328ae04fa6Smrg  case $1 in
13344af7230Smrg    -c) ;;
13444af7230Smrg
13544af7230Smrg    -C) copy_on_change=true;;
1368ae04fa6Smrg
13744af7230Smrg    -d) dir_arg=true;;
1388ae04fa6Smrg
1398ae04fa6Smrg    -g) chgrpcmd="$chgrpprog $2"
14080f56f3aSmrg        shift;;
1418ae04fa6Smrg
1428ae04fa6Smrg    --help) echo "$usage"; exit $?;;
1438ae04fa6Smrg
1448ae04fa6Smrg    -m) mode=$2
14580f56f3aSmrg        case $mode in
14680f56f3aSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14780f56f3aSmrg            echo "$0: invalid mode: $mode" >&2
14880f56f3aSmrg            exit 1;;
14980f56f3aSmrg        esac
15080f56f3aSmrg        shift;;
1518ae04fa6Smrg
1528ae04fa6Smrg    -o) chowncmd="$chownprog $2"
15380f56f3aSmrg        shift;;
1548ae04fa6Smrg
155138a9f8aSmrg    -p) cpprog="$cpprog -p";;
156138a9f8aSmrg
15744af7230Smrg    -s) stripcmd=$stripprog;;
1588ae04fa6Smrg
159138a9f8aSmrg    -S) backupsuffix="$2"
160138a9f8aSmrg        shift;;
161138a9f8aSmrg
16280f56f3aSmrg    -t)
16380f56f3aSmrg        is_target_a_directory=always
16480f56f3aSmrg        dst_arg=$2
16580f56f3aSmrg        # Protect names problematic for 'test' and other utilities.
16680f56f3aSmrg        case $dst_arg in
16780f56f3aSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16880f56f3aSmrg        esac
16980f56f3aSmrg        shift;;
1708ae04fa6Smrg
17180f56f3aSmrg    -T) is_target_a_directory=never;;
1728ae04fa6Smrg
1738ae04fa6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1748ae04fa6Smrg
17580f56f3aSmrg    --) shift
17680f56f3aSmrg        break;;
1778ae04fa6Smrg
17880f56f3aSmrg    -*) echo "$0: invalid option: $1" >&2
17980f56f3aSmrg        exit 1;;
1808ae04fa6Smrg
1818ae04fa6Smrg    *)  break;;
1828ae04fa6Smrg  esac
18344af7230Smrg  shift
1848ae04fa6Smrgdone
1858ae04fa6Smrg
18680f56f3aSmrg# We allow the use of options -d and -T together, by making -d
18780f56f3aSmrg# take the precedence; this is for compatibility with GNU install.
18880f56f3aSmrg
18980f56f3aSmrgif test -n "$dir_arg"; then
19080f56f3aSmrg  if test -n "$dst_arg"; then
19180f56f3aSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19280f56f3aSmrg    exit 1
19380f56f3aSmrg  fi
19480f56f3aSmrgfi
19580f56f3aSmrg
19644af7230Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1978ae04fa6Smrg  # When -d is used, all remaining arguments are directories to create.
1988ae04fa6Smrg  # When -t is used, the destination is already specified.
1998ae04fa6Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2008ae04fa6Smrg  for arg
2018ae04fa6Smrg  do
20244af7230Smrg    if test -n "$dst_arg"; then
2038ae04fa6Smrg      # $@ is not empty: it contains at least $arg.
20444af7230Smrg      set fnord "$@" "$dst_arg"
2058ae04fa6Smrg      shift # fnord
2068ae04fa6Smrg    fi
2078ae04fa6Smrg    shift # arg
20844af7230Smrg    dst_arg=$arg
20980f56f3aSmrg    # Protect names problematic for 'test' and other utilities.
210405c8079Smrg    case $dst_arg in
211405c8079Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212405c8079Smrg    esac
2138ae04fa6Smrg  done
2148ae04fa6Smrgfi
2158ae04fa6Smrg
2168ae04fa6Smrgif test $# -eq 0; then
2178ae04fa6Smrg  if test -z "$dir_arg"; then
2188ae04fa6Smrg    echo "$0: no input file specified." >&2
2198ae04fa6Smrg    exit 1
2208ae04fa6Smrg  fi
22180f56f3aSmrg  # It's OK to call 'install-sh -d' without argument.
2228ae04fa6Smrg  # This can happen when creating conditional directories.
2238ae04fa6Smrg  exit 0
2248ae04fa6Smrgfi
2258ae04fa6Smrg
22680f56f3aSmrgif test -z "$dir_arg"; then
22780f56f3aSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22880f56f3aSmrg    if test ! -d "$dst_arg"; then
22980f56f3aSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23080f56f3aSmrg      exit 1
23180f56f3aSmrg    fi
23280f56f3aSmrg  fi
23380f56f3aSmrgfi
23480f56f3aSmrg
2358ae04fa6Smrgif test -z "$dir_arg"; then
236405c8079Smrg  do_exit='(exit $ret); exit $ret'
237405c8079Smrg  trap "ret=129; $do_exit" 1
238405c8079Smrg  trap "ret=130; $do_exit" 2
239405c8079Smrg  trap "ret=141; $do_exit" 13
240405c8079Smrg  trap "ret=143; $do_exit" 15
2418ae04fa6Smrg
2428ae04fa6Smrg  # Set umask so as not to create temps with too-generous modes.
2438ae04fa6Smrg  # However, 'strip' requires both read and write access to temps.
2448ae04fa6Smrg  case $mode in
2458ae04fa6Smrg    # Optimize common cases.
2468ae04fa6Smrg    *644) cp_umask=133;;
2478ae04fa6Smrg    *755) cp_umask=22;;
2488ae04fa6Smrg
2498ae04fa6Smrg    *[0-7])
2508ae04fa6Smrg      if test -z "$stripcmd"; then
25180f56f3aSmrg        u_plus_rw=
2528ae04fa6Smrg      else
25380f56f3aSmrg        u_plus_rw='% 200'
2548ae04fa6Smrg      fi
2558ae04fa6Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2568ae04fa6Smrg    *)
2578ae04fa6Smrg      if test -z "$stripcmd"; then
25880f56f3aSmrg        u_plus_rw=
2598ae04fa6Smrg      else
26080f56f3aSmrg        u_plus_rw=,u+rw
2618ae04fa6Smrg      fi
2628ae04fa6Smrg      cp_umask=$mode$u_plus_rw;;
2638ae04fa6Smrg  esac
2648ae04fa6Smrgfi
2658ae04fa6Smrg
2668ae04fa6Smrgfor src
2678ae04fa6Smrgdo
26880f56f3aSmrg  # Protect names problematic for 'test' and other utilities.
2698ae04fa6Smrg  case $src in
270405c8079Smrg    -* | [=\(\)!]) src=./$src;;
2718ae04fa6Smrg  esac
2728ae04fa6Smrg
2738ae04fa6Smrg  if test -n "$dir_arg"; then
2748ae04fa6Smrg    dst=$src
2758ae04fa6Smrg    dstdir=$dst
2768ae04fa6Smrg    test -d "$dstdir"
2778ae04fa6Smrg    dstdir_status=$?
278138a9f8aSmrg    # Don't chown directories that already exist.
279138a9f8aSmrg    if test $dstdir_status = 0; then
280138a9f8aSmrg      chowncmd=""
281138a9f8aSmrg    fi
2828ae04fa6Smrg  else
2838ae04fa6Smrg
2848ae04fa6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2858ae04fa6Smrg    # might cause directories to be created, which would be especially bad
2868ae04fa6Smrg    # if $src (and thus $dsttmp) contains '*'.
2878ae04fa6Smrg    if test ! -f "$src" && test ! -d "$src"; then
2888ae04fa6Smrg      echo "$0: $src does not exist." >&2
2898ae04fa6Smrg      exit 1
2908ae04fa6Smrg    fi
2918ae04fa6Smrg
29244af7230Smrg    if test -z "$dst_arg"; then
2938ae04fa6Smrg      echo "$0: no destination specified." >&2
2948ae04fa6Smrg      exit 1
2958ae04fa6Smrg    fi
29644af7230Smrg    dst=$dst_arg
2978ae04fa6Smrg
298138a9f8aSmrg    # If destination is a directory, append the input filename.
2998ae04fa6Smrg    if test -d "$dst"; then
30080f56f3aSmrg      if test "$is_target_a_directory" = never; then
30180f56f3aSmrg        echo "$0: $dst_arg: Is a directory" >&2
30280f56f3aSmrg        exit 1
3038ae04fa6Smrg      fi
3048ae04fa6Smrg      dstdir=$dst
305138a9f8aSmrg      dstbase=`basename "$src"`
306138a9f8aSmrg      case $dst in
307138a9f8aSmrg	*/) dst=$dst$dstbase;;
308138a9f8aSmrg	*)  dst=$dst/$dstbase;;
309138a9f8aSmrg      esac
3108ae04fa6Smrg      dstdir_status=0
3118ae04fa6Smrg    else
31280f56f3aSmrg      dstdir=`dirname "$dst"`
3138ae04fa6Smrg      test -d "$dstdir"
3148ae04fa6Smrg      dstdir_status=$?
3158ae04fa6Smrg    fi
3168ae04fa6Smrg  fi
3178ae04fa6Smrg
318138a9f8aSmrg  case $dstdir in
319138a9f8aSmrg    */) dstdirslash=$dstdir;;
320138a9f8aSmrg    *)  dstdirslash=$dstdir/;;
321138a9f8aSmrg  esac
322138a9f8aSmrg
3238ae04fa6Smrg  obsolete_mkdir_used=false
3248ae04fa6Smrg
3258ae04fa6Smrg  if test $dstdir_status != 0; then
3268ae04fa6Smrg    case $posix_mkdir in
3278ae04fa6Smrg      '')
32880f56f3aSmrg        # With -d, create the new directory with the user-specified mode.
32980f56f3aSmrg        # Otherwise, rely on $mkdir_umask.
33080f56f3aSmrg        if test -n "$dir_arg"; then
33180f56f3aSmrg          mkdir_mode=-m$mode
33280f56f3aSmrg        else
33380f56f3aSmrg          mkdir_mode=
33480f56f3aSmrg        fi
33580f56f3aSmrg
33680f56f3aSmrg        posix_mkdir=false
337138a9f8aSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338138a9f8aSmrg	# here however when possible just to lower collision chance.
339138a9f8aSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340138a9f8aSmrg
341138a9f8aSmrg	trap '
342138a9f8aSmrg	  ret=$?
343138a9f8aSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344138a9f8aSmrg	  exit $ret
345138a9f8aSmrg	' 0
346138a9f8aSmrg
347138a9f8aSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348138a9f8aSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349138a9f8aSmrg	# directory is successfully created first before we actually test
350138a9f8aSmrg	# 'mkdir -p'.
351138a9f8aSmrg	if (umask $mkdir_umask &&
352138a9f8aSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353138a9f8aSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354138a9f8aSmrg	then
355138a9f8aSmrg	  if test -z "$dir_arg" || {
356138a9f8aSmrg	       # Check for POSIX incompatibilities with -m.
357138a9f8aSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358138a9f8aSmrg	       # other-writable bit of parent directory when it shouldn't.
359138a9f8aSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360138a9f8aSmrg	       test_tmpdir="$tmpdir/a"
361138a9f8aSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362138a9f8aSmrg	       case $ls_ld_tmpdir in
363138a9f8aSmrg		 d????-?r-*) different_mode=700;;
364138a9f8aSmrg		 d????-?--*) different_mode=755;;
365138a9f8aSmrg		 *) false;;
366138a9f8aSmrg	       esac &&
367138a9f8aSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368138a9f8aSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369138a9f8aSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370138a9f8aSmrg	       }
371138a9f8aSmrg	     }
372138a9f8aSmrg	  then posix_mkdir=:
373138a9f8aSmrg	  fi
374138a9f8aSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375138a9f8aSmrg	else
376138a9f8aSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377138a9f8aSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378138a9f8aSmrg	fi
379138a9f8aSmrg	trap '' 0;;
3808ae04fa6Smrg    esac
3818ae04fa6Smrg
3828ae04fa6Smrg    if
3838ae04fa6Smrg      $posix_mkdir && (
38480f56f3aSmrg        umask $mkdir_umask &&
38580f56f3aSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3868ae04fa6Smrg      )
3878ae04fa6Smrg    then :
3888ae04fa6Smrg    else
3898ae04fa6Smrg
390138a9f8aSmrg      # mkdir does not conform to POSIX,
3918ae04fa6Smrg      # or it failed possibly due to a race condition.  Create the
3928ae04fa6Smrg      # directory the slow way, step by step, checking for races as we go.
3938ae04fa6Smrg
3948ae04fa6Smrg      case $dstdir in
39580f56f3aSmrg        /*) prefix='/';;
39680f56f3aSmrg        [-=\(\)!]*) prefix='./';;
39780f56f3aSmrg        *)  prefix='';;
3988ae04fa6Smrg      esac
3998ae04fa6Smrg
4008ae04fa6Smrg      oIFS=$IFS
4018ae04fa6Smrg      IFS=/
40280f56f3aSmrg      set -f
4038ae04fa6Smrg      set fnord $dstdir
4048ae04fa6Smrg      shift
40580f56f3aSmrg      set +f
4068ae04fa6Smrg      IFS=$oIFS
4078ae04fa6Smrg
4088ae04fa6Smrg      prefixes=
4098ae04fa6Smrg
4108ae04fa6Smrg      for d
4118ae04fa6Smrg      do
41280f56f3aSmrg        test X"$d" = X && continue
41380f56f3aSmrg
41480f56f3aSmrg        prefix=$prefix$d
41580f56f3aSmrg        if test -d "$prefix"; then
41680f56f3aSmrg          prefixes=
41780f56f3aSmrg        else
41880f56f3aSmrg          if $posix_mkdir; then
419138a9f8aSmrg            (umask $mkdir_umask &&
42080f56f3aSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42180f56f3aSmrg            # Don't fail if two instances are running concurrently.
42280f56f3aSmrg            test -d "$prefix" || exit 1
42380f56f3aSmrg          else
42480f56f3aSmrg            case $prefix in
42580f56f3aSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42680f56f3aSmrg              *) qprefix=$prefix;;
42780f56f3aSmrg            esac
42880f56f3aSmrg            prefixes="$prefixes '$qprefix'"
42980f56f3aSmrg          fi
43080f56f3aSmrg        fi
43180f56f3aSmrg        prefix=$prefix/
4328ae04fa6Smrg      done
4338ae04fa6Smrg
4348ae04fa6Smrg      if test -n "$prefixes"; then
43580f56f3aSmrg        # Don't fail if two instances are running concurrently.
43680f56f3aSmrg        (umask $mkdir_umask &&
43780f56f3aSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43880f56f3aSmrg          test -d "$dstdir" || exit 1
43980f56f3aSmrg        obsolete_mkdir_used=true
4408ae04fa6Smrg      fi
4418ae04fa6Smrg    fi
4428ae04fa6Smrg  fi
4438ae04fa6Smrg
4448ae04fa6Smrg  if test -n "$dir_arg"; then
4458ae04fa6Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4468ae04fa6Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4478ae04fa6Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4488ae04fa6Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4498ae04fa6Smrg  else
4508ae04fa6Smrg
4518ae04fa6Smrg    # Make a couple of temp file names in the proper directory.
452138a9f8aSmrg    dsttmp=${dstdirslash}_inst.$$_
453138a9f8aSmrg    rmtmp=${dstdirslash}_rm.$$_
4548ae04fa6Smrg
4558ae04fa6Smrg    # Trap to clean up those temp files at exit.
4568ae04fa6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4578ae04fa6Smrg
4588ae04fa6Smrg    # Copy the file name to the temp name.
459138a9f8aSmrg    (umask $cp_umask &&
460138a9f8aSmrg     { test -z "$stripcmd" || {
461138a9f8aSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462138a9f8aSmrg	 # which would cause strip to fail.
463138a9f8aSmrg	 if test -z "$doit"; then
464138a9f8aSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465138a9f8aSmrg	 else
466138a9f8aSmrg	   $doit touch "$dsttmp"
467138a9f8aSmrg	 fi
468138a9f8aSmrg       }
469138a9f8aSmrg     } &&
470138a9f8aSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4718ae04fa6Smrg
4728ae04fa6Smrg    # and set any options; do chmod last to preserve setuid bits.
4738ae04fa6Smrg    #
4748ae04fa6Smrg    # If any of these fail, we abort the whole thing.  If we want to
4758ae04fa6Smrg    # ignore errors from any of these, just make sure not to ignore
4768ae04fa6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4778ae04fa6Smrg    #
47844af7230Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47944af7230Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48044af7230Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48144af7230Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48244af7230Smrg
48344af7230Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48444af7230Smrg    if $copy_on_change &&
48580f56f3aSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48680f56f3aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48780f56f3aSmrg       set -f &&
48844af7230Smrg       set X $old && old=:$2:$4:$5:$6 &&
48944af7230Smrg       set X $new && new=:$2:$4:$5:$6 &&
49080f56f3aSmrg       set +f &&
49144af7230Smrg       test "$old" = "$new" &&
49244af7230Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49344af7230Smrg    then
49444af7230Smrg      rm -f "$dsttmp"
49544af7230Smrg    else
496138a9f8aSmrg      # If $backupsuffix is set, and the file being installed
497138a9f8aSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498138a9f8aSmrg      # e.g., if mv doesn't support -f.
499138a9f8aSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500138a9f8aSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501138a9f8aSmrg      fi
502138a9f8aSmrg
50344af7230Smrg      # Rename the file to the real destination.
50444af7230Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50544af7230Smrg
50644af7230Smrg      # The rename failed, perhaps because mv can't rename something else
50744af7230Smrg      # to itself, or perhaps because mv is so ancient that it does not
50844af7230Smrg      # support -f.
50944af7230Smrg      {
51080f56f3aSmrg        # Now remove or move aside any old file at destination location.
51180f56f3aSmrg        # We try this two ways since rm can't unlink itself on some
51280f56f3aSmrg        # systems and the destination file might be busy for other
51380f56f3aSmrg        # reasons.  In this case, the final cleanup might fail but the new
51480f56f3aSmrg        # file should still install successfully.
51580f56f3aSmrg        {
51680f56f3aSmrg          test ! -f "$dst" ||
517138a9f8aSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51880f56f3aSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519138a9f8aSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52080f56f3aSmrg          } ||
52180f56f3aSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52280f56f3aSmrg            (exit 1); exit 1
52380f56f3aSmrg          }
52480f56f3aSmrg        } &&
52580f56f3aSmrg
52680f56f3aSmrg        # Now rename the file to the real destination.
52780f56f3aSmrg        $doit $mvcmd "$dsttmp" "$dst"
52844af7230Smrg      }
52944af7230Smrg    fi || exit 1
5308ae04fa6Smrg
5318ae04fa6Smrg    trap '' 0
5328ae04fa6Smrg  fi
5338ae04fa6Smrgdone
5348ae04fa6Smrg
5358ae04fa6Smrg# Local variables:
536138a9f8aSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5378ae04fa6Smrg# time-stamp-start: "scriptversion="
5388ae04fa6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539138a9f8aSmrg# time-stamp-time-zone: "UTC0"
54044af7230Smrg# time-stamp-end: "; # UTC"
5418ae04fa6Smrg# End:
542