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