install-sh revision cc4920e5
1caade7ccSmrg#!/bin/sh
2caade7ccSmrg# install - install a program, script, or datafile
3caade7ccSmrg
4cc4920e5Smrgscriptversion=2013-12-25.23; # UTC
5caade7ccSmrg
6caade7ccSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7caade7ccSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8caade7ccSmrg# following copyright and license.
9caade7ccSmrg#
10caade7ccSmrg# Copyright (C) 1994 X Consortium
11caade7ccSmrg#
12caade7ccSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13caade7ccSmrg# of this software and associated documentation files (the "Software"), to
14caade7ccSmrg# deal in the Software without restriction, including without limitation the
15caade7ccSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16caade7ccSmrg# sell copies of the Software, and to permit persons to whom the Software is
17caade7ccSmrg# furnished to do so, subject to the following conditions:
18caade7ccSmrg#
19caade7ccSmrg# The above copyright notice and this permission notice shall be included in
20caade7ccSmrg# all copies or substantial portions of the Software.
21caade7ccSmrg#
22caade7ccSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23caade7ccSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24caade7ccSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25caade7ccSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26caade7ccSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27caade7ccSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28caade7ccSmrg#
29caade7ccSmrg# Except as contained in this notice, the name of the X Consortium shall not
30caade7ccSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31caade7ccSmrg# ings in this Software without prior written authorization from the X Consor-
32caade7ccSmrg# tium.
33caade7ccSmrg#
34caade7ccSmrg#
35caade7ccSmrg# FSF changes to this file are in the public domain.
36caade7ccSmrg#
37caade7ccSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38af9a7ee5Smrg# 'make' implicit rules from creating a file called install from it
39caade7ccSmrg# when there is no Makefile.
40caade7ccSmrg#
41caade7ccSmrg# This script is compatible with the BSD install script, but was written
42caade7ccSmrg# from scratch.
43caade7ccSmrg
44cc4920e5Smrgtab='	'
45caade7ccSmrgnl='
46caade7ccSmrg'
47cc4920e5SmrgIFS=" $tab$nl"
48caade7ccSmrg
49cc4920e5Smrg# Set DOITPROG to "echo" to test this script.
50caade7ccSmrg
51485f0483Smrgdoit=${DOITPROG-}
52cc4920e5Smrgdoit_exec=${doit:-exec}
53caade7ccSmrg
54caade7ccSmrg# Put in absolute file names if you don't have them in your path;
55caade7ccSmrg# or use environment vars.
56caade7ccSmrg
57485f0483Smrgchgrpprog=${CHGRPPROG-chgrp}
58485f0483Smrgchmodprog=${CHMODPROG-chmod}
59485f0483Smrgchownprog=${CHOWNPROG-chown}
60485f0483Smrgcmpprog=${CMPPROG-cmp}
61485f0483Smrgcpprog=${CPPROG-cp}
62485f0483Smrgmkdirprog=${MKDIRPROG-mkdir}
63485f0483Smrgmvprog=${MVPROG-mv}
64485f0483Smrgrmprog=${RMPROG-rm}
65485f0483Smrgstripprog=${STRIPPROG-strip}
66485f0483Smrg
67caade7ccSmrgposix_mkdir=
68caade7ccSmrg
69caade7ccSmrg# Desired mode of installed file.
70caade7ccSmrgmode=0755
71caade7ccSmrg
72485f0483Smrgchgrpcmd=
73caade7ccSmrgchmodcmd=$chmodprog
74caade7ccSmrgchowncmd=
75485f0483Smrgmvcmd=$mvprog
76caade7ccSmrgrmcmd="$rmprog -f"
77485f0483Smrgstripcmd=
78485f0483Smrg
79caade7ccSmrgsrc=
80caade7ccSmrgdst=
81caade7ccSmrgdir_arg=
82485f0483Smrgdst_arg=
83485f0483Smrg
84485f0483Smrgcopy_on_change=false
85cc4920e5Smrgis_target_a_directory=possibly
86caade7ccSmrg
87485f0483Smrgusage="\
88485f0483SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89caade7ccSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90caade7ccSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91caade7ccSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92caade7ccSmrg
93caade7ccSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94caade7ccSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95caade7ccSmrgIn the 4th, create DIRECTORIES.
96caade7ccSmrg
97caade7ccSmrgOptions:
98485f0483Smrg     --help     display this help and exit.
99485f0483Smrg     --version  display version info and exit.
100485f0483Smrg
101485f0483Smrg  -c            (ignored)
102485f0483Smrg  -C            install only if different (preserve the last data modification time)
103485f0483Smrg  -d            create directories instead of installing files.
104485f0483Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105485f0483Smrg  -m MODE       $chmodprog installed files to MODE.
106485f0483Smrg  -o USER       $chownprog installed files to USER.
107485f0483Smrg  -s            $stripprog installed files.
108485f0483Smrg  -t DIRECTORY  install into DIRECTORY.
109485f0483Smrg  -T            report an error if DSTFILE is a directory.
110caade7ccSmrg
111caade7ccSmrgEnvironment variables override the default commands:
112485f0483Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113485f0483Smrg  RMPROG STRIPPROG
114caade7ccSmrg"
115caade7ccSmrg
116caade7ccSmrgwhile test $# -ne 0; do
117caade7ccSmrg  case $1 in
118485f0483Smrg    -c) ;;
119485f0483Smrg
120485f0483Smrg    -C) copy_on_change=true;;
121caade7ccSmrg
122485f0483Smrg    -d) dir_arg=true;;
123caade7ccSmrg
124caade7ccSmrg    -g) chgrpcmd="$chgrpprog $2"
125cc4920e5Smrg        shift;;
126caade7ccSmrg
127caade7ccSmrg    --help) echo "$usage"; exit $?;;
128caade7ccSmrg
129caade7ccSmrg    -m) mode=$2
130cc4920e5Smrg        case $mode in
131cc4920e5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132cc4920e5Smrg            echo "$0: invalid mode: $mode" >&2
133cc4920e5Smrg            exit 1;;
134cc4920e5Smrg        esac
135cc4920e5Smrg        shift;;
136caade7ccSmrg
137caade7ccSmrg    -o) chowncmd="$chownprog $2"
138cc4920e5Smrg        shift;;
139caade7ccSmrg
140485f0483Smrg    -s) stripcmd=$stripprog;;
141caade7ccSmrg
142cc4920e5Smrg    -t)
143cc4920e5Smrg        is_target_a_directory=always
144cc4920e5Smrg        dst_arg=$2
145cc4920e5Smrg        # Protect names problematic for 'test' and other utilities.
146cc4920e5Smrg        case $dst_arg in
147cc4920e5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148cc4920e5Smrg        esac
149cc4920e5Smrg        shift;;
150caade7ccSmrg
151cc4920e5Smrg    -T) is_target_a_directory=never;;
152caade7ccSmrg
153caade7ccSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154caade7ccSmrg
155cc4920e5Smrg    --) shift
156cc4920e5Smrg        break;;
157caade7ccSmrg
158cc4920e5Smrg    -*) echo "$0: invalid option: $1" >&2
159cc4920e5Smrg        exit 1;;
160caade7ccSmrg
161caade7ccSmrg    *)  break;;
162caade7ccSmrg  esac
163485f0483Smrg  shift
164caade7ccSmrgdone
165caade7ccSmrg
166cc4920e5Smrg# We allow the use of options -d and -T together, by making -d
167cc4920e5Smrg# take the precedence; this is for compatibility with GNU install.
168cc4920e5Smrg
169cc4920e5Smrgif test -n "$dir_arg"; then
170cc4920e5Smrg  if test -n "$dst_arg"; then
171cc4920e5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172cc4920e5Smrg    exit 1
173cc4920e5Smrg  fi
174cc4920e5Smrgfi
175cc4920e5Smrg
176485f0483Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177caade7ccSmrg  # When -d is used, all remaining arguments are directories to create.
178caade7ccSmrg  # When -t is used, the destination is already specified.
179caade7ccSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180caade7ccSmrg  for arg
181caade7ccSmrg  do
182485f0483Smrg    if test -n "$dst_arg"; then
183caade7ccSmrg      # $@ is not empty: it contains at least $arg.
184485f0483Smrg      set fnord "$@" "$dst_arg"
185caade7ccSmrg      shift # fnord
186caade7ccSmrg    fi
187caade7ccSmrg    shift # arg
188485f0483Smrg    dst_arg=$arg
189af9a7ee5Smrg    # Protect names problematic for 'test' and other utilities.
190af9a7ee5Smrg    case $dst_arg in
191af9a7ee5Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192af9a7ee5Smrg    esac
193caade7ccSmrg  done
194caade7ccSmrgfi
195caade7ccSmrg
196caade7ccSmrgif test $# -eq 0; then
197caade7ccSmrg  if test -z "$dir_arg"; then
198caade7ccSmrg    echo "$0: no input file specified." >&2
199caade7ccSmrg    exit 1
200caade7ccSmrg  fi
201af9a7ee5Smrg  # It's OK to call 'install-sh -d' without argument.
202caade7ccSmrg  # This can happen when creating conditional directories.
203caade7ccSmrg  exit 0
204caade7ccSmrgfi
205caade7ccSmrg
206cc4920e5Smrgif test -z "$dir_arg"; then
207cc4920e5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208cc4920e5Smrg    if test ! -d "$dst_arg"; then
209cc4920e5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210cc4920e5Smrg      exit 1
211cc4920e5Smrg    fi
212cc4920e5Smrg  fi
213cc4920e5Smrgfi
214cc4920e5Smrg
215caade7ccSmrgif test -z "$dir_arg"; then
216af9a7ee5Smrg  do_exit='(exit $ret); exit $ret'
217af9a7ee5Smrg  trap "ret=129; $do_exit" 1
218af9a7ee5Smrg  trap "ret=130; $do_exit" 2
219af9a7ee5Smrg  trap "ret=141; $do_exit" 13
220af9a7ee5Smrg  trap "ret=143; $do_exit" 15
221caade7ccSmrg
222caade7ccSmrg  # Set umask so as not to create temps with too-generous modes.
223caade7ccSmrg  # However, 'strip' requires both read and write access to temps.
224caade7ccSmrg  case $mode in
225caade7ccSmrg    # Optimize common cases.
226caade7ccSmrg    *644) cp_umask=133;;
227caade7ccSmrg    *755) cp_umask=22;;
228caade7ccSmrg
229caade7ccSmrg    *[0-7])
230caade7ccSmrg      if test -z "$stripcmd"; then
231cc4920e5Smrg        u_plus_rw=
232caade7ccSmrg      else
233cc4920e5Smrg        u_plus_rw='% 200'
234caade7ccSmrg      fi
235caade7ccSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236caade7ccSmrg    *)
237caade7ccSmrg      if test -z "$stripcmd"; then
238cc4920e5Smrg        u_plus_rw=
239caade7ccSmrg      else
240cc4920e5Smrg        u_plus_rw=,u+rw
241caade7ccSmrg      fi
242caade7ccSmrg      cp_umask=$mode$u_plus_rw;;
243caade7ccSmrg  esac
244caade7ccSmrgfi
245caade7ccSmrg
246caade7ccSmrgfor src
247caade7ccSmrgdo
248af9a7ee5Smrg  # Protect names problematic for 'test' and other utilities.
249caade7ccSmrg  case $src in
250af9a7ee5Smrg    -* | [=\(\)!]) src=./$src;;
251caade7ccSmrg  esac
252caade7ccSmrg
253caade7ccSmrg  if test -n "$dir_arg"; then
254caade7ccSmrg    dst=$src
255caade7ccSmrg    dstdir=$dst
256caade7ccSmrg    test -d "$dstdir"
257caade7ccSmrg    dstdir_status=$?
258caade7ccSmrg  else
259caade7ccSmrg
260caade7ccSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261caade7ccSmrg    # might cause directories to be created, which would be especially bad
262caade7ccSmrg    # if $src (and thus $dsttmp) contains '*'.
263caade7ccSmrg    if test ! -f "$src" && test ! -d "$src"; then
264caade7ccSmrg      echo "$0: $src does not exist." >&2
265caade7ccSmrg      exit 1
266caade7ccSmrg    fi
267caade7ccSmrg
268485f0483Smrg    if test -z "$dst_arg"; then
269caade7ccSmrg      echo "$0: no destination specified." >&2
270caade7ccSmrg      exit 1
271caade7ccSmrg    fi
272485f0483Smrg    dst=$dst_arg
273caade7ccSmrg
274caade7ccSmrg    # If destination is a directory, append the input filename; won't work
275caade7ccSmrg    # if double slashes aren't ignored.
276caade7ccSmrg    if test -d "$dst"; then
277cc4920e5Smrg      if test "$is_target_a_directory" = never; then
278cc4920e5Smrg        echo "$0: $dst_arg: Is a directory" >&2
279cc4920e5Smrg        exit 1
280caade7ccSmrg      fi
281caade7ccSmrg      dstdir=$dst
282caade7ccSmrg      dst=$dstdir/`basename "$src"`
283caade7ccSmrg      dstdir_status=0
284caade7ccSmrg    else
285cc4920e5Smrg      dstdir=`dirname "$dst"`
286caade7ccSmrg      test -d "$dstdir"
287caade7ccSmrg      dstdir_status=$?
288caade7ccSmrg    fi
289caade7ccSmrg  fi
290caade7ccSmrg
291caade7ccSmrg  obsolete_mkdir_used=false
292caade7ccSmrg
293caade7ccSmrg  if test $dstdir_status != 0; then
294caade7ccSmrg    case $posix_mkdir in
295caade7ccSmrg      '')
296cc4920e5Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297cc4920e5Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298cc4920e5Smrg        umask=`umask`
299cc4920e5Smrg        case $stripcmd.$umask in
300cc4920e5Smrg          # Optimize common cases.
301cc4920e5Smrg          *[2367][2367]) mkdir_umask=$umask;;
302cc4920e5Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303cc4920e5Smrg
304cc4920e5Smrg          *[0-7])
305cc4920e5Smrg            mkdir_umask=`expr $umask + 22 \
306cc4920e5Smrg              - $umask % 100 % 40 + $umask % 20 \
307cc4920e5Smrg              - $umask % 10 % 4 + $umask % 2
308cc4920e5Smrg            `;;
309cc4920e5Smrg          *) mkdir_umask=$umask,go-w;;
310cc4920e5Smrg        esac
311cc4920e5Smrg
312cc4920e5Smrg        # With -d, create the new directory with the user-specified mode.
313cc4920e5Smrg        # Otherwise, rely on $mkdir_umask.
314cc4920e5Smrg        if test -n "$dir_arg"; then
315cc4920e5Smrg          mkdir_mode=-m$mode
316cc4920e5Smrg        else
317cc4920e5Smrg          mkdir_mode=
318cc4920e5Smrg        fi
319cc4920e5Smrg
320cc4920e5Smrg        posix_mkdir=false
321cc4920e5Smrg        case $umask in
322cc4920e5Smrg          *[123567][0-7][0-7])
323cc4920e5Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324cc4920e5Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325cc4920e5Smrg            ;;
326cc4920e5Smrg          *)
327cc4920e5Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328cc4920e5Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329cc4920e5Smrg
330cc4920e5Smrg            if (umask $mkdir_umask &&
331cc4920e5Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332cc4920e5Smrg            then
333cc4920e5Smrg              if test -z "$dir_arg" || {
334cc4920e5Smrg                   # Check for POSIX incompatibilities with -m.
335cc4920e5Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336cc4920e5Smrg                   # other-writable bit of parent directory when it shouldn't.
337cc4920e5Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338cc4920e5Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339cc4920e5Smrg                   case $ls_ld_tmpdir in
340cc4920e5Smrg                     d????-?r-*) different_mode=700;;
341cc4920e5Smrg                     d????-?--*) different_mode=755;;
342cc4920e5Smrg                     *) false;;
343cc4920e5Smrg                   esac &&
344cc4920e5Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345cc4920e5Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346cc4920e5Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347cc4920e5Smrg                   }
348cc4920e5Smrg                 }
349cc4920e5Smrg              then posix_mkdir=:
350cc4920e5Smrg              fi
351cc4920e5Smrg              rmdir "$tmpdir/d" "$tmpdir"
352cc4920e5Smrg            else
353cc4920e5Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354cc4920e5Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355cc4920e5Smrg            fi
356cc4920e5Smrg            trap '' 0;;
357cc4920e5Smrg        esac;;
358caade7ccSmrg    esac
359caade7ccSmrg
360caade7ccSmrg    if
361caade7ccSmrg      $posix_mkdir && (
362cc4920e5Smrg        umask $mkdir_umask &&
363cc4920e5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364caade7ccSmrg      )
365caade7ccSmrg    then :
366caade7ccSmrg    else
367caade7ccSmrg
368caade7ccSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369caade7ccSmrg      # or it failed possibly due to a race condition.  Create the
370caade7ccSmrg      # directory the slow way, step by step, checking for races as we go.
371caade7ccSmrg
372caade7ccSmrg      case $dstdir in
373cc4920e5Smrg        /*) prefix='/';;
374cc4920e5Smrg        [-=\(\)!]*) prefix='./';;
375cc4920e5Smrg        *)  prefix='';;
376caade7ccSmrg      esac
377caade7ccSmrg
378caade7ccSmrg      oIFS=$IFS
379caade7ccSmrg      IFS=/
380cc4920e5Smrg      set -f
381caade7ccSmrg      set fnord $dstdir
382caade7ccSmrg      shift
383cc4920e5Smrg      set +f
384caade7ccSmrg      IFS=$oIFS
385caade7ccSmrg
386caade7ccSmrg      prefixes=
387caade7ccSmrg
388caade7ccSmrg      for d
389caade7ccSmrg      do
390cc4920e5Smrg        test X"$d" = X && continue
391cc4920e5Smrg
392cc4920e5Smrg        prefix=$prefix$d
393cc4920e5Smrg        if test -d "$prefix"; then
394cc4920e5Smrg          prefixes=
395cc4920e5Smrg        else
396cc4920e5Smrg          if $posix_mkdir; then
397cc4920e5Smrg            (umask=$mkdir_umask &&
398cc4920e5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399cc4920e5Smrg            # Don't fail if two instances are running concurrently.
400cc4920e5Smrg            test -d "$prefix" || exit 1
401cc4920e5Smrg          else
402cc4920e5Smrg            case $prefix in
403cc4920e5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404cc4920e5Smrg              *) qprefix=$prefix;;
405cc4920e5Smrg            esac
406cc4920e5Smrg            prefixes="$prefixes '$qprefix'"
407cc4920e5Smrg          fi
408cc4920e5Smrg        fi
409cc4920e5Smrg        prefix=$prefix/
410caade7ccSmrg      done
411caade7ccSmrg
412caade7ccSmrg      if test -n "$prefixes"; then
413cc4920e5Smrg        # Don't fail if two instances are running concurrently.
414cc4920e5Smrg        (umask $mkdir_umask &&
415cc4920e5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416cc4920e5Smrg          test -d "$dstdir" || exit 1
417cc4920e5Smrg        obsolete_mkdir_used=true
418caade7ccSmrg      fi
419caade7ccSmrg    fi
420caade7ccSmrg  fi
421caade7ccSmrg
422caade7ccSmrg  if test -n "$dir_arg"; then
423caade7ccSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424caade7ccSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425caade7ccSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426caade7ccSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427caade7ccSmrg  else
428caade7ccSmrg
429caade7ccSmrg    # Make a couple of temp file names in the proper directory.
430caade7ccSmrg    dsttmp=$dstdir/_inst.$$_
431caade7ccSmrg    rmtmp=$dstdir/_rm.$$_
432caade7ccSmrg
433caade7ccSmrg    # Trap to clean up those temp files at exit.
434caade7ccSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435caade7ccSmrg
436caade7ccSmrg    # Copy the file name to the temp name.
437caade7ccSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438caade7ccSmrg
439caade7ccSmrg    # and set any options; do chmod last to preserve setuid bits.
440caade7ccSmrg    #
441caade7ccSmrg    # If any of these fail, we abort the whole thing.  If we want to
442caade7ccSmrg    # ignore errors from any of these, just make sure not to ignore
443caade7ccSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444caade7ccSmrg    #
445485f0483Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446485f0483Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447485f0483Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448485f0483Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449485f0483Smrg
450485f0483Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451485f0483Smrg    if $copy_on_change &&
452cc4920e5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453cc4920e5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454cc4920e5Smrg       set -f &&
455485f0483Smrg       set X $old && old=:$2:$4:$5:$6 &&
456485f0483Smrg       set X $new && new=:$2:$4:$5:$6 &&
457cc4920e5Smrg       set +f &&
458485f0483Smrg       test "$old" = "$new" &&
459485f0483Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460485f0483Smrg    then
461485f0483Smrg      rm -f "$dsttmp"
462485f0483Smrg    else
463485f0483Smrg      # Rename the file to the real destination.
464485f0483Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465485f0483Smrg
466485f0483Smrg      # The rename failed, perhaps because mv can't rename something else
467485f0483Smrg      # to itself, or perhaps because mv is so ancient that it does not
468485f0483Smrg      # support -f.
469485f0483Smrg      {
470cc4920e5Smrg        # Now remove or move aside any old file at destination location.
471cc4920e5Smrg        # We try this two ways since rm can't unlink itself on some
472cc4920e5Smrg        # systems and the destination file might be busy for other
473cc4920e5Smrg        # reasons.  In this case, the final cleanup might fail but the new
474cc4920e5Smrg        # file should still install successfully.
475cc4920e5Smrg        {
476cc4920e5Smrg          test ! -f "$dst" ||
477cc4920e5Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478cc4920e5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479cc4920e5Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480cc4920e5Smrg          } ||
481cc4920e5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482cc4920e5Smrg            (exit 1); exit 1
483cc4920e5Smrg          }
484cc4920e5Smrg        } &&
485cc4920e5Smrg
486cc4920e5Smrg        # Now rename the file to the real destination.
487cc4920e5Smrg        $doit $mvcmd "$dsttmp" "$dst"
488485f0483Smrg      }
489485f0483Smrg    fi || exit 1
490caade7ccSmrg
491caade7ccSmrg    trap '' 0
492caade7ccSmrg  fi
493caade7ccSmrgdone
494caade7ccSmrg
495caade7ccSmrg# Local variables:
496caade7ccSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497caade7ccSmrg# time-stamp-start: "scriptversion="
498caade7ccSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499485f0483Smrg# time-stamp-time-zone: "UTC"
500485f0483Smrg# time-stamp-end: "; # UTC"
501caade7ccSmrg# End:
502