xref: /freebsd/contrib/ntp/sntp/libevent/build-aux/install-sh (revision e43d33d286a1aa41b6fc6a209f28a18e8cd7437a)
12b15cb3dSCy Schubert#!/bin/sh
22b15cb3dSCy Schubert# install - install a program, script, or datafile
32b15cb3dSCy Schubert
4*2d4e511cSCy Schubertscriptversion=2013-12-25.23; # UTC
52b15cb3dSCy Schubert
62b15cb3dSCy Schubert# This originates from X11R5 (mit/util/scripts/install.sh), which was
72b15cb3dSCy Schubert# later released in X11R6 (xc/config/util/install.sh) with the
82b15cb3dSCy Schubert# following copyright and license.
92b15cb3dSCy Schubert#
102b15cb3dSCy Schubert# Copyright (C) 1994 X Consortium
112b15cb3dSCy Schubert#
122b15cb3dSCy Schubert# Permission is hereby granted, free of charge, to any person obtaining a copy
132b15cb3dSCy Schubert# of this software and associated documentation files (the "Software"), to
142b15cb3dSCy Schubert# deal in the Software without restriction, including without limitation the
152b15cb3dSCy Schubert# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
162b15cb3dSCy Schubert# sell copies of the Software, and to permit persons to whom the Software is
172b15cb3dSCy Schubert# furnished to do so, subject to the following conditions:
182b15cb3dSCy Schubert#
192b15cb3dSCy Schubert# The above copyright notice and this permission notice shall be included in
202b15cb3dSCy Schubert# all copies or substantial portions of the Software.
212b15cb3dSCy Schubert#
222b15cb3dSCy Schubert# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
232b15cb3dSCy Schubert# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
242b15cb3dSCy Schubert# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
252b15cb3dSCy Schubert# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
262b15cb3dSCy Schubert# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
272b15cb3dSCy Schubert# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
282b15cb3dSCy Schubert#
292b15cb3dSCy Schubert# Except as contained in this notice, the name of the X Consortium shall not
302b15cb3dSCy Schubert# be used in advertising or otherwise to promote the sale, use or other deal-
312b15cb3dSCy Schubert# ings in this Software without prior written authorization from the X Consor-
322b15cb3dSCy Schubert# tium.
332b15cb3dSCy Schubert#
342b15cb3dSCy Schubert#
352b15cb3dSCy Schubert# FSF changes to this file are in the public domain.
362b15cb3dSCy Schubert#
372b15cb3dSCy Schubert# Calling this script install-sh is preferred over install.sh, to prevent
38f0574f5cSXin LI# 'make' implicit rules from creating a file called install from it
392b15cb3dSCy Schubert# when there is no Makefile.
402b15cb3dSCy Schubert#
412b15cb3dSCy Schubert# This script is compatible with the BSD install script, but was written
422b15cb3dSCy Schubert# from scratch.
432b15cb3dSCy Schubert
44f0574f5cSXin LItab='	'
452b15cb3dSCy Schubertnl='
462b15cb3dSCy Schubert'
47f0574f5cSXin LIIFS=" $tab$nl"
482b15cb3dSCy Schubert
49f0574f5cSXin LI# Set DOITPROG to "echo" to test this script.
502b15cb3dSCy Schubert
512b15cb3dSCy Schubertdoit=${DOITPROG-}
52f0574f5cSXin LIdoit_exec=${doit:-exec}
532b15cb3dSCy Schubert
542b15cb3dSCy Schubert# Put in absolute file names if you don't have them in your path;
552b15cb3dSCy Schubert# or use environment vars.
562b15cb3dSCy Schubert
572b15cb3dSCy Schubertchgrpprog=${CHGRPPROG-chgrp}
582b15cb3dSCy Schubertchmodprog=${CHMODPROG-chmod}
592b15cb3dSCy Schubertchownprog=${CHOWNPROG-chown}
602b15cb3dSCy Schubertcmpprog=${CMPPROG-cmp}
612b15cb3dSCy Schubertcpprog=${CPPROG-cp}
622b15cb3dSCy Schubertmkdirprog=${MKDIRPROG-mkdir}
632b15cb3dSCy Schubertmvprog=${MVPROG-mv}
642b15cb3dSCy Schubertrmprog=${RMPROG-rm}
652b15cb3dSCy Schubertstripprog=${STRIPPROG-strip}
662b15cb3dSCy Schubert
672b15cb3dSCy Schubertposix_mkdir=
682b15cb3dSCy Schubert
692b15cb3dSCy Schubert# Desired mode of installed file.
702b15cb3dSCy Schubertmode=0755
712b15cb3dSCy Schubert
722b15cb3dSCy Schubertchgrpcmd=
732b15cb3dSCy Schubertchmodcmd=$chmodprog
742b15cb3dSCy Schubertchowncmd=
752b15cb3dSCy Schubertmvcmd=$mvprog
762b15cb3dSCy Schubertrmcmd="$rmprog -f"
772b15cb3dSCy Schubertstripcmd=
782b15cb3dSCy Schubert
792b15cb3dSCy Schubertsrc=
802b15cb3dSCy Schubertdst=
812b15cb3dSCy Schubertdir_arg=
822b15cb3dSCy Schubertdst_arg=
832b15cb3dSCy Schubert
842b15cb3dSCy Schubertcopy_on_change=false
85f0574f5cSXin LIis_target_a_directory=possibly
862b15cb3dSCy Schubert
872b15cb3dSCy Schubertusage="\
882b15cb3dSCy SchubertUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
892b15cb3dSCy Schubert   or: $0 [OPTION]... SRCFILES... DIRECTORY
902b15cb3dSCy Schubert   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
912b15cb3dSCy Schubert   or: $0 [OPTION]... -d DIRECTORIES...
922b15cb3dSCy Schubert
932b15cb3dSCy SchubertIn the 1st form, copy SRCFILE to DSTFILE.
942b15cb3dSCy SchubertIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
952b15cb3dSCy SchubertIn the 4th, create DIRECTORIES.
962b15cb3dSCy Schubert
972b15cb3dSCy SchubertOptions:
982b15cb3dSCy Schubert     --help     display this help and exit.
992b15cb3dSCy Schubert     --version  display version info and exit.
1002b15cb3dSCy Schubert
1012b15cb3dSCy Schubert  -c            (ignored)
1022b15cb3dSCy Schubert  -C            install only if different (preserve the last data modification time)
1032b15cb3dSCy Schubert  -d            create directories instead of installing files.
1042b15cb3dSCy Schubert  -g GROUP      $chgrpprog installed files to GROUP.
1052b15cb3dSCy Schubert  -m MODE       $chmodprog installed files to MODE.
1062b15cb3dSCy Schubert  -o USER       $chownprog installed files to USER.
1072b15cb3dSCy Schubert  -s            $stripprog installed files.
1082b15cb3dSCy Schubert  -t DIRECTORY  install into DIRECTORY.
1092b15cb3dSCy Schubert  -T            report an error if DSTFILE is a directory.
1102b15cb3dSCy Schubert
1112b15cb3dSCy SchubertEnvironment variables override the default commands:
1122b15cb3dSCy Schubert  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1132b15cb3dSCy Schubert  RMPROG STRIPPROG
1142b15cb3dSCy Schubert"
1152b15cb3dSCy Schubert
1162b15cb3dSCy Schubertwhile test $# -ne 0; do
1172b15cb3dSCy Schubert  case $1 in
1182b15cb3dSCy Schubert    -c) ;;
1192b15cb3dSCy Schubert
1202b15cb3dSCy Schubert    -C) copy_on_change=true;;
1212b15cb3dSCy Schubert
1222b15cb3dSCy Schubert    -d) dir_arg=true;;
1232b15cb3dSCy Schubert
1242b15cb3dSCy Schubert    -g) chgrpcmd="$chgrpprog $2"
1252b15cb3dSCy Schubert        shift;;
1262b15cb3dSCy Schubert
1272b15cb3dSCy Schubert    --help) echo "$usage"; exit $?;;
1282b15cb3dSCy Schubert
1292b15cb3dSCy Schubert    -m) mode=$2
1302b15cb3dSCy Schubert        case $mode in
131f0574f5cSXin LI          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1322b15cb3dSCy Schubert            echo "$0: invalid mode: $mode" >&2
1332b15cb3dSCy Schubert            exit 1;;
1342b15cb3dSCy Schubert        esac
1352b15cb3dSCy Schubert        shift;;
1362b15cb3dSCy Schubert
1372b15cb3dSCy Schubert    -o) chowncmd="$chownprog $2"
1382b15cb3dSCy Schubert        shift;;
1392b15cb3dSCy Schubert
1402b15cb3dSCy Schubert    -s) stripcmd=$stripprog;;
1412b15cb3dSCy Schubert
142f0574f5cSXin LI    -t)
143f0574f5cSXin LI        is_target_a_directory=always
144f0574f5cSXin LI        dst_arg=$2
145f0574f5cSXin LI        # Protect names problematic for 'test' and other utilities.
146f0574f5cSXin LI        case $dst_arg in
147f0574f5cSXin LI          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148f0574f5cSXin LI        esac
1492b15cb3dSCy Schubert        shift;;
1502b15cb3dSCy Schubert
151f0574f5cSXin LI    -T) is_target_a_directory=never;;
1522b15cb3dSCy Schubert
1532b15cb3dSCy Schubert    --version) echo "$0 $scriptversion"; exit $?;;
1542b15cb3dSCy Schubert
1552b15cb3dSCy Schubert    --) shift
1562b15cb3dSCy Schubert        break;;
1572b15cb3dSCy Schubert
1582b15cb3dSCy Schubert    -*) echo "$0: invalid option: $1" >&2
1592b15cb3dSCy Schubert        exit 1;;
1602b15cb3dSCy Schubert
1612b15cb3dSCy Schubert    *)  break;;
1622b15cb3dSCy Schubert  esac
1632b15cb3dSCy Schubert  shift
1642b15cb3dSCy Schubertdone
1652b15cb3dSCy Schubert
166f0574f5cSXin LI# We allow the use of options -d and -T together, by making -d
167f0574f5cSXin LI# take the precedence; this is for compatibility with GNU install.
168f0574f5cSXin LI
169f0574f5cSXin LIif test -n "$dir_arg"; then
170f0574f5cSXin LI  if test -n "$dst_arg"; then
171f0574f5cSXin LI    echo "$0: target directory not allowed when installing a directory." >&2
172f0574f5cSXin LI    exit 1
173f0574f5cSXin LI  fi
174f0574f5cSXin LIfi
175f0574f5cSXin LI
1762b15cb3dSCy Schubertif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772b15cb3dSCy Schubert  # When -d is used, all remaining arguments are directories to create.
1782b15cb3dSCy Schubert  # When -t is used, the destination is already specified.
1792b15cb3dSCy Schubert  # Otherwise, the last argument is the destination.  Remove it from $@.
1802b15cb3dSCy Schubert  for arg
1812b15cb3dSCy Schubert  do
1822b15cb3dSCy Schubert    if test -n "$dst_arg"; then
1832b15cb3dSCy Schubert      # $@ is not empty: it contains at least $arg.
1842b15cb3dSCy Schubert      set fnord "$@" "$dst_arg"
1852b15cb3dSCy Schubert      shift # fnord
1862b15cb3dSCy Schubert    fi
1872b15cb3dSCy Schubert    shift # arg
1882b15cb3dSCy Schubert    dst_arg=$arg
189f0574f5cSXin LI    # Protect names problematic for 'test' and other utilities.
190f0574f5cSXin LI    case $dst_arg in
191f0574f5cSXin LI      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192f0574f5cSXin LI    esac
1932b15cb3dSCy Schubert  done
1942b15cb3dSCy Schubertfi
1952b15cb3dSCy Schubert
1962b15cb3dSCy Schubertif test $# -eq 0; then
1972b15cb3dSCy Schubert  if test -z "$dir_arg"; then
1982b15cb3dSCy Schubert    echo "$0: no input file specified." >&2
1992b15cb3dSCy Schubert    exit 1
2002b15cb3dSCy Schubert  fi
201f0574f5cSXin LI  # It's OK to call 'install-sh -d' without argument.
2022b15cb3dSCy Schubert  # This can happen when creating conditional directories.
2032b15cb3dSCy Schubert  exit 0
2042b15cb3dSCy Schubertfi
2052b15cb3dSCy Schubert
2062b15cb3dSCy Schubertif test -z "$dir_arg"; then
207f0574f5cSXin LI  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208f0574f5cSXin LI    if test ! -d "$dst_arg"; then
209f0574f5cSXin LI      echo "$0: $dst_arg: Is not a directory." >&2
210f0574f5cSXin LI      exit 1
211f0574f5cSXin LI    fi
212f0574f5cSXin LI  fi
213f0574f5cSXin LIfi
214f0574f5cSXin LI
215f0574f5cSXin LIif test -z "$dir_arg"; then
216f0574f5cSXin LI  do_exit='(exit $ret); exit $ret'
217f0574f5cSXin LI  trap "ret=129; $do_exit" 1
218f0574f5cSXin LI  trap "ret=130; $do_exit" 2
219f0574f5cSXin LI  trap "ret=141; $do_exit" 13
220f0574f5cSXin LI  trap "ret=143; $do_exit" 15
2212b15cb3dSCy Schubert
2222b15cb3dSCy Schubert  # Set umask so as not to create temps with too-generous modes.
2232b15cb3dSCy Schubert  # However, 'strip' requires both read and write access to temps.
2242b15cb3dSCy Schubert  case $mode in
2252b15cb3dSCy Schubert    # Optimize common cases.
2262b15cb3dSCy Schubert    *644) cp_umask=133;;
2272b15cb3dSCy Schubert    *755) cp_umask=22;;
2282b15cb3dSCy Schubert
2292b15cb3dSCy Schubert    *[0-7])
2302b15cb3dSCy Schubert      if test -z "$stripcmd"; then
2312b15cb3dSCy Schubert        u_plus_rw=
2322b15cb3dSCy Schubert      else
2332b15cb3dSCy Schubert        u_plus_rw='% 200'
2342b15cb3dSCy Schubert      fi
2352b15cb3dSCy Schubert      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2362b15cb3dSCy Schubert    *)
2372b15cb3dSCy Schubert      if test -z "$stripcmd"; then
2382b15cb3dSCy Schubert        u_plus_rw=
2392b15cb3dSCy Schubert      else
2402b15cb3dSCy Schubert        u_plus_rw=,u+rw
2412b15cb3dSCy Schubert      fi
2422b15cb3dSCy Schubert      cp_umask=$mode$u_plus_rw;;
2432b15cb3dSCy Schubert  esac
2442b15cb3dSCy Schubertfi
2452b15cb3dSCy Schubert
2462b15cb3dSCy Schubertfor src
2472b15cb3dSCy Schubertdo
248f0574f5cSXin LI  # Protect names problematic for 'test' and other utilities.
2492b15cb3dSCy Schubert  case $src in
250f0574f5cSXin LI    -* | [=\(\)!]) src=./$src;;
2512b15cb3dSCy Schubert  esac
2522b15cb3dSCy Schubert
2532b15cb3dSCy Schubert  if test -n "$dir_arg"; then
2542b15cb3dSCy Schubert    dst=$src
2552b15cb3dSCy Schubert    dstdir=$dst
2562b15cb3dSCy Schubert    test -d "$dstdir"
2572b15cb3dSCy Schubert    dstdir_status=$?
2582b15cb3dSCy Schubert  else
2592b15cb3dSCy Schubert
2602b15cb3dSCy Schubert    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2612b15cb3dSCy Schubert    # might cause directories to be created, which would be especially bad
2622b15cb3dSCy Schubert    # if $src (and thus $dsttmp) contains '*'.
2632b15cb3dSCy Schubert    if test ! -f "$src" && test ! -d "$src"; then
2642b15cb3dSCy Schubert      echo "$0: $src does not exist." >&2
2652b15cb3dSCy Schubert      exit 1
2662b15cb3dSCy Schubert    fi
2672b15cb3dSCy Schubert
2682b15cb3dSCy Schubert    if test -z "$dst_arg"; then
2692b15cb3dSCy Schubert      echo "$0: no destination specified." >&2
2702b15cb3dSCy Schubert      exit 1
2712b15cb3dSCy Schubert    fi
2722b15cb3dSCy Schubert    dst=$dst_arg
2732b15cb3dSCy Schubert
2742b15cb3dSCy Schubert    # If destination is a directory, append the input filename; won't work
2752b15cb3dSCy Schubert    # if double slashes aren't ignored.
2762b15cb3dSCy Schubert    if test -d "$dst"; then
277f0574f5cSXin LI      if test "$is_target_a_directory" = never; then
2782b15cb3dSCy Schubert        echo "$0: $dst_arg: Is a directory" >&2
2792b15cb3dSCy Schubert        exit 1
2802b15cb3dSCy Schubert      fi
2812b15cb3dSCy Schubert      dstdir=$dst
2822b15cb3dSCy Schubert      dst=$dstdir/`basename "$src"`
2832b15cb3dSCy Schubert      dstdir_status=0
2842b15cb3dSCy Schubert    else
285f0574f5cSXin LI      dstdir=`dirname "$dst"`
2862b15cb3dSCy Schubert      test -d "$dstdir"
2872b15cb3dSCy Schubert      dstdir_status=$?
2882b15cb3dSCy Schubert    fi
2892b15cb3dSCy Schubert  fi
2902b15cb3dSCy Schubert
2912b15cb3dSCy Schubert  obsolete_mkdir_used=false
2922b15cb3dSCy Schubert
2932b15cb3dSCy Schubert  if test $dstdir_status != 0; then
2942b15cb3dSCy Schubert    case $posix_mkdir in
2952b15cb3dSCy Schubert      '')
2962b15cb3dSCy Schubert        # Create intermediate dirs using mode 755 as modified by the umask.
2972b15cb3dSCy Schubert        # This is like FreeBSD 'install' as of 1997-10-28.
2982b15cb3dSCy Schubert        umask=`umask`
2992b15cb3dSCy Schubert        case $stripcmd.$umask in
3002b15cb3dSCy Schubert          # Optimize common cases.
3012b15cb3dSCy Schubert          *[2367][2367]) mkdir_umask=$umask;;
3022b15cb3dSCy Schubert          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3032b15cb3dSCy Schubert
3042b15cb3dSCy Schubert          *[0-7])
3052b15cb3dSCy Schubert            mkdir_umask=`expr $umask + 22 \
3062b15cb3dSCy Schubert              - $umask % 100 % 40 + $umask % 20 \
3072b15cb3dSCy Schubert              - $umask % 10 % 4 + $umask % 2
3082b15cb3dSCy Schubert            `;;
3092b15cb3dSCy Schubert          *) mkdir_umask=$umask,go-w;;
3102b15cb3dSCy Schubert        esac
3112b15cb3dSCy Schubert
3122b15cb3dSCy Schubert        # With -d, create the new directory with the user-specified mode.
3132b15cb3dSCy Schubert        # Otherwise, rely on $mkdir_umask.
3142b15cb3dSCy Schubert        if test -n "$dir_arg"; then
3152b15cb3dSCy Schubert          mkdir_mode=-m$mode
3162b15cb3dSCy Schubert        else
3172b15cb3dSCy Schubert          mkdir_mode=
3182b15cb3dSCy Schubert        fi
3192b15cb3dSCy Schubert
3202b15cb3dSCy Schubert        posix_mkdir=false
3212b15cb3dSCy Schubert        case $umask in
3222b15cb3dSCy Schubert          *[123567][0-7][0-7])
3232b15cb3dSCy Schubert            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3242b15cb3dSCy Schubert            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3252b15cb3dSCy Schubert            ;;
3262b15cb3dSCy Schubert          *)
3272b15cb3dSCy Schubert            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3282b15cb3dSCy Schubert            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3292b15cb3dSCy Schubert
3302b15cb3dSCy Schubert            if (umask $mkdir_umask &&
3312b15cb3dSCy Schubert                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3322b15cb3dSCy Schubert            then
3332b15cb3dSCy Schubert              if test -z "$dir_arg" || {
3342b15cb3dSCy Schubert                   # Check for POSIX incompatibilities with -m.
3352b15cb3dSCy Schubert                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336f0574f5cSXin LI                   # other-writable bit of parent directory when it shouldn't.
3372b15cb3dSCy Schubert                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3382b15cb3dSCy Schubert                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3392b15cb3dSCy Schubert                   case $ls_ld_tmpdir in
3402b15cb3dSCy Schubert                     d????-?r-*) different_mode=700;;
3412b15cb3dSCy Schubert                     d????-?--*) different_mode=755;;
3422b15cb3dSCy Schubert                     *) false;;
3432b15cb3dSCy Schubert                   esac &&
3442b15cb3dSCy Schubert                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3452b15cb3dSCy Schubert                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3462b15cb3dSCy Schubert                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3472b15cb3dSCy Schubert                   }
3482b15cb3dSCy Schubert                 }
3492b15cb3dSCy Schubert              then posix_mkdir=:
3502b15cb3dSCy Schubert              fi
3512b15cb3dSCy Schubert              rmdir "$tmpdir/d" "$tmpdir"
3522b15cb3dSCy Schubert            else
3532b15cb3dSCy Schubert              # Remove any dirs left behind by ancient mkdir implementations.
3542b15cb3dSCy Schubert              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3552b15cb3dSCy Schubert            fi
3562b15cb3dSCy Schubert            trap '' 0;;
3572b15cb3dSCy Schubert        esac;;
3582b15cb3dSCy Schubert    esac
3592b15cb3dSCy Schubert
3602b15cb3dSCy Schubert    if
3612b15cb3dSCy Schubert      $posix_mkdir && (
3622b15cb3dSCy Schubert        umask $mkdir_umask &&
3632b15cb3dSCy Schubert        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3642b15cb3dSCy Schubert      )
3652b15cb3dSCy Schubert    then :
3662b15cb3dSCy Schubert    else
3672b15cb3dSCy Schubert
3682b15cb3dSCy Schubert      # The umask is ridiculous, or mkdir does not conform to POSIX,
3692b15cb3dSCy Schubert      # or it failed possibly due to a race condition.  Create the
3702b15cb3dSCy Schubert      # directory the slow way, step by step, checking for races as we go.
3712b15cb3dSCy Schubert
3722b15cb3dSCy Schubert      case $dstdir in
3732b15cb3dSCy Schubert        /*) prefix='/';;
374f0574f5cSXin LI        [-=\(\)!]*) prefix='./';;
3752b15cb3dSCy Schubert        *)  prefix='';;
3762b15cb3dSCy Schubert      esac
3772b15cb3dSCy Schubert
3782b15cb3dSCy Schubert      oIFS=$IFS
3792b15cb3dSCy Schubert      IFS=/
380f0574f5cSXin LI      set -f
3812b15cb3dSCy Schubert      set fnord $dstdir
3822b15cb3dSCy Schubert      shift
383f0574f5cSXin LI      set +f
3842b15cb3dSCy Schubert      IFS=$oIFS
3852b15cb3dSCy Schubert
3862b15cb3dSCy Schubert      prefixes=
3872b15cb3dSCy Schubert
3882b15cb3dSCy Schubert      for d
3892b15cb3dSCy Schubert      do
390f0574f5cSXin LI        test X"$d" = X && continue
3912b15cb3dSCy Schubert
3922b15cb3dSCy Schubert        prefix=$prefix$d
3932b15cb3dSCy Schubert        if test -d "$prefix"; then
3942b15cb3dSCy Schubert          prefixes=
3952b15cb3dSCy Schubert        else
3962b15cb3dSCy Schubert          if $posix_mkdir; then
3972b15cb3dSCy Schubert            (umask=$mkdir_umask &&
3982b15cb3dSCy Schubert             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3992b15cb3dSCy Schubert            # Don't fail if two instances are running concurrently.
4002b15cb3dSCy Schubert            test -d "$prefix" || exit 1
4012b15cb3dSCy Schubert          else
4022b15cb3dSCy Schubert            case $prefix in
4032b15cb3dSCy Schubert              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4042b15cb3dSCy Schubert              *) qprefix=$prefix;;
4052b15cb3dSCy Schubert            esac
4062b15cb3dSCy Schubert            prefixes="$prefixes '$qprefix'"
4072b15cb3dSCy Schubert          fi
4082b15cb3dSCy Schubert        fi
4092b15cb3dSCy Schubert        prefix=$prefix/
4102b15cb3dSCy Schubert      done
4112b15cb3dSCy Schubert
4122b15cb3dSCy Schubert      if test -n "$prefixes"; then
4132b15cb3dSCy Schubert        # Don't fail if two instances are running concurrently.
4142b15cb3dSCy Schubert        (umask $mkdir_umask &&
4152b15cb3dSCy Schubert         eval "\$doit_exec \$mkdirprog $prefixes") ||
4162b15cb3dSCy Schubert          test -d "$dstdir" || exit 1
4172b15cb3dSCy Schubert        obsolete_mkdir_used=true
4182b15cb3dSCy Schubert      fi
4192b15cb3dSCy Schubert    fi
4202b15cb3dSCy Schubert  fi
4212b15cb3dSCy Schubert
4222b15cb3dSCy Schubert  if test -n "$dir_arg"; then
4232b15cb3dSCy Schubert    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4242b15cb3dSCy Schubert    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4252b15cb3dSCy Schubert    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4262b15cb3dSCy Schubert      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4272b15cb3dSCy Schubert  else
4282b15cb3dSCy Schubert
4292b15cb3dSCy Schubert    # Make a couple of temp file names in the proper directory.
4302b15cb3dSCy Schubert    dsttmp=$dstdir/_inst.$$_
4312b15cb3dSCy Schubert    rmtmp=$dstdir/_rm.$$_
4322b15cb3dSCy Schubert
4332b15cb3dSCy Schubert    # Trap to clean up those temp files at exit.
4342b15cb3dSCy Schubert    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4352b15cb3dSCy Schubert
4362b15cb3dSCy Schubert    # Copy the file name to the temp name.
4372b15cb3dSCy Schubert    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4382b15cb3dSCy Schubert
4392b15cb3dSCy Schubert    # and set any options; do chmod last to preserve setuid bits.
4402b15cb3dSCy Schubert    #
4412b15cb3dSCy Schubert    # If any of these fail, we abort the whole thing.  If we want to
4422b15cb3dSCy Schubert    # ignore errors from any of these, just make sure not to ignore
4432b15cb3dSCy Schubert    # errors from the above "$doit $cpprog $src $dsttmp" command.
4442b15cb3dSCy Schubert    #
4452b15cb3dSCy Schubert    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4462b15cb3dSCy Schubert    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4472b15cb3dSCy Schubert    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4482b15cb3dSCy Schubert    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4492b15cb3dSCy Schubert
4502b15cb3dSCy Schubert    # If -C, don't bother to copy if it wouldn't change the file.
4512b15cb3dSCy Schubert    if $copy_on_change &&
4522b15cb3dSCy Schubert       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4532b15cb3dSCy Schubert       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454f0574f5cSXin LI       set -f &&
4552b15cb3dSCy Schubert       set X $old && old=:$2:$4:$5:$6 &&
4562b15cb3dSCy Schubert       set X $new && new=:$2:$4:$5:$6 &&
457f0574f5cSXin LI       set +f &&
4582b15cb3dSCy Schubert       test "$old" = "$new" &&
4592b15cb3dSCy Schubert       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4602b15cb3dSCy Schubert    then
4612b15cb3dSCy Schubert      rm -f "$dsttmp"
4622b15cb3dSCy Schubert    else
4632b15cb3dSCy Schubert      # Rename the file to the real destination.
4642b15cb3dSCy Schubert      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4652b15cb3dSCy Schubert
4662b15cb3dSCy Schubert      # The rename failed, perhaps because mv can't rename something else
4672b15cb3dSCy Schubert      # to itself, or perhaps because mv is so ancient that it does not
4682b15cb3dSCy Schubert      # support -f.
4692b15cb3dSCy Schubert      {
4702b15cb3dSCy Schubert        # Now remove or move aside any old file at destination location.
4712b15cb3dSCy Schubert        # We try this two ways since rm can't unlink itself on some
4722b15cb3dSCy Schubert        # systems and the destination file might be busy for other
4732b15cb3dSCy Schubert        # reasons.  In this case, the final cleanup might fail but the new
4742b15cb3dSCy Schubert        # file should still install successfully.
4752b15cb3dSCy Schubert        {
4762b15cb3dSCy Schubert          test ! -f "$dst" ||
4772b15cb3dSCy Schubert          $doit $rmcmd -f "$dst" 2>/dev/null ||
4782b15cb3dSCy Schubert          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4792b15cb3dSCy Schubert            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4802b15cb3dSCy Schubert          } ||
4812b15cb3dSCy Schubert          { echo "$0: cannot unlink or rename $dst" >&2
4822b15cb3dSCy Schubert            (exit 1); exit 1
4832b15cb3dSCy Schubert          }
4842b15cb3dSCy Schubert        } &&
4852b15cb3dSCy Schubert
4862b15cb3dSCy Schubert        # Now rename the file to the real destination.
4872b15cb3dSCy Schubert        $doit $mvcmd "$dsttmp" "$dst"
4882b15cb3dSCy Schubert      }
4892b15cb3dSCy Schubert    fi || exit 1
4902b15cb3dSCy Schubert
4912b15cb3dSCy Schubert    trap '' 0
4922b15cb3dSCy Schubert  fi
4932b15cb3dSCy Schubertdone
4942b15cb3dSCy Schubert
4952b15cb3dSCy Schubert# Local variables:
4962b15cb3dSCy Schubert# eval: (add-hook 'write-file-hooks 'time-stamp)
4972b15cb3dSCy Schubert# time-stamp-start: "scriptversion="
4982b15cb3dSCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
499*2d4e511cSCy Schubert# time-stamp-time-zone: "UTC"
5002b15cb3dSCy Schubert# time-stamp-end: "; # UTC"
5012b15cb3dSCy Schubert# End:
502