build-scripts/install-sh
changeset 1361 19418e4422cb
child 12335 5e68900d2232
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/build-scripts/install-sh	Thu Feb 16 10:11:48 2006 +0000
     1.3 @@ -0,0 +1,323 @@
     1.4 +#!/bin/sh
     1.5 +# install - install a program, script, or datafile
     1.6 +
     1.7 +scriptversion=2005-02-02.21
     1.8 +
     1.9 +# This originates from X11R5 (mit/util/scripts/install.sh), which was
    1.10 +# later released in X11R6 (xc/config/util/install.sh) with the
    1.11 +# following copyright and license.
    1.12 +#
    1.13 +# Copyright (C) 1994 X Consortium
    1.14 +#
    1.15 +# Permission is hereby granted, free of charge, to any person obtaining a copy
    1.16 +# of this software and associated documentation files (the "Software"), to
    1.17 +# deal in the Software without restriction, including without limitation the
    1.18 +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    1.19 +# sell copies of the Software, and to permit persons to whom the Software is
    1.20 +# furnished to do so, subject to the following conditions:
    1.21 +#
    1.22 +# The above copyright notice and this permission notice shall be included in
    1.23 +# all copies or substantial portions of the Software.
    1.24 +#
    1.25 +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.26 +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.27 +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    1.28 +# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    1.29 +# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
    1.30 +# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.31 +#
    1.32 +# Except as contained in this notice, the name of the X Consortium shall not
    1.33 +# be used in advertising or otherwise to promote the sale, use or other deal-
    1.34 +# ings in this Software without prior written authorization from the X Consor-
    1.35 +# tium.
    1.36 +#
    1.37 +#
    1.38 +# FSF changes to this file are in the public domain.
    1.39 +#
    1.40 +# Calling this script install-sh is preferred over install.sh, to prevent
    1.41 +# `make' implicit rules from creating a file called install from it
    1.42 +# when there is no Makefile.
    1.43 +#
    1.44 +# This script is compatible with the BSD install script, but was written
    1.45 +# from scratch.  It can only install one file at a time, a restriction
    1.46 +# shared with many OS's install programs.
    1.47 +
    1.48 +# set DOITPROG to echo to test this script
    1.49 +
    1.50 +# Don't use :- since 4.3BSD and earlier shells don't like it.
    1.51 +doit="${DOITPROG-}"
    1.52 +
    1.53 +# put in absolute paths if you don't have them in your path; or use env. vars.
    1.54 +
    1.55 +mvprog="${MVPROG-mv}"
    1.56 +cpprog="${CPPROG-cp}"
    1.57 +chmodprog="${CHMODPROG-chmod}"
    1.58 +chownprog="${CHOWNPROG-chown}"
    1.59 +chgrpprog="${CHGRPPROG-chgrp}"
    1.60 +stripprog="${STRIPPROG-strip}"
    1.61 +rmprog="${RMPROG-rm}"
    1.62 +mkdirprog="${MKDIRPROG-mkdir}"
    1.63 +
    1.64 +chmodcmd="$chmodprog 0755"
    1.65 +chowncmd=
    1.66 +chgrpcmd=
    1.67 +stripcmd=
    1.68 +rmcmd="$rmprog -f"
    1.69 +mvcmd="$mvprog"
    1.70 +src=
    1.71 +dst=
    1.72 +dir_arg=
    1.73 +dstarg=
    1.74 +no_target_directory=
    1.75 +
    1.76 +usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
    1.77 +   or: $0 [OPTION]... SRCFILES... DIRECTORY
    1.78 +   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
    1.79 +   or: $0 [OPTION]... -d DIRECTORIES...
    1.80 +
    1.81 +In the 1st form, copy SRCFILE to DSTFILE.
    1.82 +In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
    1.83 +In the 4th, create DIRECTORIES.
    1.84 +
    1.85 +Options:
    1.86 +-c         (ignored)
    1.87 +-d         create directories instead of installing files.
    1.88 +-g GROUP   $chgrpprog installed files to GROUP.
    1.89 +-m MODE    $chmodprog installed files to MODE.
    1.90 +-o USER    $chownprog installed files to USER.
    1.91 +-s         $stripprog installed files.
    1.92 +-t DIRECTORY  install into DIRECTORY.
    1.93 +-T         report an error if DSTFILE is a directory.
    1.94 +--help     display this help and exit.
    1.95 +--version  display version info and exit.
    1.96 +
    1.97 +Environment variables override the default commands:
    1.98 +  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
    1.99 +"
   1.100 +
   1.101 +while test -n "$1"; do
   1.102 +  case $1 in
   1.103 +    -c) shift
   1.104 +        continue;;
   1.105 +
   1.106 +    -d) dir_arg=true
   1.107 +        shift
   1.108 +        continue;;
   1.109 +
   1.110 +    -g) chgrpcmd="$chgrpprog $2"
   1.111 +        shift
   1.112 +        shift
   1.113 +        continue;;
   1.114 +
   1.115 +    --help) echo "$usage"; exit $?;;
   1.116 +
   1.117 +    -m) chmodcmd="$chmodprog $2"
   1.118 +        shift
   1.119 +        shift
   1.120 +        continue;;
   1.121 +
   1.122 +    -o) chowncmd="$chownprog $2"
   1.123 +        shift
   1.124 +        shift
   1.125 +        continue;;
   1.126 +
   1.127 +    -s) stripcmd=$stripprog
   1.128 +        shift
   1.129 +        continue;;
   1.130 +
   1.131 +    -t) dstarg=$2
   1.132 +	shift
   1.133 +	shift
   1.134 +	continue;;
   1.135 +
   1.136 +    -T) no_target_directory=true
   1.137 +	shift
   1.138 +	continue;;
   1.139 +
   1.140 +    --version) echo "$0 $scriptversion"; exit $?;;
   1.141 +
   1.142 +    *)  # When -d is used, all remaining arguments are directories to create.
   1.143 +	# When -t is used, the destination is already specified.
   1.144 +	test -n "$dir_arg$dstarg" && break
   1.145 +        # Otherwise, the last argument is the destination.  Remove it from $@.
   1.146 +	for arg
   1.147 +	do
   1.148 +          if test -n "$dstarg"; then
   1.149 +	    # $@ is not empty: it contains at least $arg.
   1.150 +	    set fnord "$@" "$dstarg"
   1.151 +	    shift # fnord
   1.152 +	  fi
   1.153 +	  shift # arg
   1.154 +	  dstarg=$arg
   1.155 +	done
   1.156 +	break;;
   1.157 +  esac
   1.158 +done
   1.159 +
   1.160 +if test -z "$1"; then
   1.161 +  if test -z "$dir_arg"; then
   1.162 +    echo "$0: no input file specified." >&2
   1.163 +    exit 1
   1.164 +  fi
   1.165 +  # It's OK to call `install-sh -d' without argument.
   1.166 +  # This can happen when creating conditional directories.
   1.167 +  exit 0
   1.168 +fi
   1.169 +
   1.170 +for src
   1.171 +do
   1.172 +  # Protect names starting with `-'.
   1.173 +  case $src in
   1.174 +    -*) src=./$src ;;
   1.175 +  esac
   1.176 +
   1.177 +  if test -n "$dir_arg"; then
   1.178 +    dst=$src
   1.179 +    src=
   1.180 +
   1.181 +    if test -d "$dst"; then
   1.182 +      mkdircmd=:
   1.183 +      chmodcmd=
   1.184 +    else
   1.185 +      mkdircmd=$mkdirprog
   1.186 +    fi
   1.187 +  else
   1.188 +    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
   1.189 +    # might cause directories to be created, which would be especially bad
   1.190 +    # if $src (and thus $dsttmp) contains '*'.
   1.191 +    if test ! -f "$src" && test ! -d "$src"; then
   1.192 +      echo "$0: $src does not exist." >&2
   1.193 +      exit 1
   1.194 +    fi
   1.195 +
   1.196 +    if test -z "$dstarg"; then
   1.197 +      echo "$0: no destination specified." >&2
   1.198 +      exit 1
   1.199 +    fi
   1.200 +
   1.201 +    dst=$dstarg
   1.202 +    # Protect names starting with `-'.
   1.203 +    case $dst in
   1.204 +      -*) dst=./$dst ;;
   1.205 +    esac
   1.206 +
   1.207 +    # If destination is a directory, append the input filename; won't work
   1.208 +    # if double slashes aren't ignored.
   1.209 +    if test -d "$dst"; then
   1.210 +      if test -n "$no_target_directory"; then
   1.211 +	echo "$0: $dstarg: Is a directory" >&2
   1.212 +	exit 1
   1.213 +      fi
   1.214 +      dst=$dst/`basename "$src"`
   1.215 +    fi
   1.216 +  fi
   1.217 +
   1.218 +  # This sed command emulates the dirname command.
   1.219 +  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
   1.220 +
   1.221 +  # Make sure that the destination directory exists.
   1.222 +
   1.223 +  # Skip lots of stat calls in the usual case.
   1.224 +  if test ! -d "$dstdir"; then
   1.225 +    defaultIFS='
   1.226 +	 '
   1.227 +    IFS="${IFS-$defaultIFS}"
   1.228 +
   1.229 +    oIFS=$IFS
   1.230 +    # Some sh's can't handle IFS=/ for some reason.
   1.231 +    IFS='%'
   1.232 +    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
   1.233 +    shift
   1.234 +    IFS=$oIFS
   1.235 +
   1.236 +    pathcomp=
   1.237 +
   1.238 +    while test $# -ne 0 ; do
   1.239 +      pathcomp=$pathcomp$1
   1.240 +      shift
   1.241 +      if test ! -d "$pathcomp"; then
   1.242 +        $mkdirprog "$pathcomp"
   1.243 +	# mkdir can fail with a `File exist' error in case several
   1.244 +	# install-sh are creating the directory concurrently.  This
   1.245 +	# is OK.
   1.246 +	test -d "$pathcomp" || exit
   1.247 +      fi
   1.248 +      pathcomp=$pathcomp/
   1.249 +    done
   1.250 +  fi
   1.251 +
   1.252 +  if test -n "$dir_arg"; then
   1.253 +    $doit $mkdircmd "$dst" \
   1.254 +      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
   1.255 +      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
   1.256 +      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
   1.257 +      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
   1.258 +
   1.259 +  else
   1.260 +    dstfile=`basename "$dst"`
   1.261 +
   1.262 +    # Make a couple of temp file names in the proper directory.
   1.263 +    dsttmp=$dstdir/_inst.$$_
   1.264 +    rmtmp=$dstdir/_rm.$$_
   1.265 +
   1.266 +    # Trap to clean up those temp files at exit.
   1.267 +    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
   1.268 +    trap '(exit $?); exit' 1 2 13 15
   1.269 +
   1.270 +    # Copy the file name to the temp name.
   1.271 +    $doit $cpprog "$src" "$dsttmp" &&
   1.272 +
   1.273 +    # and set any options; do chmod last to preserve setuid bits.
   1.274 +    #
   1.275 +    # If any of these fail, we abort the whole thing.  If we want to
   1.276 +    # ignore errors from any of these, just make sure not to ignore
   1.277 +    # errors from the above "$doit $cpprog $src $dsttmp" command.
   1.278 +    #
   1.279 +    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
   1.280 +      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
   1.281 +      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
   1.282 +      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
   1.283 +
   1.284 +    # Now rename the file to the real destination.
   1.285 +    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
   1.286 +      || {
   1.287 +	   # The rename failed, perhaps because mv can't rename something else
   1.288 +	   # to itself, or perhaps because mv is so ancient that it does not
   1.289 +	   # support -f.
   1.290 +
   1.291 +	   # Now remove or move aside any old file at destination location.
   1.292 +	   # We try this two ways since rm can't unlink itself on some
   1.293 +	   # systems and the destination file might be busy for other
   1.294 +	   # reasons.  In this case, the final cleanup might fail but the new
   1.295 +	   # file should still install successfully.
   1.296 +	   {
   1.297 +	     if test -f "$dstdir/$dstfile"; then
   1.298 +	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
   1.299 +	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
   1.300 +	       || {
   1.301 +		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
   1.302 +		 (exit 1); exit 1
   1.303 +	       }
   1.304 +	     else
   1.305 +	       :
   1.306 +	     fi
   1.307 +	   } &&
   1.308 +
   1.309 +	   # Now rename the file to the real destination.
   1.310 +	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
   1.311 +	 }
   1.312 +    }
   1.313 +  fi || { (exit 1); exit 1; }
   1.314 +done
   1.315 +
   1.316 +# The final little trick to "correctly" pass the exit status to the exit trap.
   1.317 +{
   1.318 +  (exit 0); exit 0
   1.319 +}
   1.320 +
   1.321 +# Local variables:
   1.322 +# eval: (add-hook 'write-file-hooks 'time-stamp)
   1.323 +# time-stamp-start: "scriptversion="
   1.324 +# time-stamp-format: "%:y-%02m-%02d.%02H"
   1.325 +# time-stamp-end: "$"
   1.326 +# End: