install-sh revision 7b5fefc8
1644fc5c1Stron#!/bin/sh
2644fc5c1Stron# install - install a program, script, or datafile
32227fc13Smrg
47b5fefc8Smrgscriptversion=2020-11-14.01; # UTC
52227fc13Smrg
6644fc5c1Stron# This originates from X11R5 (mit/util/scripts/install.sh), which was
7644fc5c1Stron# later released in X11R6 (xc/config/util/install.sh) with the
8644fc5c1Stron# following copyright and license.
9644fc5c1Stron#
10644fc5c1Stron# Copyright (C) 1994 X Consortium
11644fc5c1Stron#
12644fc5c1Stron# Permission is hereby granted, free of charge, to any person obtaining a copy
13644fc5c1Stron# of this software and associated documentation files (the "Software"), to
14644fc5c1Stron# deal in the Software without restriction, including without limitation the
15644fc5c1Stron# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16644fc5c1Stron# sell copies of the Software, and to permit persons to whom the Software is
17644fc5c1Stron# furnished to do so, subject to the following conditions:
18644fc5c1Stron#
19644fc5c1Stron# The above copyright notice and this permission notice shall be included in
20644fc5c1Stron# all copies or substantial portions of the Software.
21644fc5c1Stron#
22644fc5c1Stron# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23644fc5c1Stron# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24644fc5c1Stron# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25644fc5c1Stron# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26644fc5c1Stron# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27644fc5c1Stron# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28644fc5c1Stron#
29644fc5c1Stron# Except as contained in this notice, the name of the X Consortium shall not
30644fc5c1Stron# be used in advertising or otherwise to promote the sale, use or other deal-
31644fc5c1Stron# ings in this Software without prior written authorization from the X Consor-
32644fc5c1Stron# tium.
33644fc5c1Stron#
34644fc5c1Stron#
35644fc5c1Stron# FSF changes to this file are in the public domain.
36644fc5c1Stron#
37644fc5c1Stron# Calling this script install-sh is preferred over install.sh, to prevent
387b5fefc8Smrg# 'make' implicit rules from creating a file called install from it
39644fc5c1Stron# when there is no Makefile.
40644fc5c1Stron#
41644fc5c1Stron# This script is compatible with the BSD install script, but was written
422227fc13Smrg# from scratch.
43644fc5c1Stron
447b5fefc8Smrgtab='	'
452227fc13Smrgnl='
462227fc13Smrg'
477b5fefc8SmrgIFS=" $tab$nl"
48644fc5c1Stron
497b5fefc8Smrg# Set DOITPROG to "echo" to test this script.
50644fc5c1Stron
512227fc13Smrgdoit=${DOITPROG-}
527b5fefc8Smrgdoit_exec=${doit:-exec}
53644fc5c1Stron
542227fc13Smrg# Put in absolute file names if you don't have them in your path;
552227fc13Smrg# or use environment vars.
562227fc13Smrg
572227fc13Smrgchgrpprog=${CHGRPPROG-chgrp}
582227fc13Smrgchmodprog=${CHMODPROG-chmod}
592227fc13Smrgchownprog=${CHOWNPROG-chown}
602227fc13Smrgcmpprog=${CMPPROG-cmp}
612227fc13Smrgcpprog=${CPPROG-cp}
622227fc13Smrgmkdirprog=${MKDIRPROG-mkdir}
632227fc13Smrgmvprog=${MVPROG-mv}
642227fc13Smrgrmprog=${RMPROG-rm}
652227fc13Smrgstripprog=${STRIPPROG-strip}
662227fc13Smrg
672227fc13Smrgposix_mkdir=
682227fc13Smrg
692227fc13Smrg# Desired mode of installed file.
702227fc13Smrgmode=0755
712227fc13Smrg
727b5fefc8Smrg# Create dirs (including intermediate dirs) using mode 755.
737b5fefc8Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
747b5fefc8Smrgmkdir_umask=22
757b5fefc8Smrg
767b5fefc8Smrgbackupsuffix=
772227fc13Smrgchgrpcmd=
782227fc13Smrgchmodcmd=$chmodprog
792227fc13Smrgchowncmd=
802227fc13Smrgmvcmd=$mvprog
812227fc13Smrgrmcmd="$rmprog -f"
822227fc13Smrgstripcmd=
83644fc5c1Stron
842227fc13Smrgsrc=
852227fc13Smrgdst=
862227fc13Smrgdir_arg=
872227fc13Smrgdst_arg=
88644fc5c1Stron
892227fc13Smrgcopy_on_change=false
907b5fefc8Smrgis_target_a_directory=possibly
91644fc5c1Stron
922227fc13Smrgusage="\
932227fc13SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
942227fc13Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
952227fc13Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
962227fc13Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97644fc5c1Stron
982227fc13SmrgIn the 1st form, copy SRCFILE to DSTFILE.
992227fc13SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1002227fc13SmrgIn the 4th, create DIRECTORIES.
101644fc5c1Stron
1022227fc13SmrgOptions:
1032227fc13Smrg     --help     display this help and exit.
1042227fc13Smrg     --version  display version info and exit.
105644fc5c1Stron
1062227fc13Smrg  -c            (ignored)
1077b5fefc8Smrg  -C            install only if different (preserve data modification time)
1082227fc13Smrg  -d            create directories instead of installing files.
1092227fc13Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1102227fc13Smrg  -m MODE       $chmodprog installed files to MODE.
1112227fc13Smrg  -o USER       $chownprog installed files to USER.
1127b5fefc8Smrg  -p            pass -p to $cpprog.
1132227fc13Smrg  -s            $stripprog installed files.
1147b5fefc8Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1152227fc13Smrg  -t DIRECTORY  install into DIRECTORY.
1162227fc13Smrg  -T            report an error if DSTFILE is a directory.
117644fc5c1Stron
1182227fc13SmrgEnvironment variables override the default commands:
1192227fc13Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1202227fc13Smrg  RMPROG STRIPPROG
1217b5fefc8Smrg
1227b5fefc8SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1237b5fefc8Smrgit's up to you to specify -f if you want it.
1247b5fefc8Smrg
1257b5fefc8SmrgIf -S is not specified, no backups are attempted.
1267b5fefc8Smrg
1277b5fefc8SmrgEmail bug reports to bug-automake@gnu.org.
1287b5fefc8SmrgAutomake home page: https://www.gnu.org/software/automake/
1292227fc13Smrg"
130644fc5c1Stron
1312227fc13Smrgwhile test $# -ne 0; do
1322227fc13Smrg  case $1 in
1332227fc13Smrg    -c) ;;
134644fc5c1Stron
1352227fc13Smrg    -C) copy_on_change=true;;
136644fc5c1Stron
1372227fc13Smrg    -d) dir_arg=true;;
138644fc5c1Stron
1392227fc13Smrg    -g) chgrpcmd="$chgrpprog $2"
1407b5fefc8Smrg        shift;;
141644fc5c1Stron
1422227fc13Smrg    --help) echo "$usage"; exit $?;;
143644fc5c1Stron
1442227fc13Smrg    -m) mode=$2
1457b5fefc8Smrg        case $mode in
1467b5fefc8Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1477b5fefc8Smrg            echo "$0: invalid mode: $mode" >&2
1487b5fefc8Smrg            exit 1;;
1497b5fefc8Smrg        esac
1507b5fefc8Smrg        shift;;
151644fc5c1Stron
1522227fc13Smrg    -o) chowncmd="$chownprog $2"
1537b5fefc8Smrg        shift;;
1547b5fefc8Smrg
1557b5fefc8Smrg    -p) cpprog="$cpprog -p";;
156644fc5c1Stron
1572227fc13Smrg    -s) stripcmd=$stripprog;;
158644fc5c1Stron
1597b5fefc8Smrg    -S) backupsuffix="$2"
1607b5fefc8Smrg        shift;;
1617b5fefc8Smrg
1627b5fefc8Smrg    -t)
1637b5fefc8Smrg        is_target_a_directory=always
1647b5fefc8Smrg        dst_arg=$2
1657b5fefc8Smrg        # Protect names problematic for 'test' and other utilities.
1667b5fefc8Smrg        case $dst_arg in
1677b5fefc8Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1687b5fefc8Smrg        esac
1697b5fefc8Smrg        shift;;
170644fc5c1Stron
1717b5fefc8Smrg    -T) is_target_a_directory=never;;
172644fc5c1Stron
1732227fc13Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174644fc5c1Stron
1757b5fefc8Smrg    --) shift
1767b5fefc8Smrg        break;;
177644fc5c1Stron
1787b5fefc8Smrg    -*) echo "$0: invalid option: $1" >&2
1797b5fefc8Smrg        exit 1;;
180644fc5c1Stron
1812227fc13Smrg    *)  break;;
1822227fc13Smrg  esac
1832227fc13Smrg  shift
1842227fc13Smrgdone
185644fc5c1Stron
1867b5fefc8Smrg# We allow the use of options -d and -T together, by making -d
1877b5fefc8Smrg# take the precedence; this is for compatibility with GNU install.
1887b5fefc8Smrg
1897b5fefc8Smrgif test -n "$dir_arg"; then
1907b5fefc8Smrg  if test -n "$dst_arg"; then
1917b5fefc8Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1927b5fefc8Smrg    exit 1
1937b5fefc8Smrg  fi
1947b5fefc8Smrgfi
1957b5fefc8Smrg
1962227fc13Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1972227fc13Smrg  # When -d is used, all remaining arguments are directories to create.
1982227fc13Smrg  # When -t is used, the destination is already specified.
1992227fc13Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2002227fc13Smrg  for arg
2012227fc13Smrg  do
2022227fc13Smrg    if test -n "$dst_arg"; then
2032227fc13Smrg      # $@ is not empty: it contains at least $arg.
2042227fc13Smrg      set fnord "$@" "$dst_arg"
2052227fc13Smrg      shift # fnord
2062227fc13Smrg    fi
2072227fc13Smrg    shift # arg
2082227fc13Smrg    dst_arg=$arg
2097b5fefc8Smrg    # Protect names problematic for 'test' and other utilities.
2107b5fefc8Smrg    case $dst_arg in
2117b5fefc8Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2127b5fefc8Smrg    esac
2132227fc13Smrg  done
2142227fc13Smrgfi
215644fc5c1Stron
2162227fc13Smrgif test $# -eq 0; then
2172227fc13Smrg  if test -z "$dir_arg"; then
2182227fc13Smrg    echo "$0: no input file specified." >&2
2192227fc13Smrg    exit 1
2202227fc13Smrg  fi
2217b5fefc8Smrg  # It's OK to call 'install-sh -d' without argument.
2222227fc13Smrg  # This can happen when creating conditional directories.
2232227fc13Smrg  exit 0
2242227fc13Smrgfi
225644fc5c1Stron
2262227fc13Smrgif test -z "$dir_arg"; then
2277b5fefc8Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2287b5fefc8Smrg    if test ! -d "$dst_arg"; then
2297b5fefc8Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2307b5fefc8Smrg      exit 1
2317b5fefc8Smrg    fi
2327b5fefc8Smrg  fi
2337b5fefc8Smrgfi
2347b5fefc8Smrg
2357b5fefc8Smrgif test -z "$dir_arg"; then
2367b5fefc8Smrg  do_exit='(exit $ret); exit $ret'
2377b5fefc8Smrg  trap "ret=129; $do_exit" 1
2387b5fefc8Smrg  trap "ret=130; $do_exit" 2
2397b5fefc8Smrg  trap "ret=141; $do_exit" 13
2407b5fefc8Smrg  trap "ret=143; $do_exit" 15
2412227fc13Smrg
2422227fc13Smrg  # Set umask so as not to create temps with too-generous modes.
2432227fc13Smrg  # However, 'strip' requires both read and write access to temps.
2442227fc13Smrg  case $mode in
2452227fc13Smrg    # Optimize common cases.
2462227fc13Smrg    *644) cp_umask=133;;
2472227fc13Smrg    *755) cp_umask=22;;
2482227fc13Smrg
2492227fc13Smrg    *[0-7])
2502227fc13Smrg      if test -z "$stripcmd"; then
2517b5fefc8Smrg        u_plus_rw=
2522227fc13Smrg      else
2537b5fefc8Smrg        u_plus_rw='% 200'
2542227fc13Smrg      fi
2552227fc13Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2562227fc13Smrg    *)
2572227fc13Smrg      if test -z "$stripcmd"; then
2587b5fefc8Smrg        u_plus_rw=
2592227fc13Smrg      else
2607b5fefc8Smrg        u_plus_rw=,u+rw
2612227fc13Smrg      fi
2622227fc13Smrg      cp_umask=$mode$u_plus_rw;;
2632227fc13Smrg  esac
2642227fc13Smrgfi
265644fc5c1Stron
2662227fc13Smrgfor src
2672227fc13Smrgdo
2687b5fefc8Smrg  # Protect names problematic for 'test' and other utilities.
2692227fc13Smrg  case $src in
2707b5fefc8Smrg    -* | [=\(\)!]) src=./$src;;
2712227fc13Smrg  esac
2722227fc13Smrg
2732227fc13Smrg  if test -n "$dir_arg"; then
2742227fc13Smrg    dst=$src
2752227fc13Smrg    dstdir=$dst
2762227fc13Smrg    test -d "$dstdir"
2772227fc13Smrg    dstdir_status=$?
2787b5fefc8Smrg    # Don't chown directories that already exist.
2797b5fefc8Smrg    if test $dstdir_status = 0; then
2807b5fefc8Smrg      chowncmd=""
2817b5fefc8Smrg    fi
2822227fc13Smrg  else
2832227fc13Smrg
2842227fc13Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2852227fc13Smrg    # might cause directories to be created, which would be especially bad
2862227fc13Smrg    # if $src (and thus $dsttmp) contains '*'.
2872227fc13Smrg    if test ! -f "$src" && test ! -d "$src"; then
2882227fc13Smrg      echo "$0: $src does not exist." >&2
2892227fc13Smrg      exit 1
2902227fc13Smrg    fi
2912227fc13Smrg
2922227fc13Smrg    if test -z "$dst_arg"; then
2932227fc13Smrg      echo "$0: no destination specified." >&2
2942227fc13Smrg      exit 1
2952227fc13Smrg    fi
2962227fc13Smrg    dst=$dst_arg
297644fc5c1Stron
2987b5fefc8Smrg    # If destination is a directory, append the input filename.
2992227fc13Smrg    if test -d "$dst"; then
3007b5fefc8Smrg      if test "$is_target_a_directory" = never; then
3017b5fefc8Smrg        echo "$0: $dst_arg: Is a directory" >&2
3027b5fefc8Smrg        exit 1
3032227fc13Smrg      fi
3042227fc13Smrg      dstdir=$dst
3057b5fefc8Smrg      dstbase=`basename "$src"`
3067b5fefc8Smrg      case $dst in
3077b5fefc8Smrg	*/) dst=$dst$dstbase;;
3087b5fefc8Smrg	*)  dst=$dst/$dstbase;;
3097b5fefc8Smrg      esac
3102227fc13Smrg      dstdir_status=0
3112227fc13Smrg    else
3127b5fefc8Smrg      dstdir=`dirname "$dst"`
3132227fc13Smrg      test -d "$dstdir"
3142227fc13Smrg      dstdir_status=$?
3152227fc13Smrg    fi
3162227fc13Smrg  fi
3172227fc13Smrg
3187b5fefc8Smrg  case $dstdir in
3197b5fefc8Smrg    */) dstdirslash=$dstdir;;
3207b5fefc8Smrg    *)  dstdirslash=$dstdir/;;
3217b5fefc8Smrg  esac
3227b5fefc8Smrg
3232227fc13Smrg  obsolete_mkdir_used=false
3242227fc13Smrg
3252227fc13Smrg  if test $dstdir_status != 0; then
3262227fc13Smrg    case $posix_mkdir in
3272227fc13Smrg      '')
3287b5fefc8Smrg        # With -d, create the new directory with the user-specified mode.
3297b5fefc8Smrg        # Otherwise, rely on $mkdir_umask.
3307b5fefc8Smrg        if test -n "$dir_arg"; then
3317b5fefc8Smrg          mkdir_mode=-m$mode
3327b5fefc8Smrg        else
3337b5fefc8Smrg          mkdir_mode=
3347b5fefc8Smrg        fi
3357b5fefc8Smrg
3367b5fefc8Smrg        posix_mkdir=false
3377b5fefc8Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3387b5fefc8Smrg	# here however when possible just to lower collision chance.
3397b5fefc8Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3407b5fefc8Smrg
3417b5fefc8Smrg	trap '
3427b5fefc8Smrg	  ret=$?
3437b5fefc8Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3447b5fefc8Smrg	  exit $ret
3457b5fefc8Smrg	' 0
3467b5fefc8Smrg
3477b5fefc8Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3487b5fefc8Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3497b5fefc8Smrg	# directory is successfully created first before we actually test
3507b5fefc8Smrg	# 'mkdir -p'.
3517b5fefc8Smrg	if (umask $mkdir_umask &&
3527b5fefc8Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3537b5fefc8Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3547b5fefc8Smrg	then
3557b5fefc8Smrg	  if test -z "$dir_arg" || {
3567b5fefc8Smrg	       # Check for POSIX incompatibilities with -m.
3577b5fefc8Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3587b5fefc8Smrg	       # other-writable bit of parent directory when it shouldn't.
3597b5fefc8Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3607b5fefc8Smrg	       test_tmpdir="$tmpdir/a"
3617b5fefc8Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3627b5fefc8Smrg	       case $ls_ld_tmpdir in
3637b5fefc8Smrg		 d????-?r-*) different_mode=700;;
3647b5fefc8Smrg		 d????-?--*) different_mode=755;;
3657b5fefc8Smrg		 *) false;;
3667b5fefc8Smrg	       esac &&
3677b5fefc8Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3687b5fefc8Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3697b5fefc8Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3707b5fefc8Smrg	       }
3717b5fefc8Smrg	     }
3727b5fefc8Smrg	  then posix_mkdir=:
3737b5fefc8Smrg	  fi
3747b5fefc8Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375644fc5c1Stron	else
3767b5fefc8Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3777b5fefc8Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378644fc5c1Stron	fi
3797b5fefc8Smrg	trap '' 0;;
3802227fc13Smrg    esac
381644fc5c1Stron
3822227fc13Smrg    if
3832227fc13Smrg      $posix_mkdir && (
3847b5fefc8Smrg        umask $mkdir_umask &&
3857b5fefc8Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3862227fc13Smrg      )
3872227fc13Smrg    then :
3882227fc13Smrg    else
3892227fc13Smrg
3907b5fefc8Smrg      # mkdir does not conform to POSIX,
3912227fc13Smrg      # or it failed possibly due to a race condition.  Create the
3922227fc13Smrg      # directory the slow way, step by step, checking for races as we go.
3932227fc13Smrg
3942227fc13Smrg      case $dstdir in
3957b5fefc8Smrg        /*) prefix='/';;
3967b5fefc8Smrg        [-=\(\)!]*) prefix='./';;
3977b5fefc8Smrg        *)  prefix='';;
3982227fc13Smrg      esac
3992227fc13Smrg
4002227fc13Smrg      oIFS=$IFS
4012227fc13Smrg      IFS=/
4027b5fefc8Smrg      set -f
4032227fc13Smrg      set fnord $dstdir
4042227fc13Smrg      shift
4057b5fefc8Smrg      set +f
4062227fc13Smrg      IFS=$oIFS
4072227fc13Smrg
4082227fc13Smrg      prefixes=
4092227fc13Smrg
4102227fc13Smrg      for d
4112227fc13Smrg      do
4127b5fefc8Smrg        test X"$d" = X && continue
4137b5fefc8Smrg
4147b5fefc8Smrg        prefix=$prefix$d
4157b5fefc8Smrg        if test -d "$prefix"; then
4167b5fefc8Smrg          prefixes=
4177b5fefc8Smrg        else
4187b5fefc8Smrg          if $posix_mkdir; then
4197b5fefc8Smrg            (umask $mkdir_umask &&
4207b5fefc8Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4217b5fefc8Smrg            # Don't fail if two instances are running concurrently.
4227b5fefc8Smrg            test -d "$prefix" || exit 1
4237b5fefc8Smrg          else
4247b5fefc8Smrg            case $prefix in
4257b5fefc8Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4267b5fefc8Smrg              *) qprefix=$prefix;;
4277b5fefc8Smrg            esac
4287b5fefc8Smrg            prefixes="$prefixes '$qprefix'"
4297b5fefc8Smrg          fi
4307b5fefc8Smrg        fi
4317b5fefc8Smrg        prefix=$prefix/
4322227fc13Smrg      done
4332227fc13Smrg
4342227fc13Smrg      if test -n "$prefixes"; then
4357b5fefc8Smrg        # Don't fail if two instances are running concurrently.
4367b5fefc8Smrg        (umask $mkdir_umask &&
4377b5fefc8Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4387b5fefc8Smrg          test -d "$dstdir" || exit 1
4397b5fefc8Smrg        obsolete_mkdir_used=true
4402227fc13Smrg      fi
4412227fc13Smrg    fi
4422227fc13Smrg  fi
4432227fc13Smrg
4442227fc13Smrg  if test -n "$dir_arg"; then
4452227fc13Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4462227fc13Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4472227fc13Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4482227fc13Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4492227fc13Smrg  else
4502227fc13Smrg
4512227fc13Smrg    # Make a couple of temp file names in the proper directory.
4527b5fefc8Smrg    dsttmp=${dstdirslash}_inst.$$_
4537b5fefc8Smrg    rmtmp=${dstdirslash}_rm.$$_
4542227fc13Smrg
4552227fc13Smrg    # Trap to clean up those temp files at exit.
4562227fc13Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4572227fc13Smrg
4582227fc13Smrg    # Copy the file name to the temp name.
4597b5fefc8Smrg    (umask $cp_umask &&
4607b5fefc8Smrg     { test -z "$stripcmd" || {
4617b5fefc8Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4627b5fefc8Smrg	 # which would cause strip to fail.
4637b5fefc8Smrg	 if test -z "$doit"; then
4647b5fefc8Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4657b5fefc8Smrg	 else
4667b5fefc8Smrg	   $doit touch "$dsttmp"
4677b5fefc8Smrg	 fi
4687b5fefc8Smrg       }
4697b5fefc8Smrg     } &&
4707b5fefc8Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4712227fc13Smrg
4722227fc13Smrg    # and set any options; do chmod last to preserve setuid bits.
4732227fc13Smrg    #
4742227fc13Smrg    # If any of these fail, we abort the whole thing.  If we want to
4752227fc13Smrg    # ignore errors from any of these, just make sure not to ignore
4762227fc13Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4772227fc13Smrg    #
4782227fc13Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4792227fc13Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4802227fc13Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4812227fc13Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4822227fc13Smrg
4832227fc13Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4842227fc13Smrg    if $copy_on_change &&
4857b5fefc8Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4867b5fefc8Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4877b5fefc8Smrg       set -f &&
4882227fc13Smrg       set X $old && old=:$2:$4:$5:$6 &&
4892227fc13Smrg       set X $new && new=:$2:$4:$5:$6 &&
4907b5fefc8Smrg       set +f &&
4912227fc13Smrg       test "$old" = "$new" &&
4922227fc13Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4932227fc13Smrg    then
4942227fc13Smrg      rm -f "$dsttmp"
4952227fc13Smrg    else
4967b5fefc8Smrg      # If $backupsuffix is set, and the file being installed
4977b5fefc8Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4987b5fefc8Smrg      # e.g., if mv doesn't support -f.
4997b5fefc8Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5007b5fefc8Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5017b5fefc8Smrg      fi
5027b5fefc8Smrg
5032227fc13Smrg      # Rename the file to the real destination.
5042227fc13Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5052227fc13Smrg
5062227fc13Smrg      # The rename failed, perhaps because mv can't rename something else
5072227fc13Smrg      # to itself, or perhaps because mv is so ancient that it does not
5082227fc13Smrg      # support -f.
5092227fc13Smrg      {
5107b5fefc8Smrg        # Now remove or move aside any old file at destination location.
5117b5fefc8Smrg        # We try this two ways since rm can't unlink itself on some
5127b5fefc8Smrg        # systems and the destination file might be busy for other
5137b5fefc8Smrg        # reasons.  In this case, the final cleanup might fail but the new
5147b5fefc8Smrg        # file should still install successfully.
5157b5fefc8Smrg        {
5167b5fefc8Smrg          test ! -f "$dst" ||
5177b5fefc8Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5187b5fefc8Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5197b5fefc8Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5207b5fefc8Smrg          } ||
5217b5fefc8Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5227b5fefc8Smrg            (exit 1); exit 1
5237b5fefc8Smrg          }
5247b5fefc8Smrg        } &&
5257b5fefc8Smrg
5267b5fefc8Smrg        # Now rename the file to the real destination.
5277b5fefc8Smrg        $doit $mvcmd "$dsttmp" "$dst"
5282227fc13Smrg      }
5292227fc13Smrg    fi || exit 1
5302227fc13Smrg
5312227fc13Smrg    trap '' 0
5322227fc13Smrg  fi
5332227fc13Smrgdone
534644fc5c1Stron
5352227fc13Smrg# Local variables:
5367b5fefc8Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5372227fc13Smrg# time-stamp-start: "scriptversion="
5382227fc13Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5397b5fefc8Smrg# time-stamp-time-zone: "UTC0"
5402227fc13Smrg# time-stamp-end: "; # UTC"
5412227fc13Smrg# End:
542