1b7d53ee5Smrg#!/bin/sh
2b7d53ee5Smrg# install - install a program, script, or datafile
32ceb2653Smrg
487402a6fSmrgscriptversion=2020-11-14.01; # UTC
52ceb2653Smrg
6b7d53ee5Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7b7d53ee5Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8b7d53ee5Smrg# following copyright and license.
9b7d53ee5Smrg#
10b7d53ee5Smrg# Copyright (C) 1994 X Consortium
11b7d53ee5Smrg#
12b7d53ee5Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13b7d53ee5Smrg# of this software and associated documentation files (the "Software"), to
14b7d53ee5Smrg# deal in the Software without restriction, including without limitation the
15b7d53ee5Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16b7d53ee5Smrg# sell copies of the Software, and to permit persons to whom the Software is
17b7d53ee5Smrg# furnished to do so, subject to the following conditions:
18b7d53ee5Smrg#
19b7d53ee5Smrg# The above copyright notice and this permission notice shall be included in
20b7d53ee5Smrg# all copies or substantial portions of the Software.
21b7d53ee5Smrg#
22b7d53ee5Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23b7d53ee5Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24b7d53ee5Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25b7d53ee5Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26b7d53ee5Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27b7d53ee5Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28b7d53ee5Smrg#
29b7d53ee5Smrg# Except as contained in this notice, the name of the X Consortium shall not
30b7d53ee5Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31b7d53ee5Smrg# ings in this Software without prior written authorization from the X Consor-
32b7d53ee5Smrg# tium.
33b7d53ee5Smrg#
34b7d53ee5Smrg#
35b7d53ee5Smrg# FSF changes to this file are in the public domain.
36b7d53ee5Smrg#
37b7d53ee5Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3887402a6fSmrg# 'make' implicit rules from creating a file called install from it
39b7d53ee5Smrg# when there is no Makefile.
40b7d53ee5Smrg#
41b7d53ee5Smrg# This script is compatible with the BSD install script, but was written
422ceb2653Smrg# from scratch.
43b7d53ee5Smrg
4487402a6fSmrgtab='	'
452ceb2653Smrgnl='
462ceb2653Smrg'
4787402a6fSmrgIFS=" $tab$nl"
48b7d53ee5Smrg
4987402a6fSmrg# Set DOITPROG to "echo" to test this script.
50b7d53ee5Smrg
512ceb2653Smrgdoit=${DOITPROG-}
5287402a6fSmrgdoit_exec=${doit:-exec}
53b7d53ee5Smrg
542ceb2653Smrg# Put in absolute file names if you don't have them in your path;
552ceb2653Smrg# or use environment vars.
562ceb2653Smrg
572ceb2653Smrgchgrpprog=${CHGRPPROG-chgrp}
582ceb2653Smrgchmodprog=${CHMODPROG-chmod}
592ceb2653Smrgchownprog=${CHOWNPROG-chown}
602ceb2653Smrgcmpprog=${CMPPROG-cmp}
612ceb2653Smrgcpprog=${CPPROG-cp}
622ceb2653Smrgmkdirprog=${MKDIRPROG-mkdir}
632ceb2653Smrgmvprog=${MVPROG-mv}
642ceb2653Smrgrmprog=${RMPROG-rm}
652ceb2653Smrgstripprog=${STRIPPROG-strip}
662ceb2653Smrg
672ceb2653Smrgposix_mkdir=
682ceb2653Smrg
692ceb2653Smrg# Desired mode of installed file.
702ceb2653Smrgmode=0755
712ceb2653Smrg
7287402a6fSmrg# Create dirs (including intermediate dirs) using mode 755.
7387402a6fSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7487402a6fSmrgmkdir_umask=22
7587402a6fSmrg
7687402a6fSmrgbackupsuffix=
772ceb2653Smrgchgrpcmd=
782ceb2653Smrgchmodcmd=$chmodprog
792ceb2653Smrgchowncmd=
802ceb2653Smrgmvcmd=$mvprog
812ceb2653Smrgrmcmd="$rmprog -f"
822ceb2653Smrgstripcmd=
83b7d53ee5Smrg
842ceb2653Smrgsrc=
852ceb2653Smrgdst=
862ceb2653Smrgdir_arg=
872ceb2653Smrgdst_arg=
88b7d53ee5Smrg
892ceb2653Smrgcopy_on_change=false
9087402a6fSmrgis_target_a_directory=possibly
91b7d53ee5Smrg
922ceb2653Smrgusage="\
932ceb2653SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
942ceb2653Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
952ceb2653Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
962ceb2653Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97b7d53ee5Smrg
982ceb2653SmrgIn the 1st form, copy SRCFILE to DSTFILE.
992ceb2653SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1002ceb2653SmrgIn the 4th, create DIRECTORIES.
101b7d53ee5Smrg
1022ceb2653SmrgOptions:
1032ceb2653Smrg     --help     display this help and exit.
1042ceb2653Smrg     --version  display version info and exit.
105b7d53ee5Smrg
1062ceb2653Smrg  -c            (ignored)
10787402a6fSmrg  -C            install only if different (preserve data modification time)
1082ceb2653Smrg  -d            create directories instead of installing files.
1092ceb2653Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1102ceb2653Smrg  -m MODE       $chmodprog installed files to MODE.
1112ceb2653Smrg  -o USER       $chownprog installed files to USER.
11287402a6fSmrg  -p            pass -p to $cpprog.
1132ceb2653Smrg  -s            $stripprog installed files.
11487402a6fSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1152ceb2653Smrg  -t DIRECTORY  install into DIRECTORY.
1162ceb2653Smrg  -T            report an error if DSTFILE is a directory.
117b7d53ee5Smrg
1182ceb2653SmrgEnvironment variables override the default commands:
1192ceb2653Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1202ceb2653Smrg  RMPROG STRIPPROG
12187402a6fSmrg
12287402a6fSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12387402a6fSmrgit's up to you to specify -f if you want it.
12487402a6fSmrg
12587402a6fSmrgIf -S is not specified, no backups are attempted.
12687402a6fSmrg
12787402a6fSmrgEmail bug reports to bug-automake@gnu.org.
12887402a6fSmrgAutomake home page: https://www.gnu.org/software/automake/
1292ceb2653Smrg"
130b7d53ee5Smrg
1312ceb2653Smrgwhile test $# -ne 0; do
1322ceb2653Smrg  case $1 in
1332ceb2653Smrg    -c) ;;
134b7d53ee5Smrg
1352ceb2653Smrg    -C) copy_on_change=true;;
136b7d53ee5Smrg
1372ceb2653Smrg    -d) dir_arg=true;;
138b7d53ee5Smrg
1392ceb2653Smrg    -g) chgrpcmd="$chgrpprog $2"
14087402a6fSmrg        shift;;
141b7d53ee5Smrg
1422ceb2653Smrg    --help) echo "$usage"; exit $?;;
143b7d53ee5Smrg
1442ceb2653Smrg    -m) mode=$2
14587402a6fSmrg        case $mode in
14687402a6fSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14787402a6fSmrg            echo "$0: invalid mode: $mode" >&2
14887402a6fSmrg            exit 1;;
14987402a6fSmrg        esac
15087402a6fSmrg        shift;;
151b7d53ee5Smrg
1522ceb2653Smrg    -o) chowncmd="$chownprog $2"
15387402a6fSmrg        shift;;
15487402a6fSmrg
15587402a6fSmrg    -p) cpprog="$cpprog -p";;
156b7d53ee5Smrg
1572ceb2653Smrg    -s) stripcmd=$stripprog;;
158b7d53ee5Smrg
15987402a6fSmrg    -S) backupsuffix="$2"
16087402a6fSmrg        shift;;
16187402a6fSmrg
16287402a6fSmrg    -t)
16387402a6fSmrg        is_target_a_directory=always
16487402a6fSmrg        dst_arg=$2
16587402a6fSmrg        # Protect names problematic for 'test' and other utilities.
16687402a6fSmrg        case $dst_arg in
16787402a6fSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16887402a6fSmrg        esac
16987402a6fSmrg        shift;;
170b7d53ee5Smrg
17187402a6fSmrg    -T) is_target_a_directory=never;;
172b7d53ee5Smrg
1732ceb2653Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174b7d53ee5Smrg
17587402a6fSmrg    --) shift
17687402a6fSmrg        break;;
177b7d53ee5Smrg
17887402a6fSmrg    -*) echo "$0: invalid option: $1" >&2
17987402a6fSmrg        exit 1;;
180b7d53ee5Smrg
1812ceb2653Smrg    *)  break;;
1822ceb2653Smrg  esac
1832ceb2653Smrg  shift
1842ceb2653Smrgdone
185b7d53ee5Smrg
18687402a6fSmrg# We allow the use of options -d and -T together, by making -d
18787402a6fSmrg# take the precedence; this is for compatibility with GNU install.
18887402a6fSmrg
18987402a6fSmrgif test -n "$dir_arg"; then
19087402a6fSmrg  if test -n "$dst_arg"; then
19187402a6fSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19287402a6fSmrg    exit 1
19387402a6fSmrg  fi
19487402a6fSmrgfi
19587402a6fSmrg
1962ceb2653Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1972ceb2653Smrg  # When -d is used, all remaining arguments are directories to create.
1982ceb2653Smrg  # When -t is used, the destination is already specified.
1992ceb2653Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2002ceb2653Smrg  for arg
2012ceb2653Smrg  do
2022ceb2653Smrg    if test -n "$dst_arg"; then
2032ceb2653Smrg      # $@ is not empty: it contains at least $arg.
2042ceb2653Smrg      set fnord "$@" "$dst_arg"
2052ceb2653Smrg      shift # fnord
2062ceb2653Smrg    fi
2072ceb2653Smrg    shift # arg
2082ceb2653Smrg    dst_arg=$arg
20987402a6fSmrg    # Protect names problematic for 'test' and other utilities.
21087402a6fSmrg    case $dst_arg in
21187402a6fSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21287402a6fSmrg    esac
2132ceb2653Smrg  done
2142ceb2653Smrgfi
215b7d53ee5Smrg
2162ceb2653Smrgif test $# -eq 0; then
2172ceb2653Smrg  if test -z "$dir_arg"; then
2182ceb2653Smrg    echo "$0: no input file specified." >&2
2192ceb2653Smrg    exit 1
2202ceb2653Smrg  fi
22187402a6fSmrg  # It's OK to call 'install-sh -d' without argument.
2222ceb2653Smrg  # This can happen when creating conditional directories.
2232ceb2653Smrg  exit 0
2242ceb2653Smrgfi
225b7d53ee5Smrg
2262ceb2653Smrgif test -z "$dir_arg"; then
22787402a6fSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22887402a6fSmrg    if test ! -d "$dst_arg"; then
22987402a6fSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23087402a6fSmrg      exit 1
23187402a6fSmrg    fi
23287402a6fSmrg  fi
23387402a6fSmrgfi
23487402a6fSmrg
23587402a6fSmrgif test -z "$dir_arg"; then
23687402a6fSmrg  do_exit='(exit $ret); exit $ret'
23787402a6fSmrg  trap "ret=129; $do_exit" 1
23887402a6fSmrg  trap "ret=130; $do_exit" 2
23987402a6fSmrg  trap "ret=141; $do_exit" 13
24087402a6fSmrg  trap "ret=143; $do_exit" 15
2412ceb2653Smrg
2422ceb2653Smrg  # Set umask so as not to create temps with too-generous modes.
2432ceb2653Smrg  # However, 'strip' requires both read and write access to temps.
2442ceb2653Smrg  case $mode in
2452ceb2653Smrg    # Optimize common cases.
2462ceb2653Smrg    *644) cp_umask=133;;
2472ceb2653Smrg    *755) cp_umask=22;;
2482ceb2653Smrg
2492ceb2653Smrg    *[0-7])
2502ceb2653Smrg      if test -z "$stripcmd"; then
25187402a6fSmrg        u_plus_rw=
2522ceb2653Smrg      else
25387402a6fSmrg        u_plus_rw='% 200'
2542ceb2653Smrg      fi
2552ceb2653Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2562ceb2653Smrg    *)
2572ceb2653Smrg      if test -z "$stripcmd"; then
25887402a6fSmrg        u_plus_rw=
2592ceb2653Smrg      else
26087402a6fSmrg        u_plus_rw=,u+rw
2612ceb2653Smrg      fi
2622ceb2653Smrg      cp_umask=$mode$u_plus_rw;;
2632ceb2653Smrg  esac
2642ceb2653Smrgfi
265b7d53ee5Smrg
2662ceb2653Smrgfor src
2672ceb2653Smrgdo
26887402a6fSmrg  # Protect names problematic for 'test' and other utilities.
2692ceb2653Smrg  case $src in
27087402a6fSmrg    -* | [=\(\)!]) src=./$src;;
2712ceb2653Smrg  esac
2722ceb2653Smrg
2732ceb2653Smrg  if test -n "$dir_arg"; then
2742ceb2653Smrg    dst=$src
2752ceb2653Smrg    dstdir=$dst
2762ceb2653Smrg    test -d "$dstdir"
2772ceb2653Smrg    dstdir_status=$?
27887402a6fSmrg    # Don't chown directories that already exist.
27987402a6fSmrg    if test $dstdir_status = 0; then
28087402a6fSmrg      chowncmd=""
28187402a6fSmrg    fi
2822ceb2653Smrg  else
2832ceb2653Smrg
2842ceb2653Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2852ceb2653Smrg    # might cause directories to be created, which would be especially bad
2862ceb2653Smrg    # if $src (and thus $dsttmp) contains '*'.
2872ceb2653Smrg    if test ! -f "$src" && test ! -d "$src"; then
2882ceb2653Smrg      echo "$0: $src does not exist." >&2
2892ceb2653Smrg      exit 1
2902ceb2653Smrg    fi
2912ceb2653Smrg
2922ceb2653Smrg    if test -z "$dst_arg"; then
2932ceb2653Smrg      echo "$0: no destination specified." >&2
2942ceb2653Smrg      exit 1
2952ceb2653Smrg    fi
2962ceb2653Smrg    dst=$dst_arg
297b7d53ee5Smrg
29887402a6fSmrg    # If destination is a directory, append the input filename.
2992ceb2653Smrg    if test -d "$dst"; then
30087402a6fSmrg      if test "$is_target_a_directory" = never; then
30187402a6fSmrg        echo "$0: $dst_arg: Is a directory" >&2
30287402a6fSmrg        exit 1
3032ceb2653Smrg      fi
3042ceb2653Smrg      dstdir=$dst
30587402a6fSmrg      dstbase=`basename "$src"`
30687402a6fSmrg      case $dst in
30787402a6fSmrg	*/) dst=$dst$dstbase;;
30887402a6fSmrg	*)  dst=$dst/$dstbase;;
30987402a6fSmrg      esac
3102ceb2653Smrg      dstdir_status=0
3112ceb2653Smrg    else
31287402a6fSmrg      dstdir=`dirname "$dst"`
3132ceb2653Smrg      test -d "$dstdir"
3142ceb2653Smrg      dstdir_status=$?
3152ceb2653Smrg    fi
3162ceb2653Smrg  fi
3172ceb2653Smrg
31887402a6fSmrg  case $dstdir in
31987402a6fSmrg    */) dstdirslash=$dstdir;;
32087402a6fSmrg    *)  dstdirslash=$dstdir/;;
32187402a6fSmrg  esac
32287402a6fSmrg
3232ceb2653Smrg  obsolete_mkdir_used=false
3242ceb2653Smrg
3252ceb2653Smrg  if test $dstdir_status != 0; then
3262ceb2653Smrg    case $posix_mkdir in
3272ceb2653Smrg      '')
32887402a6fSmrg        # With -d, create the new directory with the user-specified mode.
32987402a6fSmrg        # Otherwise, rely on $mkdir_umask.
33087402a6fSmrg        if test -n "$dir_arg"; then
33187402a6fSmrg          mkdir_mode=-m$mode
33287402a6fSmrg        else
33387402a6fSmrg          mkdir_mode=
33487402a6fSmrg        fi
33587402a6fSmrg
33687402a6fSmrg        posix_mkdir=false
33787402a6fSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33887402a6fSmrg	# here however when possible just to lower collision chance.
33987402a6fSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34087402a6fSmrg
34187402a6fSmrg	trap '
34287402a6fSmrg	  ret=$?
34387402a6fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34487402a6fSmrg	  exit $ret
34587402a6fSmrg	' 0
34687402a6fSmrg
34787402a6fSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34887402a6fSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34987402a6fSmrg	# directory is successfully created first before we actually test
35087402a6fSmrg	# 'mkdir -p'.
35187402a6fSmrg	if (umask $mkdir_umask &&
35287402a6fSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35387402a6fSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35487402a6fSmrg	then
35587402a6fSmrg	  if test -z "$dir_arg" || {
35687402a6fSmrg	       # Check for POSIX incompatibilities with -m.
35787402a6fSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35887402a6fSmrg	       # other-writable bit of parent directory when it shouldn't.
35987402a6fSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36087402a6fSmrg	       test_tmpdir="$tmpdir/a"
36187402a6fSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36287402a6fSmrg	       case $ls_ld_tmpdir in
36387402a6fSmrg		 d????-?r-*) different_mode=700;;
36487402a6fSmrg		 d????-?--*) different_mode=755;;
36587402a6fSmrg		 *) false;;
36687402a6fSmrg	       esac &&
36787402a6fSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36887402a6fSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36987402a6fSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37087402a6fSmrg	       }
37187402a6fSmrg	     }
37287402a6fSmrg	  then posix_mkdir=:
37387402a6fSmrg	  fi
37487402a6fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375b7d53ee5Smrg	else
37687402a6fSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37787402a6fSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378b7d53ee5Smrg	fi
37987402a6fSmrg	trap '' 0;;
3802ceb2653Smrg    esac
381b7d53ee5Smrg
3822ceb2653Smrg    if
3832ceb2653Smrg      $posix_mkdir && (
38487402a6fSmrg        umask $mkdir_umask &&
38587402a6fSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3862ceb2653Smrg      )
3872ceb2653Smrg    then :
3882ceb2653Smrg    else
3892ceb2653Smrg
39087402a6fSmrg      # mkdir does not conform to POSIX,
3912ceb2653Smrg      # or it failed possibly due to a race condition.  Create the
3922ceb2653Smrg      # directory the slow way, step by step, checking for races as we go.
3932ceb2653Smrg
3942ceb2653Smrg      case $dstdir in
39587402a6fSmrg        /*) prefix='/';;
39687402a6fSmrg        [-=\(\)!]*) prefix='./';;
39787402a6fSmrg        *)  prefix='';;
3982ceb2653Smrg      esac
3992ceb2653Smrg
4002ceb2653Smrg      oIFS=$IFS
4012ceb2653Smrg      IFS=/
40287402a6fSmrg      set -f
4032ceb2653Smrg      set fnord $dstdir
4042ceb2653Smrg      shift
40587402a6fSmrg      set +f
4062ceb2653Smrg      IFS=$oIFS
4072ceb2653Smrg
4082ceb2653Smrg      prefixes=
4092ceb2653Smrg
4102ceb2653Smrg      for d
4112ceb2653Smrg      do
41287402a6fSmrg        test X"$d" = X && continue
41387402a6fSmrg
41487402a6fSmrg        prefix=$prefix$d
41587402a6fSmrg        if test -d "$prefix"; then
41687402a6fSmrg          prefixes=
41787402a6fSmrg        else
41887402a6fSmrg          if $posix_mkdir; then
41987402a6fSmrg            (umask $mkdir_umask &&
42087402a6fSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42187402a6fSmrg            # Don't fail if two instances are running concurrently.
42287402a6fSmrg            test -d "$prefix" || exit 1
42387402a6fSmrg          else
42487402a6fSmrg            case $prefix in
42587402a6fSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42687402a6fSmrg              *) qprefix=$prefix;;
42787402a6fSmrg            esac
42887402a6fSmrg            prefixes="$prefixes '$qprefix'"
42987402a6fSmrg          fi
43087402a6fSmrg        fi
43187402a6fSmrg        prefix=$prefix/
4322ceb2653Smrg      done
4332ceb2653Smrg
4342ceb2653Smrg      if test -n "$prefixes"; then
43587402a6fSmrg        # Don't fail if two instances are running concurrently.
43687402a6fSmrg        (umask $mkdir_umask &&
43787402a6fSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43887402a6fSmrg          test -d "$dstdir" || exit 1
43987402a6fSmrg        obsolete_mkdir_used=true
4402ceb2653Smrg      fi
4412ceb2653Smrg    fi
4422ceb2653Smrg  fi
4432ceb2653Smrg
4442ceb2653Smrg  if test -n "$dir_arg"; then
4452ceb2653Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4462ceb2653Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4472ceb2653Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4482ceb2653Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4492ceb2653Smrg  else
4502ceb2653Smrg
4512ceb2653Smrg    # Make a couple of temp file names in the proper directory.
45287402a6fSmrg    dsttmp=${dstdirslash}_inst.$$_
45387402a6fSmrg    rmtmp=${dstdirslash}_rm.$$_
4542ceb2653Smrg
4552ceb2653Smrg    # Trap to clean up those temp files at exit.
4562ceb2653Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4572ceb2653Smrg
4582ceb2653Smrg    # Copy the file name to the temp name.
45987402a6fSmrg    (umask $cp_umask &&
46087402a6fSmrg     { test -z "$stripcmd" || {
46187402a6fSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46287402a6fSmrg	 # which would cause strip to fail.
46387402a6fSmrg	 if test -z "$doit"; then
46487402a6fSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46587402a6fSmrg	 else
46687402a6fSmrg	   $doit touch "$dsttmp"
46787402a6fSmrg	 fi
46887402a6fSmrg       }
46987402a6fSmrg     } &&
47087402a6fSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4712ceb2653Smrg
4722ceb2653Smrg    # and set any options; do chmod last to preserve setuid bits.
4732ceb2653Smrg    #
4742ceb2653Smrg    # If any of these fail, we abort the whole thing.  If we want to
4752ceb2653Smrg    # ignore errors from any of these, just make sure not to ignore
4762ceb2653Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4772ceb2653Smrg    #
4782ceb2653Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4792ceb2653Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4802ceb2653Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4812ceb2653Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4822ceb2653Smrg
4832ceb2653Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4842ceb2653Smrg    if $copy_on_change &&
48587402a6fSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48687402a6fSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48787402a6fSmrg       set -f &&
4882ceb2653Smrg       set X $old && old=:$2:$4:$5:$6 &&
4892ceb2653Smrg       set X $new && new=:$2:$4:$5:$6 &&
49087402a6fSmrg       set +f &&
4912ceb2653Smrg       test "$old" = "$new" &&
4922ceb2653Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4932ceb2653Smrg    then
4942ceb2653Smrg      rm -f "$dsttmp"
4952ceb2653Smrg    else
49687402a6fSmrg      # If $backupsuffix is set, and the file being installed
49787402a6fSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49887402a6fSmrg      # e.g., if mv doesn't support -f.
49987402a6fSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50087402a6fSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50187402a6fSmrg      fi
50287402a6fSmrg
5032ceb2653Smrg      # Rename the file to the real destination.
5042ceb2653Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5052ceb2653Smrg
5062ceb2653Smrg      # The rename failed, perhaps because mv can't rename something else
5072ceb2653Smrg      # to itself, or perhaps because mv is so ancient that it does not
5082ceb2653Smrg      # support -f.
5092ceb2653Smrg      {
51087402a6fSmrg        # Now remove or move aside any old file at destination location.
51187402a6fSmrg        # We try this two ways since rm can't unlink itself on some
51287402a6fSmrg        # systems and the destination file might be busy for other
51387402a6fSmrg        # reasons.  In this case, the final cleanup might fail but the new
51487402a6fSmrg        # file should still install successfully.
51587402a6fSmrg        {
51687402a6fSmrg          test ! -f "$dst" ||
51787402a6fSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51887402a6fSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51987402a6fSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52087402a6fSmrg          } ||
52187402a6fSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52287402a6fSmrg            (exit 1); exit 1
52387402a6fSmrg          }
52487402a6fSmrg        } &&
52587402a6fSmrg
52687402a6fSmrg        # Now rename the file to the real destination.
52787402a6fSmrg        $doit $mvcmd "$dsttmp" "$dst"
5282ceb2653Smrg      }
5292ceb2653Smrg    fi || exit 1
5302ceb2653Smrg
5312ceb2653Smrg    trap '' 0
5322ceb2653Smrg  fi
5332ceb2653Smrgdone
534b7d53ee5Smrg
5352ceb2653Smrg# Local variables:
53687402a6fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5372ceb2653Smrg# time-stamp-start: "scriptversion="
5382ceb2653Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53987402a6fSmrg# time-stamp-time-zone: "UTC0"
5402ceb2653Smrg# time-stamp-end: "; # UTC"
5412ceb2653Smrg# End:
542