install-sh revision 5eefee25
15eefee25Smacallan#!/bin/sh
25eefee25Smacallan# install - install a program, script, or datafile
35eefee25Smacallan
45eefee25Smacallanscriptversion=2005-05-14.22
55eefee25Smacallan
65eefee25Smacallan# This originates from X11R5 (mit/util/scripts/install.sh), which was
75eefee25Smacallan# later released in X11R6 (xc/config/util/install.sh) with the
85eefee25Smacallan# following copyright and license.
95eefee25Smacallan#
105eefee25Smacallan# Copyright (C) 1994 X Consortium
115eefee25Smacallan#
125eefee25Smacallan# Permission is hereby granted, free of charge, to any person obtaining a copy
135eefee25Smacallan# of this software and associated documentation files (the "Software"), to
145eefee25Smacallan# deal in the Software without restriction, including without limitation the
155eefee25Smacallan# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
165eefee25Smacallan# sell copies of the Software, and to permit persons to whom the Software is
175eefee25Smacallan# furnished to do so, subject to the following conditions:
185eefee25Smacallan#
195eefee25Smacallan# The above copyright notice and this permission notice shall be included in
205eefee25Smacallan# all copies or substantial portions of the Software.
215eefee25Smacallan#
225eefee25Smacallan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
235eefee25Smacallan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
245eefee25Smacallan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
255eefee25Smacallan# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
265eefee25Smacallan# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
275eefee25Smacallan# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
285eefee25Smacallan#
295eefee25Smacallan# Except as contained in this notice, the name of the X Consortium shall not
305eefee25Smacallan# be used in advertising or otherwise to promote the sale, use or other deal-
315eefee25Smacallan# ings in this Software without prior written authorization from the X Consor-
325eefee25Smacallan# tium.
335eefee25Smacallan#
345eefee25Smacallan#
355eefee25Smacallan# FSF changes to this file are in the public domain.
365eefee25Smacallan#
375eefee25Smacallan# Calling this script install-sh is preferred over install.sh, to prevent
385eefee25Smacallan# `make' implicit rules from creating a file called install from it
395eefee25Smacallan# when there is no Makefile.
405eefee25Smacallan#
415eefee25Smacallan# This script is compatible with the BSD install script, but was written
425eefee25Smacallan# from scratch.  It can only install one file at a time, a restriction
435eefee25Smacallan# shared with many OS's install programs.
445eefee25Smacallan
455eefee25Smacallan# set DOITPROG to echo to test this script
465eefee25Smacallan
475eefee25Smacallan# Don't use :- since 4.3BSD and earlier shells don't like it.
485eefee25Smacallandoit="${DOITPROG-}"
495eefee25Smacallan
505eefee25Smacallan# put in absolute paths if you don't have them in your path; or use env. vars.
515eefee25Smacallan
525eefee25Smacallanmvprog="${MVPROG-mv}"
535eefee25Smacallancpprog="${CPPROG-cp}"
545eefee25Smacallanchmodprog="${CHMODPROG-chmod}"
555eefee25Smacallanchownprog="${CHOWNPROG-chown}"
565eefee25Smacallanchgrpprog="${CHGRPPROG-chgrp}"
575eefee25Smacallanstripprog="${STRIPPROG-strip}"
585eefee25Smacallanrmprog="${RMPROG-rm}"
595eefee25Smacallanmkdirprog="${MKDIRPROG-mkdir}"
605eefee25Smacallan
615eefee25Smacallanchmodcmd="$chmodprog 0755"
625eefee25Smacallanchowncmd=
635eefee25Smacallanchgrpcmd=
645eefee25Smacallanstripcmd=
655eefee25Smacallanrmcmd="$rmprog -f"
665eefee25Smacallanmvcmd="$mvprog"
675eefee25Smacallansrc=
685eefee25Smacallandst=
695eefee25Smacallandir_arg=
705eefee25Smacallandstarg=
715eefee25Smacallanno_target_directory=
725eefee25Smacallan
735eefee25Smacallanusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
745eefee25Smacallan   or: $0 [OPTION]... SRCFILES... DIRECTORY
755eefee25Smacallan   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
765eefee25Smacallan   or: $0 [OPTION]... -d DIRECTORIES...
775eefee25Smacallan
785eefee25SmacallanIn the 1st form, copy SRCFILE to DSTFILE.
795eefee25SmacallanIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
805eefee25SmacallanIn the 4th, create DIRECTORIES.
815eefee25Smacallan
825eefee25SmacallanOptions:
835eefee25Smacallan-c         (ignored)
845eefee25Smacallan-d         create directories instead of installing files.
855eefee25Smacallan-g GROUP   $chgrpprog installed files to GROUP.
865eefee25Smacallan-m MODE    $chmodprog installed files to MODE.
875eefee25Smacallan-o USER    $chownprog installed files to USER.
885eefee25Smacallan-s         $stripprog installed files.
895eefee25Smacallan-t DIRECTORY  install into DIRECTORY.
905eefee25Smacallan-T         report an error if DSTFILE is a directory.
915eefee25Smacallan--help     display this help and exit.
925eefee25Smacallan--version  display version info and exit.
935eefee25Smacallan
945eefee25SmacallanEnvironment variables override the default commands:
955eefee25Smacallan  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
965eefee25Smacallan"
975eefee25Smacallan
985eefee25Smacallanwhile test -n "$1"; do
995eefee25Smacallan  case $1 in
1005eefee25Smacallan    -c) shift
1015eefee25Smacallan        continue;;
1025eefee25Smacallan
1035eefee25Smacallan    -d) dir_arg=true
1045eefee25Smacallan        shift
1055eefee25Smacallan        continue;;
1065eefee25Smacallan
1075eefee25Smacallan    -g) chgrpcmd="$chgrpprog $2"
1085eefee25Smacallan        shift
1095eefee25Smacallan        shift
1105eefee25Smacallan        continue;;
1115eefee25Smacallan
1125eefee25Smacallan    --help) echo "$usage"; exit $?;;
1135eefee25Smacallan
1145eefee25Smacallan    -m) chmodcmd="$chmodprog $2"
1155eefee25Smacallan        shift
1165eefee25Smacallan        shift
1175eefee25Smacallan        continue;;
1185eefee25Smacallan
1195eefee25Smacallan    -o) chowncmd="$chownprog $2"
1205eefee25Smacallan        shift
1215eefee25Smacallan        shift
1225eefee25Smacallan        continue;;
1235eefee25Smacallan
1245eefee25Smacallan    -s) stripcmd=$stripprog
1255eefee25Smacallan        shift
1265eefee25Smacallan        continue;;
1275eefee25Smacallan
1285eefee25Smacallan    -t) dstarg=$2
1295eefee25Smacallan	shift
1305eefee25Smacallan	shift
1315eefee25Smacallan	continue;;
1325eefee25Smacallan
1335eefee25Smacallan    -T) no_target_directory=true
1345eefee25Smacallan	shift
1355eefee25Smacallan	continue;;
1365eefee25Smacallan
1375eefee25Smacallan    --version) echo "$0 $scriptversion"; exit $?;;
1385eefee25Smacallan
1395eefee25Smacallan    *)  # When -d is used, all remaining arguments are directories to create.
1405eefee25Smacallan	# When -t is used, the destination is already specified.
1415eefee25Smacallan	test -n "$dir_arg$dstarg" && break
1425eefee25Smacallan        # Otherwise, the last argument is the destination.  Remove it from $@.
1435eefee25Smacallan	for arg
1445eefee25Smacallan	do
1455eefee25Smacallan          if test -n "$dstarg"; then
1465eefee25Smacallan	    # $@ is not empty: it contains at least $arg.
1475eefee25Smacallan	    set fnord "$@" "$dstarg"
1485eefee25Smacallan	    shift # fnord
1495eefee25Smacallan	  fi
1505eefee25Smacallan	  shift # arg
1515eefee25Smacallan	  dstarg=$arg
1525eefee25Smacallan	done
1535eefee25Smacallan	break;;
1545eefee25Smacallan  esac
1555eefee25Smacallandone
1565eefee25Smacallan
1575eefee25Smacallanif test -z "$1"; then
1585eefee25Smacallan  if test -z "$dir_arg"; then
1595eefee25Smacallan    echo "$0: no input file specified." >&2
1605eefee25Smacallan    exit 1
1615eefee25Smacallan  fi
1625eefee25Smacallan  # It's OK to call `install-sh -d' without argument.
1635eefee25Smacallan  # This can happen when creating conditional directories.
1645eefee25Smacallan  exit 0
1655eefee25Smacallanfi
1665eefee25Smacallan
1675eefee25Smacallanfor src
1685eefee25Smacallando
1695eefee25Smacallan  # Protect names starting with `-'.
1705eefee25Smacallan  case $src in
1715eefee25Smacallan    -*) src=./$src ;;
1725eefee25Smacallan  esac
1735eefee25Smacallan
1745eefee25Smacallan  if test -n "$dir_arg"; then
1755eefee25Smacallan    dst=$src
1765eefee25Smacallan    src=
1775eefee25Smacallan
1785eefee25Smacallan    if test -d "$dst"; then
1795eefee25Smacallan      mkdircmd=:
1805eefee25Smacallan      chmodcmd=
1815eefee25Smacallan    else
1825eefee25Smacallan      mkdircmd=$mkdirprog
1835eefee25Smacallan    fi
1845eefee25Smacallan  else
1855eefee25Smacallan    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
1865eefee25Smacallan    # might cause directories to be created, which would be especially bad
1875eefee25Smacallan    # if $src (and thus $dsttmp) contains '*'.
1885eefee25Smacallan    if test ! -f "$src" && test ! -d "$src"; then
1895eefee25Smacallan      echo "$0: $src does not exist." >&2
1905eefee25Smacallan      exit 1
1915eefee25Smacallan    fi
1925eefee25Smacallan
1935eefee25Smacallan    if test -z "$dstarg"; then
1945eefee25Smacallan      echo "$0: no destination specified." >&2
1955eefee25Smacallan      exit 1
1965eefee25Smacallan    fi
1975eefee25Smacallan
1985eefee25Smacallan    dst=$dstarg
1995eefee25Smacallan    # Protect names starting with `-'.
2005eefee25Smacallan    case $dst in
2015eefee25Smacallan      -*) dst=./$dst ;;
2025eefee25Smacallan    esac
2035eefee25Smacallan
2045eefee25Smacallan    # If destination is a directory, append the input filename; won't work
2055eefee25Smacallan    # if double slashes aren't ignored.
2065eefee25Smacallan    if test -d "$dst"; then
2075eefee25Smacallan      if test -n "$no_target_directory"; then
2085eefee25Smacallan	echo "$0: $dstarg: Is a directory" >&2
2095eefee25Smacallan	exit 1
2105eefee25Smacallan      fi
2115eefee25Smacallan      dst=$dst/`basename "$src"`
2125eefee25Smacallan    fi
2135eefee25Smacallan  fi
2145eefee25Smacallan
2155eefee25Smacallan  # This sed command emulates the dirname command.
2165eefee25Smacallan  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
2175eefee25Smacallan
2185eefee25Smacallan  # Make sure that the destination directory exists.
2195eefee25Smacallan
2205eefee25Smacallan  # Skip lots of stat calls in the usual case.
2215eefee25Smacallan  if test ! -d "$dstdir"; then
2225eefee25Smacallan    defaultIFS='
2235eefee25Smacallan	 '
2245eefee25Smacallan    IFS="${IFS-$defaultIFS}"
2255eefee25Smacallan
2265eefee25Smacallan    oIFS=$IFS
2275eefee25Smacallan    # Some sh's can't handle IFS=/ for some reason.
2285eefee25Smacallan    IFS='%'
2295eefee25Smacallan    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
2305eefee25Smacallan    shift
2315eefee25Smacallan    IFS=$oIFS
2325eefee25Smacallan
2335eefee25Smacallan    pathcomp=
2345eefee25Smacallan
2355eefee25Smacallan    while test $# -ne 0 ; do
2365eefee25Smacallan      pathcomp=$pathcomp$1
2375eefee25Smacallan      shift
2385eefee25Smacallan      if test ! -d "$pathcomp"; then
2395eefee25Smacallan        $mkdirprog "$pathcomp"
2405eefee25Smacallan	# mkdir can fail with a `File exist' error in case several
2415eefee25Smacallan	# install-sh are creating the directory concurrently.  This
2425eefee25Smacallan	# is OK.
2435eefee25Smacallan	test -d "$pathcomp" || exit
2445eefee25Smacallan      fi
2455eefee25Smacallan      pathcomp=$pathcomp/
2465eefee25Smacallan    done
2475eefee25Smacallan  fi
2485eefee25Smacallan
2495eefee25Smacallan  if test -n "$dir_arg"; then
2505eefee25Smacallan    $doit $mkdircmd "$dst" \
2515eefee25Smacallan      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
2525eefee25Smacallan      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
2535eefee25Smacallan      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
2545eefee25Smacallan      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
2555eefee25Smacallan
2565eefee25Smacallan  else
2575eefee25Smacallan    dstfile=`basename "$dst"`
2585eefee25Smacallan
2595eefee25Smacallan    # Make a couple of temp file names in the proper directory.
2605eefee25Smacallan    dsttmp=$dstdir/_inst.$$_
2615eefee25Smacallan    rmtmp=$dstdir/_rm.$$_
2625eefee25Smacallan
2635eefee25Smacallan    # Trap to clean up those temp files at exit.
2645eefee25Smacallan    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
2655eefee25Smacallan    trap '(exit $?); exit' 1 2 13 15
2665eefee25Smacallan
2675eefee25Smacallan    # Copy the file name to the temp name.
2685eefee25Smacallan    $doit $cpprog "$src" "$dsttmp" &&
2695eefee25Smacallan
2705eefee25Smacallan    # and set any options; do chmod last to preserve setuid bits.
2715eefee25Smacallan    #
2725eefee25Smacallan    # If any of these fail, we abort the whole thing.  If we want to
2735eefee25Smacallan    # ignore errors from any of these, just make sure not to ignore
2745eefee25Smacallan    # errors from the above "$doit $cpprog $src $dsttmp" command.
2755eefee25Smacallan    #
2765eefee25Smacallan    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
2775eefee25Smacallan      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
2785eefee25Smacallan      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
2795eefee25Smacallan      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
2805eefee25Smacallan
2815eefee25Smacallan    # Now rename the file to the real destination.
2825eefee25Smacallan    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
2835eefee25Smacallan      || {
2845eefee25Smacallan	   # The rename failed, perhaps because mv can't rename something else
2855eefee25Smacallan	   # to itself, or perhaps because mv is so ancient that it does not
2865eefee25Smacallan	   # support -f.
2875eefee25Smacallan
2885eefee25Smacallan	   # Now remove or move aside any old file at destination location.
2895eefee25Smacallan	   # We try this two ways since rm can't unlink itself on some
2905eefee25Smacallan	   # systems and the destination file might be busy for other
2915eefee25Smacallan	   # reasons.  In this case, the final cleanup might fail but the new
2925eefee25Smacallan	   # file should still install successfully.
2935eefee25Smacallan	   {
2945eefee25Smacallan	     if test -f "$dstdir/$dstfile"; then
2955eefee25Smacallan	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
2965eefee25Smacallan	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
2975eefee25Smacallan	       || {
2985eefee25Smacallan		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2995eefee25Smacallan		 (exit 1); exit 1
3005eefee25Smacallan	       }
3015eefee25Smacallan	     else
3025eefee25Smacallan	       :
3035eefee25Smacallan	     fi
3045eefee25Smacallan	   } &&
3055eefee25Smacallan
3065eefee25Smacallan	   # Now rename the file to the real destination.
3075eefee25Smacallan	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
3085eefee25Smacallan	 }
3095eefee25Smacallan    }
3105eefee25Smacallan  fi || { (exit 1); exit 1; }
3115eefee25Smacallandone
3125eefee25Smacallan
3135eefee25Smacallan# The final little trick to "correctly" pass the exit status to the exit trap.
3145eefee25Smacallan{
3155eefee25Smacallan  (exit 0); exit 0
3165eefee25Smacallan}
3175eefee25Smacallan
3185eefee25Smacallan# Local variables:
3195eefee25Smacallan# eval: (add-hook 'write-file-hooks 'time-stamp)
3205eefee25Smacallan# time-stamp-start: "scriptversion="
3215eefee25Smacallan# time-stamp-format: "%:y-%02m-%02d.%02H"
3225eefee25Smacallan# time-stamp-end: "$"
3235eefee25Smacallan# End:
324