15c10afb9Smrg#!/bin/sh
25c10afb9Smrg# install - install a program, script, or datafile
3b3078addSmrg
4cf2f63c2Smrgscriptversion=2013-12-25.23; # UTC
5b3078addSmrg
65c10afb9Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
75c10afb9Smrg# later released in X11R6 (xc/config/util/install.sh) with the
85c10afb9Smrg# following copyright and license.
95c10afb9Smrg#
105c10afb9Smrg# Copyright (C) 1994 X Consortium
115c10afb9Smrg#
125c10afb9Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
135c10afb9Smrg# of this software and associated documentation files (the "Software"), to
145c10afb9Smrg# deal in the Software without restriction, including without limitation the
155c10afb9Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
165c10afb9Smrg# sell copies of the Software, and to permit persons to whom the Software is
175c10afb9Smrg# furnished to do so, subject to the following conditions:
185c10afb9Smrg#
195c10afb9Smrg# The above copyright notice and this permission notice shall be included in
205c10afb9Smrg# all copies or substantial portions of the Software.
215c10afb9Smrg#
225c10afb9Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
235c10afb9Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
245c10afb9Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
255c10afb9Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
265c10afb9Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
275c10afb9Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
285c10afb9Smrg#
295c10afb9Smrg# Except as contained in this notice, the name of the X Consortium shall not
305c10afb9Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
315c10afb9Smrg# ings in this Software without prior written authorization from the X Consor-
325c10afb9Smrg# tium.
335c10afb9Smrg#
345c10afb9Smrg#
355c10afb9Smrg# FSF changes to this file are in the public domain.
365c10afb9Smrg#
375c10afb9Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38cf2f63c2Smrg# 'make' implicit rules from creating a file called install from it
395c10afb9Smrg# when there is no Makefile.
405c10afb9Smrg#
415c10afb9Smrg# This script is compatible with the BSD install script, but was written
42b3078addSmrg# from scratch.
435c10afb9Smrg
44cf2f63c2Smrgtab='	'
45b3078addSmrgnl='
46b3078addSmrg'
47cf2f63c2SmrgIFS=" $tab$nl"
485c10afb9Smrg
49cf2f63c2Smrg# Set DOITPROG to "echo" to test this script.
505c10afb9Smrg
51b3078addSmrgdoit=${DOITPROG-}
52cf2f63c2Smrgdoit_exec=${doit:-exec}
535c10afb9Smrg
54b3078addSmrg# Put in absolute file names if you don't have them in your path;
55b3078addSmrg# or use environment vars.
56b3078addSmrg
57b3078addSmrgchgrpprog=${CHGRPPROG-chgrp}
58b3078addSmrgchmodprog=${CHMODPROG-chmod}
59b3078addSmrgchownprog=${CHOWNPROG-chown}
60b3078addSmrgcmpprog=${CMPPROG-cmp}
61b3078addSmrgcpprog=${CPPROG-cp}
62b3078addSmrgmkdirprog=${MKDIRPROG-mkdir}
63b3078addSmrgmvprog=${MVPROG-mv}
64b3078addSmrgrmprog=${RMPROG-rm}
65b3078addSmrgstripprog=${STRIPPROG-strip}
66b3078addSmrg
67b3078addSmrgposix_mkdir=
68b3078addSmrg
69b3078addSmrg# Desired mode of installed file.
70b3078addSmrgmode=0755
71b3078addSmrg
72b3078addSmrgchgrpcmd=
73b3078addSmrgchmodcmd=$chmodprog
74b3078addSmrgchowncmd=
75b3078addSmrgmvcmd=$mvprog
76b3078addSmrgrmcmd="$rmprog -f"
77b3078addSmrgstripcmd=
785c10afb9Smrg
79b3078addSmrgsrc=
80b3078addSmrgdst=
81b3078addSmrgdir_arg=
82b3078addSmrgdst_arg=
835c10afb9Smrg
84b3078addSmrgcopy_on_change=false
85cf2f63c2Smrgis_target_a_directory=possibly
865c10afb9Smrg
87b3078addSmrgusage="\
88b3078addSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89b3078addSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90b3078addSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91b3078addSmrg   or: $0 [OPTION]... -d DIRECTORIES...
925c10afb9Smrg
93b3078addSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94b3078addSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95b3078addSmrgIn the 4th, create DIRECTORIES.
965c10afb9Smrg
97b3078addSmrgOptions:
98b3078addSmrg     --help     display this help and exit.
99b3078addSmrg     --version  display version info and exit.
1005c10afb9Smrg
101b3078addSmrg  -c            (ignored)
102b3078addSmrg  -C            install only if different (preserve the last data modification time)
103b3078addSmrg  -d            create directories instead of installing files.
104b3078addSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105b3078addSmrg  -m MODE       $chmodprog installed files to MODE.
106b3078addSmrg  -o USER       $chownprog installed files to USER.
107b3078addSmrg  -s            $stripprog installed files.
108b3078addSmrg  -t DIRECTORY  install into DIRECTORY.
109b3078addSmrg  -T            report an error if DSTFILE is a directory.
1105c10afb9Smrg
111b3078addSmrgEnvironment variables override the default commands:
112b3078addSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113b3078addSmrg  RMPROG STRIPPROG
114b3078addSmrg"
1155c10afb9Smrg
116b3078addSmrgwhile test $# -ne 0; do
117b3078addSmrg  case $1 in
118b3078addSmrg    -c) ;;
1195c10afb9Smrg
120b3078addSmrg    -C) copy_on_change=true;;
1215c10afb9Smrg
122b3078addSmrg    -d) dir_arg=true;;
1235c10afb9Smrg
124b3078addSmrg    -g) chgrpcmd="$chgrpprog $2"
125cf2f63c2Smrg        shift;;
1265c10afb9Smrg
127b3078addSmrg    --help) echo "$usage"; exit $?;;
1285c10afb9Smrg
129b3078addSmrg    -m) mode=$2
130cf2f63c2Smrg        case $mode in
131cf2f63c2Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132cf2f63c2Smrg            echo "$0: invalid mode: $mode" >&2
133cf2f63c2Smrg            exit 1;;
134cf2f63c2Smrg        esac
135cf2f63c2Smrg        shift;;
1365c10afb9Smrg
137b3078addSmrg    -o) chowncmd="$chownprog $2"
138cf2f63c2Smrg        shift;;
1395c10afb9Smrg
140b3078addSmrg    -s) stripcmd=$stripprog;;
1415c10afb9Smrg
142cf2f63c2Smrg    -t)
143cf2f63c2Smrg        is_target_a_directory=always
144cf2f63c2Smrg        dst_arg=$2
145cf2f63c2Smrg        # Protect names problematic for 'test' and other utilities.
146cf2f63c2Smrg        case $dst_arg in
147cf2f63c2Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148cf2f63c2Smrg        esac
149cf2f63c2Smrg        shift;;
1505c10afb9Smrg
151cf2f63c2Smrg    -T) is_target_a_directory=never;;
1525c10afb9Smrg
153b3078addSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1545c10afb9Smrg
155cf2f63c2Smrg    --) shift
156cf2f63c2Smrg        break;;
1575c10afb9Smrg
158cf2f63c2Smrg    -*) echo "$0: invalid option: $1" >&2
159cf2f63c2Smrg        exit 1;;
1605c10afb9Smrg
161b3078addSmrg    *)  break;;
162b3078addSmrg  esac
163b3078addSmrg  shift
164b3078addSmrgdone
1655c10afb9Smrg
166cf2f63c2Smrg# We allow the use of options -d and -T together, by making -d
167cf2f63c2Smrg# take the precedence; this is for compatibility with GNU install.
168cf2f63c2Smrg
169cf2f63c2Smrgif test -n "$dir_arg"; then
170cf2f63c2Smrg  if test -n "$dst_arg"; then
171cf2f63c2Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172cf2f63c2Smrg    exit 1
173cf2f63c2Smrg  fi
174cf2f63c2Smrgfi
175cf2f63c2Smrg
176b3078addSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177b3078addSmrg  # When -d is used, all remaining arguments are directories to create.
178b3078addSmrg  # When -t is used, the destination is already specified.
179b3078addSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180b3078addSmrg  for arg
181b3078addSmrg  do
182b3078addSmrg    if test -n "$dst_arg"; then
183b3078addSmrg      # $@ is not empty: it contains at least $arg.
184b3078addSmrg      set fnord "$@" "$dst_arg"
185b3078addSmrg      shift # fnord
186b3078addSmrg    fi
187b3078addSmrg    shift # arg
188b3078addSmrg    dst_arg=$arg
189cf2f63c2Smrg    # Protect names problematic for 'test' and other utilities.
190cf2f63c2Smrg    case $dst_arg in
191cf2f63c2Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192cf2f63c2Smrg    esac
193b3078addSmrg  done
194b3078addSmrgfi
1955c10afb9Smrg
196b3078addSmrgif test $# -eq 0; then
197b3078addSmrg  if test -z "$dir_arg"; then
198b3078addSmrg    echo "$0: no input file specified." >&2
199b3078addSmrg    exit 1
200b3078addSmrg  fi
201cf2f63c2Smrg  # It's OK to call 'install-sh -d' without argument.
202b3078addSmrg  # This can happen when creating conditional directories.
203b3078addSmrg  exit 0
204b3078addSmrgfi
2055c10afb9Smrg
206b3078addSmrgif test -z "$dir_arg"; then
207cf2f63c2Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208cf2f63c2Smrg    if test ! -d "$dst_arg"; then
209cf2f63c2Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210cf2f63c2Smrg      exit 1
211cf2f63c2Smrg    fi
212cf2f63c2Smrg  fi
213cf2f63c2Smrgfi
214cf2f63c2Smrg
215cf2f63c2Smrgif test -z "$dir_arg"; then
216cf2f63c2Smrg  do_exit='(exit $ret); exit $ret'
217cf2f63c2Smrg  trap "ret=129; $do_exit" 1
218cf2f63c2Smrg  trap "ret=130; $do_exit" 2
219cf2f63c2Smrg  trap "ret=141; $do_exit" 13
220cf2f63c2Smrg  trap "ret=143; $do_exit" 15
221b3078addSmrg
222b3078addSmrg  # Set umask so as not to create temps with too-generous modes.
223b3078addSmrg  # However, 'strip' requires both read and write access to temps.
224b3078addSmrg  case $mode in
225b3078addSmrg    # Optimize common cases.
226b3078addSmrg    *644) cp_umask=133;;
227b3078addSmrg    *755) cp_umask=22;;
228b3078addSmrg
229b3078addSmrg    *[0-7])
230b3078addSmrg      if test -z "$stripcmd"; then
231cf2f63c2Smrg        u_plus_rw=
232b3078addSmrg      else
233cf2f63c2Smrg        u_plus_rw='% 200'
234b3078addSmrg      fi
235b3078addSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236b3078addSmrg    *)
237b3078addSmrg      if test -z "$stripcmd"; then
238cf2f63c2Smrg        u_plus_rw=
239b3078addSmrg      else
240cf2f63c2Smrg        u_plus_rw=,u+rw
241b3078addSmrg      fi
242b3078addSmrg      cp_umask=$mode$u_plus_rw;;
243b3078addSmrg  esac
244b3078addSmrgfi
2455c10afb9Smrg
246b3078addSmrgfor src
247b3078addSmrgdo
248cf2f63c2Smrg  # Protect names problematic for 'test' and other utilities.
249b3078addSmrg  case $src in
250cf2f63c2Smrg    -* | [=\(\)!]) src=./$src;;
251b3078addSmrg  esac
252b3078addSmrg
253b3078addSmrg  if test -n "$dir_arg"; then
254b3078addSmrg    dst=$src
255b3078addSmrg    dstdir=$dst
256b3078addSmrg    test -d "$dstdir"
257b3078addSmrg    dstdir_status=$?
258b3078addSmrg  else
259b3078addSmrg
260b3078addSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261b3078addSmrg    # might cause directories to be created, which would be especially bad
262b3078addSmrg    # if $src (and thus $dsttmp) contains '*'.
263b3078addSmrg    if test ! -f "$src" && test ! -d "$src"; then
264b3078addSmrg      echo "$0: $src does not exist." >&2
265b3078addSmrg      exit 1
266b3078addSmrg    fi
267b3078addSmrg
268b3078addSmrg    if test -z "$dst_arg"; then
269b3078addSmrg      echo "$0: no destination specified." >&2
270b3078addSmrg      exit 1
271b3078addSmrg    fi
272b3078addSmrg    dst=$dst_arg
2735c10afb9Smrg
274b3078addSmrg    # If destination is a directory, append the input filename; won't work
275b3078addSmrg    # if double slashes aren't ignored.
276b3078addSmrg    if test -d "$dst"; then
277cf2f63c2Smrg      if test "$is_target_a_directory" = never; then
278cf2f63c2Smrg        echo "$0: $dst_arg: Is a directory" >&2
279cf2f63c2Smrg        exit 1
280b3078addSmrg      fi
281b3078addSmrg      dstdir=$dst
282b3078addSmrg      dst=$dstdir/`basename "$src"`
283b3078addSmrg      dstdir_status=0
284b3078addSmrg    else
285cf2f63c2Smrg      dstdir=`dirname "$dst"`
286b3078addSmrg      test -d "$dstdir"
287b3078addSmrg      dstdir_status=$?
288b3078addSmrg    fi
289b3078addSmrg  fi
290b3078addSmrg
291b3078addSmrg  obsolete_mkdir_used=false
292b3078addSmrg
293b3078addSmrg  if test $dstdir_status != 0; then
294b3078addSmrg    case $posix_mkdir in
295b3078addSmrg      '')
296cf2f63c2Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297cf2f63c2Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298cf2f63c2Smrg        umask=`umask`
299cf2f63c2Smrg        case $stripcmd.$umask in
300cf2f63c2Smrg          # Optimize common cases.
301cf2f63c2Smrg          *[2367][2367]) mkdir_umask=$umask;;
302cf2f63c2Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303cf2f63c2Smrg
304cf2f63c2Smrg          *[0-7])
305cf2f63c2Smrg            mkdir_umask=`expr $umask + 22 \
306cf2f63c2Smrg              - $umask % 100 % 40 + $umask % 20 \
307cf2f63c2Smrg              - $umask % 10 % 4 + $umask % 2
308cf2f63c2Smrg            `;;
309cf2f63c2Smrg          *) mkdir_umask=$umask,go-w;;
310cf2f63c2Smrg        esac
311cf2f63c2Smrg
312cf2f63c2Smrg        # With -d, create the new directory with the user-specified mode.
313cf2f63c2Smrg        # Otherwise, rely on $mkdir_umask.
314cf2f63c2Smrg        if test -n "$dir_arg"; then
315cf2f63c2Smrg          mkdir_mode=-m$mode
316cf2f63c2Smrg        else
317cf2f63c2Smrg          mkdir_mode=
318cf2f63c2Smrg        fi
319cf2f63c2Smrg
320cf2f63c2Smrg        posix_mkdir=false
321cf2f63c2Smrg        case $umask in
322cf2f63c2Smrg          *[123567][0-7][0-7])
323cf2f63c2Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324cf2f63c2Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325cf2f63c2Smrg            ;;
326cf2f63c2Smrg          *)
327cf2f63c2Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328cf2f63c2Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329cf2f63c2Smrg
330cf2f63c2Smrg            if (umask $mkdir_umask &&
331cf2f63c2Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332cf2f63c2Smrg            then
333cf2f63c2Smrg              if test -z "$dir_arg" || {
334cf2f63c2Smrg                   # Check for POSIX incompatibilities with -m.
335cf2f63c2Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336cf2f63c2Smrg                   # other-writable bit of parent directory when it shouldn't.
337cf2f63c2Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338cf2f63c2Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339cf2f63c2Smrg                   case $ls_ld_tmpdir in
340cf2f63c2Smrg                     d????-?r-*) different_mode=700;;
341cf2f63c2Smrg                     d????-?--*) different_mode=755;;
342cf2f63c2Smrg                     *) false;;
343cf2f63c2Smrg                   esac &&
344cf2f63c2Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345cf2f63c2Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346cf2f63c2Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347cf2f63c2Smrg                   }
348cf2f63c2Smrg                 }
349cf2f63c2Smrg              then posix_mkdir=:
350cf2f63c2Smrg              fi
351cf2f63c2Smrg              rmdir "$tmpdir/d" "$tmpdir"
352cf2f63c2Smrg            else
353cf2f63c2Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354cf2f63c2Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355cf2f63c2Smrg            fi
356cf2f63c2Smrg            trap '' 0;;
357cf2f63c2Smrg        esac;;
358b3078addSmrg    esac
3595c10afb9Smrg
360b3078addSmrg    if
361b3078addSmrg      $posix_mkdir && (
362cf2f63c2Smrg        umask $mkdir_umask &&
363cf2f63c2Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364b3078addSmrg      )
365b3078addSmrg    then :
366b3078addSmrg    else
367b3078addSmrg
368b3078addSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369b3078addSmrg      # or it failed possibly due to a race condition.  Create the
370b3078addSmrg      # directory the slow way, step by step, checking for races as we go.
371b3078addSmrg
372b3078addSmrg      case $dstdir in
373cf2f63c2Smrg        /*) prefix='/';;
374cf2f63c2Smrg        [-=\(\)!]*) prefix='./';;
375cf2f63c2Smrg        *)  prefix='';;
376b3078addSmrg      esac
377b3078addSmrg
378b3078addSmrg      oIFS=$IFS
379b3078addSmrg      IFS=/
380cf2f63c2Smrg      set -f
381b3078addSmrg      set fnord $dstdir
382b3078addSmrg      shift
383cf2f63c2Smrg      set +f
384b3078addSmrg      IFS=$oIFS
385b3078addSmrg
386b3078addSmrg      prefixes=
387b3078addSmrg
388b3078addSmrg      for d
389b3078addSmrg      do
390cf2f63c2Smrg        test X"$d" = X && continue
391cf2f63c2Smrg
392cf2f63c2Smrg        prefix=$prefix$d
393cf2f63c2Smrg        if test -d "$prefix"; then
394cf2f63c2Smrg          prefixes=
395cf2f63c2Smrg        else
396cf2f63c2Smrg          if $posix_mkdir; then
397cf2f63c2Smrg            (umask=$mkdir_umask &&
398cf2f63c2Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399cf2f63c2Smrg            # Don't fail if two instances are running concurrently.
400cf2f63c2Smrg            test -d "$prefix" || exit 1
401cf2f63c2Smrg          else
402cf2f63c2Smrg            case $prefix in
403cf2f63c2Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404cf2f63c2Smrg              *) qprefix=$prefix;;
405cf2f63c2Smrg            esac
406cf2f63c2Smrg            prefixes="$prefixes '$qprefix'"
407cf2f63c2Smrg          fi
408cf2f63c2Smrg        fi
409cf2f63c2Smrg        prefix=$prefix/
410b3078addSmrg      done
411b3078addSmrg
412b3078addSmrg      if test -n "$prefixes"; then
413cf2f63c2Smrg        # Don't fail if two instances are running concurrently.
414cf2f63c2Smrg        (umask $mkdir_umask &&
415cf2f63c2Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416cf2f63c2Smrg          test -d "$dstdir" || exit 1
417cf2f63c2Smrg        obsolete_mkdir_used=true
418b3078addSmrg      fi
419b3078addSmrg    fi
420b3078addSmrg  fi
421b3078addSmrg
422b3078addSmrg  if test -n "$dir_arg"; then
423b3078addSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424b3078addSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425b3078addSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426b3078addSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427b3078addSmrg  else
428b3078addSmrg
429b3078addSmrg    # Make a couple of temp file names in the proper directory.
430b3078addSmrg    dsttmp=$dstdir/_inst.$$_
431b3078addSmrg    rmtmp=$dstdir/_rm.$$_
432b3078addSmrg
433b3078addSmrg    # Trap to clean up those temp files at exit.
434b3078addSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435b3078addSmrg
436b3078addSmrg    # Copy the file name to the temp name.
437b3078addSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438b3078addSmrg
439b3078addSmrg    # and set any options; do chmod last to preserve setuid bits.
440b3078addSmrg    #
441b3078addSmrg    # If any of these fail, we abort the whole thing.  If we want to
442b3078addSmrg    # ignore errors from any of these, just make sure not to ignore
443b3078addSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444b3078addSmrg    #
445b3078addSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446b3078addSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447b3078addSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448b3078addSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449b3078addSmrg
450b3078addSmrg    # If -C, don't bother to copy if it wouldn't change the file.
451b3078addSmrg    if $copy_on_change &&
452cf2f63c2Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453cf2f63c2Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454cf2f63c2Smrg       set -f &&
455b3078addSmrg       set X $old && old=:$2:$4:$5:$6 &&
456b3078addSmrg       set X $new && new=:$2:$4:$5:$6 &&
457cf2f63c2Smrg       set +f &&
458b3078addSmrg       test "$old" = "$new" &&
459b3078addSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460b3078addSmrg    then
461b3078addSmrg      rm -f "$dsttmp"
462b3078addSmrg    else
463b3078addSmrg      # Rename the file to the real destination.
464b3078addSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465b3078addSmrg
466b3078addSmrg      # The rename failed, perhaps because mv can't rename something else
467b3078addSmrg      # to itself, or perhaps because mv is so ancient that it does not
468b3078addSmrg      # support -f.
469b3078addSmrg      {
470cf2f63c2Smrg        # Now remove or move aside any old file at destination location.
471cf2f63c2Smrg        # We try this two ways since rm can't unlink itself on some
472cf2f63c2Smrg        # systems and the destination file might be busy for other
473cf2f63c2Smrg        # reasons.  In this case, the final cleanup might fail but the new
474cf2f63c2Smrg        # file should still install successfully.
475cf2f63c2Smrg        {
476cf2f63c2Smrg          test ! -f "$dst" ||
477cf2f63c2Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478cf2f63c2Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479cf2f63c2Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480cf2f63c2Smrg          } ||
481cf2f63c2Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482cf2f63c2Smrg            (exit 1); exit 1
483cf2f63c2Smrg          }
484cf2f63c2Smrg        } &&
485cf2f63c2Smrg
486cf2f63c2Smrg        # Now rename the file to the real destination.
487cf2f63c2Smrg        $doit $mvcmd "$dsttmp" "$dst"
488b3078addSmrg      }
489b3078addSmrg    fi || exit 1
490b3078addSmrg
491b3078addSmrg    trap '' 0
492b3078addSmrg  fi
493b3078addSmrgdone
4945c10afb9Smrg
495b3078addSmrg# Local variables:
496b3078addSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497b3078addSmrg# time-stamp-start: "scriptversion="
498b3078addSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499b3078addSmrg# time-stamp-time-zone: "UTC"
500b3078addSmrg# time-stamp-end: "; # UTC"
501b3078addSmrg# End:
502