install-sh revision 09885543
109885543Smrg#!/bin/sh
209885543Smrg# install - install a program, script, or datafile
309885543Smrg
409885543Smrgscriptversion=2005-05-14.22
509885543Smrg
609885543Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
709885543Smrg# later released in X11R6 (xc/config/util/install.sh) with the
809885543Smrg# following copyright and license.
909885543Smrg#
1009885543Smrg# Copyright (C) 1994 X Consortium
1109885543Smrg#
1209885543Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1309885543Smrg# of this software and associated documentation files (the "Software"), to
1409885543Smrg# deal in the Software without restriction, including without limitation the
1509885543Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1609885543Smrg# sell copies of the Software, and to permit persons to whom the Software is
1709885543Smrg# furnished to do so, subject to the following conditions:
1809885543Smrg#
1909885543Smrg# The above copyright notice and this permission notice shall be included in
2009885543Smrg# all copies or substantial portions of the Software.
2109885543Smrg#
2209885543Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2309885543Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2409885543Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2509885543Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2609885543Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2709885543Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2809885543Smrg#
2909885543Smrg# Except as contained in this notice, the name of the X Consortium shall not
3009885543Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3109885543Smrg# ings in this Software without prior written authorization from the X Consor-
3209885543Smrg# tium.
3309885543Smrg#
3409885543Smrg#
3509885543Smrg# FSF changes to this file are in the public domain.
3609885543Smrg#
3709885543Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3809885543Smrg# `make' implicit rules from creating a file called install from it
3909885543Smrg# when there is no Makefile.
4009885543Smrg#
4109885543Smrg# This script is compatible with the BSD install script, but was written
4209885543Smrg# from scratch.  It can only install one file at a time, a restriction
4309885543Smrg# shared with many OS's install programs.
4409885543Smrg
4509885543Smrg# set DOITPROG to echo to test this script
4609885543Smrg
4709885543Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
4809885543Smrgdoit="${DOITPROG-}"
4909885543Smrg
5009885543Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
5109885543Smrg
5209885543Smrgmvprog="${MVPROG-mv}"
5309885543Smrgcpprog="${CPPROG-cp}"
5409885543Smrgchmodprog="${CHMODPROG-chmod}"
5509885543Smrgchownprog="${CHOWNPROG-chown}"
5609885543Smrgchgrpprog="${CHGRPPROG-chgrp}"
5709885543Smrgstripprog="${STRIPPROG-strip}"
5809885543Smrgrmprog="${RMPROG-rm}"
5909885543Smrgmkdirprog="${MKDIRPROG-mkdir}"
6009885543Smrg
6109885543Smrgchmodcmd="$chmodprog 0755"
6209885543Smrgchowncmd=
6309885543Smrgchgrpcmd=
6409885543Smrgstripcmd=
6509885543Smrgrmcmd="$rmprog -f"
6609885543Smrgmvcmd="$mvprog"
6709885543Smrgsrc=
6809885543Smrgdst=
6909885543Smrgdir_arg=
7009885543Smrgdstarg=
7109885543Smrgno_target_directory=
7209885543Smrg
7309885543Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
7409885543Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
7509885543Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
7609885543Smrg   or: $0 [OPTION]... -d DIRECTORIES...
7709885543Smrg
7809885543SmrgIn the 1st form, copy SRCFILE to DSTFILE.
7909885543SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
8009885543SmrgIn the 4th, create DIRECTORIES.
8109885543Smrg
8209885543SmrgOptions:
8309885543Smrg-c         (ignored)
8409885543Smrg-d         create directories instead of installing files.
8509885543Smrg-g GROUP   $chgrpprog installed files to GROUP.
8609885543Smrg-m MODE    $chmodprog installed files to MODE.
8709885543Smrg-o USER    $chownprog installed files to USER.
8809885543Smrg-s         $stripprog installed files.
8909885543Smrg-t DIRECTORY  install into DIRECTORY.
9009885543Smrg-T         report an error if DSTFILE is a directory.
9109885543Smrg--help     display this help and exit.
9209885543Smrg--version  display version info and exit.
9309885543Smrg
9409885543SmrgEnvironment variables override the default commands:
9509885543Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
9609885543Smrg"
9709885543Smrg
9809885543Smrgwhile test -n "$1"; do
9909885543Smrg  case $1 in
10009885543Smrg    -c) shift
10109885543Smrg        continue;;
10209885543Smrg
10309885543Smrg    -d) dir_arg=true
10409885543Smrg        shift
10509885543Smrg        continue;;
10609885543Smrg
10709885543Smrg    -g) chgrpcmd="$chgrpprog $2"
10809885543Smrg        shift
10909885543Smrg        shift
11009885543Smrg        continue;;
11109885543Smrg
11209885543Smrg    --help) echo "$usage"; exit $?;;
11309885543Smrg
11409885543Smrg    -m) chmodcmd="$chmodprog $2"
11509885543Smrg        shift
11609885543Smrg        shift
11709885543Smrg        continue;;
11809885543Smrg
11909885543Smrg    -o) chowncmd="$chownprog $2"
12009885543Smrg        shift
12109885543Smrg        shift
12209885543Smrg        continue;;
12309885543Smrg
12409885543Smrg    -s) stripcmd=$stripprog
12509885543Smrg        shift
12609885543Smrg        continue;;
12709885543Smrg
12809885543Smrg    -t) dstarg=$2
12909885543Smrg	shift
13009885543Smrg	shift
13109885543Smrg	continue;;
13209885543Smrg
13309885543Smrg    -T) no_target_directory=true
13409885543Smrg	shift
13509885543Smrg	continue;;
13609885543Smrg
13709885543Smrg    --version) echo "$0 $scriptversion"; exit $?;;
13809885543Smrg
13909885543Smrg    *)  # When -d is used, all remaining arguments are directories to create.
14009885543Smrg	# When -t is used, the destination is already specified.
14109885543Smrg	test -n "$dir_arg$dstarg" && break
14209885543Smrg        # Otherwise, the last argument is the destination.  Remove it from $@.
14309885543Smrg	for arg
14409885543Smrg	do
14509885543Smrg          if test -n "$dstarg"; then
14609885543Smrg	    # $@ is not empty: it contains at least $arg.
14709885543Smrg	    set fnord "$@" "$dstarg"
14809885543Smrg	    shift # fnord
14909885543Smrg	  fi
15009885543Smrg	  shift # arg
15109885543Smrg	  dstarg=$arg
15209885543Smrg	done
15309885543Smrg	break;;
15409885543Smrg  esac
15509885543Smrgdone
15609885543Smrg
15709885543Smrgif test -z "$1"; then
15809885543Smrg  if test -z "$dir_arg"; then
15909885543Smrg    echo "$0: no input file specified." >&2
16009885543Smrg    exit 1
16109885543Smrg  fi
16209885543Smrg  # It's OK to call `install-sh -d' without argument.
16309885543Smrg  # This can happen when creating conditional directories.
16409885543Smrg  exit 0
16509885543Smrgfi
16609885543Smrg
16709885543Smrgfor src
16809885543Smrgdo
16909885543Smrg  # Protect names starting with `-'.
17009885543Smrg  case $src in
17109885543Smrg    -*) src=./$src ;;
17209885543Smrg  esac
17309885543Smrg
17409885543Smrg  if test -n "$dir_arg"; then
17509885543Smrg    dst=$src
17609885543Smrg    src=
17709885543Smrg
17809885543Smrg    if test -d "$dst"; then
17909885543Smrg      mkdircmd=:
18009885543Smrg      chmodcmd=
18109885543Smrg    else
18209885543Smrg      mkdircmd=$mkdirprog
18309885543Smrg    fi
18409885543Smrg  else
18509885543Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
18609885543Smrg    # might cause directories to be created, which would be especially bad
18709885543Smrg    # if $src (and thus $dsttmp) contains '*'.
18809885543Smrg    if test ! -f "$src" && test ! -d "$src"; then
18909885543Smrg      echo "$0: $src does not exist." >&2
19009885543Smrg      exit 1
19109885543Smrg    fi
19209885543Smrg
19309885543Smrg    if test -z "$dstarg"; then
19409885543Smrg      echo "$0: no destination specified." >&2
19509885543Smrg      exit 1
19609885543Smrg    fi
19709885543Smrg
19809885543Smrg    dst=$dstarg
19909885543Smrg    # Protect names starting with `-'.
20009885543Smrg    case $dst in
20109885543Smrg      -*) dst=./$dst ;;
20209885543Smrg    esac
20309885543Smrg
20409885543Smrg    # If destination is a directory, append the input filename; won't work
20509885543Smrg    # if double slashes aren't ignored.
20609885543Smrg    if test -d "$dst"; then
20709885543Smrg      if test -n "$no_target_directory"; then
20809885543Smrg	echo "$0: $dstarg: Is a directory" >&2
20909885543Smrg	exit 1
21009885543Smrg      fi
21109885543Smrg      dst=$dst/`basename "$src"`
21209885543Smrg    fi
21309885543Smrg  fi
21409885543Smrg
21509885543Smrg  # This sed command emulates the dirname command.
21609885543Smrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
21709885543Smrg
21809885543Smrg  # Make sure that the destination directory exists.
21909885543Smrg
22009885543Smrg  # Skip lots of stat calls in the usual case.
22109885543Smrg  if test ! -d "$dstdir"; then
22209885543Smrg    defaultIFS='
22309885543Smrg	 '
22409885543Smrg    IFS="${IFS-$defaultIFS}"
22509885543Smrg
22609885543Smrg    oIFS=$IFS
22709885543Smrg    # Some sh's can't handle IFS=/ for some reason.
22809885543Smrg    IFS='%'
22909885543Smrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
23009885543Smrg    shift
23109885543Smrg    IFS=$oIFS
23209885543Smrg
23309885543Smrg    pathcomp=
23409885543Smrg
23509885543Smrg    while test $# -ne 0 ; do
23609885543Smrg      pathcomp=$pathcomp$1
23709885543Smrg      shift
23809885543Smrg      if test ! -d "$pathcomp"; then
23909885543Smrg        $mkdirprog "$pathcomp"
24009885543Smrg	# mkdir can fail with a `File exist' error in case several
24109885543Smrg	# install-sh are creating the directory concurrently.  This
24209885543Smrg	# is OK.
24309885543Smrg	test -d "$pathcomp" || exit
24409885543Smrg      fi
24509885543Smrg      pathcomp=$pathcomp/
24609885543Smrg    done
24709885543Smrg  fi
24809885543Smrg
24909885543Smrg  if test -n "$dir_arg"; then
25009885543Smrg    $doit $mkdircmd "$dst" \
25109885543Smrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
25209885543Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
25309885543Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
25409885543Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
25509885543Smrg
25609885543Smrg  else
25709885543Smrg    dstfile=`basename "$dst"`
25809885543Smrg
25909885543Smrg    # Make a couple of temp file names in the proper directory.
26009885543Smrg    dsttmp=$dstdir/_inst.$$_
26109885543Smrg    rmtmp=$dstdir/_rm.$$_
26209885543Smrg
26309885543Smrg    # Trap to clean up those temp files at exit.
26409885543Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
26509885543Smrg    trap '(exit $?); exit' 1 2 13 15
26609885543Smrg
26709885543Smrg    # Copy the file name to the temp name.
26809885543Smrg    $doit $cpprog "$src" "$dsttmp" &&
26909885543Smrg
27009885543Smrg    # and set any options; do chmod last to preserve setuid bits.
27109885543Smrg    #
27209885543Smrg    # If any of these fail, we abort the whole thing.  If we want to
27309885543Smrg    # ignore errors from any of these, just make sure not to ignore
27409885543Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
27509885543Smrg    #
27609885543Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
27709885543Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
27809885543Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
27909885543Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
28009885543Smrg
28109885543Smrg    # Now rename the file to the real destination.
28209885543Smrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
28309885543Smrg      || {
28409885543Smrg	   # The rename failed, perhaps because mv can't rename something else
28509885543Smrg	   # to itself, or perhaps because mv is so ancient that it does not
28609885543Smrg	   # support -f.
28709885543Smrg
28809885543Smrg	   # Now remove or move aside any old file at destination location.
28909885543Smrg	   # We try this two ways since rm can't unlink itself on some
29009885543Smrg	   # systems and the destination file might be busy for other
29109885543Smrg	   # reasons.  In this case, the final cleanup might fail but the new
29209885543Smrg	   # file should still install successfully.
29309885543Smrg	   {
29409885543Smrg	     if test -f "$dstdir/$dstfile"; then
29509885543Smrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
29609885543Smrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
29709885543Smrg	       || {
29809885543Smrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
29909885543Smrg		 (exit 1); exit 1
30009885543Smrg	       }
30109885543Smrg	     else
30209885543Smrg	       :
30309885543Smrg	     fi
30409885543Smrg	   } &&
30509885543Smrg
30609885543Smrg	   # Now rename the file to the real destination.
30709885543Smrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
30809885543Smrg	 }
30909885543Smrg    }
31009885543Smrg  fi || { (exit 1); exit 1; }
31109885543Smrgdone
31209885543Smrg
31309885543Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
31409885543Smrg{
31509885543Smrg  (exit 0); exit 0
31609885543Smrg}
31709885543Smrg
31809885543Smrg# Local variables:
31909885543Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
32009885543Smrg# time-stamp-start: "scriptversion="
32109885543Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
32209885543Smrg# time-stamp-end: "$"
32309885543Smrg# End:
324