1ed029a5fSmrg#!/bin/sh
2ed029a5fSmrg# install - install a program, script, or datafile
3dd519f38Smrg
45d10e53fSmrgscriptversion=2020-11-14.01; # UTC
5dd519f38Smrg
6ed029a5fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7ed029a5fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8ed029a5fSmrg# following copyright and license.
9ed029a5fSmrg#
10ed029a5fSmrg# Copyright (C) 1994 X Consortium
11ed029a5fSmrg#
12ed029a5fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13ed029a5fSmrg# of this software and associated documentation files (the "Software"), to
14ed029a5fSmrg# deal in the Software without restriction, including without limitation the
15ed029a5fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16ed029a5fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17ed029a5fSmrg# furnished to do so, subject to the following conditions:
18ed029a5fSmrg#
19ed029a5fSmrg# The above copyright notice and this permission notice shall be included in
20ed029a5fSmrg# all copies or substantial portions of the Software.
21ed029a5fSmrg#
22ed029a5fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23ed029a5fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24ed029a5fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25ed029a5fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26ed029a5fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27ed029a5fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28ed029a5fSmrg#
29ed029a5fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30ed029a5fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31ed029a5fSmrg# ings in this Software without prior written authorization from the X Consor-
32ed029a5fSmrg# tium.
33ed029a5fSmrg#
34ed029a5fSmrg#
35ed029a5fSmrg# FSF changes to this file are in the public domain.
36ed029a5fSmrg#
37ed029a5fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
385d10e53fSmrg# 'make' implicit rules from creating a file called install from it
39ed029a5fSmrg# when there is no Makefile.
40ed029a5fSmrg#
41ed029a5fSmrg# This script is compatible with the BSD install script, but was written
42dd519f38Smrg# from scratch.
43ed029a5fSmrg
445d10e53fSmrgtab='	'
45dd519f38Smrgnl='
46dd519f38Smrg'
475d10e53fSmrgIFS=" $tab$nl"
48ed029a5fSmrg
495d10e53fSmrg# Set DOITPROG to "echo" to test this script.
50ed029a5fSmrg
51dd519f38Smrgdoit=${DOITPROG-}
525d10e53fSmrgdoit_exec=${doit:-exec}
53ed029a5fSmrg
54dd519f38Smrg# Put in absolute file names if you don't have them in your path;
55dd519f38Smrg# or use environment vars.
56dd519f38Smrg
57dd519f38Smrgchgrpprog=${CHGRPPROG-chgrp}
58dd519f38Smrgchmodprog=${CHMODPROG-chmod}
59dd519f38Smrgchownprog=${CHOWNPROG-chown}
60dd519f38Smrgcmpprog=${CMPPROG-cmp}
61dd519f38Smrgcpprog=${CPPROG-cp}
62dd519f38Smrgmkdirprog=${MKDIRPROG-mkdir}
63dd519f38Smrgmvprog=${MVPROG-mv}
64dd519f38Smrgrmprog=${RMPROG-rm}
65dd519f38Smrgstripprog=${STRIPPROG-strip}
66dd519f38Smrg
67dd519f38Smrgposix_mkdir=
68dd519f38Smrg
69dd519f38Smrg# Desired mode of installed file.
70dd519f38Smrgmode=0755
71dd519f38Smrg
725d10e53fSmrg# Create dirs (including intermediate dirs) using mode 755.
735d10e53fSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
745d10e53fSmrgmkdir_umask=22
755d10e53fSmrg
765d10e53fSmrgbackupsuffix=
77dd519f38Smrgchgrpcmd=
78dd519f38Smrgchmodcmd=$chmodprog
79dd519f38Smrgchowncmd=
80dd519f38Smrgmvcmd=$mvprog
81dd519f38Smrgrmcmd="$rmprog -f"
82dd519f38Smrgstripcmd=
83ed029a5fSmrg
84dd519f38Smrgsrc=
85dd519f38Smrgdst=
86dd519f38Smrgdir_arg=
87dd519f38Smrgdst_arg=
88ed029a5fSmrg
89dd519f38Smrgcopy_on_change=false
905d10e53fSmrgis_target_a_directory=possibly
91ed029a5fSmrg
92dd519f38Smrgusage="\
93dd519f38SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94dd519f38Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95dd519f38Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96dd519f38Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97ed029a5fSmrg
98dd519f38SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99dd519f38SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100dd519f38SmrgIn the 4th, create DIRECTORIES.
101ed029a5fSmrg
102dd519f38SmrgOptions:
103dd519f38Smrg     --help     display this help and exit.
104dd519f38Smrg     --version  display version info and exit.
105ed029a5fSmrg
106dd519f38Smrg  -c            (ignored)
1075d10e53fSmrg  -C            install only if different (preserve data modification time)
108dd519f38Smrg  -d            create directories instead of installing files.
109dd519f38Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110dd519f38Smrg  -m MODE       $chmodprog installed files to MODE.
111dd519f38Smrg  -o USER       $chownprog installed files to USER.
1125d10e53fSmrg  -p            pass -p to $cpprog.
113dd519f38Smrg  -s            $stripprog installed files.
1145d10e53fSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115dd519f38Smrg  -t DIRECTORY  install into DIRECTORY.
116dd519f38Smrg  -T            report an error if DSTFILE is a directory.
117ed029a5fSmrg
118dd519f38SmrgEnvironment variables override the default commands:
119dd519f38Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120dd519f38Smrg  RMPROG STRIPPROG
1215d10e53fSmrg
1225d10e53fSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1235d10e53fSmrgit's up to you to specify -f if you want it.
1245d10e53fSmrg
1255d10e53fSmrgIf -S is not specified, no backups are attempted.
1265d10e53fSmrg
1275d10e53fSmrgEmail bug reports to bug-automake@gnu.org.
1285d10e53fSmrgAutomake home page: https://www.gnu.org/software/automake/
129dd519f38Smrg"
130ed029a5fSmrg
131dd519f38Smrgwhile test $# -ne 0; do
132dd519f38Smrg  case $1 in
133dd519f38Smrg    -c) ;;
134ed029a5fSmrg
135dd519f38Smrg    -C) copy_on_change=true;;
136ed029a5fSmrg
137dd519f38Smrg    -d) dir_arg=true;;
138ed029a5fSmrg
139dd519f38Smrg    -g) chgrpcmd="$chgrpprog $2"
1405d10e53fSmrg        shift;;
141ed029a5fSmrg
142dd519f38Smrg    --help) echo "$usage"; exit $?;;
143ed029a5fSmrg
144dd519f38Smrg    -m) mode=$2
1455d10e53fSmrg        case $mode in
1465d10e53fSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1475d10e53fSmrg            echo "$0: invalid mode: $mode" >&2
1485d10e53fSmrg            exit 1;;
1495d10e53fSmrg        esac
1505d10e53fSmrg        shift;;
151ed029a5fSmrg
152dd519f38Smrg    -o) chowncmd="$chownprog $2"
1535d10e53fSmrg        shift;;
1545d10e53fSmrg
1555d10e53fSmrg    -p) cpprog="$cpprog -p";;
156ed029a5fSmrg
157dd519f38Smrg    -s) stripcmd=$stripprog;;
158ed029a5fSmrg
1595d10e53fSmrg    -S) backupsuffix="$2"
1605d10e53fSmrg        shift;;
1615d10e53fSmrg
1625d10e53fSmrg    -t)
1635d10e53fSmrg        is_target_a_directory=always
1645d10e53fSmrg        dst_arg=$2
1655d10e53fSmrg        # Protect names problematic for 'test' and other utilities.
1665d10e53fSmrg        case $dst_arg in
1675d10e53fSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1685d10e53fSmrg        esac
1695d10e53fSmrg        shift;;
170ed029a5fSmrg
1715d10e53fSmrg    -T) is_target_a_directory=never;;
172ed029a5fSmrg
173dd519f38Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174ed029a5fSmrg
1755d10e53fSmrg    --) shift
1765d10e53fSmrg        break;;
177ed029a5fSmrg
1785d10e53fSmrg    -*) echo "$0: invalid option: $1" >&2
1795d10e53fSmrg        exit 1;;
180ed029a5fSmrg
181dd519f38Smrg    *)  break;;
182dd519f38Smrg  esac
183dd519f38Smrg  shift
184dd519f38Smrgdone
185ed029a5fSmrg
1865d10e53fSmrg# We allow the use of options -d and -T together, by making -d
1875d10e53fSmrg# take the precedence; this is for compatibility with GNU install.
1885d10e53fSmrg
1895d10e53fSmrgif test -n "$dir_arg"; then
1905d10e53fSmrg  if test -n "$dst_arg"; then
1915d10e53fSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1925d10e53fSmrg    exit 1
1935d10e53fSmrg  fi
1945d10e53fSmrgfi
1955d10e53fSmrg
196dd519f38Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197dd519f38Smrg  # When -d is used, all remaining arguments are directories to create.
198dd519f38Smrg  # When -t is used, the destination is already specified.
199dd519f38Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200dd519f38Smrg  for arg
201dd519f38Smrg  do
202dd519f38Smrg    if test -n "$dst_arg"; then
203dd519f38Smrg      # $@ is not empty: it contains at least $arg.
204dd519f38Smrg      set fnord "$@" "$dst_arg"
205dd519f38Smrg      shift # fnord
206dd519f38Smrg    fi
207dd519f38Smrg    shift # arg
208dd519f38Smrg    dst_arg=$arg
2095d10e53fSmrg    # Protect names problematic for 'test' and other utilities.
2105d10e53fSmrg    case $dst_arg in
2115d10e53fSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2125d10e53fSmrg    esac
213dd519f38Smrg  done
214dd519f38Smrgfi
215ed029a5fSmrg
216dd519f38Smrgif test $# -eq 0; then
217dd519f38Smrg  if test -z "$dir_arg"; then
218dd519f38Smrg    echo "$0: no input file specified." >&2
219dd519f38Smrg    exit 1
220dd519f38Smrg  fi
2215d10e53fSmrg  # It's OK to call 'install-sh -d' without argument.
222dd519f38Smrg  # This can happen when creating conditional directories.
223dd519f38Smrg  exit 0
224dd519f38Smrgfi
225ed029a5fSmrg
226dd519f38Smrgif test -z "$dir_arg"; then
2275d10e53fSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2285d10e53fSmrg    if test ! -d "$dst_arg"; then
2295d10e53fSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2305d10e53fSmrg      exit 1
2315d10e53fSmrg    fi
2325d10e53fSmrg  fi
2335d10e53fSmrgfi
2345d10e53fSmrg
2355d10e53fSmrgif test -z "$dir_arg"; then
2365d10e53fSmrg  do_exit='(exit $ret); exit $ret'
2375d10e53fSmrg  trap "ret=129; $do_exit" 1
2385d10e53fSmrg  trap "ret=130; $do_exit" 2
2395d10e53fSmrg  trap "ret=141; $do_exit" 13
2405d10e53fSmrg  trap "ret=143; $do_exit" 15
241dd519f38Smrg
242dd519f38Smrg  # Set umask so as not to create temps with too-generous modes.
243dd519f38Smrg  # However, 'strip' requires both read and write access to temps.
244dd519f38Smrg  case $mode in
245dd519f38Smrg    # Optimize common cases.
246dd519f38Smrg    *644) cp_umask=133;;
247dd519f38Smrg    *755) cp_umask=22;;
248dd519f38Smrg
249dd519f38Smrg    *[0-7])
250dd519f38Smrg      if test -z "$stripcmd"; then
2515d10e53fSmrg        u_plus_rw=
252dd519f38Smrg      else
2535d10e53fSmrg        u_plus_rw='% 200'
254dd519f38Smrg      fi
255dd519f38Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256dd519f38Smrg    *)
257dd519f38Smrg      if test -z "$stripcmd"; then
2585d10e53fSmrg        u_plus_rw=
259dd519f38Smrg      else
2605d10e53fSmrg        u_plus_rw=,u+rw
261dd519f38Smrg      fi
262dd519f38Smrg      cp_umask=$mode$u_plus_rw;;
263dd519f38Smrg  esac
264dd519f38Smrgfi
265ed029a5fSmrg
266dd519f38Smrgfor src
267dd519f38Smrgdo
2685d10e53fSmrg  # Protect names problematic for 'test' and other utilities.
269dd519f38Smrg  case $src in
2705d10e53fSmrg    -* | [=\(\)!]) src=./$src;;
271dd519f38Smrg  esac
272dd519f38Smrg
273dd519f38Smrg  if test -n "$dir_arg"; then
274dd519f38Smrg    dst=$src
275dd519f38Smrg    dstdir=$dst
276dd519f38Smrg    test -d "$dstdir"
277dd519f38Smrg    dstdir_status=$?
2785d10e53fSmrg    # Don't chown directories that already exist.
2795d10e53fSmrg    if test $dstdir_status = 0; then
2805d10e53fSmrg      chowncmd=""
2815d10e53fSmrg    fi
282dd519f38Smrg  else
283dd519f38Smrg
284dd519f38Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285dd519f38Smrg    # might cause directories to be created, which would be especially bad
286dd519f38Smrg    # if $src (and thus $dsttmp) contains '*'.
287dd519f38Smrg    if test ! -f "$src" && test ! -d "$src"; then
288dd519f38Smrg      echo "$0: $src does not exist." >&2
289dd519f38Smrg      exit 1
290dd519f38Smrg    fi
291dd519f38Smrg
292dd519f38Smrg    if test -z "$dst_arg"; then
293dd519f38Smrg      echo "$0: no destination specified." >&2
294dd519f38Smrg      exit 1
295dd519f38Smrg    fi
296dd519f38Smrg    dst=$dst_arg
297ed029a5fSmrg
2985d10e53fSmrg    # If destination is a directory, append the input filename.
299dd519f38Smrg    if test -d "$dst"; then
3005d10e53fSmrg      if test "$is_target_a_directory" = never; then
3015d10e53fSmrg        echo "$0: $dst_arg: Is a directory" >&2
3025d10e53fSmrg        exit 1
303dd519f38Smrg      fi
304dd519f38Smrg      dstdir=$dst
3055d10e53fSmrg      dstbase=`basename "$src"`
3065d10e53fSmrg      case $dst in
3075d10e53fSmrg	*/) dst=$dst$dstbase;;
3085d10e53fSmrg	*)  dst=$dst/$dstbase;;
3095d10e53fSmrg      esac
310dd519f38Smrg      dstdir_status=0
311dd519f38Smrg    else
3125d10e53fSmrg      dstdir=`dirname "$dst"`
313dd519f38Smrg      test -d "$dstdir"
314dd519f38Smrg      dstdir_status=$?
315dd519f38Smrg    fi
316dd519f38Smrg  fi
317dd519f38Smrg
3185d10e53fSmrg  case $dstdir in
3195d10e53fSmrg    */) dstdirslash=$dstdir;;
3205d10e53fSmrg    *)  dstdirslash=$dstdir/;;
3215d10e53fSmrg  esac
3225d10e53fSmrg
323dd519f38Smrg  obsolete_mkdir_used=false
324dd519f38Smrg
325dd519f38Smrg  if test $dstdir_status != 0; then
326dd519f38Smrg    case $posix_mkdir in
327dd519f38Smrg      '')
3285d10e53fSmrg        # With -d, create the new directory with the user-specified mode.
3295d10e53fSmrg        # Otherwise, rely on $mkdir_umask.
3305d10e53fSmrg        if test -n "$dir_arg"; then
3315d10e53fSmrg          mkdir_mode=-m$mode
3325d10e53fSmrg        else
3335d10e53fSmrg          mkdir_mode=
3345d10e53fSmrg        fi
3355d10e53fSmrg
3365d10e53fSmrg        posix_mkdir=false
3375d10e53fSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3385d10e53fSmrg	# here however when possible just to lower collision chance.
3395d10e53fSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3405d10e53fSmrg
3415d10e53fSmrg	trap '
3425d10e53fSmrg	  ret=$?
3435d10e53fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3445d10e53fSmrg	  exit $ret
3455d10e53fSmrg	' 0
3465d10e53fSmrg
3475d10e53fSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3485d10e53fSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3495d10e53fSmrg	# directory is successfully created first before we actually test
3505d10e53fSmrg	# 'mkdir -p'.
3515d10e53fSmrg	if (umask $mkdir_umask &&
3525d10e53fSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3535d10e53fSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3545d10e53fSmrg	then
3555d10e53fSmrg	  if test -z "$dir_arg" || {
3565d10e53fSmrg	       # Check for POSIX incompatibilities with -m.
3575d10e53fSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3585d10e53fSmrg	       # other-writable bit of parent directory when it shouldn't.
3595d10e53fSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3605d10e53fSmrg	       test_tmpdir="$tmpdir/a"
3615d10e53fSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3625d10e53fSmrg	       case $ls_ld_tmpdir in
3635d10e53fSmrg		 d????-?r-*) different_mode=700;;
3645d10e53fSmrg		 d????-?--*) different_mode=755;;
3655d10e53fSmrg		 *) false;;
3665d10e53fSmrg	       esac &&
3675d10e53fSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3685d10e53fSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3695d10e53fSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3705d10e53fSmrg	       }
3715d10e53fSmrg	     }
3725d10e53fSmrg	  then posix_mkdir=:
3735d10e53fSmrg	  fi
3745d10e53fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375ed029a5fSmrg	else
3765d10e53fSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3775d10e53fSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378ed029a5fSmrg	fi
3795d10e53fSmrg	trap '' 0;;
380dd519f38Smrg    esac
381ed029a5fSmrg
382dd519f38Smrg    if
383dd519f38Smrg      $posix_mkdir && (
3845d10e53fSmrg        umask $mkdir_umask &&
3855d10e53fSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386dd519f38Smrg      )
387dd519f38Smrg    then :
388dd519f38Smrg    else
389dd519f38Smrg
3905d10e53fSmrg      # mkdir does not conform to POSIX,
391dd519f38Smrg      # or it failed possibly due to a race condition.  Create the
392dd519f38Smrg      # directory the slow way, step by step, checking for races as we go.
393dd519f38Smrg
394dd519f38Smrg      case $dstdir in
3955d10e53fSmrg        /*) prefix='/';;
3965d10e53fSmrg        [-=\(\)!]*) prefix='./';;
3975d10e53fSmrg        *)  prefix='';;
398dd519f38Smrg      esac
399dd519f38Smrg
400dd519f38Smrg      oIFS=$IFS
401dd519f38Smrg      IFS=/
4025d10e53fSmrg      set -f
403dd519f38Smrg      set fnord $dstdir
404dd519f38Smrg      shift
4055d10e53fSmrg      set +f
406dd519f38Smrg      IFS=$oIFS
407dd519f38Smrg
408dd519f38Smrg      prefixes=
409dd519f38Smrg
410dd519f38Smrg      for d
411dd519f38Smrg      do
4125d10e53fSmrg        test X"$d" = X && continue
4135d10e53fSmrg
4145d10e53fSmrg        prefix=$prefix$d
4155d10e53fSmrg        if test -d "$prefix"; then
4165d10e53fSmrg          prefixes=
4175d10e53fSmrg        else
4185d10e53fSmrg          if $posix_mkdir; then
4195d10e53fSmrg            (umask $mkdir_umask &&
4205d10e53fSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4215d10e53fSmrg            # Don't fail if two instances are running concurrently.
4225d10e53fSmrg            test -d "$prefix" || exit 1
4235d10e53fSmrg          else
4245d10e53fSmrg            case $prefix in
4255d10e53fSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4265d10e53fSmrg              *) qprefix=$prefix;;
4275d10e53fSmrg            esac
4285d10e53fSmrg            prefixes="$prefixes '$qprefix'"
4295d10e53fSmrg          fi
4305d10e53fSmrg        fi
4315d10e53fSmrg        prefix=$prefix/
432dd519f38Smrg      done
433dd519f38Smrg
434dd519f38Smrg      if test -n "$prefixes"; then
4355d10e53fSmrg        # Don't fail if two instances are running concurrently.
4365d10e53fSmrg        (umask $mkdir_umask &&
4375d10e53fSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4385d10e53fSmrg          test -d "$dstdir" || exit 1
4395d10e53fSmrg        obsolete_mkdir_used=true
440dd519f38Smrg      fi
441dd519f38Smrg    fi
442dd519f38Smrg  fi
443dd519f38Smrg
444dd519f38Smrg  if test -n "$dir_arg"; then
445dd519f38Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446dd519f38Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447dd519f38Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448dd519f38Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449dd519f38Smrg  else
450dd519f38Smrg
451dd519f38Smrg    # Make a couple of temp file names in the proper directory.
4525d10e53fSmrg    dsttmp=${dstdirslash}_inst.$$_
4535d10e53fSmrg    rmtmp=${dstdirslash}_rm.$$_
454dd519f38Smrg
455dd519f38Smrg    # Trap to clean up those temp files at exit.
456dd519f38Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457dd519f38Smrg
458dd519f38Smrg    # Copy the file name to the temp name.
4595d10e53fSmrg    (umask $cp_umask &&
4605d10e53fSmrg     { test -z "$stripcmd" || {
4615d10e53fSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4625d10e53fSmrg	 # which would cause strip to fail.
4635d10e53fSmrg	 if test -z "$doit"; then
4645d10e53fSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4655d10e53fSmrg	 else
4665d10e53fSmrg	   $doit touch "$dsttmp"
4675d10e53fSmrg	 fi
4685d10e53fSmrg       }
4695d10e53fSmrg     } &&
4705d10e53fSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471dd519f38Smrg
472dd519f38Smrg    # and set any options; do chmod last to preserve setuid bits.
473dd519f38Smrg    #
474dd519f38Smrg    # If any of these fail, we abort the whole thing.  If we want to
475dd519f38Smrg    # ignore errors from any of these, just make sure not to ignore
476dd519f38Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477dd519f38Smrg    #
478dd519f38Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479dd519f38Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480dd519f38Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481dd519f38Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482dd519f38Smrg
483dd519f38Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484dd519f38Smrg    if $copy_on_change &&
4855d10e53fSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4865d10e53fSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4875d10e53fSmrg       set -f &&
488dd519f38Smrg       set X $old && old=:$2:$4:$5:$6 &&
489dd519f38Smrg       set X $new && new=:$2:$4:$5:$6 &&
4905d10e53fSmrg       set +f &&
491dd519f38Smrg       test "$old" = "$new" &&
492dd519f38Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493dd519f38Smrg    then
494dd519f38Smrg      rm -f "$dsttmp"
495dd519f38Smrg    else
4965d10e53fSmrg      # If $backupsuffix is set, and the file being installed
4975d10e53fSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4985d10e53fSmrg      # e.g., if mv doesn't support -f.
4995d10e53fSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5005d10e53fSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5015d10e53fSmrg      fi
5025d10e53fSmrg
503dd519f38Smrg      # Rename the file to the real destination.
504dd519f38Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505dd519f38Smrg
506dd519f38Smrg      # The rename failed, perhaps because mv can't rename something else
507dd519f38Smrg      # to itself, or perhaps because mv is so ancient that it does not
508dd519f38Smrg      # support -f.
509dd519f38Smrg      {
5105d10e53fSmrg        # Now remove or move aside any old file at destination location.
5115d10e53fSmrg        # We try this two ways since rm can't unlink itself on some
5125d10e53fSmrg        # systems and the destination file might be busy for other
5135d10e53fSmrg        # reasons.  In this case, the final cleanup might fail but the new
5145d10e53fSmrg        # file should still install successfully.
5155d10e53fSmrg        {
5165d10e53fSmrg          test ! -f "$dst" ||
5175d10e53fSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5185d10e53fSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5195d10e53fSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5205d10e53fSmrg          } ||
5215d10e53fSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5225d10e53fSmrg            (exit 1); exit 1
5235d10e53fSmrg          }
5245d10e53fSmrg        } &&
5255d10e53fSmrg
5265d10e53fSmrg        # Now rename the file to the real destination.
5275d10e53fSmrg        $doit $mvcmd "$dsttmp" "$dst"
528dd519f38Smrg      }
529dd519f38Smrg    fi || exit 1
530dd519f38Smrg
531dd519f38Smrg    trap '' 0
532dd519f38Smrg  fi
533dd519f38Smrgdone
534ed029a5fSmrg
535dd519f38Smrg# Local variables:
5365d10e53fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537dd519f38Smrg# time-stamp-start: "scriptversion="
538dd519f38Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5395d10e53fSmrg# time-stamp-time-zone: "UTC0"
540dd519f38Smrg# time-stamp-end: "; # UTC"
541dd519f38Smrg# End:
542