install-sh revision 6f03b1f6
16f03b1f6Smrg#!/bin/sh
26f03b1f6Smrg# install - install a program, script, or datafile
36f03b1f6Smrg
46f03b1f6Smrgscriptversion=2005-05-14.22
56f03b1f6Smrg
66f03b1f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76f03b1f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86f03b1f6Smrg# following copyright and license.
96f03b1f6Smrg#
106f03b1f6Smrg# Copyright (C) 1994 X Consortium
116f03b1f6Smrg#
126f03b1f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136f03b1f6Smrg# of this software and associated documentation files (the "Software"), to
146f03b1f6Smrg# deal in the Software without restriction, including without limitation the
156f03b1f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166f03b1f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
176f03b1f6Smrg# furnished to do so, subject to the following conditions:
186f03b1f6Smrg#
196f03b1f6Smrg# The above copyright notice and this permission notice shall be included in
206f03b1f6Smrg# all copies or substantial portions of the Software.
216f03b1f6Smrg#
226f03b1f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236f03b1f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246f03b1f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256f03b1f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266f03b1f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276f03b1f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286f03b1f6Smrg#
296f03b1f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
306f03b1f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316f03b1f6Smrg# ings in this Software without prior written authorization from the X Consor-
326f03b1f6Smrg# tium.
336f03b1f6Smrg#
346f03b1f6Smrg#
356f03b1f6Smrg# FSF changes to this file are in the public domain.
366f03b1f6Smrg#
376f03b1f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
386f03b1f6Smrg# `make' implicit rules from creating a file called install from it
396f03b1f6Smrg# when there is no Makefile.
406f03b1f6Smrg#
416f03b1f6Smrg# This script is compatible with the BSD install script, but was written
426f03b1f6Smrg# from scratch.  It can only install one file at a time, a restriction
436f03b1f6Smrg# shared with many OS's install programs.
446f03b1f6Smrg
456f03b1f6Smrg# set DOITPROG to echo to test this script
466f03b1f6Smrg
476f03b1f6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
486f03b1f6Smrgdoit="${DOITPROG-}"
496f03b1f6Smrg
506f03b1f6Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
516f03b1f6Smrg
526f03b1f6Smrgmvprog="${MVPROG-mv}"
536f03b1f6Smrgcpprog="${CPPROG-cp}"
546f03b1f6Smrgchmodprog="${CHMODPROG-chmod}"
556f03b1f6Smrgchownprog="${CHOWNPROG-chown}"
566f03b1f6Smrgchgrpprog="${CHGRPPROG-chgrp}"
576f03b1f6Smrgstripprog="${STRIPPROG-strip}"
586f03b1f6Smrgrmprog="${RMPROG-rm}"
596f03b1f6Smrgmkdirprog="${MKDIRPROG-mkdir}"
606f03b1f6Smrg
616f03b1f6Smrgchmodcmd="$chmodprog 0755"
626f03b1f6Smrgchowncmd=
636f03b1f6Smrgchgrpcmd=
646f03b1f6Smrgstripcmd=
656f03b1f6Smrgrmcmd="$rmprog -f"
666f03b1f6Smrgmvcmd="$mvprog"
676f03b1f6Smrgsrc=
686f03b1f6Smrgdst=
696f03b1f6Smrgdir_arg=
706f03b1f6Smrgdstarg=
716f03b1f6Smrgno_target_directory=
726f03b1f6Smrg
736f03b1f6Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
746f03b1f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
756f03b1f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
766f03b1f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
776f03b1f6Smrg
786f03b1f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
796f03b1f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
806f03b1f6SmrgIn the 4th, create DIRECTORIES.
816f03b1f6Smrg
826f03b1f6SmrgOptions:
836f03b1f6Smrg-c         (ignored)
846f03b1f6Smrg-d         create directories instead of installing files.
856f03b1f6Smrg-g GROUP   $chgrpprog installed files to GROUP.
866f03b1f6Smrg-m MODE    $chmodprog installed files to MODE.
876f03b1f6Smrg-o USER    $chownprog installed files to USER.
886f03b1f6Smrg-s         $stripprog installed files.
896f03b1f6Smrg-t DIRECTORY  install into DIRECTORY.
906f03b1f6Smrg-T         report an error if DSTFILE is a directory.
916f03b1f6Smrg--help     display this help and exit.
926f03b1f6Smrg--version  display version info and exit.
936f03b1f6Smrg
946f03b1f6SmrgEnvironment variables override the default commands:
956f03b1f6Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
966f03b1f6Smrg"
976f03b1f6Smrg
986f03b1f6Smrgwhile test -n "$1"; do
996f03b1f6Smrg  case $1 in
1006f03b1f6Smrg    -c) shift
1016f03b1f6Smrg        continue;;
1026f03b1f6Smrg
1036f03b1f6Smrg    -d) dir_arg=true
1046f03b1f6Smrg        shift
1056f03b1f6Smrg        continue;;
1066f03b1f6Smrg
1076f03b1f6Smrg    -g) chgrpcmd="$chgrpprog $2"
1086f03b1f6Smrg        shift
1096f03b1f6Smrg        shift
1106f03b1f6Smrg        continue;;
1116f03b1f6Smrg
1126f03b1f6Smrg    --help) echo "$usage"; exit $?;;
1136f03b1f6Smrg
1146f03b1f6Smrg    -m) chmodcmd="$chmodprog $2"
1156f03b1f6Smrg        shift
1166f03b1f6Smrg        shift
1176f03b1f6Smrg        continue;;
1186f03b1f6Smrg
1196f03b1f6Smrg    -o) chowncmd="$chownprog $2"
1206f03b1f6Smrg        shift
1216f03b1f6Smrg        shift
1226f03b1f6Smrg        continue;;
1236f03b1f6Smrg
1246f03b1f6Smrg    -s) stripcmd=$stripprog
1256f03b1f6Smrg        shift
1266f03b1f6Smrg        continue;;
1276f03b1f6Smrg
1286f03b1f6Smrg    -t) dstarg=$2
1296f03b1f6Smrg	shift
1306f03b1f6Smrg	shift
1316f03b1f6Smrg	continue;;
1326f03b1f6Smrg
1336f03b1f6Smrg    -T) no_target_directory=true
1346f03b1f6Smrg	shift
1356f03b1f6Smrg	continue;;
1366f03b1f6Smrg
1376f03b1f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1386f03b1f6Smrg
1396f03b1f6Smrg    *)  # When -d is used, all remaining arguments are directories to create.
1406f03b1f6Smrg	# When -t is used, the destination is already specified.
1416f03b1f6Smrg	test -n "$dir_arg$dstarg" && break
1426f03b1f6Smrg        # Otherwise, the last argument is the destination.  Remove it from $@.
1436f03b1f6Smrg	for arg
1446f03b1f6Smrg	do
1456f03b1f6Smrg          if test -n "$dstarg"; then
1466f03b1f6Smrg	    # $@ is not empty: it contains at least $arg.
1476f03b1f6Smrg	    set fnord "$@" "$dstarg"
1486f03b1f6Smrg	    shift # fnord
1496f03b1f6Smrg	  fi
1506f03b1f6Smrg	  shift # arg
1516f03b1f6Smrg	  dstarg=$arg
1526f03b1f6Smrg	done
1536f03b1f6Smrg	break;;
1546f03b1f6Smrg  esac
1556f03b1f6Smrgdone
1566f03b1f6Smrg
1576f03b1f6Smrgif test -z "$1"; then
1586f03b1f6Smrg  if test -z "$dir_arg"; then
1596f03b1f6Smrg    echo "$0: no input file specified." >&2
1606f03b1f6Smrg    exit 1
1616f03b1f6Smrg  fi
1626f03b1f6Smrg  # It's OK to call `install-sh -d' without argument.
1636f03b1f6Smrg  # This can happen when creating conditional directories.
1646f03b1f6Smrg  exit 0
1656f03b1f6Smrgfi
1666f03b1f6Smrg
1676f03b1f6Smrgfor src
1686f03b1f6Smrgdo
1696f03b1f6Smrg  # Protect names starting with `-'.
1706f03b1f6Smrg  case $src in
1716f03b1f6Smrg    -*) src=./$src ;;
1726f03b1f6Smrg  esac
1736f03b1f6Smrg
1746f03b1f6Smrg  if test -n "$dir_arg"; then
1756f03b1f6Smrg    dst=$src
1766f03b1f6Smrg    src=
1776f03b1f6Smrg
1786f03b1f6Smrg    if test -d "$dst"; then
1796f03b1f6Smrg      mkdircmd=:
1806f03b1f6Smrg      chmodcmd=
1816f03b1f6Smrg    else
1826f03b1f6Smrg      mkdircmd=$mkdirprog
1836f03b1f6Smrg    fi
1846f03b1f6Smrg  else
1856f03b1f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
1866f03b1f6Smrg    # might cause directories to be created, which would be especially bad
1876f03b1f6Smrg    # if $src (and thus $dsttmp) contains '*'.
1886f03b1f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
1896f03b1f6Smrg      echo "$0: $src does not exist." >&2
1906f03b1f6Smrg      exit 1
1916f03b1f6Smrg    fi
1926f03b1f6Smrg
1936f03b1f6Smrg    if test -z "$dstarg"; then
1946f03b1f6Smrg      echo "$0: no destination specified." >&2
1956f03b1f6Smrg      exit 1
1966f03b1f6Smrg    fi
1976f03b1f6Smrg
1986f03b1f6Smrg    dst=$dstarg
1996f03b1f6Smrg    # Protect names starting with `-'.
2006f03b1f6Smrg    case $dst in
2016f03b1f6Smrg      -*) dst=./$dst ;;
2026f03b1f6Smrg    esac
2036f03b1f6Smrg
2046f03b1f6Smrg    # If destination is a directory, append the input filename; won't work
2056f03b1f6Smrg    # if double slashes aren't ignored.
2066f03b1f6Smrg    if test -d "$dst"; then
2076f03b1f6Smrg      if test -n "$no_target_directory"; then
2086f03b1f6Smrg	echo "$0: $dstarg: Is a directory" >&2
2096f03b1f6Smrg	exit 1
2106f03b1f6Smrg      fi
2116f03b1f6Smrg      dst=$dst/`basename "$src"`
2126f03b1f6Smrg    fi
2136f03b1f6Smrg  fi
2146f03b1f6Smrg
2156f03b1f6Smrg  # This sed command emulates the dirname command.
2166f03b1f6Smrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
2176f03b1f6Smrg
2186f03b1f6Smrg  # Make sure that the destination directory exists.
2196f03b1f6Smrg
2206f03b1f6Smrg  # Skip lots of stat calls in the usual case.
2216f03b1f6Smrg  if test ! -d "$dstdir"; then
2226f03b1f6Smrg    defaultIFS='
2236f03b1f6Smrg	 '
2246f03b1f6Smrg    IFS="${IFS-$defaultIFS}"
2256f03b1f6Smrg
2266f03b1f6Smrg    oIFS=$IFS
2276f03b1f6Smrg    # Some sh's can't handle IFS=/ for some reason.
2286f03b1f6Smrg    IFS='%'
2296f03b1f6Smrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
2306f03b1f6Smrg    shift
2316f03b1f6Smrg    IFS=$oIFS
2326f03b1f6Smrg
2336f03b1f6Smrg    pathcomp=
2346f03b1f6Smrg
2356f03b1f6Smrg    while test $# -ne 0 ; do
2366f03b1f6Smrg      pathcomp=$pathcomp$1
2376f03b1f6Smrg      shift
2386f03b1f6Smrg      if test ! -d "$pathcomp"; then
2396f03b1f6Smrg        $mkdirprog "$pathcomp"
2406f03b1f6Smrg	# mkdir can fail with a `File exist' error in case several
2416f03b1f6Smrg	# install-sh are creating the directory concurrently.  This
2426f03b1f6Smrg	# is OK.
2436f03b1f6Smrg	test -d "$pathcomp" || exit
2446f03b1f6Smrg      fi
2456f03b1f6Smrg      pathcomp=$pathcomp/
2466f03b1f6Smrg    done
2476f03b1f6Smrg  fi
2486f03b1f6Smrg
2496f03b1f6Smrg  if test -n "$dir_arg"; then
2506f03b1f6Smrg    $doit $mkdircmd "$dst" \
2516f03b1f6Smrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
2526f03b1f6Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
2536f03b1f6Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
2546f03b1f6Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
2556f03b1f6Smrg
2566f03b1f6Smrg  else
2576f03b1f6Smrg    dstfile=`basename "$dst"`
2586f03b1f6Smrg
2596f03b1f6Smrg    # Make a couple of temp file names in the proper directory.
2606f03b1f6Smrg    dsttmp=$dstdir/_inst.$$_
2616f03b1f6Smrg    rmtmp=$dstdir/_rm.$$_
2626f03b1f6Smrg
2636f03b1f6Smrg    # Trap to clean up those temp files at exit.
2646f03b1f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
2656f03b1f6Smrg    trap '(exit $?); exit' 1 2 13 15
2666f03b1f6Smrg
2676f03b1f6Smrg    # Copy the file name to the temp name.
2686f03b1f6Smrg    $doit $cpprog "$src" "$dsttmp" &&
2696f03b1f6Smrg
2706f03b1f6Smrg    # and set any options; do chmod last to preserve setuid bits.
2716f03b1f6Smrg    #
2726f03b1f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
2736f03b1f6Smrg    # ignore errors from any of these, just make sure not to ignore
2746f03b1f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
2756f03b1f6Smrg    #
2766f03b1f6Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
2776f03b1f6Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
2786f03b1f6Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
2796f03b1f6Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
2806f03b1f6Smrg
2816f03b1f6Smrg    # Now rename the file to the real destination.
2826f03b1f6Smrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
2836f03b1f6Smrg      || {
2846f03b1f6Smrg	   # The rename failed, perhaps because mv can't rename something else
2856f03b1f6Smrg	   # to itself, or perhaps because mv is so ancient that it does not
2866f03b1f6Smrg	   # support -f.
2876f03b1f6Smrg
2886f03b1f6Smrg	   # Now remove or move aside any old file at destination location.
2896f03b1f6Smrg	   # We try this two ways since rm can't unlink itself on some
2906f03b1f6Smrg	   # systems and the destination file might be busy for other
2916f03b1f6Smrg	   # reasons.  In this case, the final cleanup might fail but the new
2926f03b1f6Smrg	   # file should still install successfully.
2936f03b1f6Smrg	   {
2946f03b1f6Smrg	     if test -f "$dstdir/$dstfile"; then
2956f03b1f6Smrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
2966f03b1f6Smrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
2976f03b1f6Smrg	       || {
2986f03b1f6Smrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2996f03b1f6Smrg		 (exit 1); exit 1
3006f03b1f6Smrg	       }
3016f03b1f6Smrg	     else
3026f03b1f6Smrg	       :
3036f03b1f6Smrg	     fi
3046f03b1f6Smrg	   } &&
3056f03b1f6Smrg
3066f03b1f6Smrg	   # Now rename the file to the real destination.
3076f03b1f6Smrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
3086f03b1f6Smrg	 }
3096f03b1f6Smrg    }
3106f03b1f6Smrg  fi || { (exit 1); exit 1; }
3116f03b1f6Smrgdone
3126f03b1f6Smrg
3136f03b1f6Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
3146f03b1f6Smrg{
3156f03b1f6Smrg  (exit 0); exit 0
3166f03b1f6Smrg}
3176f03b1f6Smrg
3186f03b1f6Smrg# Local variables:
3196f03b1f6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
3206f03b1f6Smrg# time-stamp-start: "scriptversion="
3216f03b1f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
3226f03b1f6Smrg# time-stamp-end: "$"
3236f03b1f6Smrg# End:
324