install-sh revision 95b296d0
195b296d0Smrg#!/bin/sh
295b296d0Smrg# install - install a program, script, or datafile
395b296d0Smrg
495b296d0Smrgscriptversion=2005-05-14.22
595b296d0Smrg
695b296d0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
795b296d0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
895b296d0Smrg# following copyright and license.
995b296d0Smrg#
1095b296d0Smrg# Copyright (C) 1994 X Consortium
1195b296d0Smrg#
1295b296d0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1395b296d0Smrg# of this software and associated documentation files (the "Software"), to
1495b296d0Smrg# deal in the Software without restriction, including without limitation the
1595b296d0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1695b296d0Smrg# sell copies of the Software, and to permit persons to whom the Software is
1795b296d0Smrg# furnished to do so, subject to the following conditions:
1895b296d0Smrg#
1995b296d0Smrg# The above copyright notice and this permission notice shall be included in
2095b296d0Smrg# all copies or substantial portions of the Software.
2195b296d0Smrg#
2295b296d0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2395b296d0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2495b296d0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2595b296d0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2695b296d0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2795b296d0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2895b296d0Smrg#
2995b296d0Smrg# Except as contained in this notice, the name of the X Consortium shall not
3095b296d0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3195b296d0Smrg# ings in this Software without prior written authorization from the X Consor-
3295b296d0Smrg# tium.
3395b296d0Smrg#
3495b296d0Smrg#
3595b296d0Smrg# FSF changes to this file are in the public domain.
3695b296d0Smrg#
3795b296d0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3895b296d0Smrg# `make' implicit rules from creating a file called install from it
3995b296d0Smrg# when there is no Makefile.
4095b296d0Smrg#
4195b296d0Smrg# This script is compatible with the BSD install script, but was written
4295b296d0Smrg# from scratch.  It can only install one file at a time, a restriction
4395b296d0Smrg# shared with many OS's install programs.
4495b296d0Smrg
4595b296d0Smrg# set DOITPROG to echo to test this script
4695b296d0Smrg
4795b296d0Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
4895b296d0Smrgdoit="${DOITPROG-}"
4995b296d0Smrg
5095b296d0Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
5195b296d0Smrg
5295b296d0Smrgmvprog="${MVPROG-mv}"
5395b296d0Smrgcpprog="${CPPROG-cp}"
5495b296d0Smrgchmodprog="${CHMODPROG-chmod}"
5595b296d0Smrgchownprog="${CHOWNPROG-chown}"
5695b296d0Smrgchgrpprog="${CHGRPPROG-chgrp}"
5795b296d0Smrgstripprog="${STRIPPROG-strip}"
5895b296d0Smrgrmprog="${RMPROG-rm}"
5995b296d0Smrgmkdirprog="${MKDIRPROG-mkdir}"
6095b296d0Smrg
6195b296d0Smrgchmodcmd="$chmodprog 0755"
6295b296d0Smrgchowncmd=
6395b296d0Smrgchgrpcmd=
6495b296d0Smrgstripcmd=
6595b296d0Smrgrmcmd="$rmprog -f"
6695b296d0Smrgmvcmd="$mvprog"
6795b296d0Smrgsrc=
6895b296d0Smrgdst=
6995b296d0Smrgdir_arg=
7095b296d0Smrgdstarg=
7195b296d0Smrgno_target_directory=
7295b296d0Smrg
7395b296d0Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
7495b296d0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
7595b296d0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
7695b296d0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
7795b296d0Smrg
7895b296d0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
7995b296d0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
8095b296d0SmrgIn the 4th, create DIRECTORIES.
8195b296d0Smrg
8295b296d0SmrgOptions:
8395b296d0Smrg-c         (ignored)
8495b296d0Smrg-d         create directories instead of installing files.
8595b296d0Smrg-g GROUP   $chgrpprog installed files to GROUP.
8695b296d0Smrg-m MODE    $chmodprog installed files to MODE.
8795b296d0Smrg-o USER    $chownprog installed files to USER.
8895b296d0Smrg-s         $stripprog installed files.
8995b296d0Smrg-t DIRECTORY  install into DIRECTORY.
9095b296d0Smrg-T         report an error if DSTFILE is a directory.
9195b296d0Smrg--help     display this help and exit.
9295b296d0Smrg--version  display version info and exit.
9395b296d0Smrg
9495b296d0SmrgEnvironment variables override the default commands:
9595b296d0Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
9695b296d0Smrg"
9795b296d0Smrg
9895b296d0Smrgwhile test -n "$1"; do
9995b296d0Smrg  case $1 in
10095b296d0Smrg    -c) shift
10195b296d0Smrg        continue;;
10295b296d0Smrg
10395b296d0Smrg    -d) dir_arg=true
10495b296d0Smrg        shift
10595b296d0Smrg        continue;;
10695b296d0Smrg
10795b296d0Smrg    -g) chgrpcmd="$chgrpprog $2"
10895b296d0Smrg        shift
10995b296d0Smrg        shift
11095b296d0Smrg        continue;;
11195b296d0Smrg
11295b296d0Smrg    --help) echo "$usage"; exit $?;;
11395b296d0Smrg
11495b296d0Smrg    -m) chmodcmd="$chmodprog $2"
11595b296d0Smrg        shift
11695b296d0Smrg        shift
11795b296d0Smrg        continue;;
11895b296d0Smrg
11995b296d0Smrg    -o) chowncmd="$chownprog $2"
12095b296d0Smrg        shift
12195b296d0Smrg        shift
12295b296d0Smrg        continue;;
12395b296d0Smrg
12495b296d0Smrg    -s) stripcmd=$stripprog
12595b296d0Smrg        shift
12695b296d0Smrg        continue;;
12795b296d0Smrg
12895b296d0Smrg    -t) dstarg=$2
12995b296d0Smrg	shift
13095b296d0Smrg	shift
13195b296d0Smrg	continue;;
13295b296d0Smrg
13395b296d0Smrg    -T) no_target_directory=true
13495b296d0Smrg	shift
13595b296d0Smrg	continue;;
13695b296d0Smrg
13795b296d0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
13895b296d0Smrg
13995b296d0Smrg    *)  # When -d is used, all remaining arguments are directories to create.
14095b296d0Smrg	# When -t is used, the destination is already specified.
14195b296d0Smrg	test -n "$dir_arg$dstarg" && break
14295b296d0Smrg        # Otherwise, the last argument is the destination.  Remove it from $@.
14395b296d0Smrg	for arg
14495b296d0Smrg	do
14595b296d0Smrg          if test -n "$dstarg"; then
14695b296d0Smrg	    # $@ is not empty: it contains at least $arg.
14795b296d0Smrg	    set fnord "$@" "$dstarg"
14895b296d0Smrg	    shift # fnord
14995b296d0Smrg	  fi
15095b296d0Smrg	  shift # arg
15195b296d0Smrg	  dstarg=$arg
15295b296d0Smrg	done
15395b296d0Smrg	break;;
15495b296d0Smrg  esac
15595b296d0Smrgdone
15695b296d0Smrg
15795b296d0Smrgif test -z "$1"; then
15895b296d0Smrg  if test -z "$dir_arg"; then
15995b296d0Smrg    echo "$0: no input file specified." >&2
16095b296d0Smrg    exit 1
16195b296d0Smrg  fi
16295b296d0Smrg  # It's OK to call `install-sh -d' without argument.
16395b296d0Smrg  # This can happen when creating conditional directories.
16495b296d0Smrg  exit 0
16595b296d0Smrgfi
16695b296d0Smrg
16795b296d0Smrgfor src
16895b296d0Smrgdo
16995b296d0Smrg  # Protect names starting with `-'.
17095b296d0Smrg  case $src in
17195b296d0Smrg    -*) src=./$src ;;
17295b296d0Smrg  esac
17395b296d0Smrg
17495b296d0Smrg  if test -n "$dir_arg"; then
17595b296d0Smrg    dst=$src
17695b296d0Smrg    src=
17795b296d0Smrg
17895b296d0Smrg    if test -d "$dst"; then
17995b296d0Smrg      mkdircmd=:
18095b296d0Smrg      chmodcmd=
18195b296d0Smrg    else
18295b296d0Smrg      mkdircmd=$mkdirprog
18395b296d0Smrg    fi
18495b296d0Smrg  else
18595b296d0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
18695b296d0Smrg    # might cause directories to be created, which would be especially bad
18795b296d0Smrg    # if $src (and thus $dsttmp) contains '*'.
18895b296d0Smrg    if test ! -f "$src" && test ! -d "$src"; then
18995b296d0Smrg      echo "$0: $src does not exist." >&2
19095b296d0Smrg      exit 1
19195b296d0Smrg    fi
19295b296d0Smrg
19395b296d0Smrg    if test -z "$dstarg"; then
19495b296d0Smrg      echo "$0: no destination specified." >&2
19595b296d0Smrg      exit 1
19695b296d0Smrg    fi
19795b296d0Smrg
19895b296d0Smrg    dst=$dstarg
19995b296d0Smrg    # Protect names starting with `-'.
20095b296d0Smrg    case $dst in
20195b296d0Smrg      -*) dst=./$dst ;;
20295b296d0Smrg    esac
20395b296d0Smrg
20495b296d0Smrg    # If destination is a directory, append the input filename; won't work
20595b296d0Smrg    # if double slashes aren't ignored.
20695b296d0Smrg    if test -d "$dst"; then
20795b296d0Smrg      if test -n "$no_target_directory"; then
20895b296d0Smrg	echo "$0: $dstarg: Is a directory" >&2
20995b296d0Smrg	exit 1
21095b296d0Smrg      fi
21195b296d0Smrg      dst=$dst/`basename "$src"`
21295b296d0Smrg    fi
21395b296d0Smrg  fi
21495b296d0Smrg
21595b296d0Smrg  # This sed command emulates the dirname command.
21695b296d0Smrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
21795b296d0Smrg
21895b296d0Smrg  # Make sure that the destination directory exists.
21995b296d0Smrg
22095b296d0Smrg  # Skip lots of stat calls in the usual case.
22195b296d0Smrg  if test ! -d "$dstdir"; then
22295b296d0Smrg    defaultIFS='
22395b296d0Smrg	 '
22495b296d0Smrg    IFS="${IFS-$defaultIFS}"
22595b296d0Smrg
22695b296d0Smrg    oIFS=$IFS
22795b296d0Smrg    # Some sh's can't handle IFS=/ for some reason.
22895b296d0Smrg    IFS='%'
22995b296d0Smrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
23095b296d0Smrg    shift
23195b296d0Smrg    IFS=$oIFS
23295b296d0Smrg
23395b296d0Smrg    pathcomp=
23495b296d0Smrg
23595b296d0Smrg    while test $# -ne 0 ; do
23695b296d0Smrg      pathcomp=$pathcomp$1
23795b296d0Smrg      shift
23895b296d0Smrg      if test ! -d "$pathcomp"; then
23995b296d0Smrg        $mkdirprog "$pathcomp"
24095b296d0Smrg	# mkdir can fail with a `File exist' error in case several
24195b296d0Smrg	# install-sh are creating the directory concurrently.  This
24295b296d0Smrg	# is OK.
24395b296d0Smrg	test -d "$pathcomp" || exit
24495b296d0Smrg      fi
24595b296d0Smrg      pathcomp=$pathcomp/
24695b296d0Smrg    done
24795b296d0Smrg  fi
24895b296d0Smrg
24995b296d0Smrg  if test -n "$dir_arg"; then
25095b296d0Smrg    $doit $mkdircmd "$dst" \
25195b296d0Smrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
25295b296d0Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
25395b296d0Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
25495b296d0Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
25595b296d0Smrg
25695b296d0Smrg  else
25795b296d0Smrg    dstfile=`basename "$dst"`
25895b296d0Smrg
25995b296d0Smrg    # Make a couple of temp file names in the proper directory.
26095b296d0Smrg    dsttmp=$dstdir/_inst.$$_
26195b296d0Smrg    rmtmp=$dstdir/_rm.$$_
26295b296d0Smrg
26395b296d0Smrg    # Trap to clean up those temp files at exit.
26495b296d0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
26595b296d0Smrg    trap '(exit $?); exit' 1 2 13 15
26695b296d0Smrg
26795b296d0Smrg    # Copy the file name to the temp name.
26895b296d0Smrg    $doit $cpprog "$src" "$dsttmp" &&
26995b296d0Smrg
27095b296d0Smrg    # and set any options; do chmod last to preserve setuid bits.
27195b296d0Smrg    #
27295b296d0Smrg    # If any of these fail, we abort the whole thing.  If we want to
27395b296d0Smrg    # ignore errors from any of these, just make sure not to ignore
27495b296d0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
27595b296d0Smrg    #
27695b296d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
27795b296d0Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
27895b296d0Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
27995b296d0Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
28095b296d0Smrg
28195b296d0Smrg    # Now rename the file to the real destination.
28295b296d0Smrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
28395b296d0Smrg      || {
28495b296d0Smrg	   # The rename failed, perhaps because mv can't rename something else
28595b296d0Smrg	   # to itself, or perhaps because mv is so ancient that it does not
28695b296d0Smrg	   # support -f.
28795b296d0Smrg
28895b296d0Smrg	   # Now remove or move aside any old file at destination location.
28995b296d0Smrg	   # We try this two ways since rm can't unlink itself on some
29095b296d0Smrg	   # systems and the destination file might be busy for other
29195b296d0Smrg	   # reasons.  In this case, the final cleanup might fail but the new
29295b296d0Smrg	   # file should still install successfully.
29395b296d0Smrg	   {
29495b296d0Smrg	     if test -f "$dstdir/$dstfile"; then
29595b296d0Smrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
29695b296d0Smrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
29795b296d0Smrg	       || {
29895b296d0Smrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
29995b296d0Smrg		 (exit 1); exit 1
30095b296d0Smrg	       }
30195b296d0Smrg	     else
30295b296d0Smrg	       :
30395b296d0Smrg	     fi
30495b296d0Smrg	   } &&
30595b296d0Smrg
30695b296d0Smrg	   # Now rename the file to the real destination.
30795b296d0Smrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
30895b296d0Smrg	 }
30995b296d0Smrg    }
31095b296d0Smrg  fi || { (exit 1); exit 1; }
31195b296d0Smrgdone
31295b296d0Smrg
31395b296d0Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
31495b296d0Smrg{
31595b296d0Smrg  (exit 0); exit 0
31695b296d0Smrg}
31795b296d0Smrg
31895b296d0Smrg# Local variables:
31995b296d0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
32095b296d0Smrg# time-stamp-start: "scriptversion="
32195b296d0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
32295b296d0Smrg# time-stamp-end: "$"
32395b296d0Smrg# End:
324