install-sh revision 02be438a
102be438aSmrg#!/bin/sh
202be438aSmrg# install - install a program, script, or datafile
302be438aSmrg
402be438aSmrgscriptversion=2005-05-14.22
502be438aSmrg
602be438aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
702be438aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
802be438aSmrg# following copyright and license.
902be438aSmrg#
1002be438aSmrg# Copyright (C) 1994 X Consortium
1102be438aSmrg#
1202be438aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1302be438aSmrg# of this software and associated documentation files (the "Software"), to
1402be438aSmrg# deal in the Software without restriction, including without limitation the
1502be438aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1602be438aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1702be438aSmrg# furnished to do so, subject to the following conditions:
1802be438aSmrg#
1902be438aSmrg# The above copyright notice and this permission notice shall be included in
2002be438aSmrg# all copies or substantial portions of the Software.
2102be438aSmrg#
2202be438aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2302be438aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2402be438aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2502be438aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2602be438aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2702be438aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2802be438aSmrg#
2902be438aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3002be438aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3102be438aSmrg# ings in this Software without prior written authorization from the X Consor-
3202be438aSmrg# tium.
3302be438aSmrg#
3402be438aSmrg#
3502be438aSmrg# FSF changes to this file are in the public domain.
3602be438aSmrg#
3702be438aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3802be438aSmrg# `make' implicit rules from creating a file called install from it
3902be438aSmrg# when there is no Makefile.
4002be438aSmrg#
4102be438aSmrg# This script is compatible with the BSD install script, but was written
4202be438aSmrg# from scratch.  It can only install one file at a time, a restriction
4302be438aSmrg# shared with many OS's install programs.
4402be438aSmrg
4502be438aSmrg# set DOITPROG to echo to test this script
4602be438aSmrg
4702be438aSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
4802be438aSmrgdoit="${DOITPROG-}"
4902be438aSmrg
5002be438aSmrg# put in absolute paths if you don't have them in your path; or use env. vars.
5102be438aSmrg
5202be438aSmrgmvprog="${MVPROG-mv}"
5302be438aSmrgcpprog="${CPPROG-cp}"
5402be438aSmrgchmodprog="${CHMODPROG-chmod}"
5502be438aSmrgchownprog="${CHOWNPROG-chown}"
5602be438aSmrgchgrpprog="${CHGRPPROG-chgrp}"
5702be438aSmrgstripprog="${STRIPPROG-strip}"
5802be438aSmrgrmprog="${RMPROG-rm}"
5902be438aSmrgmkdirprog="${MKDIRPROG-mkdir}"
6002be438aSmrg
6102be438aSmrgchmodcmd="$chmodprog 0755"
6202be438aSmrgchowncmd=
6302be438aSmrgchgrpcmd=
6402be438aSmrgstripcmd=
6502be438aSmrgrmcmd="$rmprog -f"
6602be438aSmrgmvcmd="$mvprog"
6702be438aSmrgsrc=
6802be438aSmrgdst=
6902be438aSmrgdir_arg=
7002be438aSmrgdstarg=
7102be438aSmrgno_target_directory=
7202be438aSmrg
7302be438aSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
7402be438aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
7502be438aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
7602be438aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
7702be438aSmrg
7802be438aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
7902be438aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
8002be438aSmrgIn the 4th, create DIRECTORIES.
8102be438aSmrg
8202be438aSmrgOptions:
8302be438aSmrg-c         (ignored)
8402be438aSmrg-d         create directories instead of installing files.
8502be438aSmrg-g GROUP   $chgrpprog installed files to GROUP.
8602be438aSmrg-m MODE    $chmodprog installed files to MODE.
8702be438aSmrg-o USER    $chownprog installed files to USER.
8802be438aSmrg-s         $stripprog installed files.
8902be438aSmrg-t DIRECTORY  install into DIRECTORY.
9002be438aSmrg-T         report an error if DSTFILE is a directory.
9102be438aSmrg--help     display this help and exit.
9202be438aSmrg--version  display version info and exit.
9302be438aSmrg
9402be438aSmrgEnvironment variables override the default commands:
9502be438aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
9602be438aSmrg"
9702be438aSmrg
9802be438aSmrgwhile test -n "$1"; do
9902be438aSmrg  case $1 in
10002be438aSmrg    -c) shift
10102be438aSmrg        continue;;
10202be438aSmrg
10302be438aSmrg    -d) dir_arg=true
10402be438aSmrg        shift
10502be438aSmrg        continue;;
10602be438aSmrg
10702be438aSmrg    -g) chgrpcmd="$chgrpprog $2"
10802be438aSmrg        shift
10902be438aSmrg        shift
11002be438aSmrg        continue;;
11102be438aSmrg
11202be438aSmrg    --help) echo "$usage"; exit $?;;
11302be438aSmrg
11402be438aSmrg    -m) chmodcmd="$chmodprog $2"
11502be438aSmrg        shift
11602be438aSmrg        shift
11702be438aSmrg        continue;;
11802be438aSmrg
11902be438aSmrg    -o) chowncmd="$chownprog $2"
12002be438aSmrg        shift
12102be438aSmrg        shift
12202be438aSmrg        continue;;
12302be438aSmrg
12402be438aSmrg    -s) stripcmd=$stripprog
12502be438aSmrg        shift
12602be438aSmrg        continue;;
12702be438aSmrg
12802be438aSmrg    -t) dstarg=$2
12902be438aSmrg	shift
13002be438aSmrg	shift
13102be438aSmrg	continue;;
13202be438aSmrg
13302be438aSmrg    -T) no_target_directory=true
13402be438aSmrg	shift
13502be438aSmrg	continue;;
13602be438aSmrg
13702be438aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
13802be438aSmrg
13902be438aSmrg    *)  # When -d is used, all remaining arguments are directories to create.
14002be438aSmrg	# When -t is used, the destination is already specified.
14102be438aSmrg	test -n "$dir_arg$dstarg" && break
14202be438aSmrg        # Otherwise, the last argument is the destination.  Remove it from $@.
14302be438aSmrg	for arg
14402be438aSmrg	do
14502be438aSmrg          if test -n "$dstarg"; then
14602be438aSmrg	    # $@ is not empty: it contains at least $arg.
14702be438aSmrg	    set fnord "$@" "$dstarg"
14802be438aSmrg	    shift # fnord
14902be438aSmrg	  fi
15002be438aSmrg	  shift # arg
15102be438aSmrg	  dstarg=$arg
15202be438aSmrg	done
15302be438aSmrg	break;;
15402be438aSmrg  esac
15502be438aSmrgdone
15602be438aSmrg
15702be438aSmrgif test -z "$1"; then
15802be438aSmrg  if test -z "$dir_arg"; then
15902be438aSmrg    echo "$0: no input file specified." >&2
16002be438aSmrg    exit 1
16102be438aSmrg  fi
16202be438aSmrg  # It's OK to call `install-sh -d' without argument.
16302be438aSmrg  # This can happen when creating conditional directories.
16402be438aSmrg  exit 0
16502be438aSmrgfi
16602be438aSmrg
16702be438aSmrgfor src
16802be438aSmrgdo
16902be438aSmrg  # Protect names starting with `-'.
17002be438aSmrg  case $src in
17102be438aSmrg    -*) src=./$src ;;
17202be438aSmrg  esac
17302be438aSmrg
17402be438aSmrg  if test -n "$dir_arg"; then
17502be438aSmrg    dst=$src
17602be438aSmrg    src=
17702be438aSmrg
17802be438aSmrg    if test -d "$dst"; then
17902be438aSmrg      mkdircmd=:
18002be438aSmrg      chmodcmd=
18102be438aSmrg    else
18202be438aSmrg      mkdircmd=$mkdirprog
18302be438aSmrg    fi
18402be438aSmrg  else
18502be438aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
18602be438aSmrg    # might cause directories to be created, which would be especially bad
18702be438aSmrg    # if $src (and thus $dsttmp) contains '*'.
18802be438aSmrg    if test ! -f "$src" && test ! -d "$src"; then
18902be438aSmrg      echo "$0: $src does not exist." >&2
19002be438aSmrg      exit 1
19102be438aSmrg    fi
19202be438aSmrg
19302be438aSmrg    if test -z "$dstarg"; then
19402be438aSmrg      echo "$0: no destination specified." >&2
19502be438aSmrg      exit 1
19602be438aSmrg    fi
19702be438aSmrg
19802be438aSmrg    dst=$dstarg
19902be438aSmrg    # Protect names starting with `-'.
20002be438aSmrg    case $dst in
20102be438aSmrg      -*) dst=./$dst ;;
20202be438aSmrg    esac
20302be438aSmrg
20402be438aSmrg    # If destination is a directory, append the input filename; won't work
20502be438aSmrg    # if double slashes aren't ignored.
20602be438aSmrg    if test -d "$dst"; then
20702be438aSmrg      if test -n "$no_target_directory"; then
20802be438aSmrg	echo "$0: $dstarg: Is a directory" >&2
20902be438aSmrg	exit 1
21002be438aSmrg      fi
21102be438aSmrg      dst=$dst/`basename "$src"`
21202be438aSmrg    fi
21302be438aSmrg  fi
21402be438aSmrg
21502be438aSmrg  # This sed command emulates the dirname command.
21602be438aSmrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
21702be438aSmrg
21802be438aSmrg  # Make sure that the destination directory exists.
21902be438aSmrg
22002be438aSmrg  # Skip lots of stat calls in the usual case.
22102be438aSmrg  if test ! -d "$dstdir"; then
22202be438aSmrg    defaultIFS='
22302be438aSmrg	 '
22402be438aSmrg    IFS="${IFS-$defaultIFS}"
22502be438aSmrg
22602be438aSmrg    oIFS=$IFS
22702be438aSmrg    # Some sh's can't handle IFS=/ for some reason.
22802be438aSmrg    IFS='%'
22902be438aSmrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
23002be438aSmrg    shift
23102be438aSmrg    IFS=$oIFS
23202be438aSmrg
23302be438aSmrg    pathcomp=
23402be438aSmrg
23502be438aSmrg    while test $# -ne 0 ; do
23602be438aSmrg      pathcomp=$pathcomp$1
23702be438aSmrg      shift
23802be438aSmrg      if test ! -d "$pathcomp"; then
23902be438aSmrg        $mkdirprog "$pathcomp"
24002be438aSmrg	# mkdir can fail with a `File exist' error in case several
24102be438aSmrg	# install-sh are creating the directory concurrently.  This
24202be438aSmrg	# is OK.
24302be438aSmrg	test -d "$pathcomp" || exit
24402be438aSmrg      fi
24502be438aSmrg      pathcomp=$pathcomp/
24602be438aSmrg    done
24702be438aSmrg  fi
24802be438aSmrg
24902be438aSmrg  if test -n "$dir_arg"; then
25002be438aSmrg    $doit $mkdircmd "$dst" \
25102be438aSmrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
25202be438aSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
25302be438aSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
25402be438aSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
25502be438aSmrg
25602be438aSmrg  else
25702be438aSmrg    dstfile=`basename "$dst"`
25802be438aSmrg
25902be438aSmrg    # Make a couple of temp file names in the proper directory.
26002be438aSmrg    dsttmp=$dstdir/_inst.$$_
26102be438aSmrg    rmtmp=$dstdir/_rm.$$_
26202be438aSmrg
26302be438aSmrg    # Trap to clean up those temp files at exit.
26402be438aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
26502be438aSmrg    trap '(exit $?); exit' 1 2 13 15
26602be438aSmrg
26702be438aSmrg    # Copy the file name to the temp name.
26802be438aSmrg    $doit $cpprog "$src" "$dsttmp" &&
26902be438aSmrg
27002be438aSmrg    # and set any options; do chmod last to preserve setuid bits.
27102be438aSmrg    #
27202be438aSmrg    # If any of these fail, we abort the whole thing.  If we want to
27302be438aSmrg    # ignore errors from any of these, just make sure not to ignore
27402be438aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
27502be438aSmrg    #
27602be438aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
27702be438aSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
27802be438aSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
27902be438aSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
28002be438aSmrg
28102be438aSmrg    # Now rename the file to the real destination.
28202be438aSmrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
28302be438aSmrg      || {
28402be438aSmrg	   # The rename failed, perhaps because mv can't rename something else
28502be438aSmrg	   # to itself, or perhaps because mv is so ancient that it does not
28602be438aSmrg	   # support -f.
28702be438aSmrg
28802be438aSmrg	   # Now remove or move aside any old file at destination location.
28902be438aSmrg	   # We try this two ways since rm can't unlink itself on some
29002be438aSmrg	   # systems and the destination file might be busy for other
29102be438aSmrg	   # reasons.  In this case, the final cleanup might fail but the new
29202be438aSmrg	   # file should still install successfully.
29302be438aSmrg	   {
29402be438aSmrg	     if test -f "$dstdir/$dstfile"; then
29502be438aSmrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
29602be438aSmrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
29702be438aSmrg	       || {
29802be438aSmrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
29902be438aSmrg		 (exit 1); exit 1
30002be438aSmrg	       }
30102be438aSmrg	     else
30202be438aSmrg	       :
30302be438aSmrg	     fi
30402be438aSmrg	   } &&
30502be438aSmrg
30602be438aSmrg	   # Now rename the file to the real destination.
30702be438aSmrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
30802be438aSmrg	 }
30902be438aSmrg    }
31002be438aSmrg  fi || { (exit 1); exit 1; }
31102be438aSmrgdone
31202be438aSmrg
31302be438aSmrg# The final little trick to "correctly" pass the exit status to the exit trap.
31402be438aSmrg{
31502be438aSmrg  (exit 0); exit 0
31602be438aSmrg}
31702be438aSmrg
31802be438aSmrg# Local variables:
31902be438aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
32002be438aSmrg# time-stamp-start: "scriptversion="
32102be438aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
32202be438aSmrg# time-stamp-end: "$"
32302be438aSmrg# End:
324