19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#!/bin/sh
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# install - install a program, script, or datafile
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallscriptversion=2005-02-02.21
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# This originates from X11R5 (mit/util/scripts/install.sh), which was
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# later released in X11R6 (xc/config/util/install.sh) with the
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# following copyright and license.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# Copyright (C) 1994 X Consortium
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# Permission is hereby granted, free of charge, to any person obtaining a copy
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# of this software and associated documentation files (the "Software"), to
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# deal in the Software without restriction, including without limitation the
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# sell copies of the Software, and to permit persons to whom the Software is
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# furnished to do so, subject to the following conditions:
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# The above copyright notice and this permission notice shall be included in
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# all copies or substantial portions of the Software.
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# Except as contained in this notice, the name of the X Consortium shall not
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# be used in advertising or otherwise to promote the sale, use or other deal-
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# ings in this Software without prior written authorization from the X Consor-
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# tium.
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# FSF changes to this file are in the public domain.
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# Calling this script install-sh is preferred over install.sh, to prevent
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# `make' implicit rules from creating a file called install from it
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# when there is no Makefile.
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# This script is compatible with the BSD install script, but was written
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# from scratch.  It can only install one file at a time, a restriction
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# shared with many OS's install programs.
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# set DOITPROG to echo to test this script
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# Don't use :- since 4.3BSD and earlier shells don't like it.
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldoit="${DOITPROG-}"
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# put in absolute paths if you don't have them in your path; or use env. vars.
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallmvprog="${MVPROG-mv}"
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallcpprog="${CPPROG-cp}"
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchmodprog="${CHMODPROG-chmod}"
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchownprog="${CHOWNPROG-chown}"
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchgrpprog="${CHGRPPROG-chgrp}"
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstripprog="${STRIPPROG-strip}"
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallrmprog="${RMPROG-rm}"
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallmkdirprog="${MKDIRPROG-mkdir}"
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchmodcmd="$chmodprog 0755"
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchowncmd=
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchgrpcmd=
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstripcmd=
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallrmcmd="$rmprog -f"
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallmvcmd="$mvprog"
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallsrc=
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldst=
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldir_arg=
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldstarg=
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallno_target_directory=
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   or: $0 [OPTION]... SRCFILES... DIRECTORY
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   or: $0 [OPTION]... -d DIRECTORIES...
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallIn the 1st form, copy SRCFILE to DSTFILE.
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallIn the 4th, create DIRECTORIES.
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallOptions:
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-c         (ignored)
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-d         create directories instead of installing files.
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-g GROUP   $chgrpprog installed files to GROUP.
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-m MODE    $chmodprog installed files to MODE.
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-o USER    $chownprog installed files to USER.
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-s         $stripprog installed files.
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-t DIRECTORY  install into DIRECTORY.
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall-T         report an error if DSTFILE is a directory.
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall--help     display this help and exit.
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall--version  display version info and exit.
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallEnvironment variables override the default commands:
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall"
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallwhile test -n "$1"; do
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  case $1 in
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -c) shift
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -d) dir_arg=true
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -g) chgrpcmd="$chgrpprog $2"
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    --help) echo "$usage"; exit $?;;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -m) chmodcmd="$chmodprog $2"
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -o) chowncmd="$chownprog $2"
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -s) stripcmd=$stripprog
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        shift
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -t) dstarg=$2
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shift
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shift
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	continue;;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -T) no_target_directory=true
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shift
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	continue;;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    --version) echo "$0 $scriptversion"; exit $?;;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *)  # When -d is used, all remaining arguments are directories to create.
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	# When -t is used, the destination is already specified.
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	test -n "$dir_arg$dstarg" && break
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        # Otherwise, the last argument is the destination.  Remove it from $@.
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for arg
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if test -n "$dstarg"; then
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    # $@ is not empty: it contains at least $arg.
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    set fnord "$@" "$dstarg"
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    shift # fnord
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  fi
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  shift # arg
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  dstarg=$arg
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	done
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  esac
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldone
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallif test -z "$1"; then
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if test -z "$dir_arg"; then
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    echo "$0: no input file specified." >&2
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    exit 1
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fi
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  # It's OK to call `install-sh -d' without argument.
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  # This can happen when creating conditional directories.
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  exit 0
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallfi
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallfor src
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldo
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  # Protect names starting with `-'.
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  case $src in
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    -*) src=./$src ;;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  esac
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if test -n "$dir_arg"; then
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dst=$src
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    src=
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if test -d "$dst"; then
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      mkdircmd=:
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      chmodcmd=
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      mkdircmd=$mkdirprog
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fi
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # might cause directories to be created, which would be especially bad
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # if $src (and thus $dsttmp) contains '*'.
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if test ! -f "$src" && test ! -d "$src"; then
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      echo "$0: $src does not exist." >&2
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      exit 1
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fi
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if test -z "$dstarg"; then
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      echo "$0: no destination specified." >&2
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      exit 1
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fi
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dst=$dstarg
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Protect names starting with `-'.
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case $dst in
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      -*) dst=./$dst ;;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    esac
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # If destination is a directory, append the input filename; won't work
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # if double slashes aren't ignored.
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if test -d "$dst"; then
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if test -n "$no_target_directory"; then
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	echo "$0: $dstarg: Is a directory" >&2
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	exit 1
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fi
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dst=$dst/`basename "$src"`
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fi
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fi
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  # This sed command emulates the dirname command.
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  # Make sure that the destination directory exists.
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  # Skip lots of stat calls in the usual case.
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if test ! -d "$dstdir"; then
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    defaultIFS='
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 '
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    IFS="${IFS-$defaultIFS}"
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    oIFS=$IFS
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Some sh's can't handle IFS=/ for some reason.
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    IFS='%'
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    shift
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    IFS=$oIFS
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pathcomp=
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while test $# -ne 0 ; do
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pathcomp=$pathcomp$1
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      shift
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if test ! -d "$pathcomp"; then
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        $mkdirprog "$pathcomp"
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	# mkdir can fail with a `File exist' error in case several
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	# install-sh are creating the directory concurrently.  This
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	# is OK.
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	test -d "$pathcomp" || exit
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fi
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pathcomp=$pathcomp/
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    done
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fi
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if test -n "$dir_arg"; then
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    $doit $mkdircmd "$dst" \
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dstfile=`basename "$dst"`
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Make a couple of temp file names in the proper directory.
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dsttmp=$dstdir/_inst.$$_
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rmtmp=$dstdir/_rm.$$_
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Trap to clean up those temp files at exit.
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    trap '(exit $?); exit' 1 2 13 15
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Copy the file name to the temp name.
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    $doit $cpprog "$src" "$dsttmp" &&
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # and set any options; do chmod last to preserve setuid bits.
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # If any of these fail, we abort the whole thing.  If we want to
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # ignore errors from any of these, just make sure not to ignore
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # errors from the above "$doit $cpprog $src $dsttmp" command.
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    # Now rename the file to the real destination.
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      || {
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # The rename failed, perhaps because mv can't rename something else
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # to itself, or perhaps because mv is so ancient that it does not
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # support -f.
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # Now remove or move aside any old file at destination location.
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # We try this two ways since rm can't unlink itself on some
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # systems and the destination file might be busy for other
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # reasons.  In this case, the final cleanup might fail but the new
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # file should still install successfully.
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   {
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     if test -f "$dstdir/$dstfile"; then
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       || {
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 (exit 1); exit 1
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       }
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     else
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       :
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     fi
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   } &&
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   # Now rename the file to the real destination.
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 }
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fi || { (exit 1); exit 1; }
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldone
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# The final little trick to "correctly" pass the exit status to the exit trap.
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  (exit 0); exit 0
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# Local variables:
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# eval: (add-hook 'write-file-hooks 'time-stamp)
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# time-stamp-start: "scriptversion="
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# time-stamp-format: "%:y-%02m-%02d.%02H"
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# time-stamp-end: "$"
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# End:
324