14a041c5bSmacallan#!/bin/sh
24a041c5bSmacallan# install - install a program, script, or datafile
34a041c5bSmacallan
48fc2d0c6Smrgscriptversion=2020-11-14.01; # UTC
54a041c5bSmacallan
64a041c5bSmacallan# This originates from X11R5 (mit/util/scripts/install.sh), which was
74a041c5bSmacallan# later released in X11R6 (xc/config/util/install.sh) with the
84a041c5bSmacallan# following copyright and license.
94a041c5bSmacallan#
104a041c5bSmacallan# Copyright (C) 1994 X Consortium
114a041c5bSmacallan#
124a041c5bSmacallan# Permission is hereby granted, free of charge, to any person obtaining a copy
134a041c5bSmacallan# of this software and associated documentation files (the "Software"), to
144a041c5bSmacallan# deal in the Software without restriction, including without limitation the
154a041c5bSmacallan# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164a041c5bSmacallan# sell copies of the Software, and to permit persons to whom the Software is
174a041c5bSmacallan# furnished to do so, subject to the following conditions:
184a041c5bSmacallan#
194a041c5bSmacallan# The above copyright notice and this permission notice shall be included in
204a041c5bSmacallan# all copies or substantial portions of the Software.
214a041c5bSmacallan#
224a041c5bSmacallan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234a041c5bSmacallan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244a041c5bSmacallan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254a041c5bSmacallan# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264a041c5bSmacallan# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274a041c5bSmacallan# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284a041c5bSmacallan#
294a041c5bSmacallan# Except as contained in this notice, the name of the X Consortium shall not
304a041c5bSmacallan# be used in advertising or otherwise to promote the sale, use or other deal-
314a041c5bSmacallan# ings in this Software without prior written authorization from the X Consor-
324a041c5bSmacallan# tium.
334a041c5bSmacallan#
344a041c5bSmacallan#
354a041c5bSmacallan# FSF changes to this file are in the public domain.
364a041c5bSmacallan#
374a041c5bSmacallan# Calling this script install-sh is preferred over install.sh, to prevent
3854b44505Smrg# 'make' implicit rules from creating a file called install from it
394a041c5bSmacallan# when there is no Makefile.
404a041c5bSmacallan#
414a041c5bSmacallan# This script is compatible with the BSD install script, but was written
424a041c5bSmacallan# from scratch.
434a041c5bSmacallan
440a392d7eSmrgtab='	'
454a041c5bSmacallannl='
464a041c5bSmacallan'
470a392d7eSmrgIFS=" $tab$nl"
484a041c5bSmacallan
490a392d7eSmrg# Set DOITPROG to "echo" to test this script.
504a041c5bSmacallan
514a041c5bSmacallandoit=${DOITPROG-}
520a392d7eSmrgdoit_exec=${doit:-exec}
534a041c5bSmacallan
544a041c5bSmacallan# Put in absolute file names if you don't have them in your path;
554a041c5bSmacallan# or use environment vars.
564a041c5bSmacallan
574a041c5bSmacallanchgrpprog=${CHGRPPROG-chgrp}
584a041c5bSmacallanchmodprog=${CHMODPROG-chmod}
594a041c5bSmacallanchownprog=${CHOWNPROG-chown}
604a041c5bSmacallancmpprog=${CMPPROG-cmp}
614a041c5bSmacallancpprog=${CPPROG-cp}
624a041c5bSmacallanmkdirprog=${MKDIRPROG-mkdir}
634a041c5bSmacallanmvprog=${MVPROG-mv}
644a041c5bSmacallanrmprog=${RMPROG-rm}
654a041c5bSmacallanstripprog=${STRIPPROG-strip}
664a041c5bSmacallan
674a041c5bSmacallanposix_mkdir=
684a041c5bSmacallan
694a041c5bSmacallan# Desired mode of installed file.
704a041c5bSmacallanmode=0755
714a041c5bSmacallan
728fc2d0c6Smrg# Create dirs (including intermediate dirs) using mode 755.
738fc2d0c6Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
748fc2d0c6Smrgmkdir_umask=22
758fc2d0c6Smrg
768fc2d0c6Smrgbackupsuffix=
774a041c5bSmacallanchgrpcmd=
784a041c5bSmacallanchmodcmd=$chmodprog
794a041c5bSmacallanchowncmd=
804a041c5bSmacallanmvcmd=$mvprog
814a041c5bSmacallanrmcmd="$rmprog -f"
824a041c5bSmacallanstripcmd=
834a041c5bSmacallan
844a041c5bSmacallansrc=
854a041c5bSmacallandst=
864a041c5bSmacallandir_arg=
874a041c5bSmacallandst_arg=
884a041c5bSmacallan
894a041c5bSmacallancopy_on_change=false
900a392d7eSmrgis_target_a_directory=possibly
914a041c5bSmacallan
924a041c5bSmacallanusage="\
934a041c5bSmacallanUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
944a041c5bSmacallan   or: $0 [OPTION]... SRCFILES... DIRECTORY
954a041c5bSmacallan   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
964a041c5bSmacallan   or: $0 [OPTION]... -d DIRECTORIES...
974a041c5bSmacallan
984a041c5bSmacallanIn the 1st form, copy SRCFILE to DSTFILE.
994a041c5bSmacallanIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1004a041c5bSmacallanIn the 4th, create DIRECTORIES.
1014a041c5bSmacallan
1024a041c5bSmacallanOptions:
1034a041c5bSmacallan     --help     display this help and exit.
1044a041c5bSmacallan     --version  display version info and exit.
1054a041c5bSmacallan
1064a041c5bSmacallan  -c            (ignored)
1078fc2d0c6Smrg  -C            install only if different (preserve data modification time)
1084a041c5bSmacallan  -d            create directories instead of installing files.
1094a041c5bSmacallan  -g GROUP      $chgrpprog installed files to GROUP.
1104a041c5bSmacallan  -m MODE       $chmodprog installed files to MODE.
1114a041c5bSmacallan  -o USER       $chownprog installed files to USER.
1128fc2d0c6Smrg  -p            pass -p to $cpprog.
1134a041c5bSmacallan  -s            $stripprog installed files.
1148fc2d0c6Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1154a041c5bSmacallan  -t DIRECTORY  install into DIRECTORY.
1164a041c5bSmacallan  -T            report an error if DSTFILE is a directory.
1174a041c5bSmacallan
1184a041c5bSmacallanEnvironment variables override the default commands:
1194a041c5bSmacallan  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1204a041c5bSmacallan  RMPROG STRIPPROG
1218fc2d0c6Smrg
1228fc2d0c6SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1238fc2d0c6Smrgit's up to you to specify -f if you want it.
1248fc2d0c6Smrg
1258fc2d0c6SmrgIf -S is not specified, no backups are attempted.
1268fc2d0c6Smrg
1278fc2d0c6SmrgEmail bug reports to bug-automake@gnu.org.
1288fc2d0c6SmrgAutomake home page: https://www.gnu.org/software/automake/
1294a041c5bSmacallan"
1304a041c5bSmacallan
1314a041c5bSmacallanwhile test $# -ne 0; do
1324a041c5bSmacallan  case $1 in
1334a041c5bSmacallan    -c) ;;
1344a041c5bSmacallan
1354a041c5bSmacallan    -C) copy_on_change=true;;
1364a041c5bSmacallan
1374a041c5bSmacallan    -d) dir_arg=true;;
1384a041c5bSmacallan
1394a041c5bSmacallan    -g) chgrpcmd="$chgrpprog $2"
1400a392d7eSmrg        shift;;
1414a041c5bSmacallan
1424a041c5bSmacallan    --help) echo "$usage"; exit $?;;
1434a041c5bSmacallan
1444a041c5bSmacallan    -m) mode=$2
1450a392d7eSmrg        case $mode in
1460a392d7eSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1470a392d7eSmrg            echo "$0: invalid mode: $mode" >&2
1480a392d7eSmrg            exit 1;;
1490a392d7eSmrg        esac
1500a392d7eSmrg        shift;;
1514a041c5bSmacallan
1524a041c5bSmacallan    -o) chowncmd="$chownprog $2"
1530a392d7eSmrg        shift;;
1544a041c5bSmacallan
1558fc2d0c6Smrg    -p) cpprog="$cpprog -p";;
1568fc2d0c6Smrg
1574a041c5bSmacallan    -s) stripcmd=$stripprog;;
1584a041c5bSmacallan
1598fc2d0c6Smrg    -S) backupsuffix="$2"
1608fc2d0c6Smrg        shift;;
1618fc2d0c6Smrg
1620a392d7eSmrg    -t)
1630a392d7eSmrg        is_target_a_directory=always
1640a392d7eSmrg        dst_arg=$2
1650a392d7eSmrg        # Protect names problematic for 'test' and other utilities.
1660a392d7eSmrg        case $dst_arg in
1670a392d7eSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1680a392d7eSmrg        esac
1690a392d7eSmrg        shift;;
1704a041c5bSmacallan
1710a392d7eSmrg    -T) is_target_a_directory=never;;
1724a041c5bSmacallan
1734a041c5bSmacallan    --version) echo "$0 $scriptversion"; exit $?;;
1744a041c5bSmacallan
1750a392d7eSmrg    --) shift
1760a392d7eSmrg        break;;
1774a041c5bSmacallan
1780a392d7eSmrg    -*) echo "$0: invalid option: $1" >&2
1790a392d7eSmrg        exit 1;;
1804a041c5bSmacallan
1814a041c5bSmacallan    *)  break;;
1824a041c5bSmacallan  esac
1834a041c5bSmacallan  shift
1844a041c5bSmacallandone
1854a041c5bSmacallan
1860a392d7eSmrg# We allow the use of options -d and -T together, by making -d
1870a392d7eSmrg# take the precedence; this is for compatibility with GNU install.
1880a392d7eSmrg
1890a392d7eSmrgif test -n "$dir_arg"; then
1900a392d7eSmrg  if test -n "$dst_arg"; then
1910a392d7eSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1920a392d7eSmrg    exit 1
1930a392d7eSmrg  fi
1940a392d7eSmrgfi
1950a392d7eSmrg
1964a041c5bSmacallanif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1974a041c5bSmacallan  # When -d is used, all remaining arguments are directories to create.
1984a041c5bSmacallan  # When -t is used, the destination is already specified.
1994a041c5bSmacallan  # Otherwise, the last argument is the destination.  Remove it from $@.
2004a041c5bSmacallan  for arg
2014a041c5bSmacallan  do
2024a041c5bSmacallan    if test -n "$dst_arg"; then
2034a041c5bSmacallan      # $@ is not empty: it contains at least $arg.
2044a041c5bSmacallan      set fnord "$@" "$dst_arg"
2054a041c5bSmacallan      shift # fnord
2064a041c5bSmacallan    fi
2074a041c5bSmacallan    shift # arg
2084a041c5bSmacallan    dst_arg=$arg
20954b44505Smrg    # Protect names problematic for 'test' and other utilities.
21054b44505Smrg    case $dst_arg in
21154b44505Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21254b44505Smrg    esac
2134a041c5bSmacallan  done
2144a041c5bSmacallanfi
2154a041c5bSmacallan
2164a041c5bSmacallanif test $# -eq 0; then
2174a041c5bSmacallan  if test -z "$dir_arg"; then
2184a041c5bSmacallan    echo "$0: no input file specified." >&2
2194a041c5bSmacallan    exit 1
2204a041c5bSmacallan  fi
22154b44505Smrg  # It's OK to call 'install-sh -d' without argument.
2224a041c5bSmacallan  # This can happen when creating conditional directories.
2234a041c5bSmacallan  exit 0
2244a041c5bSmacallanfi
2254a041c5bSmacallan
2260a392d7eSmrgif test -z "$dir_arg"; then
2270a392d7eSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2280a392d7eSmrg    if test ! -d "$dst_arg"; then
2290a392d7eSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2300a392d7eSmrg      exit 1
2310a392d7eSmrg    fi
2320a392d7eSmrg  fi
2330a392d7eSmrgfi
2340a392d7eSmrg
2354a041c5bSmacallanif test -z "$dir_arg"; then
23654b44505Smrg  do_exit='(exit $ret); exit $ret'
23754b44505Smrg  trap "ret=129; $do_exit" 1
23854b44505Smrg  trap "ret=130; $do_exit" 2
23954b44505Smrg  trap "ret=141; $do_exit" 13
24054b44505Smrg  trap "ret=143; $do_exit" 15
2414a041c5bSmacallan
2424a041c5bSmacallan  # Set umask so as not to create temps with too-generous modes.
2434a041c5bSmacallan  # However, 'strip' requires both read and write access to temps.
2444a041c5bSmacallan  case $mode in
2454a041c5bSmacallan    # Optimize common cases.
2464a041c5bSmacallan    *644) cp_umask=133;;
2474a041c5bSmacallan    *755) cp_umask=22;;
2484a041c5bSmacallan
2494a041c5bSmacallan    *[0-7])
2504a041c5bSmacallan      if test -z "$stripcmd"; then
2510a392d7eSmrg        u_plus_rw=
2524a041c5bSmacallan      else
2530a392d7eSmrg        u_plus_rw='% 200'
2544a041c5bSmacallan      fi
2554a041c5bSmacallan      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2564a041c5bSmacallan    *)
2574a041c5bSmacallan      if test -z "$stripcmd"; then
2580a392d7eSmrg        u_plus_rw=
2594a041c5bSmacallan      else
2600a392d7eSmrg        u_plus_rw=,u+rw
2614a041c5bSmacallan      fi
2624a041c5bSmacallan      cp_umask=$mode$u_plus_rw;;
2634a041c5bSmacallan  esac
2644a041c5bSmacallanfi
2654a041c5bSmacallan
2664a041c5bSmacallanfor src
2674a041c5bSmacallando
26854b44505Smrg  # Protect names problematic for 'test' and other utilities.
2694a041c5bSmacallan  case $src in
27054b44505Smrg    -* | [=\(\)!]) src=./$src;;
2714a041c5bSmacallan  esac
2724a041c5bSmacallan
2734a041c5bSmacallan  if test -n "$dir_arg"; then
2744a041c5bSmacallan    dst=$src
2754a041c5bSmacallan    dstdir=$dst
2764a041c5bSmacallan    test -d "$dstdir"
2774a041c5bSmacallan    dstdir_status=$?
2788fc2d0c6Smrg    # Don't chown directories that already exist.
2798fc2d0c6Smrg    if test $dstdir_status = 0; then
2808fc2d0c6Smrg      chowncmd=""
2818fc2d0c6Smrg    fi
2824a041c5bSmacallan  else
2834a041c5bSmacallan
2844a041c5bSmacallan    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2854a041c5bSmacallan    # might cause directories to be created, which would be especially bad
2864a041c5bSmacallan    # if $src (and thus $dsttmp) contains '*'.
2874a041c5bSmacallan    if test ! -f "$src" && test ! -d "$src"; then
2884a041c5bSmacallan      echo "$0: $src does not exist." >&2
2894a041c5bSmacallan      exit 1
2904a041c5bSmacallan    fi
2914a041c5bSmacallan
2924a041c5bSmacallan    if test -z "$dst_arg"; then
2934a041c5bSmacallan      echo "$0: no destination specified." >&2
2944a041c5bSmacallan      exit 1
2954a041c5bSmacallan    fi
2964a041c5bSmacallan    dst=$dst_arg
2974a041c5bSmacallan
2988fc2d0c6Smrg    # If destination is a directory, append the input filename.
2994a041c5bSmacallan    if test -d "$dst"; then
3000a392d7eSmrg      if test "$is_target_a_directory" = never; then
3010a392d7eSmrg        echo "$0: $dst_arg: Is a directory" >&2
3020a392d7eSmrg        exit 1
3034a041c5bSmacallan      fi
3044a041c5bSmacallan      dstdir=$dst
3058fc2d0c6Smrg      dstbase=`basename "$src"`
3068fc2d0c6Smrg      case $dst in
3078fc2d0c6Smrg	*/) dst=$dst$dstbase;;
3088fc2d0c6Smrg	*)  dst=$dst/$dstbase;;
3098fc2d0c6Smrg      esac
3104a041c5bSmacallan      dstdir_status=0
3114a041c5bSmacallan    else
3120a392d7eSmrg      dstdir=`dirname "$dst"`
3134a041c5bSmacallan      test -d "$dstdir"
3144a041c5bSmacallan      dstdir_status=$?
3154a041c5bSmacallan    fi
3164a041c5bSmacallan  fi
3174a041c5bSmacallan
3188fc2d0c6Smrg  case $dstdir in
3198fc2d0c6Smrg    */) dstdirslash=$dstdir;;
3208fc2d0c6Smrg    *)  dstdirslash=$dstdir/;;
3218fc2d0c6Smrg  esac
3228fc2d0c6Smrg
3234a041c5bSmacallan  obsolete_mkdir_used=false
3244a041c5bSmacallan
3254a041c5bSmacallan  if test $dstdir_status != 0; then
3264a041c5bSmacallan    case $posix_mkdir in
3274a041c5bSmacallan      '')
3280a392d7eSmrg        # With -d, create the new directory with the user-specified mode.
3290a392d7eSmrg        # Otherwise, rely on $mkdir_umask.
3300a392d7eSmrg        if test -n "$dir_arg"; then
3310a392d7eSmrg          mkdir_mode=-m$mode
3320a392d7eSmrg        else
3330a392d7eSmrg          mkdir_mode=
3340a392d7eSmrg        fi
3350a392d7eSmrg
3360a392d7eSmrg        posix_mkdir=false
3378fc2d0c6Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3388fc2d0c6Smrg	# here however when possible just to lower collision chance.
3398fc2d0c6Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3408fc2d0c6Smrg
3418fc2d0c6Smrg	trap '
3428fc2d0c6Smrg	  ret=$?
3438fc2d0c6Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3448fc2d0c6Smrg	  exit $ret
3458fc2d0c6Smrg	' 0
3468fc2d0c6Smrg
3478fc2d0c6Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3488fc2d0c6Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3498fc2d0c6Smrg	# directory is successfully created first before we actually test
3508fc2d0c6Smrg	# 'mkdir -p'.
3518fc2d0c6Smrg	if (umask $mkdir_umask &&
3528fc2d0c6Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3538fc2d0c6Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3548fc2d0c6Smrg	then
3558fc2d0c6Smrg	  if test -z "$dir_arg" || {
3568fc2d0c6Smrg	       # Check for POSIX incompatibilities with -m.
3578fc2d0c6Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3588fc2d0c6Smrg	       # other-writable bit of parent directory when it shouldn't.
3598fc2d0c6Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3608fc2d0c6Smrg	       test_tmpdir="$tmpdir/a"
3618fc2d0c6Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3628fc2d0c6Smrg	       case $ls_ld_tmpdir in
3638fc2d0c6Smrg		 d????-?r-*) different_mode=700;;
3648fc2d0c6Smrg		 d????-?--*) different_mode=755;;
3658fc2d0c6Smrg		 *) false;;
3668fc2d0c6Smrg	       esac &&
3678fc2d0c6Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3688fc2d0c6Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3698fc2d0c6Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3708fc2d0c6Smrg	       }
3718fc2d0c6Smrg	     }
3728fc2d0c6Smrg	  then posix_mkdir=:
3738fc2d0c6Smrg	  fi
3748fc2d0c6Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3758fc2d0c6Smrg	else
3768fc2d0c6Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3778fc2d0c6Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3788fc2d0c6Smrg	fi
3798fc2d0c6Smrg	trap '' 0;;
3804a041c5bSmacallan    esac
3814a041c5bSmacallan
3824a041c5bSmacallan    if
3834a041c5bSmacallan      $posix_mkdir && (
3840a392d7eSmrg        umask $mkdir_umask &&
3850a392d7eSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3864a041c5bSmacallan      )
3874a041c5bSmacallan    then :
3884a041c5bSmacallan    else
3894a041c5bSmacallan
3908fc2d0c6Smrg      # mkdir does not conform to POSIX,
3914a041c5bSmacallan      # or it failed possibly due to a race condition.  Create the
3924a041c5bSmacallan      # directory the slow way, step by step, checking for races as we go.
3934a041c5bSmacallan
3944a041c5bSmacallan      case $dstdir in
3950a392d7eSmrg        /*) prefix='/';;
3960a392d7eSmrg        [-=\(\)!]*) prefix='./';;
3970a392d7eSmrg        *)  prefix='';;
3984a041c5bSmacallan      esac
3994a041c5bSmacallan
4004a041c5bSmacallan      oIFS=$IFS
4014a041c5bSmacallan      IFS=/
4020a392d7eSmrg      set -f
4034a041c5bSmacallan      set fnord $dstdir
4044a041c5bSmacallan      shift
4050a392d7eSmrg      set +f
4064a041c5bSmacallan      IFS=$oIFS
4074a041c5bSmacallan
4084a041c5bSmacallan      prefixes=
4094a041c5bSmacallan
4104a041c5bSmacallan      for d
4114a041c5bSmacallan      do
4120a392d7eSmrg        test X"$d" = X && continue
4130a392d7eSmrg
4140a392d7eSmrg        prefix=$prefix$d
4150a392d7eSmrg        if test -d "$prefix"; then
4160a392d7eSmrg          prefixes=
4170a392d7eSmrg        else
4180a392d7eSmrg          if $posix_mkdir; then
4198fc2d0c6Smrg            (umask $mkdir_umask &&
4200a392d7eSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4210a392d7eSmrg            # Don't fail if two instances are running concurrently.
4220a392d7eSmrg            test -d "$prefix" || exit 1
4230a392d7eSmrg          else
4240a392d7eSmrg            case $prefix in
4250a392d7eSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4260a392d7eSmrg              *) qprefix=$prefix;;
4270a392d7eSmrg            esac
4280a392d7eSmrg            prefixes="$prefixes '$qprefix'"
4290a392d7eSmrg          fi
4300a392d7eSmrg        fi
4310a392d7eSmrg        prefix=$prefix/
4324a041c5bSmacallan      done
4334a041c5bSmacallan
4344a041c5bSmacallan      if test -n "$prefixes"; then
4350a392d7eSmrg        # Don't fail if two instances are running concurrently.
4360a392d7eSmrg        (umask $mkdir_umask &&
4370a392d7eSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4380a392d7eSmrg          test -d "$dstdir" || exit 1
4390a392d7eSmrg        obsolete_mkdir_used=true
4404a041c5bSmacallan      fi
4414a041c5bSmacallan    fi
4424a041c5bSmacallan  fi
4434a041c5bSmacallan
4444a041c5bSmacallan  if test -n "$dir_arg"; then
4454a041c5bSmacallan    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4464a041c5bSmacallan    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4474a041c5bSmacallan    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4484a041c5bSmacallan      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4494a041c5bSmacallan  else
4504a041c5bSmacallan
4514a041c5bSmacallan    # Make a couple of temp file names in the proper directory.
4528fc2d0c6Smrg    dsttmp=${dstdirslash}_inst.$$_
4538fc2d0c6Smrg    rmtmp=${dstdirslash}_rm.$$_
4544a041c5bSmacallan
4554a041c5bSmacallan    # Trap to clean up those temp files at exit.
4564a041c5bSmacallan    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4574a041c5bSmacallan
4584a041c5bSmacallan    # Copy the file name to the temp name.
4598fc2d0c6Smrg    (umask $cp_umask &&
4608fc2d0c6Smrg     { test -z "$stripcmd" || {
4618fc2d0c6Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4628fc2d0c6Smrg	 # which would cause strip to fail.
4638fc2d0c6Smrg	 if test -z "$doit"; then
4648fc2d0c6Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4658fc2d0c6Smrg	 else
4668fc2d0c6Smrg	   $doit touch "$dsttmp"
4678fc2d0c6Smrg	 fi
4688fc2d0c6Smrg       }
4698fc2d0c6Smrg     } &&
4708fc2d0c6Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4714a041c5bSmacallan
4724a041c5bSmacallan    # and set any options; do chmod last to preserve setuid bits.
4734a041c5bSmacallan    #
4744a041c5bSmacallan    # If any of these fail, we abort the whole thing.  If we want to
4754a041c5bSmacallan    # ignore errors from any of these, just make sure not to ignore
4764a041c5bSmacallan    # errors from the above "$doit $cpprog $src $dsttmp" command.
4774a041c5bSmacallan    #
4784a041c5bSmacallan    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4794a041c5bSmacallan    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4804a041c5bSmacallan    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4814a041c5bSmacallan    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4824a041c5bSmacallan
4834a041c5bSmacallan    # If -C, don't bother to copy if it wouldn't change the file.
4844a041c5bSmacallan    if $copy_on_change &&
4850a392d7eSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4860a392d7eSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4870a392d7eSmrg       set -f &&
4884a041c5bSmacallan       set X $old && old=:$2:$4:$5:$6 &&
4894a041c5bSmacallan       set X $new && new=:$2:$4:$5:$6 &&
4900a392d7eSmrg       set +f &&
4914a041c5bSmacallan       test "$old" = "$new" &&
4924a041c5bSmacallan       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4934a041c5bSmacallan    then
4944a041c5bSmacallan      rm -f "$dsttmp"
4954a041c5bSmacallan    else
4968fc2d0c6Smrg      # If $backupsuffix is set, and the file being installed
4978fc2d0c6Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4988fc2d0c6Smrg      # e.g., if mv doesn't support -f.
4998fc2d0c6Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5008fc2d0c6Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5018fc2d0c6Smrg      fi
5028fc2d0c6Smrg
5034a041c5bSmacallan      # Rename the file to the real destination.
5044a041c5bSmacallan      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5054a041c5bSmacallan
5064a041c5bSmacallan      # The rename failed, perhaps because mv can't rename something else
5074a041c5bSmacallan      # to itself, or perhaps because mv is so ancient that it does not
5084a041c5bSmacallan      # support -f.
5094a041c5bSmacallan      {
5100a392d7eSmrg        # Now remove or move aside any old file at destination location.
5110a392d7eSmrg        # We try this two ways since rm can't unlink itself on some
5120a392d7eSmrg        # systems and the destination file might be busy for other
5130a392d7eSmrg        # reasons.  In this case, the final cleanup might fail but the new
5140a392d7eSmrg        # file should still install successfully.
5150a392d7eSmrg        {
5160a392d7eSmrg          test ! -f "$dst" ||
5178fc2d0c6Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5180a392d7eSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5198fc2d0c6Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5200a392d7eSmrg          } ||
5210a392d7eSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5220a392d7eSmrg            (exit 1); exit 1
5230a392d7eSmrg          }
5240a392d7eSmrg        } &&
5250a392d7eSmrg
5260a392d7eSmrg        # Now rename the file to the real destination.
5270a392d7eSmrg        $doit $mvcmd "$dsttmp" "$dst"
5284a041c5bSmacallan      }
5294a041c5bSmacallan    fi || exit 1
5304a041c5bSmacallan
5314a041c5bSmacallan    trap '' 0
5324a041c5bSmacallan  fi
5334a041c5bSmacallandone
5344a041c5bSmacallan
5354a041c5bSmacallan# Local variables:
5368fc2d0c6Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5374a041c5bSmacallan# time-stamp-start: "scriptversion="
5384a041c5bSmacallan# time-stamp-format: "%:y-%02m-%02d.%02H"
5398fc2d0c6Smrg# time-stamp-time-zone: "UTC0"
54054b44505Smrg# time-stamp-end: "; # UTC"
5414a041c5bSmacallan# End:
542