install-sh revision d6c0b56e
18c9fbc29Smrg#!/bin/sh
28c9fbc29Smrg# install - install a program, script, or datafile
370728a38Smrg
48c9fbc29Smrgscriptversion=2014-09-12.12; # UTC
58c9fbc29Smrg
68c9fbc29Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74cd6a3aeSmrg# later released in X11R6 (xc/config/util/install.sh) with the
870728a38Smrg# following copyright and license.
94cd6a3aeSmrg#
104cd6a3aeSmrg# Copyright (C) 1994 X Consortium
118c9fbc29Smrg#
128c9fbc29Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134cd6a3aeSmrg# of this software and associated documentation files (the "Software"), to
144cd6a3aeSmrg# deal in the Software without restriction, including without limitation the
154cd6a3aeSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168c9fbc29Smrg# sell copies of the Software, and to permit persons to whom the Software is
178c9fbc29Smrg# furnished to do so, subject to the following conditions:
188c9fbc29Smrg#
194cd6a3aeSmrg# The above copyright notice and this permission notice shall be included in
208c9fbc29Smrg# all copies or substantial portions of the Software.
218c9fbc29Smrg#
224e411241Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238c9fbc29Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248c9fbc29Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258c9fbc29Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268c9fbc29Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274cd6a3aeSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284cd6a3aeSmrg#
294cd6a3aeSmrg# Except as contained in this notice, the name of the X Consortium shall not
304cd6a3aeSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
314cd6a3aeSmrg# ings in this Software without prior written authorization from the X Consor-
328c9fbc29Smrg# tium.
338c9fbc29Smrg#
348c9fbc29Smrg#
358c9fbc29Smrg# FSF changes to this file are in the public domain.
364e411241Smrg#
374e411241Smrg# Calling this script install-sh is preferred over install.sh, to prevent
384e411241Smrg# 'make' implicit rules from creating a file called install from it
394e411241Smrg# when there is no Makefile.
404e411241Smrg#
414e411241Smrg# This script is compatible with the BSD install script, but was written
424e411241Smrg# from scratch.
434cd6a3aeSmrg
444cd6a3aeSmrgtab='	'
454cd6a3aeSmrgnl='
464cd6a3aeSmrg'
474cd6a3aeSmrgIFS=" $tab$nl"
484cd6a3aeSmrg
494cd6a3aeSmrg# Set DOITPROG to "echo" to test this script.
504e411241Smrg
514e411241Smrgdoit=${DOITPROG-}
524e411241Smrgdoit_exec=${doit:-exec}
534e411241Smrg
544e411241Smrg# Put in absolute file names if you don't have them in your path;
554e411241Smrg# or use environment vars.
568c9fbc29Smrg
574e411241Smrgchgrpprog=${CHGRPPROG-chgrp}
584e411241Smrgchmodprog=${CHMODPROG-chmod}
594e411241Smrgchownprog=${CHOWNPROG-chown}
604cd6a3aeSmrgcmpprog=${CMPPROG-cmp}
614e411241Smrgcpprog=${CPPROG-cp}
624e411241Smrgmkdirprog=${MKDIRPROG-mkdir}
634e411241Smrgmvprog=${MVPROG-mv}
644e411241Smrgrmprog=${RMPROG-rm}
654e411241Smrgstripprog=${STRIPPROG-strip}
664e411241Smrg
674e411241Smrgposix_mkdir=
684e411241Smrg
698c9fbc29Smrg# Desired mode of installed file.
704e411241Smrgmode=0755
714e411241Smrg
724e411241Smrgchgrpcmd=
734e411241Smrgchmodcmd=$chmodprog
744e411241Smrgchowncmd=
754e411241Smrgmvcmd=$mvprog
764e411241Smrgrmcmd="$rmprog -f"
774e411241Smrgstripcmd=
784e411241Smrg
794e411241Smrgsrc=
804e411241Smrgdst=
818c9fbc29Smrgdir_arg=
828c9fbc29Smrgdst_arg=
838c9fbc29Smrg
848c9fbc29Smrgcopy_on_change=false
858c9fbc29Smrgis_target_a_directory=possibly
868c9fbc29Smrg
878c9fbc29Smrgusage="\
888c9fbc29SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
898c9fbc29Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
908c9fbc29Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
918c9fbc29Smrg   or: $0 [OPTION]... -d DIRECTORIES...
924cd6a3aeSmrg
934cd6a3aeSmrgIn the 1st form, copy SRCFILE to DSTFILE.
948c9fbc29SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
958c9fbc29SmrgIn the 4th, create DIRECTORIES.
968c9fbc29Smrg
978c9fbc29SmrgOptions:
988c9fbc29Smrg     --help     display this help and exit.
998c9fbc29Smrg     --version  display version info and exit.
1004cd6a3aeSmrg
1014cd6a3aeSmrg  -c            (ignored)
1028c9fbc29Smrg  -C            install only if different (preserve the last data modification time)
1038c9fbc29Smrg  -d            create directories instead of installing files.
1048c9fbc29Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1058c9fbc29Smrg  -m MODE       $chmodprog installed files to MODE.
1068c9fbc29Smrg  -o USER       $chownprog installed files to USER.
1078c9fbc29Smrg  -s            $stripprog installed files.
1088c9fbc29Smrg  -t DIRECTORY  install into DIRECTORY.
1098c9fbc29Smrg  -T            report an error if DSTFILE is a directory.
1108c9fbc29Smrg
1118c9fbc29SmrgEnvironment variables override the default commands:
1124e411241Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1134cd6a3aeSmrg  RMPROG STRIPPROG
1148c9fbc29Smrg"
1158c9fbc29Smrg
1164cd6a3aeSmrgwhile test $# -ne 0; do
1174cd6a3aeSmrg  case $1 in
1184cd6a3aeSmrg    -c) ;;
1194cd6a3aeSmrg
1204cd6a3aeSmrg    -C) copy_on_change=true;;
1214cd6a3aeSmrg
1224cd6a3aeSmrg    -d) dir_arg=true;;
1238c9fbc29Smrg
1248c9fbc29Smrg    -g) chgrpcmd="$chgrpprog $2"
1258c9fbc29Smrg        shift;;
1268c9fbc29Smrg
1278c9fbc29Smrg    --help) echo "$usage"; exit $?;;
1288c9fbc29Smrg
1294e411241Smrg    -m) mode=$2
1304e411241Smrg        case $mode in
1314e411241Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1324e411241Smrg            echo "$0: invalid mode: $mode" >&2
1338c9fbc29Smrg            exit 1;;
1344cd6a3aeSmrg        esac
1354cd6a3aeSmrg        shift;;
1364cd6a3aeSmrg
13770728a38Smrg    -o) chowncmd="$chownprog $2"
13870728a38Smrg        shift;;
13970728a38Smrg
14070728a38Smrg    -s) stripcmd=$stripprog;;
14170728a38Smrg
14270728a38Smrg    -t)
14370728a38Smrg        is_target_a_directory=always
14470728a38Smrg        dst_arg=$2
14570728a38Smrg        # Protect names problematic for 'test' and other utilities.
14670728a38Smrg        case $dst_arg in
14770728a38Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14870728a38Smrg        esac
14970728a38Smrg        shift;;
15070728a38Smrg
15170728a38Smrg    -T) is_target_a_directory=never;;
15270728a38Smrg
15370728a38Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15470728a38Smrg
15570728a38Smrg    --) shift
15670728a38Smrg        break;;
15770728a38Smrg
15870728a38Smrg    -*) echo "$0: invalid option: $1" >&2
15970728a38Smrg        exit 1;;
16070728a38Smrg
16170728a38Smrg    *)  break;;
1624cd6a3aeSmrg  esac
1634cd6a3aeSmrg  shift
1644cd6a3aeSmrgdone
1654cd6a3aeSmrg
1664cd6a3aeSmrg# We allow the use of options -d and -T together, by making -d
1674cd6a3aeSmrg# take the precedence; this is for compatibility with GNU install.
1684cd6a3aeSmrg
1694cd6a3aeSmrgif test -n "$dir_arg"; then
1704cd6a3aeSmrg  if test -n "$dst_arg"; then
1714cd6a3aeSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1724cd6a3aeSmrg    exit 1
1734cd6a3aeSmrg  fi
1744cd6a3aeSmrgfi
1754cd6a3aeSmrg
1764cd6a3aeSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1774cd6a3aeSmrg  # When -d is used, all remaining arguments are directories to create.
1784cd6a3aeSmrg  # When -t is used, the destination is already specified.
1794cd6a3aeSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1804cd6a3aeSmrg  for arg
1814cd6a3aeSmrg  do
1824cd6a3aeSmrg    if test -n "$dst_arg"; then
1834cd6a3aeSmrg      # $@ is not empty: it contains at least $arg.
1844cd6a3aeSmrg      set fnord "$@" "$dst_arg"
1854cd6a3aeSmrg      shift # fnord
1864cd6a3aeSmrg    fi
1874cd6a3aeSmrg    shift # arg
1884cd6a3aeSmrg    dst_arg=$arg
1894cd6a3aeSmrg    # Protect names problematic for 'test' and other utilities.
1904cd6a3aeSmrg    case $dst_arg in
1914cd6a3aeSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1924cd6a3aeSmrg    esac
1934cd6a3aeSmrg  done
1944cd6a3aeSmrgfi
19570728a38Smrg
19670728a38Smrgif test $# -eq 0; then
1974cd6a3aeSmrg  if test -z "$dir_arg"; then
1984cd6a3aeSmrg    echo "$0: no input file specified." >&2
1994cd6a3aeSmrg    exit 1
2004cd6a3aeSmrg  fi
2014cd6a3aeSmrg  # It's OK to call 'install-sh -d' without argument.
2024cd6a3aeSmrg  # This can happen when creating conditional directories.
2034cd6a3aeSmrg  exit 0
2044cd6a3aeSmrgfi
2054cd6a3aeSmrg
2064cd6a3aeSmrgif test -z "$dir_arg"; then
2074cd6a3aeSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2084cd6a3aeSmrg    if test ! -d "$dst_arg"; then
2094cd6a3aeSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2104cd6a3aeSmrg      exit 1
2114cd6a3aeSmrg    fi
2124cd6a3aeSmrg  fi
2134cd6a3aeSmrgfi
2144cd6a3aeSmrg
2154cd6a3aeSmrgif test -z "$dir_arg"; then
2164cd6a3aeSmrg  do_exit='(exit $ret); exit $ret'
2174cd6a3aeSmrg  trap "ret=129; $do_exit" 1
2184cd6a3aeSmrg  trap "ret=130; $do_exit" 2
2194cd6a3aeSmrg  trap "ret=141; $do_exit" 13
2204cd6a3aeSmrg  trap "ret=143; $do_exit" 15
2214cd6a3aeSmrg
2224cd6a3aeSmrg  # Set umask so as not to create temps with too-generous modes.
2234cd6a3aeSmrg  # However, 'strip' requires both read and write access to temps.
2244cd6a3aeSmrg  case $mode in
2254cd6a3aeSmrg    # Optimize common cases.
2264cd6a3aeSmrg    *644) cp_umask=133;;
2274cd6a3aeSmrg    *755) cp_umask=22;;
2284cd6a3aeSmrg
2294cd6a3aeSmrg    *[0-7])
2304cd6a3aeSmrg      if test -z "$stripcmd"; then
2314cd6a3aeSmrg        u_plus_rw=
2324cd6a3aeSmrg      else
2334cd6a3aeSmrg        u_plus_rw='% 200'
2344cd6a3aeSmrg      fi
2354cd6a3aeSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2364cd6a3aeSmrg    *)
2374cd6a3aeSmrg      if test -z "$stripcmd"; then
2384cd6a3aeSmrg        u_plus_rw=
2394cd6a3aeSmrg      else
2404cd6a3aeSmrg        u_plus_rw=,u+rw
2414cd6a3aeSmrg      fi
2424cd6a3aeSmrg      cp_umask=$mode$u_plus_rw;;
2434cd6a3aeSmrg  esac
2444cd6a3aeSmrgfi
2454cd6a3aeSmrg
2464cd6a3aeSmrgfor src
2474cd6a3aeSmrgdo
2484cd6a3aeSmrg  # Protect names problematic for 'test' and other utilities.
24970728a38Smrg  case $src in
25070728a38Smrg    -* | [=\(\)!]) src=./$src;;
25170728a38Smrg  esac
25270728a38Smrg
25370728a38Smrg  if test -n "$dir_arg"; then
25470728a38Smrg    dst=$src
25570728a38Smrg    dstdir=$dst
25670728a38Smrg    test -d "$dstdir"
25770728a38Smrg    dstdir_status=$?
25870728a38Smrg  else
25970728a38Smrg
26070728a38Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26170728a38Smrg    # might cause directories to be created, which would be especially bad
26270728a38Smrg    # if $src (and thus $dsttmp) contains '*'.
26370728a38Smrg    if test ! -f "$src" && test ! -d "$src"; then
26470728a38Smrg      echo "$0: $src does not exist." >&2
26570728a38Smrg      exit 1
26670728a38Smrg    fi
26770728a38Smrg
2684cd6a3aeSmrg    if test -z "$dst_arg"; then
2694cd6a3aeSmrg      echo "$0: no destination specified." >&2
2704cd6a3aeSmrg      exit 1
2714cd6a3aeSmrg    fi
2724cd6a3aeSmrg    dst=$dst_arg
2734cd6a3aeSmrg
2744cd6a3aeSmrg    # If destination is a directory, append the input filename; won't work
2754cd6a3aeSmrg    # if double slashes aren't ignored.
2764cd6a3aeSmrg    if test -d "$dst"; then
2774cd6a3aeSmrg      if test "$is_target_a_directory" = never; then
2784cd6a3aeSmrg        echo "$0: $dst_arg: Is a directory" >&2
2794cd6a3aeSmrg        exit 1
2804cd6a3aeSmrg      fi
2814cd6a3aeSmrg      dstdir=$dst
2824cd6a3aeSmrg      dst=$dstdir/`basename "$src"`
2834cd6a3aeSmrg      dstdir_status=0
2844cd6a3aeSmrg    else
2854cd6a3aeSmrg      dstdir=`dirname "$dst"`
2864cd6a3aeSmrg      test -d "$dstdir"
2874cd6a3aeSmrg      dstdir_status=$?
2884cd6a3aeSmrg    fi
2894cd6a3aeSmrg  fi
2904cd6a3aeSmrg
2914cd6a3aeSmrg  obsolete_mkdir_used=false
2924cd6a3aeSmrg
2934cd6a3aeSmrg  if test $dstdir_status != 0; then
2944cd6a3aeSmrg    case $posix_mkdir in
2954cd6a3aeSmrg      '')
2964cd6a3aeSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
2974cd6a3aeSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
2984cd6a3aeSmrg        umask=`umask`
2994cd6a3aeSmrg        case $stripcmd.$umask in
3004cd6a3aeSmrg          # Optimize common cases.
3014cd6a3aeSmrg          *[2367][2367]) mkdir_umask=$umask;;
3024cd6a3aeSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3034cd6a3aeSmrg
3044cd6a3aeSmrg          *[0-7])
3054cd6a3aeSmrg            mkdir_umask=`expr $umask + 22 \
3064cd6a3aeSmrg              - $umask % 100 % 40 + $umask % 20 \
3074cd6a3aeSmrg              - $umask % 10 % 4 + $umask % 2
3084cd6a3aeSmrg            `;;
3094cd6a3aeSmrg          *) mkdir_umask=$umask,go-w;;
3104cd6a3aeSmrg        esac
3114cd6a3aeSmrg
3124cd6a3aeSmrg        # With -d, create the new directory with the user-specified mode.
3134cd6a3aeSmrg        # Otherwise, rely on $mkdir_umask.
3144cd6a3aeSmrg        if test -n "$dir_arg"; then
3154cd6a3aeSmrg          mkdir_mode=-m$mode
3164cd6a3aeSmrg        else
3174cd6a3aeSmrg          mkdir_mode=
3184cd6a3aeSmrg        fi
3194cd6a3aeSmrg
3204cd6a3aeSmrg        posix_mkdir=false
3214cd6a3aeSmrg        case $umask in
3224cd6a3aeSmrg          *[123567][0-7][0-7])
3234cd6a3aeSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3244cd6a3aeSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3254cd6a3aeSmrg            ;;
3264cd6a3aeSmrg          *)
3274cd6a3aeSmrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
3284cd6a3aeSmrg            # lower collision chance
3294cd6a3aeSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3304cd6a3aeSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
3314cd6a3aeSmrg
3324cd6a3aeSmrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
3334cd6a3aeSmrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
3344cd6a3aeSmrg            # that nobody tries to guess the $tmpdir name.
3354cd6a3aeSmrg            if (umask $mkdir_umask &&
3364cd6a3aeSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
3374cd6a3aeSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3384cd6a3aeSmrg            then
3394cd6a3aeSmrg              if test -z "$dir_arg" || {
3404cd6a3aeSmrg                   # Check for POSIX incompatibilities with -m.
3414cd6a3aeSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3424cd6a3aeSmrg                   # other-writable bit of parent directory when it shouldn't.
3434cd6a3aeSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3444cd6a3aeSmrg                   test_tmpdir="$tmpdir/a"
3454cd6a3aeSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3464cd6a3aeSmrg                   case $ls_ld_tmpdir in
3474cd6a3aeSmrg                     d????-?r-*) different_mode=700;;
3484cd6a3aeSmrg                     d????-?--*) different_mode=755;;
3494cd6a3aeSmrg                     *) false;;
3504cd6a3aeSmrg                   esac &&
3514cd6a3aeSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3524cd6a3aeSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3534cd6a3aeSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3544cd6a3aeSmrg                   }
3554cd6a3aeSmrg                 }
3564cd6a3aeSmrg              then posix_mkdir=:
3574cd6a3aeSmrg              fi
3584cd6a3aeSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3594cd6a3aeSmrg            else
3604cd6a3aeSmrg              # Remove any dirs left behind by ancient mkdir implementations.
3614cd6a3aeSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3624cd6a3aeSmrg            fi
3634cd6a3aeSmrg            trap '' 0;;
3644cd6a3aeSmrg        esac;;
3654cd6a3aeSmrg    esac
3664cd6a3aeSmrg
3674cd6a3aeSmrg    if
3684cd6a3aeSmrg      $posix_mkdir && (
3694cd6a3aeSmrg        umask $mkdir_umask &&
3704cd6a3aeSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37170728a38Smrg      )
37270728a38Smrg    then :
37370728a38Smrg    else
37470728a38Smrg
37570728a38Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
37670728a38Smrg      # or it failed possibly due to a race condition.  Create the
37770728a38Smrg      # directory the slow way, step by step, checking for races as we go.
37870728a38Smrg
3794cd6a3aeSmrg      case $dstdir in
3804cd6a3aeSmrg        /*) prefix='/';;
3814cd6a3aeSmrg        [-=\(\)!]*) prefix='./';;
3824cd6a3aeSmrg        *)  prefix='';;
3834cd6a3aeSmrg      esac
3844cd6a3aeSmrg
3854cd6a3aeSmrg      oIFS=$IFS
3864cd6a3aeSmrg      IFS=/
3874cd6a3aeSmrg      set -f
3884cd6a3aeSmrg      set fnord $dstdir
3894cd6a3aeSmrg      shift
3904cd6a3aeSmrg      set +f
3914cd6a3aeSmrg      IFS=$oIFS
3924cd6a3aeSmrg
3934cd6a3aeSmrg      prefixes=
3944cd6a3aeSmrg
3954cd6a3aeSmrg      for d
3964cd6a3aeSmrg      do
3974cd6a3aeSmrg        test X"$d" = X && continue
3984cd6a3aeSmrg
3994cd6a3aeSmrg        prefix=$prefix$d
4004cd6a3aeSmrg        if test -d "$prefix"; then
4014cd6a3aeSmrg          prefixes=
4024cd6a3aeSmrg        else
4034cd6a3aeSmrg          if $posix_mkdir; then
4044cd6a3aeSmrg            (umask=$mkdir_umask &&
4054cd6a3aeSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4064cd6a3aeSmrg            # Don't fail if two instances are running concurrently.
4074cd6a3aeSmrg            test -d "$prefix" || exit 1
4084cd6a3aeSmrg          else
4094cd6a3aeSmrg            case $prefix in
4104cd6a3aeSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4114cd6a3aeSmrg              *) qprefix=$prefix;;
4124cd6a3aeSmrg            esac
4134cd6a3aeSmrg            prefixes="$prefixes '$qprefix'"
4144cd6a3aeSmrg          fi
4154cd6a3aeSmrg        fi
4164cd6a3aeSmrg        prefix=$prefix/
4174cd6a3aeSmrg      done
4184cd6a3aeSmrg
4194cd6a3aeSmrg      if test -n "$prefixes"; then
4204cd6a3aeSmrg        # Don't fail if two instances are running concurrently.
4214cd6a3aeSmrg        (umask $mkdir_umask &&
4224cd6a3aeSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4234cd6a3aeSmrg          test -d "$dstdir" || exit 1
4244cd6a3aeSmrg        obsolete_mkdir_used=true
4254cd6a3aeSmrg      fi
4264cd6a3aeSmrg    fi
4274cd6a3aeSmrg  fi
4284cd6a3aeSmrg
4294cd6a3aeSmrg  if test -n "$dir_arg"; then
4304cd6a3aeSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4318c9fbc29Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4328c9fbc29Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4338c9fbc29Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4348c9fbc29Smrg  else
4358c9fbc29Smrg
4368c9fbc29Smrg    # Make a couple of temp file names in the proper directory.
4378c9fbc29Smrg    dsttmp=$dstdir/_inst.$$_
4388c9fbc29Smrg    rmtmp=$dstdir/_rm.$$_
4398c9fbc29Smrg
4408c9fbc29Smrg    # Trap to clean up those temp files at exit.
4418c9fbc29Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4428c9fbc29Smrg
4438c9fbc29Smrg    # Copy the file name to the temp name.
4444cd6a3aeSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4454cd6a3aeSmrg
4464cd6a3aeSmrg    # and set any options; do chmod last to preserve setuid bits.
4474cd6a3aeSmrg    #
4484cd6a3aeSmrg    # If any of these fail, we abort the whole thing.  If we want to
4498c9fbc29Smrg    # ignore errors from any of these, just make sure not to ignore
4508c9fbc29Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4518c9fbc29Smrg    #
4528c9fbc29Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4538c9fbc29Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4544e411241Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4558c9fbc29Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4568c9fbc29Smrg
4578c9fbc29Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4588c9fbc29Smrg    if $copy_on_change &&
4598c9fbc29Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4608c9fbc29Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4618c9fbc29Smrg       set -f &&
4628c9fbc29Smrg       set X $old && old=:$2:$4:$5:$6 &&
4638c9fbc29Smrg       set X $new && new=:$2:$4:$5:$6 &&
4648c9fbc29Smrg       set +f &&
4658c9fbc29Smrg       test "$old" = "$new" &&
4668c9fbc29Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4678c9fbc29Smrg    then
4688c9fbc29Smrg      rm -f "$dsttmp"
4694cd6a3aeSmrg    else
4704cd6a3aeSmrg      # Rename the file to the real destination.
4714cd6a3aeSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4724cd6a3aeSmrg
4734cd6a3aeSmrg      # The rename failed, perhaps because mv can't rename something else
4744cd6a3aeSmrg      # to itself, or perhaps because mv is so ancient that it does not
4758c9fbc29Smrg      # support -f.
4768c9fbc29Smrg      {
4774cd6a3aeSmrg        # Now remove or move aside any old file at destination location.
4784cd6a3aeSmrg        # We try this two ways since rm can't unlink itself on some
4794cd6a3aeSmrg        # systems and the destination file might be busy for other
4804cd6a3aeSmrg        # reasons.  In this case, the final cleanup might fail but the new
4814cd6a3aeSmrg        # file should still install successfully.
4824cd6a3aeSmrg        {
4834cd6a3aeSmrg          test ! -f "$dst" ||
4844cd6a3aeSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4854cd6a3aeSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4864cd6a3aeSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4874cd6a3aeSmrg          } ||
4884cd6a3aeSmrg          { echo "$0: cannot unlink or rename $dst" >&2
4894cd6a3aeSmrg            (exit 1); exit 1
4904cd6a3aeSmrg          }
4914cd6a3aeSmrg        } &&
4924cd6a3aeSmrg
4934cd6a3aeSmrg        # Now rename the file to the real destination.
4944cd6a3aeSmrg        $doit $mvcmd "$dsttmp" "$dst"
4954cd6a3aeSmrg      }
4964cd6a3aeSmrg    fi || exit 1
4974cd6a3aeSmrg
4984cd6a3aeSmrg    trap '' 0
4998c9fbc29Smrg  fi
50070728a38Smrgdone
50170728a38Smrg
50270728a38Smrg# Local variables:
50370728a38Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5044cd6a3aeSmrg# time-stamp-start: "scriptversion="
5054cd6a3aeSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5064cd6a3aeSmrg# time-stamp-time-zone: "UTC"
5074cd6a3aeSmrg# time-stamp-end: "; # UTC"
5084cd6a3aeSmrg# End:
5094cd6a3aeSmrg