install-sh revision fe12f63c
11016ad83Smrg#!/bin/sh
21016ad83Smrg# install - install a program, script, or datafile
31016ad83Smrg
4fe12f63cSmrgscriptversion=2018-03-11.20; # UTC
51016ad83Smrg
61016ad83Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71016ad83Smrg# later released in X11R6 (xc/config/util/install.sh) with the
81016ad83Smrg# following copyright and license.
91016ad83Smrg#
101016ad83Smrg# Copyright (C) 1994 X Consortium
111016ad83Smrg#
121016ad83Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131016ad83Smrg# of this software and associated documentation files (the "Software"), to
141016ad83Smrg# deal in the Software without restriction, including without limitation the
151016ad83Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161016ad83Smrg# sell copies of the Software, and to permit persons to whom the Software is
171016ad83Smrg# furnished to do so, subject to the following conditions:
181016ad83Smrg#
191016ad83Smrg# The above copyright notice and this permission notice shall be included in
201016ad83Smrg# all copies or substantial portions of the Software.
211016ad83Smrg#
221016ad83Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231016ad83Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241016ad83Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251016ad83Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261016ad83Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271016ad83Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281016ad83Smrg#
291016ad83Smrg# Except as contained in this notice, the name of the X Consortium shall not
301016ad83Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
311016ad83Smrg# ings in this Software without prior written authorization from the X Consor-
321016ad83Smrg# tium.
331016ad83Smrg#
341016ad83Smrg#
351016ad83Smrg# FSF changes to this file are in the public domain.
361016ad83Smrg#
371016ad83Smrg# Calling this script install-sh is preferred over install.sh, to prevent
381016ad83Smrg# 'make' implicit rules from creating a file called install from it
391016ad83Smrg# when there is no Makefile.
401016ad83Smrg#
411016ad83Smrg# This script is compatible with the BSD install script, but was written
421016ad83Smrg# from scratch.
431016ad83Smrg
44709d36bbSmrgtab='	'
451016ad83Smrgnl='
461016ad83Smrg'
47709d36bbSmrgIFS=" $tab$nl"
481016ad83Smrg
49709d36bbSmrg# Set DOITPROG to "echo" to test this script.
501016ad83Smrg
511016ad83Smrgdoit=${DOITPROG-}
52709d36bbSmrgdoit_exec=${doit:-exec}
531016ad83Smrg
541016ad83Smrg# Put in absolute file names if you don't have them in your path;
551016ad83Smrg# or use environment vars.
561016ad83Smrg
571016ad83Smrgchgrpprog=${CHGRPPROG-chgrp}
581016ad83Smrgchmodprog=${CHMODPROG-chmod}
591016ad83Smrgchownprog=${CHOWNPROG-chown}
601016ad83Smrgcmpprog=${CMPPROG-cmp}
611016ad83Smrgcpprog=${CPPROG-cp}
621016ad83Smrgmkdirprog=${MKDIRPROG-mkdir}
631016ad83Smrgmvprog=${MVPROG-mv}
641016ad83Smrgrmprog=${RMPROG-rm}
651016ad83Smrgstripprog=${STRIPPROG-strip}
661016ad83Smrg
671016ad83Smrgposix_mkdir=
681016ad83Smrg
691016ad83Smrg# Desired mode of installed file.
701016ad83Smrgmode=0755
711016ad83Smrg
721016ad83Smrgchgrpcmd=
731016ad83Smrgchmodcmd=$chmodprog
741016ad83Smrgchowncmd=
751016ad83Smrgmvcmd=$mvprog
761016ad83Smrgrmcmd="$rmprog -f"
771016ad83Smrgstripcmd=
781016ad83Smrg
791016ad83Smrgsrc=
801016ad83Smrgdst=
811016ad83Smrgdir_arg=
821016ad83Smrgdst_arg=
831016ad83Smrg
841016ad83Smrgcopy_on_change=false
85709d36bbSmrgis_target_a_directory=possibly
861016ad83Smrg
871016ad83Smrgusage="\
881016ad83SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
891016ad83Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
901016ad83Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
911016ad83Smrg   or: $0 [OPTION]... -d DIRECTORIES...
921016ad83Smrg
931016ad83SmrgIn the 1st form, copy SRCFILE to DSTFILE.
941016ad83SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
951016ad83SmrgIn the 4th, create DIRECTORIES.
961016ad83Smrg
971016ad83SmrgOptions:
981016ad83Smrg     --help     display this help and exit.
991016ad83Smrg     --version  display version info and exit.
1001016ad83Smrg
1011016ad83Smrg  -c            (ignored)
1021016ad83Smrg  -C            install only if different (preserve the last data modification time)
1031016ad83Smrg  -d            create directories instead of installing files.
1041016ad83Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1051016ad83Smrg  -m MODE       $chmodprog installed files to MODE.
1061016ad83Smrg  -o USER       $chownprog installed files to USER.
1071016ad83Smrg  -s            $stripprog installed files.
1081016ad83Smrg  -t DIRECTORY  install into DIRECTORY.
1091016ad83Smrg  -T            report an error if DSTFILE is a directory.
1101016ad83Smrg
1111016ad83SmrgEnvironment variables override the default commands:
1121016ad83Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1131016ad83Smrg  RMPROG STRIPPROG
1141016ad83Smrg"
1151016ad83Smrg
1161016ad83Smrgwhile test $# -ne 0; do
1171016ad83Smrg  case $1 in
1181016ad83Smrg    -c) ;;
1191016ad83Smrg
1201016ad83Smrg    -C) copy_on_change=true;;
1211016ad83Smrg
1221016ad83Smrg    -d) dir_arg=true;;
1231016ad83Smrg
1241016ad83Smrg    -g) chgrpcmd="$chgrpprog $2"
125709d36bbSmrg        shift;;
1261016ad83Smrg
1271016ad83Smrg    --help) echo "$usage"; exit $?;;
1281016ad83Smrg
1291016ad83Smrg    -m) mode=$2
130709d36bbSmrg        case $mode in
131709d36bbSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132709d36bbSmrg            echo "$0: invalid mode: $mode" >&2
133709d36bbSmrg            exit 1;;
134709d36bbSmrg        esac
135709d36bbSmrg        shift;;
1361016ad83Smrg
1371016ad83Smrg    -o) chowncmd="$chownprog $2"
138709d36bbSmrg        shift;;
1391016ad83Smrg
1401016ad83Smrg    -s) stripcmd=$stripprog;;
1411016ad83Smrg
142709d36bbSmrg    -t)
143709d36bbSmrg        is_target_a_directory=always
144709d36bbSmrg        dst_arg=$2
145709d36bbSmrg        # Protect names problematic for 'test' and other utilities.
146709d36bbSmrg        case $dst_arg in
147709d36bbSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148709d36bbSmrg        esac
149709d36bbSmrg        shift;;
1501016ad83Smrg
151709d36bbSmrg    -T) is_target_a_directory=never;;
1521016ad83Smrg
1531016ad83Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1541016ad83Smrg
155709d36bbSmrg    --) shift
156709d36bbSmrg        break;;
1571016ad83Smrg
158709d36bbSmrg    -*) echo "$0: invalid option: $1" >&2
159709d36bbSmrg        exit 1;;
1601016ad83Smrg
1611016ad83Smrg    *)  break;;
1621016ad83Smrg  esac
1631016ad83Smrg  shift
1641016ad83Smrgdone
1651016ad83Smrg
166709d36bbSmrg# We allow the use of options -d and -T together, by making -d
167709d36bbSmrg# take the precedence; this is for compatibility with GNU install.
168709d36bbSmrg
169709d36bbSmrgif test -n "$dir_arg"; then
170709d36bbSmrg  if test -n "$dst_arg"; then
171709d36bbSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172709d36bbSmrg    exit 1
173709d36bbSmrg  fi
174709d36bbSmrgfi
175709d36bbSmrg
1761016ad83Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771016ad83Smrg  # When -d is used, all remaining arguments are directories to create.
1781016ad83Smrg  # When -t is used, the destination is already specified.
1791016ad83Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801016ad83Smrg  for arg
1811016ad83Smrg  do
1821016ad83Smrg    if test -n "$dst_arg"; then
1831016ad83Smrg      # $@ is not empty: it contains at least $arg.
1841016ad83Smrg      set fnord "$@" "$dst_arg"
1851016ad83Smrg      shift # fnord
1861016ad83Smrg    fi
1871016ad83Smrg    shift # arg
1881016ad83Smrg    dst_arg=$arg
1891016ad83Smrg    # Protect names problematic for 'test' and other utilities.
1901016ad83Smrg    case $dst_arg in
1911016ad83Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1921016ad83Smrg    esac
1931016ad83Smrg  done
1941016ad83Smrgfi
1951016ad83Smrg
1961016ad83Smrgif test $# -eq 0; then
1971016ad83Smrg  if test -z "$dir_arg"; then
1981016ad83Smrg    echo "$0: no input file specified." >&2
1991016ad83Smrg    exit 1
2001016ad83Smrg  fi
2011016ad83Smrg  # It's OK to call 'install-sh -d' without argument.
2021016ad83Smrg  # This can happen when creating conditional directories.
2031016ad83Smrg  exit 0
2041016ad83Smrgfi
2051016ad83Smrg
206709d36bbSmrgif test -z "$dir_arg"; then
207709d36bbSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208709d36bbSmrg    if test ! -d "$dst_arg"; then
209709d36bbSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210709d36bbSmrg      exit 1
211709d36bbSmrg    fi
212709d36bbSmrg  fi
213709d36bbSmrgfi
214709d36bbSmrg
2151016ad83Smrgif test -z "$dir_arg"; then
2161016ad83Smrg  do_exit='(exit $ret); exit $ret'
2171016ad83Smrg  trap "ret=129; $do_exit" 1
2181016ad83Smrg  trap "ret=130; $do_exit" 2
2191016ad83Smrg  trap "ret=141; $do_exit" 13
2201016ad83Smrg  trap "ret=143; $do_exit" 15
2211016ad83Smrg
2221016ad83Smrg  # Set umask so as not to create temps with too-generous modes.
2231016ad83Smrg  # However, 'strip' requires both read and write access to temps.
2241016ad83Smrg  case $mode in
2251016ad83Smrg    # Optimize common cases.
2261016ad83Smrg    *644) cp_umask=133;;
2271016ad83Smrg    *755) cp_umask=22;;
2281016ad83Smrg
2291016ad83Smrg    *[0-7])
2301016ad83Smrg      if test -z "$stripcmd"; then
231709d36bbSmrg        u_plus_rw=
2321016ad83Smrg      else
233709d36bbSmrg        u_plus_rw='% 200'
2341016ad83Smrg      fi
2351016ad83Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2361016ad83Smrg    *)
2371016ad83Smrg      if test -z "$stripcmd"; then
238709d36bbSmrg        u_plus_rw=
2391016ad83Smrg      else
240709d36bbSmrg        u_plus_rw=,u+rw
2411016ad83Smrg      fi
2421016ad83Smrg      cp_umask=$mode$u_plus_rw;;
2431016ad83Smrg  esac
2441016ad83Smrgfi
2451016ad83Smrg
2461016ad83Smrgfor src
2471016ad83Smrgdo
2481016ad83Smrg  # Protect names problematic for 'test' and other utilities.
2491016ad83Smrg  case $src in
2501016ad83Smrg    -* | [=\(\)!]) src=./$src;;
2511016ad83Smrg  esac
2521016ad83Smrg
2531016ad83Smrg  if test -n "$dir_arg"; then
2541016ad83Smrg    dst=$src
2551016ad83Smrg    dstdir=$dst
2561016ad83Smrg    test -d "$dstdir"
2571016ad83Smrg    dstdir_status=$?
2581016ad83Smrg  else
2591016ad83Smrg
2601016ad83Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2611016ad83Smrg    # might cause directories to be created, which would be especially bad
2621016ad83Smrg    # if $src (and thus $dsttmp) contains '*'.
2631016ad83Smrg    if test ! -f "$src" && test ! -d "$src"; then
2641016ad83Smrg      echo "$0: $src does not exist." >&2
2651016ad83Smrg      exit 1
2661016ad83Smrg    fi
2671016ad83Smrg
2681016ad83Smrg    if test -z "$dst_arg"; then
2691016ad83Smrg      echo "$0: no destination specified." >&2
2701016ad83Smrg      exit 1
2711016ad83Smrg    fi
2721016ad83Smrg    dst=$dst_arg
2731016ad83Smrg
274fe12f63cSmrg    # If destination is a directory, append the input filename.
2751016ad83Smrg    if test -d "$dst"; then
276709d36bbSmrg      if test "$is_target_a_directory" = never; then
277709d36bbSmrg        echo "$0: $dst_arg: Is a directory" >&2
278709d36bbSmrg        exit 1
2791016ad83Smrg      fi
2801016ad83Smrg      dstdir=$dst
281fe12f63cSmrg      dstbase=`basename "$src"`
282fe12f63cSmrg      case $dst in
283fe12f63cSmrg	*/) dst=$dst$dstbase;;
284fe12f63cSmrg	*)  dst=$dst/$dstbase;;
285fe12f63cSmrg      esac
2861016ad83Smrg      dstdir_status=0
2871016ad83Smrg    else
288709d36bbSmrg      dstdir=`dirname "$dst"`
2891016ad83Smrg      test -d "$dstdir"
2901016ad83Smrg      dstdir_status=$?
2911016ad83Smrg    fi
2921016ad83Smrg  fi
2931016ad83Smrg
294fe12f63cSmrg  case $dstdir in
295fe12f63cSmrg    */) dstdirslash=$dstdir;;
296fe12f63cSmrg    *)  dstdirslash=$dstdir/;;
297fe12f63cSmrg  esac
298fe12f63cSmrg
2991016ad83Smrg  obsolete_mkdir_used=false
3001016ad83Smrg
3011016ad83Smrg  if test $dstdir_status != 0; then
3021016ad83Smrg    case $posix_mkdir in
3031016ad83Smrg      '')
304709d36bbSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
305709d36bbSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
306709d36bbSmrg        umask=`umask`
307709d36bbSmrg        case $stripcmd.$umask in
308709d36bbSmrg          # Optimize common cases.
309709d36bbSmrg          *[2367][2367]) mkdir_umask=$umask;;
310709d36bbSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311709d36bbSmrg
312709d36bbSmrg          *[0-7])
313709d36bbSmrg            mkdir_umask=`expr $umask + 22 \
314709d36bbSmrg              - $umask % 100 % 40 + $umask % 20 \
315709d36bbSmrg              - $umask % 10 % 4 + $umask % 2
316709d36bbSmrg            `;;
317709d36bbSmrg          *) mkdir_umask=$umask,go-w;;
318709d36bbSmrg        esac
319709d36bbSmrg
320709d36bbSmrg        # With -d, create the new directory with the user-specified mode.
321709d36bbSmrg        # Otherwise, rely on $mkdir_umask.
322709d36bbSmrg        if test -n "$dir_arg"; then
323709d36bbSmrg          mkdir_mode=-m$mode
324709d36bbSmrg        else
325709d36bbSmrg          mkdir_mode=
326709d36bbSmrg        fi
327709d36bbSmrg
328709d36bbSmrg        posix_mkdir=false
329709d36bbSmrg        case $umask in
330709d36bbSmrg          *[123567][0-7][0-7])
331709d36bbSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
332709d36bbSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333709d36bbSmrg            ;;
334709d36bbSmrg          *)
335fe12f63cSmrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
336fe12f63cSmrg            # here however when possible just to lower collision chance.
337709d36bbSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
338709d36bbSmrg
339fe12f63cSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
340fe12f63cSmrg
341fe12f63cSmrg            # Because "mkdir -p" follows existing symlinks and we likely work
342fe12f63cSmrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
343fe12f63cSmrg            # directory is successfully created first before we actually test
344fe12f63cSmrg            # 'mkdir -p' feature.
345709d36bbSmrg            if (umask $mkdir_umask &&
346fe12f63cSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
347fe12f63cSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
348709d36bbSmrg            then
349709d36bbSmrg              if test -z "$dir_arg" || {
350709d36bbSmrg                   # Check for POSIX incompatibilities with -m.
351709d36bbSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
352709d36bbSmrg                   # other-writable bit of parent directory when it shouldn't.
353709d36bbSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
354fe12f63cSmrg                   test_tmpdir="$tmpdir/a"
355fe12f63cSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
356709d36bbSmrg                   case $ls_ld_tmpdir in
357709d36bbSmrg                     d????-?r-*) different_mode=700;;
358709d36bbSmrg                     d????-?--*) different_mode=755;;
359709d36bbSmrg                     *) false;;
360709d36bbSmrg                   esac &&
361fe12f63cSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
362fe12f63cSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
363709d36bbSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
364709d36bbSmrg                   }
365709d36bbSmrg                 }
366709d36bbSmrg              then posix_mkdir=:
367709d36bbSmrg              fi
368fe12f63cSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
369709d36bbSmrg            else
370709d36bbSmrg              # Remove any dirs left behind by ancient mkdir implementations.
371fe12f63cSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
372709d36bbSmrg            fi
373709d36bbSmrg            trap '' 0;;
374709d36bbSmrg        esac;;
3751016ad83Smrg    esac
3761016ad83Smrg
3771016ad83Smrg    if
3781016ad83Smrg      $posix_mkdir && (
379709d36bbSmrg        umask $mkdir_umask &&
380709d36bbSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3811016ad83Smrg      )
3821016ad83Smrg    then :
3831016ad83Smrg    else
3841016ad83Smrg
3851016ad83Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3861016ad83Smrg      # or it failed possibly due to a race condition.  Create the
3871016ad83Smrg      # directory the slow way, step by step, checking for races as we go.
3881016ad83Smrg
3891016ad83Smrg      case $dstdir in
390709d36bbSmrg        /*) prefix='/';;
391709d36bbSmrg        [-=\(\)!]*) prefix='./';;
392709d36bbSmrg        *)  prefix='';;
3931016ad83Smrg      esac
3941016ad83Smrg
3951016ad83Smrg      oIFS=$IFS
3961016ad83Smrg      IFS=/
397709d36bbSmrg      set -f
3981016ad83Smrg      set fnord $dstdir
3991016ad83Smrg      shift
400709d36bbSmrg      set +f
4011016ad83Smrg      IFS=$oIFS
4021016ad83Smrg
4031016ad83Smrg      prefixes=
4041016ad83Smrg
4051016ad83Smrg      for d
4061016ad83Smrg      do
407709d36bbSmrg        test X"$d" = X && continue
408709d36bbSmrg
409709d36bbSmrg        prefix=$prefix$d
410709d36bbSmrg        if test -d "$prefix"; then
411709d36bbSmrg          prefixes=
412709d36bbSmrg        else
413709d36bbSmrg          if $posix_mkdir; then
414709d36bbSmrg            (umask=$mkdir_umask &&
415709d36bbSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416709d36bbSmrg            # Don't fail if two instances are running concurrently.
417709d36bbSmrg            test -d "$prefix" || exit 1
418709d36bbSmrg          else
419709d36bbSmrg            case $prefix in
420709d36bbSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421709d36bbSmrg              *) qprefix=$prefix;;
422709d36bbSmrg            esac
423709d36bbSmrg            prefixes="$prefixes '$qprefix'"
424709d36bbSmrg          fi
425709d36bbSmrg        fi
426709d36bbSmrg        prefix=$prefix/
4271016ad83Smrg      done
4281016ad83Smrg
4291016ad83Smrg      if test -n "$prefixes"; then
430709d36bbSmrg        # Don't fail if two instances are running concurrently.
431709d36bbSmrg        (umask $mkdir_umask &&
432709d36bbSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
433709d36bbSmrg          test -d "$dstdir" || exit 1
434709d36bbSmrg        obsolete_mkdir_used=true
4351016ad83Smrg      fi
4361016ad83Smrg    fi
4371016ad83Smrg  fi
4381016ad83Smrg
4391016ad83Smrg  if test -n "$dir_arg"; then
4401016ad83Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4411016ad83Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4421016ad83Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4431016ad83Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4441016ad83Smrg  else
4451016ad83Smrg
4461016ad83Smrg    # Make a couple of temp file names in the proper directory.
447fe12f63cSmrg    dsttmp=${dstdirslash}_inst.$$_
448fe12f63cSmrg    rmtmp=${dstdirslash}_rm.$$_
4491016ad83Smrg
4501016ad83Smrg    # Trap to clean up those temp files at exit.
4511016ad83Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4521016ad83Smrg
4531016ad83Smrg    # Copy the file name to the temp name.
4541016ad83Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4551016ad83Smrg
4561016ad83Smrg    # and set any options; do chmod last to preserve setuid bits.
4571016ad83Smrg    #
4581016ad83Smrg    # If any of these fail, we abort the whole thing.  If we want to
4591016ad83Smrg    # ignore errors from any of these, just make sure not to ignore
4601016ad83Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4611016ad83Smrg    #
4621016ad83Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4631016ad83Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4641016ad83Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4651016ad83Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4661016ad83Smrg
4671016ad83Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4681016ad83Smrg    if $copy_on_change &&
469709d36bbSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
470709d36bbSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
471709d36bbSmrg       set -f &&
4721016ad83Smrg       set X $old && old=:$2:$4:$5:$6 &&
4731016ad83Smrg       set X $new && new=:$2:$4:$5:$6 &&
474709d36bbSmrg       set +f &&
4751016ad83Smrg       test "$old" = "$new" &&
4761016ad83Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4771016ad83Smrg    then
4781016ad83Smrg      rm -f "$dsttmp"
4791016ad83Smrg    else
4801016ad83Smrg      # Rename the file to the real destination.
4811016ad83Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4821016ad83Smrg
4831016ad83Smrg      # The rename failed, perhaps because mv can't rename something else
4841016ad83Smrg      # to itself, or perhaps because mv is so ancient that it does not
4851016ad83Smrg      # support -f.
4861016ad83Smrg      {
487709d36bbSmrg        # Now remove or move aside any old file at destination location.
488709d36bbSmrg        # We try this two ways since rm can't unlink itself on some
489709d36bbSmrg        # systems and the destination file might be busy for other
490709d36bbSmrg        # reasons.  In this case, the final cleanup might fail but the new
491709d36bbSmrg        # file should still install successfully.
492709d36bbSmrg        {
493709d36bbSmrg          test ! -f "$dst" ||
494709d36bbSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
495709d36bbSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
496709d36bbSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
497709d36bbSmrg          } ||
498709d36bbSmrg          { echo "$0: cannot unlink or rename $dst" >&2
499709d36bbSmrg            (exit 1); exit 1
500709d36bbSmrg          }
501709d36bbSmrg        } &&
502709d36bbSmrg
503709d36bbSmrg        # Now rename the file to the real destination.
504709d36bbSmrg        $doit $mvcmd "$dsttmp" "$dst"
5051016ad83Smrg      }
5061016ad83Smrg    fi || exit 1
5071016ad83Smrg
5081016ad83Smrg    trap '' 0
5091016ad83Smrg  fi
5101016ad83Smrgdone
5111016ad83Smrg
5121016ad83Smrg# Local variables:
513fe12f63cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5141016ad83Smrg# time-stamp-start: "scriptversion="
5151016ad83Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
516245c37e9Smrg# time-stamp-time-zone: "UTC0"
5171016ad83Smrg# time-stamp-end: "; # UTC"
5181016ad83Smrg# End:
519