install-sh revision ea1d6981
117ca54c3Smrg#!/bin/sh
217ca54c3Smrg# install - install a program, script, or datafile
317ca54c3Smrg
4ea1d6981Smrgscriptversion=2018-03-11.20; # UTC
517ca54c3Smrg
617ca54c3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
717ca54c3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
817ca54c3Smrg# following copyright and license.
917ca54c3Smrg#
1017ca54c3Smrg# Copyright (C) 1994 X Consortium
1117ca54c3Smrg#
1217ca54c3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1317ca54c3Smrg# of this software and associated documentation files (the "Software"), to
1417ca54c3Smrg# deal in the Software without restriction, including without limitation the
1517ca54c3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1617ca54c3Smrg# sell copies of the Software, and to permit persons to whom the Software is
1717ca54c3Smrg# furnished to do so, subject to the following conditions:
1817ca54c3Smrg#
1917ca54c3Smrg# The above copyright notice and this permission notice shall be included in
2017ca54c3Smrg# all copies or substantial portions of the Software.
2117ca54c3Smrg#
2217ca54c3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2317ca54c3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2417ca54c3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2517ca54c3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2617ca54c3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2717ca54c3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2817ca54c3Smrg#
2917ca54c3Smrg# Except as contained in this notice, the name of the X Consortium shall not
3017ca54c3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3117ca54c3Smrg# ings in this Software without prior written authorization from the X Consor-
3217ca54c3Smrg# tium.
3317ca54c3Smrg#
3417ca54c3Smrg#
3517ca54c3Smrg# FSF changes to this file are in the public domain.
3617ca54c3Smrg#
3717ca54c3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38ea1d6981Smrg# 'make' implicit rules from creating a file called install from it
3917ca54c3Smrg# when there is no Makefile.
4017ca54c3Smrg#
4117ca54c3Smrg# This script is compatible with the BSD install script, but was written
42ea1d6981Smrg# from scratch.
4317ca54c3Smrg
44ea1d6981Smrgtab='	'
45ea1d6981Smrgnl='
46ea1d6981Smrg'
47ea1d6981SmrgIFS=" $tab$nl"
4817ca54c3Smrg
49ea1d6981Smrg# Set DOITPROG to "echo" to test this script.
5017ca54c3Smrg
51ea1d6981Smrgdoit=${DOITPROG-}
52ea1d6981Smrgdoit_exec=${doit:-exec}
5317ca54c3Smrg
54ea1d6981Smrg# Put in absolute file names if you don't have them in your path;
55ea1d6981Smrg# or use environment vars.
56ea1d6981Smrg
57ea1d6981Smrgchgrpprog=${CHGRPPROG-chgrp}
58ea1d6981Smrgchmodprog=${CHMODPROG-chmod}
59ea1d6981Smrgchownprog=${CHOWNPROG-chown}
60ea1d6981Smrgcmpprog=${CMPPROG-cmp}
61ea1d6981Smrgcpprog=${CPPROG-cp}
62ea1d6981Smrgmkdirprog=${MKDIRPROG-mkdir}
63ea1d6981Smrgmvprog=${MVPROG-mv}
64ea1d6981Smrgrmprog=${RMPROG-rm}
65ea1d6981Smrgstripprog=${STRIPPROG-strip}
66ea1d6981Smrg
67ea1d6981Smrgposix_mkdir=
68ea1d6981Smrg
69ea1d6981Smrg# Desired mode of installed file.
70ea1d6981Smrgmode=0755
7117ca54c3Smrg
7217ca54c3Smrgchgrpcmd=
73ea1d6981Smrgchmodcmd=$chmodprog
74ea1d6981Smrgchowncmd=
75ea1d6981Smrgmvcmd=$mvprog
7617ca54c3Smrgrmcmd="$rmprog -f"
77ea1d6981Smrgstripcmd=
78ea1d6981Smrg
7917ca54c3Smrgsrc=
8017ca54c3Smrgdst=
8117ca54c3Smrgdir_arg=
82ea1d6981Smrgdst_arg=
8317ca54c3Smrg
84ea1d6981Smrgcopy_on_change=false
85ea1d6981Smrgis_target_a_directory=possibly
86ea1d6981Smrg
87ea1d6981Smrgusage="\
88ea1d6981SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8917ca54c3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90ea1d6981Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91ea1d6981Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9217ca54c3Smrg
93ea1d6981SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94ea1d6981SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95ea1d6981SmrgIn the 4th, create DIRECTORIES.
9617ca54c3Smrg
9717ca54c3SmrgOptions:
98ea1d6981Smrg     --help     display this help and exit.
99ea1d6981Smrg     --version  display version info and exit.
100ea1d6981Smrg
101ea1d6981Smrg  -c            (ignored)
102ea1d6981Smrg  -C            install only if different (preserve the last data modification time)
103ea1d6981Smrg  -d            create directories instead of installing files.
104ea1d6981Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105ea1d6981Smrg  -m MODE       $chmodprog installed files to MODE.
106ea1d6981Smrg  -o USER       $chownprog installed files to USER.
107ea1d6981Smrg  -s            $stripprog installed files.
108ea1d6981Smrg  -t DIRECTORY  install into DIRECTORY.
109ea1d6981Smrg  -T            report an error if DSTFILE is a directory.
11017ca54c3Smrg
11117ca54c3SmrgEnvironment variables override the default commands:
112ea1d6981Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113ea1d6981Smrg  RMPROG STRIPPROG
11417ca54c3Smrg"
11517ca54c3Smrg
116ea1d6981Smrgwhile test $# -ne 0; do
11717ca54c3Smrg  case $1 in
118ea1d6981Smrg    -c) ;;
11917ca54c3Smrg
120ea1d6981Smrg    -C) copy_on_change=true;;
12117ca54c3Smrg
122ea1d6981Smrg    -d) dir_arg=true;;
12317ca54c3Smrg
12417ca54c3Smrg    -g) chgrpcmd="$chgrpprog $2"
125ea1d6981Smrg        shift;;
12617ca54c3Smrg
127ea1d6981Smrg    --help) echo "$usage"; exit $?;;
12817ca54c3Smrg
129ea1d6981Smrg    -m) mode=$2
130ea1d6981Smrg        case $mode in
131ea1d6981Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132ea1d6981Smrg            echo "$0: invalid mode: $mode" >&2
133ea1d6981Smrg            exit 1;;
134ea1d6981Smrg        esac
135ea1d6981Smrg        shift;;
13617ca54c3Smrg
13717ca54c3Smrg    -o) chowncmd="$chownprog $2"
138ea1d6981Smrg        shift;;
139ea1d6981Smrg
140ea1d6981Smrg    -s) stripcmd=$stripprog;;
141ea1d6981Smrg
142ea1d6981Smrg    -t)
143ea1d6981Smrg        is_target_a_directory=always
144ea1d6981Smrg        dst_arg=$2
145ea1d6981Smrg        # Protect names problematic for 'test' and other utilities.
146ea1d6981Smrg        case $dst_arg in
147ea1d6981Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148ea1d6981Smrg        esac
149ea1d6981Smrg        shift;;
150ea1d6981Smrg
151ea1d6981Smrg    -T) is_target_a_directory=never;;
152ea1d6981Smrg
153ea1d6981Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154ea1d6981Smrg
155ea1d6981Smrg    --) shift
156ea1d6981Smrg        break;;
157ea1d6981Smrg
158ea1d6981Smrg    -*) echo "$0: invalid option: $1" >&2
159ea1d6981Smrg        exit 1;;
160ea1d6981Smrg
161ea1d6981Smrg    *)  break;;
16217ca54c3Smrg  esac
163ea1d6981Smrg  shift
16417ca54c3Smrgdone
16517ca54c3Smrg
166ea1d6981Smrg# We allow the use of options -d and -T together, by making -d
167ea1d6981Smrg# take the precedence; this is for compatibility with GNU install.
168ea1d6981Smrg
169ea1d6981Smrgif test -n "$dir_arg"; then
170ea1d6981Smrg  if test -n "$dst_arg"; then
171ea1d6981Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172ea1d6981Smrg    exit 1
173ea1d6981Smrg  fi
174ea1d6981Smrgfi
175ea1d6981Smrg
176ea1d6981Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177ea1d6981Smrg  # When -d is used, all remaining arguments are directories to create.
178ea1d6981Smrg  # When -t is used, the destination is already specified.
179ea1d6981Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180ea1d6981Smrg  for arg
181ea1d6981Smrg  do
182ea1d6981Smrg    if test -n "$dst_arg"; then
183ea1d6981Smrg      # $@ is not empty: it contains at least $arg.
184ea1d6981Smrg      set fnord "$@" "$dst_arg"
185ea1d6981Smrg      shift # fnord
186ea1d6981Smrg    fi
187ea1d6981Smrg    shift # arg
188ea1d6981Smrg    dst_arg=$arg
189ea1d6981Smrg    # Protect names problematic for 'test' and other utilities.
190ea1d6981Smrg    case $dst_arg in
191ea1d6981Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192ea1d6981Smrg    esac
193ea1d6981Smrg  done
194ea1d6981Smrgfi
195ea1d6981Smrg
196ea1d6981Smrgif test $# -eq 0; then
19717ca54c3Smrg  if test -z "$dir_arg"; then
19817ca54c3Smrg    echo "$0: no input file specified." >&2
19917ca54c3Smrg    exit 1
20017ca54c3Smrg  fi
201ea1d6981Smrg  # It's OK to call 'install-sh -d' without argument.
20217ca54c3Smrg  # This can happen when creating conditional directories.
20317ca54c3Smrg  exit 0
20417ca54c3Smrgfi
20517ca54c3Smrg
206ea1d6981Smrgif test -z "$dir_arg"; then
207ea1d6981Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208ea1d6981Smrg    if test ! -d "$dst_arg"; then
209ea1d6981Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210ea1d6981Smrg      exit 1
211ea1d6981Smrg    fi
212ea1d6981Smrg  fi
213ea1d6981Smrgfi
214ea1d6981Smrg
215ea1d6981Smrgif test -z "$dir_arg"; then
216ea1d6981Smrg  do_exit='(exit $ret); exit $ret'
217ea1d6981Smrg  trap "ret=129; $do_exit" 1
218ea1d6981Smrg  trap "ret=130; $do_exit" 2
219ea1d6981Smrg  trap "ret=141; $do_exit" 13
220ea1d6981Smrg  trap "ret=143; $do_exit" 15
221ea1d6981Smrg
222ea1d6981Smrg  # Set umask so as not to create temps with too-generous modes.
223ea1d6981Smrg  # However, 'strip' requires both read and write access to temps.
224ea1d6981Smrg  case $mode in
225ea1d6981Smrg    # Optimize common cases.
226ea1d6981Smrg    *644) cp_umask=133;;
227ea1d6981Smrg    *755) cp_umask=22;;
228ea1d6981Smrg
229ea1d6981Smrg    *[0-7])
230ea1d6981Smrg      if test -z "$stripcmd"; then
231ea1d6981Smrg        u_plus_rw=
232ea1d6981Smrg      else
233ea1d6981Smrg        u_plus_rw='% 200'
234ea1d6981Smrg      fi
235ea1d6981Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236ea1d6981Smrg    *)
237ea1d6981Smrg      if test -z "$stripcmd"; then
238ea1d6981Smrg        u_plus_rw=
239ea1d6981Smrg      else
240ea1d6981Smrg        u_plus_rw=,u+rw
241ea1d6981Smrg      fi
242ea1d6981Smrg      cp_umask=$mode$u_plus_rw;;
243ea1d6981Smrg  esac
244ea1d6981Smrgfi
245ea1d6981Smrg
24617ca54c3Smrgfor src
24717ca54c3Smrgdo
248ea1d6981Smrg  # Protect names problematic for 'test' and other utilities.
24917ca54c3Smrg  case $src in
250ea1d6981Smrg    -* | [=\(\)!]) src=./$src;;
25117ca54c3Smrg  esac
25217ca54c3Smrg
25317ca54c3Smrg  if test -n "$dir_arg"; then
25417ca54c3Smrg    dst=$src
255ea1d6981Smrg    dstdir=$dst
256ea1d6981Smrg    test -d "$dstdir"
257ea1d6981Smrg    dstdir_status=$?
25817ca54c3Smrg  else
259ea1d6981Smrg
260ea1d6981Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26117ca54c3Smrg    # might cause directories to be created, which would be especially bad
26217ca54c3Smrg    # if $src (and thus $dsttmp) contains '*'.
26317ca54c3Smrg    if test ! -f "$src" && test ! -d "$src"; then
26417ca54c3Smrg      echo "$0: $src does not exist." >&2
26517ca54c3Smrg      exit 1
26617ca54c3Smrg    fi
26717ca54c3Smrg
268ea1d6981Smrg    if test -z "$dst_arg"; then
26917ca54c3Smrg      echo "$0: no destination specified." >&2
27017ca54c3Smrg      exit 1
27117ca54c3Smrg    fi
272ea1d6981Smrg    dst=$dst_arg
27317ca54c3Smrg
274ea1d6981Smrg    # If destination is a directory, append the input filename.
27517ca54c3Smrg    if test -d "$dst"; then
276ea1d6981Smrg      if test "$is_target_a_directory" = never; then
277ea1d6981Smrg        echo "$0: $dst_arg: Is a directory" >&2
278ea1d6981Smrg        exit 1
279ea1d6981Smrg      fi
280ea1d6981Smrg      dstdir=$dst
281ea1d6981Smrg      dstbase=`basename "$src"`
282ea1d6981Smrg      case $dst in
283ea1d6981Smrg	*/) dst=$dst$dstbase;;
284ea1d6981Smrg	*)  dst=$dst/$dstbase;;
285ea1d6981Smrg      esac
286ea1d6981Smrg      dstdir_status=0
287ea1d6981Smrg    else
288ea1d6981Smrg      dstdir=`dirname "$dst"`
289ea1d6981Smrg      test -d "$dstdir"
290ea1d6981Smrg      dstdir_status=$?
29117ca54c3Smrg    fi
29217ca54c3Smrg  fi
29317ca54c3Smrg
294ea1d6981Smrg  case $dstdir in
295ea1d6981Smrg    */) dstdirslash=$dstdir;;
296ea1d6981Smrg    *)  dstdirslash=$dstdir/;;
297ea1d6981Smrg  esac
29817ca54c3Smrg
299ea1d6981Smrg  obsolete_mkdir_used=false
300ea1d6981Smrg
301ea1d6981Smrg  if test $dstdir_status != 0; then
302ea1d6981Smrg    case $posix_mkdir in
303ea1d6981Smrg      '')
304ea1d6981Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
305ea1d6981Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
306ea1d6981Smrg        umask=`umask`
307ea1d6981Smrg        case $stripcmd.$umask in
308ea1d6981Smrg          # Optimize common cases.
309ea1d6981Smrg          *[2367][2367]) mkdir_umask=$umask;;
310ea1d6981Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311ea1d6981Smrg
312ea1d6981Smrg          *[0-7])
313ea1d6981Smrg            mkdir_umask=`expr $umask + 22 \
314ea1d6981Smrg              - $umask % 100 % 40 + $umask % 20 \
315ea1d6981Smrg              - $umask % 10 % 4 + $umask % 2
316ea1d6981Smrg            `;;
317ea1d6981Smrg          *) mkdir_umask=$umask,go-w;;
318ea1d6981Smrg        esac
319ea1d6981Smrg
320ea1d6981Smrg        # With -d, create the new directory with the user-specified mode.
321ea1d6981Smrg        # Otherwise, rely on $mkdir_umask.
322ea1d6981Smrg        if test -n "$dir_arg"; then
323ea1d6981Smrg          mkdir_mode=-m$mode
324ea1d6981Smrg        else
325ea1d6981Smrg          mkdir_mode=
326ea1d6981Smrg        fi
327ea1d6981Smrg
328ea1d6981Smrg        posix_mkdir=false
329ea1d6981Smrg        case $umask in
330ea1d6981Smrg          *[123567][0-7][0-7])
331ea1d6981Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
332ea1d6981Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333ea1d6981Smrg            ;;
334ea1d6981Smrg          *)
335ea1d6981Smrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
336ea1d6981Smrg            # here however when possible just to lower collision chance.
337ea1d6981Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
338ea1d6981Smrg
339ea1d6981Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
340ea1d6981Smrg
341ea1d6981Smrg            # Because "mkdir -p" follows existing symlinks and we likely work
342ea1d6981Smrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
343ea1d6981Smrg            # directory is successfully created first before we actually test
344ea1d6981Smrg            # 'mkdir -p' feature.
345ea1d6981Smrg            if (umask $mkdir_umask &&
346ea1d6981Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
347ea1d6981Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
348ea1d6981Smrg            then
349ea1d6981Smrg              if test -z "$dir_arg" || {
350ea1d6981Smrg                   # Check for POSIX incompatibilities with -m.
351ea1d6981Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
352ea1d6981Smrg                   # other-writable bit of parent directory when it shouldn't.
353ea1d6981Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
354ea1d6981Smrg                   test_tmpdir="$tmpdir/a"
355ea1d6981Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
356ea1d6981Smrg                   case $ls_ld_tmpdir in
357ea1d6981Smrg                     d????-?r-*) different_mode=700;;
358ea1d6981Smrg                     d????-?--*) different_mode=755;;
359ea1d6981Smrg                     *) false;;
360ea1d6981Smrg                   esac &&
361ea1d6981Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
362ea1d6981Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
363ea1d6981Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
364ea1d6981Smrg                   }
365ea1d6981Smrg                 }
366ea1d6981Smrg              then posix_mkdir=:
367ea1d6981Smrg              fi
368ea1d6981Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
369ea1d6981Smrg            else
370ea1d6981Smrg              # Remove any dirs left behind by ancient mkdir implementations.
371ea1d6981Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
372ea1d6981Smrg            fi
373ea1d6981Smrg            trap '' 0;;
374ea1d6981Smrg        esac;;
375ea1d6981Smrg    esac
37617ca54c3Smrg
377ea1d6981Smrg    if
378ea1d6981Smrg      $posix_mkdir && (
379ea1d6981Smrg        umask $mkdir_umask &&
380ea1d6981Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
381ea1d6981Smrg      )
382ea1d6981Smrg    then :
383ea1d6981Smrg    else
38417ca54c3Smrg
385ea1d6981Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
386ea1d6981Smrg      # or it failed possibly due to a race condition.  Create the
387ea1d6981Smrg      # directory the slow way, step by step, checking for races as we go.
38817ca54c3Smrg
389ea1d6981Smrg      case $dstdir in
390ea1d6981Smrg        /*) prefix='/';;
391ea1d6981Smrg        [-=\(\)!]*) prefix='./';;
392ea1d6981Smrg        *)  prefix='';;
393ea1d6981Smrg      esac
39417ca54c3Smrg
395ea1d6981Smrg      oIFS=$IFS
396ea1d6981Smrg      IFS=/
397ea1d6981Smrg      set -f
398ea1d6981Smrg      set fnord $dstdir
39917ca54c3Smrg      shift
400ea1d6981Smrg      set +f
401ea1d6981Smrg      IFS=$oIFS
402ea1d6981Smrg
403ea1d6981Smrg      prefixes=
404ea1d6981Smrg
405ea1d6981Smrg      for d
406ea1d6981Smrg      do
407ea1d6981Smrg        test X"$d" = X && continue
408ea1d6981Smrg
409ea1d6981Smrg        prefix=$prefix$d
410ea1d6981Smrg        if test -d "$prefix"; then
411ea1d6981Smrg          prefixes=
412ea1d6981Smrg        else
413ea1d6981Smrg          if $posix_mkdir; then
414ea1d6981Smrg            (umask=$mkdir_umask &&
415ea1d6981Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416ea1d6981Smrg            # Don't fail if two instances are running concurrently.
417ea1d6981Smrg            test -d "$prefix" || exit 1
418ea1d6981Smrg          else
419ea1d6981Smrg            case $prefix in
420ea1d6981Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421ea1d6981Smrg              *) qprefix=$prefix;;
422ea1d6981Smrg            esac
423ea1d6981Smrg            prefixes="$prefixes '$qprefix'"
424ea1d6981Smrg          fi
425ea1d6981Smrg        fi
426ea1d6981Smrg        prefix=$prefix/
427ea1d6981Smrg      done
428ea1d6981Smrg
429ea1d6981Smrg      if test -n "$prefixes"; then
430ea1d6981Smrg        # Don't fail if two instances are running concurrently.
431ea1d6981Smrg        (umask $mkdir_umask &&
432ea1d6981Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
433ea1d6981Smrg          test -d "$dstdir" || exit 1
434ea1d6981Smrg        obsolete_mkdir_used=true
43517ca54c3Smrg      fi
436ea1d6981Smrg    fi
43717ca54c3Smrg  fi
43817ca54c3Smrg
43917ca54c3Smrg  if test -n "$dir_arg"; then
440ea1d6981Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
441ea1d6981Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
442ea1d6981Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
443ea1d6981Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44417ca54c3Smrg  else
44517ca54c3Smrg
44617ca54c3Smrg    # Make a couple of temp file names in the proper directory.
447ea1d6981Smrg    dsttmp=${dstdirslash}_inst.$$_
448ea1d6981Smrg    rmtmp=${dstdirslash}_rm.$$_
44917ca54c3Smrg
45017ca54c3Smrg    # Trap to clean up those temp files at exit.
451ea1d6981Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45217ca54c3Smrg
453ea1d6981Smrg    # Copy the file name to the temp name.
454ea1d6981Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45517ca54c3Smrg
45617ca54c3Smrg    # and set any options; do chmod last to preserve setuid bits.
45717ca54c3Smrg    #
45817ca54c3Smrg    # If any of these fail, we abort the whole thing.  If we want to
45917ca54c3Smrg    # ignore errors from any of these, just make sure not to ignore
460ea1d6981Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46117ca54c3Smrg    #
462ea1d6981Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
463ea1d6981Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
464ea1d6981Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
465ea1d6981Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466ea1d6981Smrg
467ea1d6981Smrg    # If -C, don't bother to copy if it wouldn't change the file.
468ea1d6981Smrg    if $copy_on_change &&
469ea1d6981Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
470ea1d6981Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
471ea1d6981Smrg       set -f &&
472ea1d6981Smrg       set X $old && old=:$2:$4:$5:$6 &&
473ea1d6981Smrg       set X $new && new=:$2:$4:$5:$6 &&
474ea1d6981Smrg       set +f &&
475ea1d6981Smrg       test "$old" = "$new" &&
476ea1d6981Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
477ea1d6981Smrg    then
478ea1d6981Smrg      rm -f "$dsttmp"
479ea1d6981Smrg    else
480ea1d6981Smrg      # Rename the file to the real destination.
481ea1d6981Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
482ea1d6981Smrg
483ea1d6981Smrg      # The rename failed, perhaps because mv can't rename something else
484ea1d6981Smrg      # to itself, or perhaps because mv is so ancient that it does not
485ea1d6981Smrg      # support -f.
486ea1d6981Smrg      {
487ea1d6981Smrg        # Now remove or move aside any old file at destination location.
488ea1d6981Smrg        # We try this two ways since rm can't unlink itself on some
489ea1d6981Smrg        # systems and the destination file might be busy for other
490ea1d6981Smrg        # reasons.  In this case, the final cleanup might fail but the new
491ea1d6981Smrg        # file should still install successfully.
492ea1d6981Smrg        {
493ea1d6981Smrg          test ! -f "$dst" ||
494ea1d6981Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
495ea1d6981Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
496ea1d6981Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
497ea1d6981Smrg          } ||
498ea1d6981Smrg          { echo "$0: cannot unlink or rename $dst" >&2
499ea1d6981Smrg            (exit 1); exit 1
500ea1d6981Smrg          }
501ea1d6981Smrg        } &&
502ea1d6981Smrg
503ea1d6981Smrg        # Now rename the file to the real destination.
504ea1d6981Smrg        $doit $mvcmd "$dsttmp" "$dst"
505ea1d6981Smrg      }
506ea1d6981Smrg    fi || exit 1
507ea1d6981Smrg
508ea1d6981Smrg    trap '' 0
509ea1d6981Smrg  fi
51017ca54c3Smrgdone
51117ca54c3Smrg
51217ca54c3Smrg# Local variables:
513ea1d6981Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
51417ca54c3Smrg# time-stamp-start: "scriptversion="
51517ca54c3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
516ea1d6981Smrg# time-stamp-time-zone: "UTC0"
517ea1d6981Smrg# time-stamp-end: "; # UTC"
51817ca54c3Smrg# End:
519