16abeab63Snia#!/bin/sh
26abeab63Snia# install - install a program, script, or datafile
36abeab63Snia
46abeab63Sniascriptversion=2013-12-25.23; # UTC
56abeab63Snia
66abeab63Snia# This originates from X11R5 (mit/util/scripts/install.sh), which was
76abeab63Snia# later released in X11R6 (xc/config/util/install.sh) with the
86abeab63Snia# following copyright and license.
96abeab63Snia#
106abeab63Snia# Copyright (C) 1994 X Consortium
116abeab63Snia#
126abeab63Snia# Permission is hereby granted, free of charge, to any person obtaining a copy
136abeab63Snia# of this software and associated documentation files (the "Software"), to
146abeab63Snia# deal in the Software without restriction, including without limitation the
156abeab63Snia# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166abeab63Snia# sell copies of the Software, and to permit persons to whom the Software is
176abeab63Snia# furnished to do so, subject to the following conditions:
186abeab63Snia#
196abeab63Snia# The above copyright notice and this permission notice shall be included in
206abeab63Snia# all copies or substantial portions of the Software.
216abeab63Snia#
226abeab63Snia# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236abeab63Snia# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246abeab63Snia# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256abeab63Snia# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266abeab63Snia# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276abeab63Snia# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286abeab63Snia#
296abeab63Snia# Except as contained in this notice, the name of the X Consortium shall not
306abeab63Snia# be used in advertising or otherwise to promote the sale, use or other deal-
316abeab63Snia# ings in this Software without prior written authorization from the X Consor-
326abeab63Snia# tium.
336abeab63Snia#
346abeab63Snia#
356abeab63Snia# FSF changes to this file are in the public domain.
366abeab63Snia#
376abeab63Snia# Calling this script install-sh is preferred over install.sh, to prevent
386abeab63Snia# 'make' implicit rules from creating a file called install from it
396abeab63Snia# when there is no Makefile.
406abeab63Snia#
416abeab63Snia# This script is compatible with the BSD install script, but was written
426abeab63Snia# from scratch.
436abeab63Snia
446abeab63Sniatab='	'
456abeab63Snianl='
466abeab63Snia'
476abeab63SniaIFS=" $tab$nl"
486abeab63Snia
496abeab63Snia# Set DOITPROG to "echo" to test this script.
506abeab63Snia
516abeab63Sniadoit=${DOITPROG-}
526abeab63Sniadoit_exec=${doit:-exec}
536abeab63Snia
546abeab63Snia# Put in absolute file names if you don't have them in your path;
556abeab63Snia# or use environment vars.
566abeab63Snia
576abeab63Sniachgrpprog=${CHGRPPROG-chgrp}
586abeab63Sniachmodprog=${CHMODPROG-chmod}
596abeab63Sniachownprog=${CHOWNPROG-chown}
606abeab63Sniacmpprog=${CMPPROG-cmp}
616abeab63Sniacpprog=${CPPROG-cp}
626abeab63Sniamkdirprog=${MKDIRPROG-mkdir}
636abeab63Sniamvprog=${MVPROG-mv}
646abeab63Sniarmprog=${RMPROG-rm}
656abeab63Sniastripprog=${STRIPPROG-strip}
666abeab63Snia
676abeab63Sniaposix_mkdir=
686abeab63Snia
696abeab63Snia# Desired mode of installed file.
706abeab63Sniamode=0755
716abeab63Snia
726abeab63Sniachgrpcmd=
736abeab63Sniachmodcmd=$chmodprog
746abeab63Sniachowncmd=
756abeab63Sniamvcmd=$mvprog
766abeab63Sniarmcmd="$rmprog -f"
776abeab63Sniastripcmd=
786abeab63Snia
796abeab63Sniasrc=
806abeab63Sniadst=
816abeab63Sniadir_arg=
826abeab63Sniadst_arg=
836abeab63Snia
846abeab63Sniacopy_on_change=false
856abeab63Sniais_target_a_directory=possibly
866abeab63Snia
876abeab63Sniausage="\
886abeab63SniaUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
896abeab63Snia   or: $0 [OPTION]... SRCFILES... DIRECTORY
906abeab63Snia   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
916abeab63Snia   or: $0 [OPTION]... -d DIRECTORIES...
926abeab63Snia
936abeab63SniaIn the 1st form, copy SRCFILE to DSTFILE.
946abeab63SniaIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
956abeab63SniaIn the 4th, create DIRECTORIES.
966abeab63Snia
976abeab63SniaOptions:
986abeab63Snia     --help     display this help and exit.
996abeab63Snia     --version  display version info and exit.
1006abeab63Snia
1016abeab63Snia  -c            (ignored)
1026abeab63Snia  -C            install only if different (preserve the last data modification time)
1036abeab63Snia  -d            create directories instead of installing files.
1046abeab63Snia  -g GROUP      $chgrpprog installed files to GROUP.
1056abeab63Snia  -m MODE       $chmodprog installed files to MODE.
1066abeab63Snia  -o USER       $chownprog installed files to USER.
1076abeab63Snia  -s            $stripprog installed files.
1086abeab63Snia  -t DIRECTORY  install into DIRECTORY.
1096abeab63Snia  -T            report an error if DSTFILE is a directory.
1106abeab63Snia
1116abeab63SniaEnvironment variables override the default commands:
1126abeab63Snia  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1136abeab63Snia  RMPROG STRIPPROG
1146abeab63Snia"
1156abeab63Snia
1166abeab63Sniawhile test $# -ne 0; do
1176abeab63Snia  case $1 in
1186abeab63Snia    -c) ;;
1196abeab63Snia
1206abeab63Snia    -C) copy_on_change=true;;
1216abeab63Snia
1226abeab63Snia    -d) dir_arg=true;;
1236abeab63Snia
1246abeab63Snia    -g) chgrpcmd="$chgrpprog $2"
1256abeab63Snia        shift;;
1266abeab63Snia
1276abeab63Snia    --help) echo "$usage"; exit $?;;
1286abeab63Snia
1296abeab63Snia    -m) mode=$2
1306abeab63Snia        case $mode in
1316abeab63Snia          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1326abeab63Snia            echo "$0: invalid mode: $mode" >&2
1336abeab63Snia            exit 1;;
1346abeab63Snia        esac
1356abeab63Snia        shift;;
1366abeab63Snia
1376abeab63Snia    -o) chowncmd="$chownprog $2"
1386abeab63Snia        shift;;
1396abeab63Snia
1406abeab63Snia    -s) stripcmd=$stripprog;;
1416abeab63Snia
1426abeab63Snia    -t)
1436abeab63Snia        is_target_a_directory=always
1446abeab63Snia        dst_arg=$2
1456abeab63Snia        # Protect names problematic for 'test' and other utilities.
1466abeab63Snia        case $dst_arg in
1476abeab63Snia          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1486abeab63Snia        esac
1496abeab63Snia        shift;;
1506abeab63Snia
1516abeab63Snia    -T) is_target_a_directory=never;;
1526abeab63Snia
1536abeab63Snia    --version) echo "$0 $scriptversion"; exit $?;;
1546abeab63Snia
1556abeab63Snia    --) shift
1566abeab63Snia        break;;
1576abeab63Snia
1586abeab63Snia    -*) echo "$0: invalid option: $1" >&2
1596abeab63Snia        exit 1;;
1606abeab63Snia
1616abeab63Snia    *)  break;;
1626abeab63Snia  esac
1636abeab63Snia  shift
1646abeab63Sniadone
1656abeab63Snia
1666abeab63Snia# We allow the use of options -d and -T together, by making -d
1676abeab63Snia# take the precedence; this is for compatibility with GNU install.
1686abeab63Snia
1696abeab63Sniaif test -n "$dir_arg"; then
1706abeab63Snia  if test -n "$dst_arg"; then
1716abeab63Snia    echo "$0: target directory not allowed when installing a directory." >&2
1726abeab63Snia    exit 1
1736abeab63Snia  fi
1746abeab63Sniafi
1756abeab63Snia
1766abeab63Sniaif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1776abeab63Snia  # When -d is used, all remaining arguments are directories to create.
1786abeab63Snia  # When -t is used, the destination is already specified.
1796abeab63Snia  # Otherwise, the last argument is the destination.  Remove it from $@.
1806abeab63Snia  for arg
1816abeab63Snia  do
1826abeab63Snia    if test -n "$dst_arg"; then
1836abeab63Snia      # $@ is not empty: it contains at least $arg.
1846abeab63Snia      set fnord "$@" "$dst_arg"
1856abeab63Snia      shift # fnord
1866abeab63Snia    fi
1876abeab63Snia    shift # arg
1886abeab63Snia    dst_arg=$arg
1896abeab63Snia    # Protect names problematic for 'test' and other utilities.
1906abeab63Snia    case $dst_arg in
1916abeab63Snia      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1926abeab63Snia    esac
1936abeab63Snia  done
1946abeab63Sniafi
1956abeab63Snia
1966abeab63Sniaif test $# -eq 0; then
1976abeab63Snia  if test -z "$dir_arg"; then
1986abeab63Snia    echo "$0: no input file specified." >&2
1996abeab63Snia    exit 1
2006abeab63Snia  fi
2016abeab63Snia  # It's OK to call 'install-sh -d' without argument.
2026abeab63Snia  # This can happen when creating conditional directories.
2036abeab63Snia  exit 0
2046abeab63Sniafi
2056abeab63Snia
2066abeab63Sniaif test -z "$dir_arg"; then
2076abeab63Snia  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2086abeab63Snia    if test ! -d "$dst_arg"; then
2096abeab63Snia      echo "$0: $dst_arg: Is not a directory." >&2
2106abeab63Snia      exit 1
2116abeab63Snia    fi
2126abeab63Snia  fi
2136abeab63Sniafi
2146abeab63Snia
2156abeab63Sniaif test -z "$dir_arg"; then
2166abeab63Snia  do_exit='(exit $ret); exit $ret'
2176abeab63Snia  trap "ret=129; $do_exit" 1
2186abeab63Snia  trap "ret=130; $do_exit" 2
2196abeab63Snia  trap "ret=141; $do_exit" 13
2206abeab63Snia  trap "ret=143; $do_exit" 15
2216abeab63Snia
2226abeab63Snia  # Set umask so as not to create temps with too-generous modes.
2236abeab63Snia  # However, 'strip' requires both read and write access to temps.
2246abeab63Snia  case $mode in
2256abeab63Snia    # Optimize common cases.
2266abeab63Snia    *644) cp_umask=133;;
2276abeab63Snia    *755) cp_umask=22;;
2286abeab63Snia
2296abeab63Snia    *[0-7])
2306abeab63Snia      if test -z "$stripcmd"; then
2316abeab63Snia        u_plus_rw=
2326abeab63Snia      else
2336abeab63Snia        u_plus_rw='% 200'
2346abeab63Snia      fi
2356abeab63Snia      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2366abeab63Snia    *)
2376abeab63Snia      if test -z "$stripcmd"; then
2386abeab63Snia        u_plus_rw=
2396abeab63Snia      else
2406abeab63Snia        u_plus_rw=,u+rw
2416abeab63Snia      fi
2426abeab63Snia      cp_umask=$mode$u_plus_rw;;
2436abeab63Snia  esac
2446abeab63Sniafi
2456abeab63Snia
2466abeab63Sniafor src
2476abeab63Sniado
2486abeab63Snia  # Protect names problematic for 'test' and other utilities.
2496abeab63Snia  case $src in
2506abeab63Snia    -* | [=\(\)!]) src=./$src;;
2516abeab63Snia  esac
2526abeab63Snia
2536abeab63Snia  if test -n "$dir_arg"; then
2546abeab63Snia    dst=$src
2556abeab63Snia    dstdir=$dst
2566abeab63Snia    test -d "$dstdir"
2576abeab63Snia    dstdir_status=$?
2586abeab63Snia  else
2596abeab63Snia
2606abeab63Snia    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2616abeab63Snia    # might cause directories to be created, which would be especially bad
2626abeab63Snia    # if $src (and thus $dsttmp) contains '*'.
2636abeab63Snia    if test ! -f "$src" && test ! -d "$src"; then
2646abeab63Snia      echo "$0: $src does not exist." >&2
2656abeab63Snia      exit 1
2666abeab63Snia    fi
2676abeab63Snia
2686abeab63Snia    if test -z "$dst_arg"; then
2696abeab63Snia      echo "$0: no destination specified." >&2
2706abeab63Snia      exit 1
2716abeab63Snia    fi
2726abeab63Snia    dst=$dst_arg
2736abeab63Snia
2746abeab63Snia    # If destination is a directory, append the input filename; won't work
2756abeab63Snia    # if double slashes aren't ignored.
2766abeab63Snia    if test -d "$dst"; then
2776abeab63Snia      if test "$is_target_a_directory" = never; then
2786abeab63Snia        echo "$0: $dst_arg: Is a directory" >&2
2796abeab63Snia        exit 1
2806abeab63Snia      fi
2816abeab63Snia      dstdir=$dst
2826abeab63Snia      dst=$dstdir/`basename "$src"`
2836abeab63Snia      dstdir_status=0
2846abeab63Snia    else
2856abeab63Snia      dstdir=`dirname "$dst"`
2866abeab63Snia      test -d "$dstdir"
2876abeab63Snia      dstdir_status=$?
2886abeab63Snia    fi
2896abeab63Snia  fi
2906abeab63Snia
2916abeab63Snia  obsolete_mkdir_used=false
2926abeab63Snia
2936abeab63Snia  if test $dstdir_status != 0; then
2946abeab63Snia    case $posix_mkdir in
2956abeab63Snia      '')
2966abeab63Snia        # Create intermediate dirs using mode 755 as modified by the umask.
2976abeab63Snia        # This is like FreeBSD 'install' as of 1997-10-28.
2986abeab63Snia        umask=`umask`
2996abeab63Snia        case $stripcmd.$umask in
3006abeab63Snia          # Optimize common cases.
3016abeab63Snia          *[2367][2367]) mkdir_umask=$umask;;
3026abeab63Snia          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3036abeab63Snia
3046abeab63Snia          *[0-7])
3056abeab63Snia            mkdir_umask=`expr $umask + 22 \
3066abeab63Snia              - $umask % 100 % 40 + $umask % 20 \
3076abeab63Snia              - $umask % 10 % 4 + $umask % 2
3086abeab63Snia            `;;
3096abeab63Snia          *) mkdir_umask=$umask,go-w;;
3106abeab63Snia        esac
3116abeab63Snia
3126abeab63Snia        # With -d, create the new directory with the user-specified mode.
3136abeab63Snia        # Otherwise, rely on $mkdir_umask.
3146abeab63Snia        if test -n "$dir_arg"; then
3156abeab63Snia          mkdir_mode=-m$mode
3166abeab63Snia        else
3176abeab63Snia          mkdir_mode=
3186abeab63Snia        fi
3196abeab63Snia
3206abeab63Snia        posix_mkdir=false
3216abeab63Snia        case $umask in
3226abeab63Snia          *[123567][0-7][0-7])
3236abeab63Snia            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3246abeab63Snia            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3256abeab63Snia            ;;
3266abeab63Snia          *)
3276abeab63Snia            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3286abeab63Snia            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3296abeab63Snia
3306abeab63Snia            if (umask $mkdir_umask &&
3316abeab63Snia                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3326abeab63Snia            then
3336abeab63Snia              if test -z "$dir_arg" || {
3346abeab63Snia                   # Check for POSIX incompatibilities with -m.
3356abeab63Snia                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3366abeab63Snia                   # other-writable bit of parent directory when it shouldn't.
3376abeab63Snia                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3386abeab63Snia                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3396abeab63Snia                   case $ls_ld_tmpdir in
3406abeab63Snia                     d????-?r-*) different_mode=700;;
3416abeab63Snia                     d????-?--*) different_mode=755;;
3426abeab63Snia                     *) false;;
3436abeab63Snia                   esac &&
3446abeab63Snia                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3456abeab63Snia                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3466abeab63Snia                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3476abeab63Snia                   }
3486abeab63Snia                 }
3496abeab63Snia              then posix_mkdir=:
3506abeab63Snia              fi
3516abeab63Snia              rmdir "$tmpdir/d" "$tmpdir"
3526abeab63Snia            else
3536abeab63Snia              # Remove any dirs left behind by ancient mkdir implementations.
3546abeab63Snia              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3556abeab63Snia            fi
3566abeab63Snia            trap '' 0;;
3576abeab63Snia        esac;;
3586abeab63Snia    esac
3596abeab63Snia
3606abeab63Snia    if
3616abeab63Snia      $posix_mkdir && (
3626abeab63Snia        umask $mkdir_umask &&
3636abeab63Snia        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3646abeab63Snia      )
3656abeab63Snia    then :
3666abeab63Snia    else
3676abeab63Snia
3686abeab63Snia      # The umask is ridiculous, or mkdir does not conform to POSIX,
3696abeab63Snia      # or it failed possibly due to a race condition.  Create the
3706abeab63Snia      # directory the slow way, step by step, checking for races as we go.
3716abeab63Snia
3726abeab63Snia      case $dstdir in
3736abeab63Snia        /*) prefix='/';;
3746abeab63Snia        [-=\(\)!]*) prefix='./';;
3756abeab63Snia        *)  prefix='';;
3766abeab63Snia      esac
3776abeab63Snia
3786abeab63Snia      oIFS=$IFS
3796abeab63Snia      IFS=/
3806abeab63Snia      set -f
3816abeab63Snia      set fnord $dstdir
3826abeab63Snia      shift
3836abeab63Snia      set +f
3846abeab63Snia      IFS=$oIFS
3856abeab63Snia
3866abeab63Snia      prefixes=
3876abeab63Snia
3886abeab63Snia      for d
3896abeab63Snia      do
3906abeab63Snia        test X"$d" = X && continue
3916abeab63Snia
3926abeab63Snia        prefix=$prefix$d
3936abeab63Snia        if test -d "$prefix"; then
3946abeab63Snia          prefixes=
3956abeab63Snia        else
3966abeab63Snia          if $posix_mkdir; then
3976abeab63Snia            (umask=$mkdir_umask &&
3986abeab63Snia             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3996abeab63Snia            # Don't fail if two instances are running concurrently.
4006abeab63Snia            test -d "$prefix" || exit 1
4016abeab63Snia          else
4026abeab63Snia            case $prefix in
4036abeab63Snia              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4046abeab63Snia              *) qprefix=$prefix;;
4056abeab63Snia            esac
4066abeab63Snia            prefixes="$prefixes '$qprefix'"
4076abeab63Snia          fi
4086abeab63Snia        fi
4096abeab63Snia        prefix=$prefix/
4106abeab63Snia      done
4116abeab63Snia
4126abeab63Snia      if test -n "$prefixes"; then
4136abeab63Snia        # Don't fail if two instances are running concurrently.
4146abeab63Snia        (umask $mkdir_umask &&
4156abeab63Snia         eval "\$doit_exec \$mkdirprog $prefixes") ||
4166abeab63Snia          test -d "$dstdir" || exit 1
4176abeab63Snia        obsolete_mkdir_used=true
4186abeab63Snia      fi
4196abeab63Snia    fi
4206abeab63Snia  fi
4216abeab63Snia
4226abeab63Snia  if test -n "$dir_arg"; then
4236abeab63Snia    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4246abeab63Snia    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4256abeab63Snia    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4266abeab63Snia      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4276abeab63Snia  else
4286abeab63Snia
4296abeab63Snia    # Make a couple of temp file names in the proper directory.
4306abeab63Snia    dsttmp=$dstdir/_inst.$$_
4316abeab63Snia    rmtmp=$dstdir/_rm.$$_
4326abeab63Snia
4336abeab63Snia    # Trap to clean up those temp files at exit.
4346abeab63Snia    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4356abeab63Snia
4366abeab63Snia    # Copy the file name to the temp name.
4376abeab63Snia    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4386abeab63Snia
4396abeab63Snia    # and set any options; do chmod last to preserve setuid bits.
4406abeab63Snia    #
4416abeab63Snia    # If any of these fail, we abort the whole thing.  If we want to
4426abeab63Snia    # ignore errors from any of these, just make sure not to ignore
4436abeab63Snia    # errors from the above "$doit $cpprog $src $dsttmp" command.
4446abeab63Snia    #
4456abeab63Snia    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4466abeab63Snia    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4476abeab63Snia    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4486abeab63Snia    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4496abeab63Snia
4506abeab63Snia    # If -C, don't bother to copy if it wouldn't change the file.
4516abeab63Snia    if $copy_on_change &&
4526abeab63Snia       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4536abeab63Snia       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4546abeab63Snia       set -f &&
4556abeab63Snia       set X $old && old=:$2:$4:$5:$6 &&
4566abeab63Snia       set X $new && new=:$2:$4:$5:$6 &&
4576abeab63Snia       set +f &&
4586abeab63Snia       test "$old" = "$new" &&
4596abeab63Snia       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4606abeab63Snia    then
4616abeab63Snia      rm -f "$dsttmp"
4626abeab63Snia    else
4636abeab63Snia      # Rename the file to the real destination.
4646abeab63Snia      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4656abeab63Snia
4666abeab63Snia      # The rename failed, perhaps because mv can't rename something else
4676abeab63Snia      # to itself, or perhaps because mv is so ancient that it does not
4686abeab63Snia      # support -f.
4696abeab63Snia      {
4706abeab63Snia        # Now remove or move aside any old file at destination location.
4716abeab63Snia        # We try this two ways since rm can't unlink itself on some
4726abeab63Snia        # systems and the destination file might be busy for other
4736abeab63Snia        # reasons.  In this case, the final cleanup might fail but the new
4746abeab63Snia        # file should still install successfully.
4756abeab63Snia        {
4766abeab63Snia          test ! -f "$dst" ||
4776abeab63Snia          $doit $rmcmd -f "$dst" 2>/dev/null ||
4786abeab63Snia          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4796abeab63Snia            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4806abeab63Snia          } ||
4816abeab63Snia          { echo "$0: cannot unlink or rename $dst" >&2
4826abeab63Snia            (exit 1); exit 1
4836abeab63Snia          }
4846abeab63Snia        } &&
4856abeab63Snia
4866abeab63Snia        # Now rename the file to the real destination.
4876abeab63Snia        $doit $mvcmd "$dsttmp" "$dst"
4886abeab63Snia      }
4896abeab63Snia    fi || exit 1
4906abeab63Snia
4916abeab63Snia    trap '' 0
4926abeab63Snia  fi
4936abeab63Sniadone
4946abeab63Snia
4956abeab63Snia# Local variables:
4966abeab63Snia# eval: (add-hook 'write-file-hooks 'time-stamp)
4976abeab63Snia# time-stamp-start: "scriptversion="
4986abeab63Snia# time-stamp-format: "%:y-%02m-%02d.%02H"
4996abeab63Snia# time-stamp-time-zone: "UTC"
5006abeab63Snia# time-stamp-end: "; # UTC"
5016abeab63Snia# End:
502