xref: /freebsd/contrib/ldns/install-sh (revision 5afab0e5e56fe90a378fb57249600e7924e1cab2)
17b5038d7SDag-Erling Smørgrav#!/bin/sh
27b5038d7SDag-Erling Smørgrav# install - install a program, script, or datafile
37b5038d7SDag-Erling Smørgrav
4*5afab0e5SDag-Erling Smørgravscriptversion=2020-11-14.01; # UTC
57b5038d7SDag-Erling Smørgrav
67b5038d7SDag-Erling Smørgrav# This originates from X11R5 (mit/util/scripts/install.sh), which was
77b5038d7SDag-Erling Smørgrav# later released in X11R6 (xc/config/util/install.sh) with the
87b5038d7SDag-Erling Smørgrav# following copyright and license.
97b5038d7SDag-Erling Smørgrav#
107b5038d7SDag-Erling Smørgrav# Copyright (C) 1994 X Consortium
117b5038d7SDag-Erling Smørgrav#
127b5038d7SDag-Erling Smørgrav# Permission is hereby granted, free of charge, to any person obtaining a copy
137b5038d7SDag-Erling Smørgrav# of this software and associated documentation files (the "Software"), to
147b5038d7SDag-Erling Smørgrav# deal in the Software without restriction, including without limitation the
157b5038d7SDag-Erling Smørgrav# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167b5038d7SDag-Erling Smørgrav# sell copies of the Software, and to permit persons to whom the Software is
177b5038d7SDag-Erling Smørgrav# furnished to do so, subject to the following conditions:
187b5038d7SDag-Erling Smørgrav#
197b5038d7SDag-Erling Smørgrav# The above copyright notice and this permission notice shall be included in
207b5038d7SDag-Erling Smørgrav# all copies or substantial portions of the Software.
217b5038d7SDag-Erling Smørgrav#
227b5038d7SDag-Erling Smørgrav# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237b5038d7SDag-Erling Smørgrav# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247b5038d7SDag-Erling Smørgrav# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257b5038d7SDag-Erling Smørgrav# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267b5038d7SDag-Erling Smørgrav# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277b5038d7SDag-Erling Smørgrav# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287b5038d7SDag-Erling Smørgrav#
297b5038d7SDag-Erling Smørgrav# Except as contained in this notice, the name of the X Consortium shall not
307b5038d7SDag-Erling Smørgrav# be used in advertising or otherwise to promote the sale, use or other deal-
317b5038d7SDag-Erling Smørgrav# ings in this Software without prior written authorization from the X Consor-
327b5038d7SDag-Erling Smørgrav# tium.
337b5038d7SDag-Erling Smørgrav#
347b5038d7SDag-Erling Smørgrav#
357b5038d7SDag-Erling Smørgrav# FSF changes to this file are in the public domain.
367b5038d7SDag-Erling Smørgrav#
377b5038d7SDag-Erling Smørgrav# Calling this script install-sh is preferred over install.sh, to prevent
3817d15b25SDag-Erling Smørgrav# 'make' implicit rules from creating a file called install from it
397b5038d7SDag-Erling Smørgrav# when there is no Makefile.
407b5038d7SDag-Erling Smørgrav#
417b5038d7SDag-Erling Smørgrav# This script is compatible with the BSD install script, but was written
427b5038d7SDag-Erling Smørgrav# from scratch.
437b5038d7SDag-Erling Smørgrav
44986ba33cSDag-Erling Smørgravtab='	'
457b5038d7SDag-Erling Smørgravnl='
467b5038d7SDag-Erling Smørgrav'
47986ba33cSDag-Erling SmørgravIFS=" $tab$nl"
487b5038d7SDag-Erling Smørgrav
49986ba33cSDag-Erling Smørgrav# Set DOITPROG to "echo" to test this script.
507b5038d7SDag-Erling Smørgrav
517b5038d7SDag-Erling Smørgravdoit=${DOITPROG-}
52986ba33cSDag-Erling Smørgravdoit_exec=${doit:-exec}
537b5038d7SDag-Erling Smørgrav
547b5038d7SDag-Erling Smørgrav# Put in absolute file names if you don't have them in your path;
557b5038d7SDag-Erling Smørgrav# or use environment vars.
567b5038d7SDag-Erling Smørgrav
577b5038d7SDag-Erling Smørgravchgrpprog=${CHGRPPROG-chgrp}
587b5038d7SDag-Erling Smørgravchmodprog=${CHMODPROG-chmod}
597b5038d7SDag-Erling Smørgravchownprog=${CHOWNPROG-chown}
607b5038d7SDag-Erling Smørgravcmpprog=${CMPPROG-cmp}
617b5038d7SDag-Erling Smørgravcpprog=${CPPROG-cp}
627b5038d7SDag-Erling Smørgravmkdirprog=${MKDIRPROG-mkdir}
637b5038d7SDag-Erling Smørgravmvprog=${MVPROG-mv}
647b5038d7SDag-Erling Smørgravrmprog=${RMPROG-rm}
657b5038d7SDag-Erling Smørgravstripprog=${STRIPPROG-strip}
667b5038d7SDag-Erling Smørgrav
677b5038d7SDag-Erling Smørgravposix_mkdir=
687b5038d7SDag-Erling Smørgrav
697b5038d7SDag-Erling Smørgrav# Desired mode of installed file.
707b5038d7SDag-Erling Smørgravmode=0755
717b5038d7SDag-Erling Smørgrav
72*5afab0e5SDag-Erling Smørgrav# Create dirs (including intermediate dirs) using mode 755.
73*5afab0e5SDag-Erling Smørgrav# This is like GNU 'install' as of coreutils 8.32 (2020).
74*5afab0e5SDag-Erling Smørgravmkdir_umask=22
75*5afab0e5SDag-Erling Smørgrav
76*5afab0e5SDag-Erling Smørgravbackupsuffix=
777b5038d7SDag-Erling Smørgravchgrpcmd=
787b5038d7SDag-Erling Smørgravchmodcmd=$chmodprog
797b5038d7SDag-Erling Smørgravchowncmd=
807b5038d7SDag-Erling Smørgravmvcmd=$mvprog
817b5038d7SDag-Erling Smørgravrmcmd="$rmprog -f"
827b5038d7SDag-Erling Smørgravstripcmd=
837b5038d7SDag-Erling Smørgrav
847b5038d7SDag-Erling Smørgravsrc=
857b5038d7SDag-Erling Smørgravdst=
867b5038d7SDag-Erling Smørgravdir_arg=
877b5038d7SDag-Erling Smørgravdst_arg=
887b5038d7SDag-Erling Smørgrav
897b5038d7SDag-Erling Smørgravcopy_on_change=false
90986ba33cSDag-Erling Smørgravis_target_a_directory=possibly
917b5038d7SDag-Erling Smørgrav
927b5038d7SDag-Erling Smørgravusage="\
937b5038d7SDag-Erling SmørgravUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
947b5038d7SDag-Erling Smørgrav   or: $0 [OPTION]... SRCFILES... DIRECTORY
957b5038d7SDag-Erling Smørgrav   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
967b5038d7SDag-Erling Smørgrav   or: $0 [OPTION]... -d DIRECTORIES...
977b5038d7SDag-Erling Smørgrav
987b5038d7SDag-Erling SmørgravIn the 1st form, copy SRCFILE to DSTFILE.
997b5038d7SDag-Erling SmørgravIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1007b5038d7SDag-Erling SmørgravIn the 4th, create DIRECTORIES.
1017b5038d7SDag-Erling Smørgrav
1027b5038d7SDag-Erling SmørgravOptions:
1037b5038d7SDag-Erling Smørgrav     --help     display this help and exit.
1047b5038d7SDag-Erling Smørgrav     --version  display version info and exit.
1057b5038d7SDag-Erling Smørgrav
1067b5038d7SDag-Erling Smørgrav  -c            (ignored)
107*5afab0e5SDag-Erling Smørgrav  -C            install only if different (preserve data modification time)
1087b5038d7SDag-Erling Smørgrav  -d            create directories instead of installing files.
1097b5038d7SDag-Erling Smørgrav  -g GROUP      $chgrpprog installed files to GROUP.
1107b5038d7SDag-Erling Smørgrav  -m MODE       $chmodprog installed files to MODE.
1117b5038d7SDag-Erling Smørgrav  -o USER       $chownprog installed files to USER.
112*5afab0e5SDag-Erling Smørgrav  -p            pass -p to $cpprog.
1137b5038d7SDag-Erling Smørgrav  -s            $stripprog installed files.
114*5afab0e5SDag-Erling Smørgrav  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1157b5038d7SDag-Erling Smørgrav  -t DIRECTORY  install into DIRECTORY.
1167b5038d7SDag-Erling Smørgrav  -T            report an error if DSTFILE is a directory.
1177b5038d7SDag-Erling Smørgrav
1187b5038d7SDag-Erling SmørgravEnvironment variables override the default commands:
1197b5038d7SDag-Erling Smørgrav  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1207b5038d7SDag-Erling Smørgrav  RMPROG STRIPPROG
121*5afab0e5SDag-Erling Smørgrav
122*5afab0e5SDag-Erling SmørgravBy default, rm is invoked with -f; when overridden with RMPROG,
123*5afab0e5SDag-Erling Smørgravit's up to you to specify -f if you want it.
124*5afab0e5SDag-Erling Smørgrav
125*5afab0e5SDag-Erling SmørgravIf -S is not specified, no backups are attempted.
126*5afab0e5SDag-Erling Smørgrav
127*5afab0e5SDag-Erling SmørgravEmail bug reports to bug-automake@gnu.org.
128*5afab0e5SDag-Erling SmørgravAutomake home page: https://www.gnu.org/software/automake/
1297b5038d7SDag-Erling Smørgrav"
1307b5038d7SDag-Erling Smørgrav
1317b5038d7SDag-Erling Smørgravwhile test $# -ne 0; do
1327b5038d7SDag-Erling Smørgrav  case $1 in
1337b5038d7SDag-Erling Smørgrav    -c) ;;
1347b5038d7SDag-Erling Smørgrav
1357b5038d7SDag-Erling Smørgrav    -C) copy_on_change=true;;
1367b5038d7SDag-Erling Smørgrav
1377b5038d7SDag-Erling Smørgrav    -d) dir_arg=true;;
1387b5038d7SDag-Erling Smørgrav
1397b5038d7SDag-Erling Smørgrav    -g) chgrpcmd="$chgrpprog $2"
1407b5038d7SDag-Erling Smørgrav        shift;;
1417b5038d7SDag-Erling Smørgrav
1427b5038d7SDag-Erling Smørgrav    --help) echo "$usage"; exit $?;;
1437b5038d7SDag-Erling Smørgrav
1447b5038d7SDag-Erling Smørgrav    -m) mode=$2
1457b5038d7SDag-Erling Smørgrav        case $mode in
146986ba33cSDag-Erling Smørgrav          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1477b5038d7SDag-Erling Smørgrav            echo "$0: invalid mode: $mode" >&2
1487b5038d7SDag-Erling Smørgrav            exit 1;;
1497b5038d7SDag-Erling Smørgrav        esac
1507b5038d7SDag-Erling Smørgrav        shift;;
1517b5038d7SDag-Erling Smørgrav
1527b5038d7SDag-Erling Smørgrav    -o) chowncmd="$chownprog $2"
1537b5038d7SDag-Erling Smørgrav        shift;;
1547b5038d7SDag-Erling Smørgrav
155*5afab0e5SDag-Erling Smørgrav    -p) cpprog="$cpprog -p";;
156*5afab0e5SDag-Erling Smørgrav
1577b5038d7SDag-Erling Smørgrav    -s) stripcmd=$stripprog;;
1587b5038d7SDag-Erling Smørgrav
159*5afab0e5SDag-Erling Smørgrav    -S) backupsuffix="$2"
160*5afab0e5SDag-Erling Smørgrav        shift;;
161*5afab0e5SDag-Erling Smørgrav
162986ba33cSDag-Erling Smørgrav    -t)
163986ba33cSDag-Erling Smørgrav        is_target_a_directory=always
164986ba33cSDag-Erling Smørgrav        dst_arg=$2
16517d15b25SDag-Erling Smørgrav        # Protect names problematic for 'test' and other utilities.
1667b5038d7SDag-Erling Smørgrav        case $dst_arg in
1677b5038d7SDag-Erling Smørgrav          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1687b5038d7SDag-Erling Smørgrav        esac
1697b5038d7SDag-Erling Smørgrav        shift;;
1707b5038d7SDag-Erling Smørgrav
171986ba33cSDag-Erling Smørgrav    -T) is_target_a_directory=never;;
1727b5038d7SDag-Erling Smørgrav
1737b5038d7SDag-Erling Smørgrav    --version) echo "$0 $scriptversion"; exit $?;;
1747b5038d7SDag-Erling Smørgrav
1757b5038d7SDag-Erling Smørgrav    --) shift
1767b5038d7SDag-Erling Smørgrav        break;;
1777b5038d7SDag-Erling Smørgrav
1787b5038d7SDag-Erling Smørgrav    -*) echo "$0: invalid option: $1" >&2
1797b5038d7SDag-Erling Smørgrav        exit 1;;
1807b5038d7SDag-Erling Smørgrav
1817b5038d7SDag-Erling Smørgrav    *)  break;;
1827b5038d7SDag-Erling Smørgrav  esac
1837b5038d7SDag-Erling Smørgrav  shift
1847b5038d7SDag-Erling Smørgravdone
1857b5038d7SDag-Erling Smørgrav
186986ba33cSDag-Erling Smørgrav# We allow the use of options -d and -T together, by making -d
187986ba33cSDag-Erling Smørgrav# take the precedence; this is for compatibility with GNU install.
188986ba33cSDag-Erling Smørgrav
189986ba33cSDag-Erling Smørgravif test -n "$dir_arg"; then
190986ba33cSDag-Erling Smørgrav  if test -n "$dst_arg"; then
191986ba33cSDag-Erling Smørgrav    echo "$0: target directory not allowed when installing a directory." >&2
192986ba33cSDag-Erling Smørgrav    exit 1
193986ba33cSDag-Erling Smørgrav  fi
194986ba33cSDag-Erling Smørgravfi
195986ba33cSDag-Erling Smørgrav
1967b5038d7SDag-Erling Smørgravif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1977b5038d7SDag-Erling Smørgrav  # When -d is used, all remaining arguments are directories to create.
1987b5038d7SDag-Erling Smørgrav  # When -t is used, the destination is already specified.
1997b5038d7SDag-Erling Smørgrav  # Otherwise, the last argument is the destination.  Remove it from $@.
2007b5038d7SDag-Erling Smørgrav  for arg
2017b5038d7SDag-Erling Smørgrav  do
2027b5038d7SDag-Erling Smørgrav    if test -n "$dst_arg"; then
2037b5038d7SDag-Erling Smørgrav      # $@ is not empty: it contains at least $arg.
2047b5038d7SDag-Erling Smørgrav      set fnord "$@" "$dst_arg"
2057b5038d7SDag-Erling Smørgrav      shift # fnord
2067b5038d7SDag-Erling Smørgrav    fi
2077b5038d7SDag-Erling Smørgrav    shift # arg
2087b5038d7SDag-Erling Smørgrav    dst_arg=$arg
20917d15b25SDag-Erling Smørgrav    # Protect names problematic for 'test' and other utilities.
2107b5038d7SDag-Erling Smørgrav    case $dst_arg in
2117b5038d7SDag-Erling Smørgrav      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2127b5038d7SDag-Erling Smørgrav    esac
2137b5038d7SDag-Erling Smørgrav  done
2147b5038d7SDag-Erling Smørgravfi
2157b5038d7SDag-Erling Smørgrav
2167b5038d7SDag-Erling Smørgravif test $# -eq 0; then
2177b5038d7SDag-Erling Smørgrav  if test -z "$dir_arg"; then
2187b5038d7SDag-Erling Smørgrav    echo "$0: no input file specified." >&2
2197b5038d7SDag-Erling Smørgrav    exit 1
2207b5038d7SDag-Erling Smørgrav  fi
22117d15b25SDag-Erling Smørgrav  # It's OK to call 'install-sh -d' without argument.
2227b5038d7SDag-Erling Smørgrav  # This can happen when creating conditional directories.
2237b5038d7SDag-Erling Smørgrav  exit 0
2247b5038d7SDag-Erling Smørgravfi
2257b5038d7SDag-Erling Smørgrav
2267b5038d7SDag-Erling Smørgravif test -z "$dir_arg"; then
227986ba33cSDag-Erling Smørgrav  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228986ba33cSDag-Erling Smørgrav    if test ! -d "$dst_arg"; then
229986ba33cSDag-Erling Smørgrav      echo "$0: $dst_arg: Is not a directory." >&2
230986ba33cSDag-Erling Smørgrav      exit 1
231986ba33cSDag-Erling Smørgrav    fi
232986ba33cSDag-Erling Smørgrav  fi
233986ba33cSDag-Erling Smørgravfi
234986ba33cSDag-Erling Smørgrav
235986ba33cSDag-Erling Smørgravif test -z "$dir_arg"; then
2367b5038d7SDag-Erling Smørgrav  do_exit='(exit $ret); exit $ret'
2377b5038d7SDag-Erling Smørgrav  trap "ret=129; $do_exit" 1
2387b5038d7SDag-Erling Smørgrav  trap "ret=130; $do_exit" 2
2397b5038d7SDag-Erling Smørgrav  trap "ret=141; $do_exit" 13
2407b5038d7SDag-Erling Smørgrav  trap "ret=143; $do_exit" 15
2417b5038d7SDag-Erling Smørgrav
2427b5038d7SDag-Erling Smørgrav  # Set umask so as not to create temps with too-generous modes.
2437b5038d7SDag-Erling Smørgrav  # However, 'strip' requires both read and write access to temps.
2447b5038d7SDag-Erling Smørgrav  case $mode in
2457b5038d7SDag-Erling Smørgrav    # Optimize common cases.
2467b5038d7SDag-Erling Smørgrav    *644) cp_umask=133;;
2477b5038d7SDag-Erling Smørgrav    *755) cp_umask=22;;
2487b5038d7SDag-Erling Smørgrav
2497b5038d7SDag-Erling Smørgrav    *[0-7])
2507b5038d7SDag-Erling Smørgrav      if test -z "$stripcmd"; then
2517b5038d7SDag-Erling Smørgrav        u_plus_rw=
2527b5038d7SDag-Erling Smørgrav      else
2537b5038d7SDag-Erling Smørgrav        u_plus_rw='% 200'
2547b5038d7SDag-Erling Smørgrav      fi
2557b5038d7SDag-Erling Smørgrav      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2567b5038d7SDag-Erling Smørgrav    *)
2577b5038d7SDag-Erling Smørgrav      if test -z "$stripcmd"; then
2587b5038d7SDag-Erling Smørgrav        u_plus_rw=
2597b5038d7SDag-Erling Smørgrav      else
2607b5038d7SDag-Erling Smørgrav        u_plus_rw=,u+rw
2617b5038d7SDag-Erling Smørgrav      fi
2627b5038d7SDag-Erling Smørgrav      cp_umask=$mode$u_plus_rw;;
2637b5038d7SDag-Erling Smørgrav  esac
2647b5038d7SDag-Erling Smørgravfi
2657b5038d7SDag-Erling Smørgrav
2667b5038d7SDag-Erling Smørgravfor src
2677b5038d7SDag-Erling Smørgravdo
26817d15b25SDag-Erling Smørgrav  # Protect names problematic for 'test' and other utilities.
2697b5038d7SDag-Erling Smørgrav  case $src in
2707b5038d7SDag-Erling Smørgrav    -* | [=\(\)!]) src=./$src;;
2717b5038d7SDag-Erling Smørgrav  esac
2727b5038d7SDag-Erling Smørgrav
2737b5038d7SDag-Erling Smørgrav  if test -n "$dir_arg"; then
2747b5038d7SDag-Erling Smørgrav    dst=$src
2757b5038d7SDag-Erling Smørgrav    dstdir=$dst
2767b5038d7SDag-Erling Smørgrav    test -d "$dstdir"
2777b5038d7SDag-Erling Smørgrav    dstdir_status=$?
278*5afab0e5SDag-Erling Smørgrav    # Don't chown directories that already exist.
279*5afab0e5SDag-Erling Smørgrav    if test $dstdir_status = 0; then
280*5afab0e5SDag-Erling Smørgrav      chowncmd=""
281*5afab0e5SDag-Erling Smørgrav    fi
2827b5038d7SDag-Erling Smørgrav  else
2837b5038d7SDag-Erling Smørgrav
2847b5038d7SDag-Erling Smørgrav    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2857b5038d7SDag-Erling Smørgrav    # might cause directories to be created, which would be especially bad
2867b5038d7SDag-Erling Smørgrav    # if $src (and thus $dsttmp) contains '*'.
2877b5038d7SDag-Erling Smørgrav    if test ! -f "$src" && test ! -d "$src"; then
2887b5038d7SDag-Erling Smørgrav      echo "$0: $src does not exist." >&2
2897b5038d7SDag-Erling Smørgrav      exit 1
2907b5038d7SDag-Erling Smørgrav    fi
2917b5038d7SDag-Erling Smørgrav
2927b5038d7SDag-Erling Smørgrav    if test -z "$dst_arg"; then
2937b5038d7SDag-Erling Smørgrav      echo "$0: no destination specified." >&2
2947b5038d7SDag-Erling Smørgrav      exit 1
2957b5038d7SDag-Erling Smørgrav    fi
2967b5038d7SDag-Erling Smørgrav    dst=$dst_arg
2977b5038d7SDag-Erling Smørgrav
298*5afab0e5SDag-Erling Smørgrav    # If destination is a directory, append the input filename.
2997b5038d7SDag-Erling Smørgrav    if test -d "$dst"; then
300986ba33cSDag-Erling Smørgrav      if test "$is_target_a_directory" = never; then
3017b5038d7SDag-Erling Smørgrav        echo "$0: $dst_arg: Is a directory" >&2
3027b5038d7SDag-Erling Smørgrav        exit 1
3037b5038d7SDag-Erling Smørgrav      fi
3047b5038d7SDag-Erling Smørgrav      dstdir=$dst
305*5afab0e5SDag-Erling Smørgrav      dstbase=`basename "$src"`
306*5afab0e5SDag-Erling Smørgrav      case $dst in
307*5afab0e5SDag-Erling Smørgrav	*/) dst=$dst$dstbase;;
308*5afab0e5SDag-Erling Smørgrav	*)  dst=$dst/$dstbase;;
309*5afab0e5SDag-Erling Smørgrav      esac
3107b5038d7SDag-Erling Smørgrav      dstdir_status=0
3117b5038d7SDag-Erling Smørgrav    else
312986ba33cSDag-Erling Smørgrav      dstdir=`dirname "$dst"`
3137b5038d7SDag-Erling Smørgrav      test -d "$dstdir"
3147b5038d7SDag-Erling Smørgrav      dstdir_status=$?
3157b5038d7SDag-Erling Smørgrav    fi
3167b5038d7SDag-Erling Smørgrav  fi
3177b5038d7SDag-Erling Smørgrav
318*5afab0e5SDag-Erling Smørgrav  case $dstdir in
319*5afab0e5SDag-Erling Smørgrav    */) dstdirslash=$dstdir;;
320*5afab0e5SDag-Erling Smørgrav    *)  dstdirslash=$dstdir/;;
321*5afab0e5SDag-Erling Smørgrav  esac
322*5afab0e5SDag-Erling Smørgrav
3237b5038d7SDag-Erling Smørgrav  obsolete_mkdir_used=false
3247b5038d7SDag-Erling Smørgrav
3257b5038d7SDag-Erling Smørgrav  if test $dstdir_status != 0; then
3267b5038d7SDag-Erling Smørgrav    case $posix_mkdir in
3277b5038d7SDag-Erling Smørgrav      '')
3287b5038d7SDag-Erling Smørgrav        # With -d, create the new directory with the user-specified mode.
3297b5038d7SDag-Erling Smørgrav        # Otherwise, rely on $mkdir_umask.
3307b5038d7SDag-Erling Smørgrav        if test -n "$dir_arg"; then
3317b5038d7SDag-Erling Smørgrav          mkdir_mode=-m$mode
3327b5038d7SDag-Erling Smørgrav        else
3337b5038d7SDag-Erling Smørgrav          mkdir_mode=
3347b5038d7SDag-Erling Smørgrav        fi
3357b5038d7SDag-Erling Smørgrav
3367b5038d7SDag-Erling Smørgrav        posix_mkdir=false
337*5afab0e5SDag-Erling Smørgrav	# The $RANDOM variable is not portable (e.g., dash).  Use it
338*5afab0e5SDag-Erling Smørgrav	# here however when possible just to lower collision chance.
3397b5038d7SDag-Erling Smørgrav	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3407b5038d7SDag-Erling Smørgrav
341*5afab0e5SDag-Erling Smørgrav	trap '
342*5afab0e5SDag-Erling Smørgrav	  ret=$?
343*5afab0e5SDag-Erling Smørgrav	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344*5afab0e5SDag-Erling Smørgrav	  exit $ret
345*5afab0e5SDag-Erling Smørgrav	' 0
346*5afab0e5SDag-Erling Smørgrav
347*5afab0e5SDag-Erling Smørgrav	# Because "mkdir -p" follows existing symlinks and we likely work
348*5afab0e5SDag-Erling Smørgrav	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349*5afab0e5SDag-Erling Smørgrav	# directory is successfully created first before we actually test
350*5afab0e5SDag-Erling Smørgrav	# 'mkdir -p'.
3517b5038d7SDag-Erling Smørgrav	if (umask $mkdir_umask &&
352986ba33cSDag-Erling Smørgrav	    $mkdirprog $mkdir_mode "$tmpdir" &&
353986ba33cSDag-Erling Smørgrav	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3547b5038d7SDag-Erling Smørgrav	then
3557b5038d7SDag-Erling Smørgrav	  if test -z "$dir_arg" || {
3567b5038d7SDag-Erling Smørgrav	       # Check for POSIX incompatibilities with -m.
3577b5038d7SDag-Erling Smørgrav	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35817d15b25SDag-Erling Smørgrav	       # other-writable bit of parent directory when it shouldn't.
3597b5038d7SDag-Erling Smørgrav	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360986ba33cSDag-Erling Smørgrav	       test_tmpdir="$tmpdir/a"
361986ba33cSDag-Erling Smørgrav	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3627b5038d7SDag-Erling Smørgrav	       case $ls_ld_tmpdir in
3637b5038d7SDag-Erling Smørgrav		 d????-?r-*) different_mode=700;;
3647b5038d7SDag-Erling Smørgrav		 d????-?--*) different_mode=755;;
3657b5038d7SDag-Erling Smørgrav		 *) false;;
3667b5038d7SDag-Erling Smørgrav	       esac &&
367986ba33cSDag-Erling Smørgrav	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368986ba33cSDag-Erling Smørgrav		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3697b5038d7SDag-Erling Smørgrav		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3707b5038d7SDag-Erling Smørgrav	       }
3717b5038d7SDag-Erling Smørgrav	     }
3727b5038d7SDag-Erling Smørgrav	  then posix_mkdir=:
3737b5038d7SDag-Erling Smørgrav	  fi
374986ba33cSDag-Erling Smørgrav	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3757b5038d7SDag-Erling Smørgrav	else
3767b5038d7SDag-Erling Smørgrav	  # Remove any dirs left behind by ancient mkdir implementations.
377986ba33cSDag-Erling Smørgrav	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3787b5038d7SDag-Erling Smørgrav	fi
3797b5038d7SDag-Erling Smørgrav	trap '' 0;;
3807b5038d7SDag-Erling Smørgrav    esac
3817b5038d7SDag-Erling Smørgrav
3827b5038d7SDag-Erling Smørgrav    if
3837b5038d7SDag-Erling Smørgrav      $posix_mkdir && (
3847b5038d7SDag-Erling Smørgrav        umask $mkdir_umask &&
3857b5038d7SDag-Erling Smørgrav        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3867b5038d7SDag-Erling Smørgrav      )
3877b5038d7SDag-Erling Smørgrav    then :
3887b5038d7SDag-Erling Smørgrav    else
3897b5038d7SDag-Erling Smørgrav
390*5afab0e5SDag-Erling Smørgrav      # mkdir does not conform to POSIX,
3917b5038d7SDag-Erling Smørgrav      # or it failed possibly due to a race condition.  Create the
3927b5038d7SDag-Erling Smørgrav      # directory the slow way, step by step, checking for races as we go.
3937b5038d7SDag-Erling Smørgrav
3947b5038d7SDag-Erling Smørgrav      case $dstdir in
3957b5038d7SDag-Erling Smørgrav        /*) prefix='/';;
3967b5038d7SDag-Erling Smørgrav        [-=\(\)!]*) prefix='./';;
3977b5038d7SDag-Erling Smørgrav        *)  prefix='';;
3987b5038d7SDag-Erling Smørgrav      esac
3997b5038d7SDag-Erling Smørgrav
4007b5038d7SDag-Erling Smørgrav      oIFS=$IFS
4017b5038d7SDag-Erling Smørgrav      IFS=/
402986ba33cSDag-Erling Smørgrav      set -f
4037b5038d7SDag-Erling Smørgrav      set fnord $dstdir
4047b5038d7SDag-Erling Smørgrav      shift
405986ba33cSDag-Erling Smørgrav      set +f
4067b5038d7SDag-Erling Smørgrav      IFS=$oIFS
4077b5038d7SDag-Erling Smørgrav
4087b5038d7SDag-Erling Smørgrav      prefixes=
4097b5038d7SDag-Erling Smørgrav
4107b5038d7SDag-Erling Smørgrav      for d
4117b5038d7SDag-Erling Smørgrav      do
4127b5038d7SDag-Erling Smørgrav        test X"$d" = X && continue
4137b5038d7SDag-Erling Smørgrav
4147b5038d7SDag-Erling Smørgrav        prefix=$prefix$d
4157b5038d7SDag-Erling Smørgrav        if test -d "$prefix"; then
4167b5038d7SDag-Erling Smørgrav          prefixes=
4177b5038d7SDag-Erling Smørgrav        else
4187b5038d7SDag-Erling Smørgrav          if $posix_mkdir; then
419*5afab0e5SDag-Erling Smørgrav            (umask $mkdir_umask &&
4207b5038d7SDag-Erling Smørgrav             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4217b5038d7SDag-Erling Smørgrav            # Don't fail if two instances are running concurrently.
4227b5038d7SDag-Erling Smørgrav            test -d "$prefix" || exit 1
4237b5038d7SDag-Erling Smørgrav          else
4247b5038d7SDag-Erling Smørgrav            case $prefix in
4257b5038d7SDag-Erling Smørgrav              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4267b5038d7SDag-Erling Smørgrav              *) qprefix=$prefix;;
4277b5038d7SDag-Erling Smørgrav            esac
4287b5038d7SDag-Erling Smørgrav            prefixes="$prefixes '$qprefix'"
4297b5038d7SDag-Erling Smørgrav          fi
4307b5038d7SDag-Erling Smørgrav        fi
4317b5038d7SDag-Erling Smørgrav        prefix=$prefix/
4327b5038d7SDag-Erling Smørgrav      done
4337b5038d7SDag-Erling Smørgrav
4347b5038d7SDag-Erling Smørgrav      if test -n "$prefixes"; then
4357b5038d7SDag-Erling Smørgrav        # Don't fail if two instances are running concurrently.
4367b5038d7SDag-Erling Smørgrav        (umask $mkdir_umask &&
4377b5038d7SDag-Erling Smørgrav         eval "\$doit_exec \$mkdirprog $prefixes") ||
4387b5038d7SDag-Erling Smørgrav          test -d "$dstdir" || exit 1
4397b5038d7SDag-Erling Smørgrav        obsolete_mkdir_used=true
4407b5038d7SDag-Erling Smørgrav      fi
4417b5038d7SDag-Erling Smørgrav    fi
4427b5038d7SDag-Erling Smørgrav  fi
4437b5038d7SDag-Erling Smørgrav
4447b5038d7SDag-Erling Smørgrav  if test -n "$dir_arg"; then
4457b5038d7SDag-Erling Smørgrav    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4467b5038d7SDag-Erling Smørgrav    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4477b5038d7SDag-Erling Smørgrav    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4487b5038d7SDag-Erling Smørgrav      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4497b5038d7SDag-Erling Smørgrav  else
4507b5038d7SDag-Erling Smørgrav
4517b5038d7SDag-Erling Smørgrav    # Make a couple of temp file names in the proper directory.
452*5afab0e5SDag-Erling Smørgrav    dsttmp=${dstdirslash}_inst.$$_
453*5afab0e5SDag-Erling Smørgrav    rmtmp=${dstdirslash}_rm.$$_
4547b5038d7SDag-Erling Smørgrav
4557b5038d7SDag-Erling Smørgrav    # Trap to clean up those temp files at exit.
4567b5038d7SDag-Erling Smørgrav    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4577b5038d7SDag-Erling Smørgrav
4587b5038d7SDag-Erling Smørgrav    # Copy the file name to the temp name.
459*5afab0e5SDag-Erling Smørgrav    (umask $cp_umask &&
460*5afab0e5SDag-Erling Smørgrav     { test -z "$stripcmd" || {
461*5afab0e5SDag-Erling Smørgrav	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462*5afab0e5SDag-Erling Smørgrav	 # which would cause strip to fail.
463*5afab0e5SDag-Erling Smørgrav	 if test -z "$doit"; then
464*5afab0e5SDag-Erling Smørgrav	   : >"$dsttmp" # No need to fork-exec 'touch'.
465*5afab0e5SDag-Erling Smørgrav	 else
466*5afab0e5SDag-Erling Smørgrav	   $doit touch "$dsttmp"
467*5afab0e5SDag-Erling Smørgrav	 fi
468*5afab0e5SDag-Erling Smørgrav       }
469*5afab0e5SDag-Erling Smørgrav     } &&
470*5afab0e5SDag-Erling Smørgrav     $doit_exec $cpprog "$src" "$dsttmp") &&
4717b5038d7SDag-Erling Smørgrav
4727b5038d7SDag-Erling Smørgrav    # and set any options; do chmod last to preserve setuid bits.
4737b5038d7SDag-Erling Smørgrav    #
4747b5038d7SDag-Erling Smørgrav    # If any of these fail, we abort the whole thing.  If we want to
4757b5038d7SDag-Erling Smørgrav    # ignore errors from any of these, just make sure not to ignore
4767b5038d7SDag-Erling Smørgrav    # errors from the above "$doit $cpprog $src $dsttmp" command.
4777b5038d7SDag-Erling Smørgrav    #
4787b5038d7SDag-Erling Smørgrav    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4797b5038d7SDag-Erling Smørgrav    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4807b5038d7SDag-Erling Smørgrav    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4817b5038d7SDag-Erling Smørgrav    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4827b5038d7SDag-Erling Smørgrav
4837b5038d7SDag-Erling Smørgrav    # If -C, don't bother to copy if it wouldn't change the file.
4847b5038d7SDag-Erling Smørgrav    if $copy_on_change &&
4857b5038d7SDag-Erling Smørgrav       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4867b5038d7SDag-Erling Smørgrav       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487986ba33cSDag-Erling Smørgrav       set -f &&
4887b5038d7SDag-Erling Smørgrav       set X $old && old=:$2:$4:$5:$6 &&
4897b5038d7SDag-Erling Smørgrav       set X $new && new=:$2:$4:$5:$6 &&
490986ba33cSDag-Erling Smørgrav       set +f &&
4917b5038d7SDag-Erling Smørgrav       test "$old" = "$new" &&
4927b5038d7SDag-Erling Smørgrav       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4937b5038d7SDag-Erling Smørgrav    then
4947b5038d7SDag-Erling Smørgrav      rm -f "$dsttmp"
4957b5038d7SDag-Erling Smørgrav    else
496*5afab0e5SDag-Erling Smørgrav      # If $backupsuffix is set, and the file being installed
497*5afab0e5SDag-Erling Smørgrav      # already exists, attempt a backup.  Don't worry if it fails,
498*5afab0e5SDag-Erling Smørgrav      # e.g., if mv doesn't support -f.
499*5afab0e5SDag-Erling Smørgrav      if test -n "$backupsuffix" && test -f "$dst"; then
500*5afab0e5SDag-Erling Smørgrav        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501*5afab0e5SDag-Erling Smørgrav      fi
502*5afab0e5SDag-Erling Smørgrav
5037b5038d7SDag-Erling Smørgrav      # Rename the file to the real destination.
5047b5038d7SDag-Erling Smørgrav      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5057b5038d7SDag-Erling Smørgrav
5067b5038d7SDag-Erling Smørgrav      # The rename failed, perhaps because mv can't rename something else
5077b5038d7SDag-Erling Smørgrav      # to itself, or perhaps because mv is so ancient that it does not
5087b5038d7SDag-Erling Smørgrav      # support -f.
5097b5038d7SDag-Erling Smørgrav      {
5107b5038d7SDag-Erling Smørgrav        # Now remove or move aside any old file at destination location.
5117b5038d7SDag-Erling Smørgrav        # We try this two ways since rm can't unlink itself on some
5127b5038d7SDag-Erling Smørgrav        # systems and the destination file might be busy for other
5137b5038d7SDag-Erling Smørgrav        # reasons.  In this case, the final cleanup might fail but the new
5147b5038d7SDag-Erling Smørgrav        # file should still install successfully.
5157b5038d7SDag-Erling Smørgrav        {
5167b5038d7SDag-Erling Smørgrav          test ! -f "$dst" ||
517*5afab0e5SDag-Erling Smørgrav          $doit $rmcmd "$dst" 2>/dev/null ||
5187b5038d7SDag-Erling Smørgrav          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519*5afab0e5SDag-Erling Smørgrav            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5207b5038d7SDag-Erling Smørgrav          } ||
5217b5038d7SDag-Erling Smørgrav          { echo "$0: cannot unlink or rename $dst" >&2
5227b5038d7SDag-Erling Smørgrav            (exit 1); exit 1
5237b5038d7SDag-Erling Smørgrav          }
5247b5038d7SDag-Erling Smørgrav        } &&
5257b5038d7SDag-Erling Smørgrav
5267b5038d7SDag-Erling Smørgrav        # Now rename the file to the real destination.
5277b5038d7SDag-Erling Smørgrav        $doit $mvcmd "$dsttmp" "$dst"
5287b5038d7SDag-Erling Smørgrav      }
5297b5038d7SDag-Erling Smørgrav    fi || exit 1
5307b5038d7SDag-Erling Smørgrav
5317b5038d7SDag-Erling Smørgrav    trap '' 0
5327b5038d7SDag-Erling Smørgrav  fi
5337b5038d7SDag-Erling Smørgravdone
5347b5038d7SDag-Erling Smørgrav
5357b5038d7SDag-Erling Smørgrav# Local variables:
536*5afab0e5SDag-Erling Smørgrav# eval: (add-hook 'before-save-hook 'time-stamp)
5377b5038d7SDag-Erling Smørgrav# time-stamp-start: "scriptversion="
5387b5038d7SDag-Erling Smørgrav# time-stamp-format: "%:y-%02m-%02d.%02H"
539*5afab0e5SDag-Erling Smørgrav# time-stamp-time-zone: "UTC0"
5407b5038d7SDag-Erling Smørgrav# time-stamp-end: "; # UTC"
5417b5038d7SDag-Erling Smørgrav# End:
542