install-sh revision 4456fccd
14456fccdSmrg#!/bin/sh
24456fccdSmrg# install - install a program, script, or datafile
34456fccdSmrg
44456fccdSmrgscriptversion=2005-05-14.22
54456fccdSmrg
64456fccdSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74456fccdSmrg# later released in X11R6 (xc/config/util/install.sh) with the
84456fccdSmrg# following copyright and license.
94456fccdSmrg#
104456fccdSmrg# Copyright (C) 1994 X Consortium
114456fccdSmrg#
124456fccdSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134456fccdSmrg# of this software and associated documentation files (the "Software"), to
144456fccdSmrg# deal in the Software without restriction, including without limitation the
154456fccdSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164456fccdSmrg# sell copies of the Software, and to permit persons to whom the Software is
174456fccdSmrg# furnished to do so, subject to the following conditions:
184456fccdSmrg#
194456fccdSmrg# The above copyright notice and this permission notice shall be included in
204456fccdSmrg# all copies or substantial portions of the Software.
214456fccdSmrg#
224456fccdSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234456fccdSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244456fccdSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254456fccdSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264456fccdSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274456fccdSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284456fccdSmrg#
294456fccdSmrg# Except as contained in this notice, the name of the X Consortium shall not
304456fccdSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
314456fccdSmrg# ings in this Software without prior written authorization from the X Consor-
324456fccdSmrg# tium.
334456fccdSmrg#
344456fccdSmrg#
354456fccdSmrg# FSF changes to this file are in the public domain.
364456fccdSmrg#
374456fccdSmrg# Calling this script install-sh is preferred over install.sh, to prevent
384456fccdSmrg# `make' implicit rules from creating a file called install from it
394456fccdSmrg# when there is no Makefile.
404456fccdSmrg#
414456fccdSmrg# This script is compatible with the BSD install script, but was written
424456fccdSmrg# from scratch.  It can only install one file at a time, a restriction
434456fccdSmrg# shared with many OS's install programs.
444456fccdSmrg
454456fccdSmrg# set DOITPROG to echo to test this script
464456fccdSmrg
474456fccdSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
484456fccdSmrgdoit="${DOITPROG-}"
494456fccdSmrg
504456fccdSmrg# put in absolute paths if you don't have them in your path; or use env. vars.
514456fccdSmrg
524456fccdSmrgmvprog="${MVPROG-mv}"
534456fccdSmrgcpprog="${CPPROG-cp}"
544456fccdSmrgchmodprog="${CHMODPROG-chmod}"
554456fccdSmrgchownprog="${CHOWNPROG-chown}"
564456fccdSmrgchgrpprog="${CHGRPPROG-chgrp}"
574456fccdSmrgstripprog="${STRIPPROG-strip}"
584456fccdSmrgrmprog="${RMPROG-rm}"
594456fccdSmrgmkdirprog="${MKDIRPROG-mkdir}"
604456fccdSmrg
614456fccdSmrgchmodcmd="$chmodprog 0755"
624456fccdSmrgchowncmd=
634456fccdSmrgchgrpcmd=
644456fccdSmrgstripcmd=
654456fccdSmrgrmcmd="$rmprog -f"
664456fccdSmrgmvcmd="$mvprog"
674456fccdSmrgsrc=
684456fccdSmrgdst=
694456fccdSmrgdir_arg=
704456fccdSmrgdstarg=
714456fccdSmrgno_target_directory=
724456fccdSmrg
734456fccdSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
744456fccdSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
754456fccdSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
764456fccdSmrg   or: $0 [OPTION]... -d DIRECTORIES...
774456fccdSmrg
784456fccdSmrgIn the 1st form, copy SRCFILE to DSTFILE.
794456fccdSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
804456fccdSmrgIn the 4th, create DIRECTORIES.
814456fccdSmrg
824456fccdSmrgOptions:
834456fccdSmrg-c         (ignored)
844456fccdSmrg-d         create directories instead of installing files.
854456fccdSmrg-g GROUP   $chgrpprog installed files to GROUP.
864456fccdSmrg-m MODE    $chmodprog installed files to MODE.
874456fccdSmrg-o USER    $chownprog installed files to USER.
884456fccdSmrg-s         $stripprog installed files.
894456fccdSmrg-t DIRECTORY  install into DIRECTORY.
904456fccdSmrg-T         report an error if DSTFILE is a directory.
914456fccdSmrg--help     display this help and exit.
924456fccdSmrg--version  display version info and exit.
934456fccdSmrg
944456fccdSmrgEnvironment variables override the default commands:
954456fccdSmrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
964456fccdSmrg"
974456fccdSmrg
984456fccdSmrgwhile test -n "$1"; do
994456fccdSmrg  case $1 in
1004456fccdSmrg    -c) shift
1014456fccdSmrg        continue;;
1024456fccdSmrg
1034456fccdSmrg    -d) dir_arg=true
1044456fccdSmrg        shift
1054456fccdSmrg        continue;;
1064456fccdSmrg
1074456fccdSmrg    -g) chgrpcmd="$chgrpprog $2"
1084456fccdSmrg        shift
1094456fccdSmrg        shift
1104456fccdSmrg        continue;;
1114456fccdSmrg
1124456fccdSmrg    --help) echo "$usage"; exit $?;;
1134456fccdSmrg
1144456fccdSmrg    -m) chmodcmd="$chmodprog $2"
1154456fccdSmrg        shift
1164456fccdSmrg        shift
1174456fccdSmrg        continue;;
1184456fccdSmrg
1194456fccdSmrg    -o) chowncmd="$chownprog $2"
1204456fccdSmrg        shift
1214456fccdSmrg        shift
1224456fccdSmrg        continue;;
1234456fccdSmrg
1244456fccdSmrg    -s) stripcmd=$stripprog
1254456fccdSmrg        shift
1264456fccdSmrg        continue;;
1274456fccdSmrg
1284456fccdSmrg    -t) dstarg=$2
1294456fccdSmrg	shift
1304456fccdSmrg	shift
1314456fccdSmrg	continue;;
1324456fccdSmrg
1334456fccdSmrg    -T) no_target_directory=true
1344456fccdSmrg	shift
1354456fccdSmrg	continue;;
1364456fccdSmrg
1374456fccdSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1384456fccdSmrg
1394456fccdSmrg    *)  # When -d is used, all remaining arguments are directories to create.
1404456fccdSmrg	# When -t is used, the destination is already specified.
1414456fccdSmrg	test -n "$dir_arg$dstarg" && break
1424456fccdSmrg        # Otherwise, the last argument is the destination.  Remove it from $@.
1434456fccdSmrg	for arg
1444456fccdSmrg	do
1454456fccdSmrg          if test -n "$dstarg"; then
1464456fccdSmrg	    # $@ is not empty: it contains at least $arg.
1474456fccdSmrg	    set fnord "$@" "$dstarg"
1484456fccdSmrg	    shift # fnord
1494456fccdSmrg	  fi
1504456fccdSmrg	  shift # arg
1514456fccdSmrg	  dstarg=$arg
1524456fccdSmrg	done
1534456fccdSmrg	break;;
1544456fccdSmrg  esac
1554456fccdSmrgdone
1564456fccdSmrg
1574456fccdSmrgif test -z "$1"; then
1584456fccdSmrg  if test -z "$dir_arg"; then
1594456fccdSmrg    echo "$0: no input file specified." >&2
1604456fccdSmrg    exit 1
1614456fccdSmrg  fi
1624456fccdSmrg  # It's OK to call `install-sh -d' without argument.
1634456fccdSmrg  # This can happen when creating conditional directories.
1644456fccdSmrg  exit 0
1654456fccdSmrgfi
1664456fccdSmrg
1674456fccdSmrgfor src
1684456fccdSmrgdo
1694456fccdSmrg  # Protect names starting with `-'.
1704456fccdSmrg  case $src in
1714456fccdSmrg    -*) src=./$src ;;
1724456fccdSmrg  esac
1734456fccdSmrg
1744456fccdSmrg  if test -n "$dir_arg"; then
1754456fccdSmrg    dst=$src
1764456fccdSmrg    src=
1774456fccdSmrg
1784456fccdSmrg    if test -d "$dst"; then
1794456fccdSmrg      mkdircmd=:
1804456fccdSmrg      chmodcmd=
1814456fccdSmrg    else
1824456fccdSmrg      mkdircmd=$mkdirprog
1834456fccdSmrg    fi
1844456fccdSmrg  else
1854456fccdSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
1864456fccdSmrg    # might cause directories to be created, which would be especially bad
1874456fccdSmrg    # if $src (and thus $dsttmp) contains '*'.
1884456fccdSmrg    if test ! -f "$src" && test ! -d "$src"; then
1894456fccdSmrg      echo "$0: $src does not exist." >&2
1904456fccdSmrg      exit 1
1914456fccdSmrg    fi
1924456fccdSmrg
1934456fccdSmrg    if test -z "$dstarg"; then
1944456fccdSmrg      echo "$0: no destination specified." >&2
1954456fccdSmrg      exit 1
1964456fccdSmrg    fi
1974456fccdSmrg
1984456fccdSmrg    dst=$dstarg
1994456fccdSmrg    # Protect names starting with `-'.
2004456fccdSmrg    case $dst in
2014456fccdSmrg      -*) dst=./$dst ;;
2024456fccdSmrg    esac
2034456fccdSmrg
2044456fccdSmrg    # If destination is a directory, append the input filename; won't work
2054456fccdSmrg    # if double slashes aren't ignored.
2064456fccdSmrg    if test -d "$dst"; then
2074456fccdSmrg      if test -n "$no_target_directory"; then
2084456fccdSmrg	echo "$0: $dstarg: Is a directory" >&2
2094456fccdSmrg	exit 1
2104456fccdSmrg      fi
2114456fccdSmrg      dst=$dst/`basename "$src"`
2124456fccdSmrg    fi
2134456fccdSmrg  fi
2144456fccdSmrg
2154456fccdSmrg  # This sed command emulates the dirname command.
2164456fccdSmrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
2174456fccdSmrg
2184456fccdSmrg  # Make sure that the destination directory exists.
2194456fccdSmrg
2204456fccdSmrg  # Skip lots of stat calls in the usual case.
2214456fccdSmrg  if test ! -d "$dstdir"; then
2224456fccdSmrg    defaultIFS='
2234456fccdSmrg	 '
2244456fccdSmrg    IFS="${IFS-$defaultIFS}"
2254456fccdSmrg
2264456fccdSmrg    oIFS=$IFS
2274456fccdSmrg    # Some sh's can't handle IFS=/ for some reason.
2284456fccdSmrg    IFS='%'
2294456fccdSmrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
2304456fccdSmrg    shift
2314456fccdSmrg    IFS=$oIFS
2324456fccdSmrg
2334456fccdSmrg    pathcomp=
2344456fccdSmrg
2354456fccdSmrg    while test $# -ne 0 ; do
2364456fccdSmrg      pathcomp=$pathcomp$1
2374456fccdSmrg      shift
2384456fccdSmrg      if test ! -d "$pathcomp"; then
2394456fccdSmrg        $mkdirprog "$pathcomp"
2404456fccdSmrg	# mkdir can fail with a `File exist' error in case several
2414456fccdSmrg	# install-sh are creating the directory concurrently.  This
2424456fccdSmrg	# is OK.
2434456fccdSmrg	test -d "$pathcomp" || exit
2444456fccdSmrg      fi
2454456fccdSmrg      pathcomp=$pathcomp/
2464456fccdSmrg    done
2474456fccdSmrg  fi
2484456fccdSmrg
2494456fccdSmrg  if test -n "$dir_arg"; then
2504456fccdSmrg    $doit $mkdircmd "$dst" \
2514456fccdSmrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
2524456fccdSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
2534456fccdSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
2544456fccdSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
2554456fccdSmrg
2564456fccdSmrg  else
2574456fccdSmrg    dstfile=`basename "$dst"`
2584456fccdSmrg
2594456fccdSmrg    # Make a couple of temp file names in the proper directory.
2604456fccdSmrg    dsttmp=$dstdir/_inst.$$_
2614456fccdSmrg    rmtmp=$dstdir/_rm.$$_
2624456fccdSmrg
2634456fccdSmrg    # Trap to clean up those temp files at exit.
2644456fccdSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
2654456fccdSmrg    trap '(exit $?); exit' 1 2 13 15
2664456fccdSmrg
2674456fccdSmrg    # Copy the file name to the temp name.
2684456fccdSmrg    $doit $cpprog "$src" "$dsttmp" &&
2694456fccdSmrg
2704456fccdSmrg    # and set any options; do chmod last to preserve setuid bits.
2714456fccdSmrg    #
2724456fccdSmrg    # If any of these fail, we abort the whole thing.  If we want to
2734456fccdSmrg    # ignore errors from any of these, just make sure not to ignore
2744456fccdSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
2754456fccdSmrg    #
2764456fccdSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
2774456fccdSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
2784456fccdSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
2794456fccdSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
2804456fccdSmrg
2814456fccdSmrg    # Now rename the file to the real destination.
2824456fccdSmrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
2834456fccdSmrg      || {
2844456fccdSmrg	   # The rename failed, perhaps because mv can't rename something else
2854456fccdSmrg	   # to itself, or perhaps because mv is so ancient that it does not
2864456fccdSmrg	   # support -f.
2874456fccdSmrg
2884456fccdSmrg	   # Now remove or move aside any old file at destination location.
2894456fccdSmrg	   # We try this two ways since rm can't unlink itself on some
2904456fccdSmrg	   # systems and the destination file might be busy for other
2914456fccdSmrg	   # reasons.  In this case, the final cleanup might fail but the new
2924456fccdSmrg	   # file should still install successfully.
2934456fccdSmrg	   {
2944456fccdSmrg	     if test -f "$dstdir/$dstfile"; then
2954456fccdSmrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
2964456fccdSmrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
2974456fccdSmrg	       || {
2984456fccdSmrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2994456fccdSmrg		 (exit 1); exit 1
3004456fccdSmrg	       }
3014456fccdSmrg	     else
3024456fccdSmrg	       :
3034456fccdSmrg	     fi
3044456fccdSmrg	   } &&
3054456fccdSmrg
3064456fccdSmrg	   # Now rename the file to the real destination.
3074456fccdSmrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
3084456fccdSmrg	 }
3094456fccdSmrg    }
3104456fccdSmrg  fi || { (exit 1); exit 1; }
3114456fccdSmrgdone
3124456fccdSmrg
3134456fccdSmrg# The final little trick to "correctly" pass the exit status to the exit trap.
3144456fccdSmrg{
3154456fccdSmrg  (exit 0); exit 0
3164456fccdSmrg}
3174456fccdSmrg
3184456fccdSmrg# Local variables:
3194456fccdSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
3204456fccdSmrg# time-stamp-start: "scriptversion="
3214456fccdSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
3224456fccdSmrg# time-stamp-end: "$"
3234456fccdSmrg# End:
324