xref: /netbsd-src/external/lgpl3/mpc/dist/build-aux/install-sh (revision 367b82799ab709709d3c3b541df56a2a14644d3e)
190a8ff21Smrg#!/bin/sh
290a8ff21Smrg# install - install a program, script, or datafile
390a8ff21Smrg
4*367b8279Smrgscriptversion=2020-11-14.01; # UTC
590a8ff21Smrg
690a8ff21Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
790a8ff21Smrg# later released in X11R6 (xc/config/util/install.sh) with the
890a8ff21Smrg# following copyright and license.
990a8ff21Smrg#
1090a8ff21Smrg# Copyright (C) 1994 X Consortium
1190a8ff21Smrg#
1290a8ff21Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1390a8ff21Smrg# of this software and associated documentation files (the "Software"), to
1490a8ff21Smrg# deal in the Software without restriction, including without limitation the
1590a8ff21Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1690a8ff21Smrg# sell copies of the Software, and to permit persons to whom the Software is
1790a8ff21Smrg# furnished to do so, subject to the following conditions:
1890a8ff21Smrg#
1990a8ff21Smrg# The above copyright notice and this permission notice shall be included in
2090a8ff21Smrg# all copies or substantial portions of the Software.
2190a8ff21Smrg#
2290a8ff21Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2390a8ff21Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2490a8ff21Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2590a8ff21Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2690a8ff21Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2790a8ff21Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2890a8ff21Smrg#
2990a8ff21Smrg# Except as contained in this notice, the name of the X Consortium shall not
3090a8ff21Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3190a8ff21Smrg# ings in this Software without prior written authorization from the X Consor-
3290a8ff21Smrg# tium.
3390a8ff21Smrg#
3490a8ff21Smrg#
3590a8ff21Smrg# FSF changes to this file are in the public domain.
3690a8ff21Smrg#
3790a8ff21Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3890a8ff21Smrg# 'make' implicit rules from creating a file called install from it
3990a8ff21Smrg# when there is no Makefile.
4090a8ff21Smrg#
4190a8ff21Smrg# This script is compatible with the BSD install script, but was written
4290a8ff21Smrg# from scratch.
4390a8ff21Smrg
4490a8ff21Smrgtab='	'
4590a8ff21Smrgnl='
4690a8ff21Smrg'
4790a8ff21SmrgIFS=" $tab$nl"
4890a8ff21Smrg
4990a8ff21Smrg# Set DOITPROG to "echo" to test this script.
5090a8ff21Smrg
5190a8ff21Smrgdoit=${DOITPROG-}
5290a8ff21Smrgdoit_exec=${doit:-exec}
5390a8ff21Smrg
5490a8ff21Smrg# Put in absolute file names if you don't have them in your path;
5590a8ff21Smrg# or use environment vars.
5690a8ff21Smrg
5790a8ff21Smrgchgrpprog=${CHGRPPROG-chgrp}
5890a8ff21Smrgchmodprog=${CHMODPROG-chmod}
5990a8ff21Smrgchownprog=${CHOWNPROG-chown}
6090a8ff21Smrgcmpprog=${CMPPROG-cmp}
6190a8ff21Smrgcpprog=${CPPROG-cp}
6290a8ff21Smrgmkdirprog=${MKDIRPROG-mkdir}
6390a8ff21Smrgmvprog=${MVPROG-mv}
6490a8ff21Smrgrmprog=${RMPROG-rm}
6590a8ff21Smrgstripprog=${STRIPPROG-strip}
6690a8ff21Smrg
6790a8ff21Smrgposix_mkdir=
6890a8ff21Smrg
6990a8ff21Smrg# Desired mode of installed file.
7090a8ff21Smrgmode=0755
7190a8ff21Smrg
72*367b8279Smrg# Create dirs (including intermediate dirs) using mode 755.
73*367b8279Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74*367b8279Smrgmkdir_umask=22
75*367b8279Smrg
76*367b8279Smrgbackupsuffix=
7790a8ff21Smrgchgrpcmd=
7890a8ff21Smrgchmodcmd=$chmodprog
7990a8ff21Smrgchowncmd=
8090a8ff21Smrgmvcmd=$mvprog
8190a8ff21Smrgrmcmd="$rmprog -f"
8290a8ff21Smrgstripcmd=
8390a8ff21Smrg
8490a8ff21Smrgsrc=
8590a8ff21Smrgdst=
8690a8ff21Smrgdir_arg=
8790a8ff21Smrgdst_arg=
8890a8ff21Smrg
8990a8ff21Smrgcopy_on_change=false
9090a8ff21Smrgis_target_a_directory=possibly
9190a8ff21Smrg
9290a8ff21Smrgusage="\
9390a8ff21SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9490a8ff21Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9590a8ff21Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9690a8ff21Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9790a8ff21Smrg
9890a8ff21SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9990a8ff21SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10090a8ff21SmrgIn the 4th, create DIRECTORIES.
10190a8ff21Smrg
10290a8ff21SmrgOptions:
10390a8ff21Smrg     --help     display this help and exit.
10490a8ff21Smrg     --version  display version info and exit.
10590a8ff21Smrg
10690a8ff21Smrg  -c            (ignored)
107*367b8279Smrg  -C            install only if different (preserve data modification time)
10890a8ff21Smrg  -d            create directories instead of installing files.
10990a8ff21Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11090a8ff21Smrg  -m MODE       $chmodprog installed files to MODE.
11190a8ff21Smrg  -o USER       $chownprog installed files to USER.
112*367b8279Smrg  -p            pass -p to $cpprog.
11390a8ff21Smrg  -s            $stripprog installed files.
114*367b8279Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11590a8ff21Smrg  -t DIRECTORY  install into DIRECTORY.
11690a8ff21Smrg  -T            report an error if DSTFILE is a directory.
11790a8ff21Smrg
11890a8ff21SmrgEnvironment variables override the default commands:
11990a8ff21Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12090a8ff21Smrg  RMPROG STRIPPROG
121*367b8279Smrg
122*367b8279SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123*367b8279Smrgit's up to you to specify -f if you want it.
124*367b8279Smrg
125*367b8279SmrgIf -S is not specified, no backups are attempted.
126*367b8279Smrg
127*367b8279SmrgEmail bug reports to bug-automake@gnu.org.
128*367b8279SmrgAutomake home page: https://www.gnu.org/software/automake/
12990a8ff21Smrg"
13090a8ff21Smrg
13190a8ff21Smrgwhile test $# -ne 0; do
13290a8ff21Smrg  case $1 in
13390a8ff21Smrg    -c) ;;
13490a8ff21Smrg
13590a8ff21Smrg    -C) copy_on_change=true;;
13690a8ff21Smrg
13790a8ff21Smrg    -d) dir_arg=true;;
13890a8ff21Smrg
13990a8ff21Smrg    -g) chgrpcmd="$chgrpprog $2"
14090a8ff21Smrg        shift;;
14190a8ff21Smrg
14290a8ff21Smrg    --help) echo "$usage"; exit $?;;
14390a8ff21Smrg
14490a8ff21Smrg    -m) mode=$2
14590a8ff21Smrg        case $mode in
14690a8ff21Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14790a8ff21Smrg            echo "$0: invalid mode: $mode" >&2
14890a8ff21Smrg            exit 1;;
14990a8ff21Smrg        esac
15090a8ff21Smrg        shift;;
15190a8ff21Smrg
15290a8ff21Smrg    -o) chowncmd="$chownprog $2"
15390a8ff21Smrg        shift;;
15490a8ff21Smrg
155*367b8279Smrg    -p) cpprog="$cpprog -p";;
156*367b8279Smrg
15790a8ff21Smrg    -s) stripcmd=$stripprog;;
15890a8ff21Smrg
159*367b8279Smrg    -S) backupsuffix="$2"
160*367b8279Smrg        shift;;
161*367b8279Smrg
16290a8ff21Smrg    -t)
16390a8ff21Smrg        is_target_a_directory=always
16490a8ff21Smrg        dst_arg=$2
16590a8ff21Smrg        # Protect names problematic for 'test' and other utilities.
16690a8ff21Smrg        case $dst_arg in
16790a8ff21Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16890a8ff21Smrg        esac
16990a8ff21Smrg        shift;;
17090a8ff21Smrg
17190a8ff21Smrg    -T) is_target_a_directory=never;;
17290a8ff21Smrg
17390a8ff21Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17490a8ff21Smrg
17590a8ff21Smrg    --) shift
17690a8ff21Smrg        break;;
17790a8ff21Smrg
17890a8ff21Smrg    -*) echo "$0: invalid option: $1" >&2
17990a8ff21Smrg        exit 1;;
18090a8ff21Smrg
18190a8ff21Smrg    *)  break;;
18290a8ff21Smrg  esac
18390a8ff21Smrg  shift
18490a8ff21Smrgdone
18590a8ff21Smrg
18690a8ff21Smrg# We allow the use of options -d and -T together, by making -d
18790a8ff21Smrg# take the precedence; this is for compatibility with GNU install.
18890a8ff21Smrg
18990a8ff21Smrgif test -n "$dir_arg"; then
19090a8ff21Smrg  if test -n "$dst_arg"; then
19190a8ff21Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19290a8ff21Smrg    exit 1
19390a8ff21Smrg  fi
19490a8ff21Smrgfi
19590a8ff21Smrg
19690a8ff21Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19790a8ff21Smrg  # When -d is used, all remaining arguments are directories to create.
19890a8ff21Smrg  # When -t is used, the destination is already specified.
19990a8ff21Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20090a8ff21Smrg  for arg
20190a8ff21Smrg  do
20290a8ff21Smrg    if test -n "$dst_arg"; then
20390a8ff21Smrg      # $@ is not empty: it contains at least $arg.
20490a8ff21Smrg      set fnord "$@" "$dst_arg"
20590a8ff21Smrg      shift # fnord
20690a8ff21Smrg    fi
20790a8ff21Smrg    shift # arg
20890a8ff21Smrg    dst_arg=$arg
20990a8ff21Smrg    # Protect names problematic for 'test' and other utilities.
21090a8ff21Smrg    case $dst_arg in
21190a8ff21Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21290a8ff21Smrg    esac
21390a8ff21Smrg  done
21490a8ff21Smrgfi
21590a8ff21Smrg
21690a8ff21Smrgif test $# -eq 0; then
21790a8ff21Smrg  if test -z "$dir_arg"; then
21890a8ff21Smrg    echo "$0: no input file specified." >&2
21990a8ff21Smrg    exit 1
22090a8ff21Smrg  fi
22190a8ff21Smrg  # It's OK to call 'install-sh -d' without argument.
22290a8ff21Smrg  # This can happen when creating conditional directories.
22390a8ff21Smrg  exit 0
22490a8ff21Smrgfi
22590a8ff21Smrg
22690a8ff21Smrgif test -z "$dir_arg"; then
22790a8ff21Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22890a8ff21Smrg    if test ! -d "$dst_arg"; then
22990a8ff21Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23090a8ff21Smrg      exit 1
23190a8ff21Smrg    fi
23290a8ff21Smrg  fi
23390a8ff21Smrgfi
23490a8ff21Smrg
23590a8ff21Smrgif test -z "$dir_arg"; then
23690a8ff21Smrg  do_exit='(exit $ret); exit $ret'
23790a8ff21Smrg  trap "ret=129; $do_exit" 1
23890a8ff21Smrg  trap "ret=130; $do_exit" 2
23990a8ff21Smrg  trap "ret=141; $do_exit" 13
24090a8ff21Smrg  trap "ret=143; $do_exit" 15
24190a8ff21Smrg
24290a8ff21Smrg  # Set umask so as not to create temps with too-generous modes.
24390a8ff21Smrg  # However, 'strip' requires both read and write access to temps.
24490a8ff21Smrg  case $mode in
24590a8ff21Smrg    # Optimize common cases.
24690a8ff21Smrg    *644) cp_umask=133;;
24790a8ff21Smrg    *755) cp_umask=22;;
24890a8ff21Smrg
24990a8ff21Smrg    *[0-7])
25090a8ff21Smrg      if test -z "$stripcmd"; then
25190a8ff21Smrg        u_plus_rw=
25290a8ff21Smrg      else
25390a8ff21Smrg        u_plus_rw='% 200'
25490a8ff21Smrg      fi
25590a8ff21Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25690a8ff21Smrg    *)
25790a8ff21Smrg      if test -z "$stripcmd"; then
25890a8ff21Smrg        u_plus_rw=
25990a8ff21Smrg      else
26090a8ff21Smrg        u_plus_rw=,u+rw
26190a8ff21Smrg      fi
26290a8ff21Smrg      cp_umask=$mode$u_plus_rw;;
26390a8ff21Smrg  esac
26490a8ff21Smrgfi
26590a8ff21Smrg
26690a8ff21Smrgfor src
26790a8ff21Smrgdo
26890a8ff21Smrg  # Protect names problematic for 'test' and other utilities.
26990a8ff21Smrg  case $src in
27090a8ff21Smrg    -* | [=\(\)!]) src=./$src;;
27190a8ff21Smrg  esac
27290a8ff21Smrg
27390a8ff21Smrg  if test -n "$dir_arg"; then
27490a8ff21Smrg    dst=$src
27590a8ff21Smrg    dstdir=$dst
27690a8ff21Smrg    test -d "$dstdir"
27790a8ff21Smrg    dstdir_status=$?
278*367b8279Smrg    # Don't chown directories that already exist.
279*367b8279Smrg    if test $dstdir_status = 0; then
280*367b8279Smrg      chowncmd=""
281*367b8279Smrg    fi
28290a8ff21Smrg  else
28390a8ff21Smrg
28490a8ff21Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28590a8ff21Smrg    # might cause directories to be created, which would be especially bad
28690a8ff21Smrg    # if $src (and thus $dsttmp) contains '*'.
28790a8ff21Smrg    if test ! -f "$src" && test ! -d "$src"; then
28890a8ff21Smrg      echo "$0: $src does not exist." >&2
28990a8ff21Smrg      exit 1
29090a8ff21Smrg    fi
29190a8ff21Smrg
29290a8ff21Smrg    if test -z "$dst_arg"; then
29390a8ff21Smrg      echo "$0: no destination specified." >&2
29490a8ff21Smrg      exit 1
29590a8ff21Smrg    fi
29690a8ff21Smrg    dst=$dst_arg
29790a8ff21Smrg
29890a8ff21Smrg    # If destination is a directory, append the input filename.
29990a8ff21Smrg    if test -d "$dst"; then
30090a8ff21Smrg      if test "$is_target_a_directory" = never; then
30190a8ff21Smrg        echo "$0: $dst_arg: Is a directory" >&2
30290a8ff21Smrg        exit 1
30390a8ff21Smrg      fi
30490a8ff21Smrg      dstdir=$dst
30590a8ff21Smrg      dstbase=`basename "$src"`
30690a8ff21Smrg      case $dst in
30790a8ff21Smrg	*/) dst=$dst$dstbase;;
30890a8ff21Smrg	*)  dst=$dst/$dstbase;;
30990a8ff21Smrg      esac
31090a8ff21Smrg      dstdir_status=0
31190a8ff21Smrg    else
31290a8ff21Smrg      dstdir=`dirname "$dst"`
31390a8ff21Smrg      test -d "$dstdir"
31490a8ff21Smrg      dstdir_status=$?
31590a8ff21Smrg    fi
31690a8ff21Smrg  fi
31790a8ff21Smrg
31890a8ff21Smrg  case $dstdir in
31990a8ff21Smrg    */) dstdirslash=$dstdir;;
32090a8ff21Smrg    *)  dstdirslash=$dstdir/;;
32190a8ff21Smrg  esac
32290a8ff21Smrg
32390a8ff21Smrg  obsolete_mkdir_used=false
32490a8ff21Smrg
32590a8ff21Smrg  if test $dstdir_status != 0; then
32690a8ff21Smrg    case $posix_mkdir in
32790a8ff21Smrg      '')
32890a8ff21Smrg        # With -d, create the new directory with the user-specified mode.
32990a8ff21Smrg        # Otherwise, rely on $mkdir_umask.
33090a8ff21Smrg        if test -n "$dir_arg"; then
33190a8ff21Smrg          mkdir_mode=-m$mode
33290a8ff21Smrg        else
33390a8ff21Smrg          mkdir_mode=
33490a8ff21Smrg        fi
33590a8ff21Smrg
33690a8ff21Smrg        posix_mkdir=false
337*367b8279Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33890a8ff21Smrg	# here however when possible just to lower collision chance.
33990a8ff21Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34090a8ff21Smrg
341*367b8279Smrg	trap '
342*367b8279Smrg	  ret=$?
343*367b8279Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344*367b8279Smrg	  exit $ret
345*367b8279Smrg	' 0
34690a8ff21Smrg
34790a8ff21Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34890a8ff21Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34990a8ff21Smrg	# directory is successfully created first before we actually test
350*367b8279Smrg	# 'mkdir -p'.
35190a8ff21Smrg	if (umask $mkdir_umask &&
35290a8ff21Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35390a8ff21Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35490a8ff21Smrg	then
35590a8ff21Smrg	  if test -z "$dir_arg" || {
35690a8ff21Smrg	       # Check for POSIX incompatibilities with -m.
35790a8ff21Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35890a8ff21Smrg	       # other-writable bit of parent directory when it shouldn't.
35990a8ff21Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36090a8ff21Smrg	       test_tmpdir="$tmpdir/a"
36190a8ff21Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36290a8ff21Smrg	       case $ls_ld_tmpdir in
36390a8ff21Smrg		 d????-?r-*) different_mode=700;;
36490a8ff21Smrg		 d????-?--*) different_mode=755;;
36590a8ff21Smrg		 *) false;;
36690a8ff21Smrg	       esac &&
36790a8ff21Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36890a8ff21Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36990a8ff21Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37090a8ff21Smrg	       }
37190a8ff21Smrg	     }
37290a8ff21Smrg	  then posix_mkdir=:
37390a8ff21Smrg	  fi
37490a8ff21Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37590a8ff21Smrg	else
37690a8ff21Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37790a8ff21Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37890a8ff21Smrg	fi
37990a8ff21Smrg	trap '' 0;;
38090a8ff21Smrg    esac
38190a8ff21Smrg
38290a8ff21Smrg    if
38390a8ff21Smrg      $posix_mkdir && (
38490a8ff21Smrg        umask $mkdir_umask &&
38590a8ff21Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38690a8ff21Smrg      )
38790a8ff21Smrg    then :
38890a8ff21Smrg    else
38990a8ff21Smrg
390*367b8279Smrg      # mkdir does not conform to POSIX,
39190a8ff21Smrg      # or it failed possibly due to a race condition.  Create the
39290a8ff21Smrg      # directory the slow way, step by step, checking for races as we go.
39390a8ff21Smrg
39490a8ff21Smrg      case $dstdir in
39590a8ff21Smrg        /*) prefix='/';;
39690a8ff21Smrg        [-=\(\)!]*) prefix='./';;
39790a8ff21Smrg        *)  prefix='';;
39890a8ff21Smrg      esac
39990a8ff21Smrg
40090a8ff21Smrg      oIFS=$IFS
40190a8ff21Smrg      IFS=/
40290a8ff21Smrg      set -f
40390a8ff21Smrg      set fnord $dstdir
40490a8ff21Smrg      shift
40590a8ff21Smrg      set +f
40690a8ff21Smrg      IFS=$oIFS
40790a8ff21Smrg
40890a8ff21Smrg      prefixes=
40990a8ff21Smrg
41090a8ff21Smrg      for d
41190a8ff21Smrg      do
41290a8ff21Smrg        test X"$d" = X && continue
41390a8ff21Smrg
41490a8ff21Smrg        prefix=$prefix$d
41590a8ff21Smrg        if test -d "$prefix"; then
41690a8ff21Smrg          prefixes=
41790a8ff21Smrg        else
41890a8ff21Smrg          if $posix_mkdir; then
419*367b8279Smrg            (umask $mkdir_umask &&
42090a8ff21Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42190a8ff21Smrg            # Don't fail if two instances are running concurrently.
42290a8ff21Smrg            test -d "$prefix" || exit 1
42390a8ff21Smrg          else
42490a8ff21Smrg            case $prefix in
42590a8ff21Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42690a8ff21Smrg              *) qprefix=$prefix;;
42790a8ff21Smrg            esac
42890a8ff21Smrg            prefixes="$prefixes '$qprefix'"
42990a8ff21Smrg          fi
43090a8ff21Smrg        fi
43190a8ff21Smrg        prefix=$prefix/
43290a8ff21Smrg      done
43390a8ff21Smrg
43490a8ff21Smrg      if test -n "$prefixes"; then
43590a8ff21Smrg        # Don't fail if two instances are running concurrently.
43690a8ff21Smrg        (umask $mkdir_umask &&
43790a8ff21Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43890a8ff21Smrg          test -d "$dstdir" || exit 1
43990a8ff21Smrg        obsolete_mkdir_used=true
44090a8ff21Smrg      fi
44190a8ff21Smrg    fi
44290a8ff21Smrg  fi
44390a8ff21Smrg
44490a8ff21Smrg  if test -n "$dir_arg"; then
44590a8ff21Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44690a8ff21Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44790a8ff21Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44890a8ff21Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44990a8ff21Smrg  else
45090a8ff21Smrg
45190a8ff21Smrg    # Make a couple of temp file names in the proper directory.
45290a8ff21Smrg    dsttmp=${dstdirslash}_inst.$$_
45390a8ff21Smrg    rmtmp=${dstdirslash}_rm.$$_
45490a8ff21Smrg
45590a8ff21Smrg    # Trap to clean up those temp files at exit.
45690a8ff21Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45790a8ff21Smrg
45890a8ff21Smrg    # Copy the file name to the temp name.
45990a8ff21Smrg    (umask $cp_umask &&
46090a8ff21Smrg     { test -z "$stripcmd" || {
46190a8ff21Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46290a8ff21Smrg	 # which would cause strip to fail.
46390a8ff21Smrg	 if test -z "$doit"; then
46490a8ff21Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46590a8ff21Smrg	 else
46690a8ff21Smrg	   $doit touch "$dsttmp"
46790a8ff21Smrg	 fi
46890a8ff21Smrg       }
46990a8ff21Smrg     } &&
47090a8ff21Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47190a8ff21Smrg
47290a8ff21Smrg    # and set any options; do chmod last to preserve setuid bits.
47390a8ff21Smrg    #
47490a8ff21Smrg    # If any of these fail, we abort the whole thing.  If we want to
47590a8ff21Smrg    # ignore errors from any of these, just make sure not to ignore
47690a8ff21Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47790a8ff21Smrg    #
47890a8ff21Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47990a8ff21Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48090a8ff21Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48190a8ff21Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48290a8ff21Smrg
48390a8ff21Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48490a8ff21Smrg    if $copy_on_change &&
48590a8ff21Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48690a8ff21Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48790a8ff21Smrg       set -f &&
48890a8ff21Smrg       set X $old && old=:$2:$4:$5:$6 &&
48990a8ff21Smrg       set X $new && new=:$2:$4:$5:$6 &&
49090a8ff21Smrg       set +f &&
49190a8ff21Smrg       test "$old" = "$new" &&
49290a8ff21Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49390a8ff21Smrg    then
49490a8ff21Smrg      rm -f "$dsttmp"
49590a8ff21Smrg    else
496*367b8279Smrg      # If $backupsuffix is set, and the file being installed
497*367b8279Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498*367b8279Smrg      # e.g., if mv doesn't support -f.
499*367b8279Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500*367b8279Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501*367b8279Smrg      fi
502*367b8279Smrg
50390a8ff21Smrg      # Rename the file to the real destination.
50490a8ff21Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50590a8ff21Smrg
50690a8ff21Smrg      # The rename failed, perhaps because mv can't rename something else
50790a8ff21Smrg      # to itself, or perhaps because mv is so ancient that it does not
50890a8ff21Smrg      # support -f.
50990a8ff21Smrg      {
51090a8ff21Smrg        # Now remove or move aside any old file at destination location.
51190a8ff21Smrg        # We try this two ways since rm can't unlink itself on some
51290a8ff21Smrg        # systems and the destination file might be busy for other
51390a8ff21Smrg        # reasons.  In this case, the final cleanup might fail but the new
51490a8ff21Smrg        # file should still install successfully.
51590a8ff21Smrg        {
51690a8ff21Smrg          test ! -f "$dst" ||
517*367b8279Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51890a8ff21Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519*367b8279Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52090a8ff21Smrg          } ||
52190a8ff21Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52290a8ff21Smrg            (exit 1); exit 1
52390a8ff21Smrg          }
52490a8ff21Smrg        } &&
52590a8ff21Smrg
52690a8ff21Smrg        # Now rename the file to the real destination.
52790a8ff21Smrg        $doit $mvcmd "$dsttmp" "$dst"
52890a8ff21Smrg      }
52990a8ff21Smrg    fi || exit 1
53090a8ff21Smrg
53190a8ff21Smrg    trap '' 0
53290a8ff21Smrg  fi
53390a8ff21Smrgdone
53490a8ff21Smrg
53590a8ff21Smrg# Local variables:
53690a8ff21Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53790a8ff21Smrg# time-stamp-start: "scriptversion="
53890a8ff21Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53990a8ff21Smrg# time-stamp-time-zone: "UTC0"
54090a8ff21Smrg# time-stamp-end: "; # UTC"
54190a8ff21Smrg# End:
542