install-sh revision c76ae52d
1c76ae52dSmrg#!/bin/sh 2c76ae52dSmrg# install - install a program, script, or datafile 3c76ae52dSmrg 4c76ae52dSmrgscriptversion=2005-05-14.22 5c76ae52dSmrg 6c76ae52dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 7c76ae52dSmrg# later released in X11R6 (xc/config/util/install.sh) with the 8c76ae52dSmrg# following copyright and license. 9c76ae52dSmrg# 10c76ae52dSmrg# Copyright (C) 1994 X Consortium 11c76ae52dSmrg# 12c76ae52dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy 13c76ae52dSmrg# of this software and associated documentation files (the "Software"), to 14c76ae52dSmrg# deal in the Software without restriction, including without limitation the 15c76ae52dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16c76ae52dSmrg# sell copies of the Software, and to permit persons to whom the Software is 17c76ae52dSmrg# furnished to do so, subject to the following conditions: 18c76ae52dSmrg# 19c76ae52dSmrg# The above copyright notice and this permission notice shall be included in 20c76ae52dSmrg# all copies or substantial portions of the Software. 21c76ae52dSmrg# 22c76ae52dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23c76ae52dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24c76ae52dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25c76ae52dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26c76ae52dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27c76ae52dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28c76ae52dSmrg# 29c76ae52dSmrg# Except as contained in this notice, the name of the X Consortium shall not 30c76ae52dSmrg# be used in advertising or otherwise to promote the sale, use or other deal- 31c76ae52dSmrg# ings in this Software without prior written authorization from the X Consor- 32c76ae52dSmrg# tium. 33c76ae52dSmrg# 34c76ae52dSmrg# 35c76ae52dSmrg# FSF changes to this file are in the public domain. 36c76ae52dSmrg# 37c76ae52dSmrg# Calling this script install-sh is preferred over install.sh, to prevent 38c76ae52dSmrg# `make' implicit rules from creating a file called install from it 39c76ae52dSmrg# when there is no Makefile. 40c76ae52dSmrg# 41c76ae52dSmrg# This script is compatible with the BSD install script, but was written 42c76ae52dSmrg# from scratch. It can only install one file at a time, a restriction 43c76ae52dSmrg# shared with many OS's install programs. 44c76ae52dSmrg 45c76ae52dSmrg# set DOITPROG to echo to test this script 46c76ae52dSmrg 47c76ae52dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it. 48c76ae52dSmrgdoit="${DOITPROG-}" 49c76ae52dSmrg 50c76ae52dSmrg# put in absolute paths if you don't have them in your path; or use env. vars. 51c76ae52dSmrg 52c76ae52dSmrgmvprog="${MVPROG-mv}" 53c76ae52dSmrgcpprog="${CPPROG-cp}" 54c76ae52dSmrgchmodprog="${CHMODPROG-chmod}" 55c76ae52dSmrgchownprog="${CHOWNPROG-chown}" 56c76ae52dSmrgchgrpprog="${CHGRPPROG-chgrp}" 57c76ae52dSmrgstripprog="${STRIPPROG-strip}" 58c76ae52dSmrgrmprog="${RMPROG-rm}" 59c76ae52dSmrgmkdirprog="${MKDIRPROG-mkdir}" 60c76ae52dSmrg 61c76ae52dSmrgchmodcmd="$chmodprog 0755" 62c76ae52dSmrgchowncmd= 63c76ae52dSmrgchgrpcmd= 64c76ae52dSmrgstripcmd= 65c76ae52dSmrgrmcmd="$rmprog -f" 66c76ae52dSmrgmvcmd="$mvprog" 67c76ae52dSmrgsrc= 68c76ae52dSmrgdst= 69c76ae52dSmrgdir_arg= 70c76ae52dSmrgdstarg= 71c76ae52dSmrgno_target_directory= 72c76ae52dSmrg 73c76ae52dSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74c76ae52dSmrg or: $0 [OPTION]... SRCFILES... DIRECTORY 75c76ae52dSmrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76c76ae52dSmrg or: $0 [OPTION]... -d DIRECTORIES... 77c76ae52dSmrg 78c76ae52dSmrgIn the 1st form, copy SRCFILE to DSTFILE. 79c76ae52dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80c76ae52dSmrgIn the 4th, create DIRECTORIES. 81c76ae52dSmrg 82c76ae52dSmrgOptions: 83c76ae52dSmrg-c (ignored) 84c76ae52dSmrg-d create directories instead of installing files. 85c76ae52dSmrg-g GROUP $chgrpprog installed files to GROUP. 86c76ae52dSmrg-m MODE $chmodprog installed files to MODE. 87c76ae52dSmrg-o USER $chownprog installed files to USER. 88c76ae52dSmrg-s $stripprog installed files. 89c76ae52dSmrg-t DIRECTORY install into DIRECTORY. 90c76ae52dSmrg-T report an error if DSTFILE is a directory. 91c76ae52dSmrg--help display this help and exit. 92c76ae52dSmrg--version display version info and exit. 93c76ae52dSmrg 94c76ae52dSmrgEnvironment variables override the default commands: 95c76ae52dSmrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96c76ae52dSmrg" 97c76ae52dSmrg 98c76ae52dSmrgwhile test -n "$1"; do 99c76ae52dSmrg case $1 in 100c76ae52dSmrg -c) shift 101c76ae52dSmrg continue;; 102c76ae52dSmrg 103c76ae52dSmrg -d) dir_arg=true 104c76ae52dSmrg shift 105c76ae52dSmrg continue;; 106c76ae52dSmrg 107c76ae52dSmrg -g) chgrpcmd="$chgrpprog $2" 108c76ae52dSmrg shift 109c76ae52dSmrg shift 110c76ae52dSmrg continue;; 111c76ae52dSmrg 112c76ae52dSmrg --help) echo "$usage"; exit $?;; 113c76ae52dSmrg 114c76ae52dSmrg -m) chmodcmd="$chmodprog $2" 115c76ae52dSmrg shift 116c76ae52dSmrg shift 117c76ae52dSmrg continue;; 118c76ae52dSmrg 119c76ae52dSmrg -o) chowncmd="$chownprog $2" 120c76ae52dSmrg shift 121c76ae52dSmrg shift 122c76ae52dSmrg continue;; 123c76ae52dSmrg 124c76ae52dSmrg -s) stripcmd=$stripprog 125c76ae52dSmrg shift 126c76ae52dSmrg continue;; 127c76ae52dSmrg 128c76ae52dSmrg -t) dstarg=$2 129c76ae52dSmrg shift 130c76ae52dSmrg shift 131c76ae52dSmrg continue;; 132c76ae52dSmrg 133c76ae52dSmrg -T) no_target_directory=true 134c76ae52dSmrg shift 135c76ae52dSmrg continue;; 136c76ae52dSmrg 137c76ae52dSmrg --version) echo "$0 $scriptversion"; exit $?;; 138c76ae52dSmrg 139c76ae52dSmrg *) # When -d is used, all remaining arguments are directories to create. 140c76ae52dSmrg # When -t is used, the destination is already specified. 141c76ae52dSmrg test -n "$dir_arg$dstarg" && break 142c76ae52dSmrg # Otherwise, the last argument is the destination. Remove it from $@. 143c76ae52dSmrg for arg 144c76ae52dSmrg do 145c76ae52dSmrg if test -n "$dstarg"; then 146c76ae52dSmrg # $@ is not empty: it contains at least $arg. 147c76ae52dSmrg set fnord "$@" "$dstarg" 148c76ae52dSmrg shift # fnord 149c76ae52dSmrg fi 150c76ae52dSmrg shift # arg 151c76ae52dSmrg dstarg=$arg 152c76ae52dSmrg done 153c76ae52dSmrg break;; 154c76ae52dSmrg esac 155c76ae52dSmrgdone 156c76ae52dSmrg 157c76ae52dSmrgif test -z "$1"; then 158c76ae52dSmrg if test -z "$dir_arg"; then 159c76ae52dSmrg echo "$0: no input file specified." >&2 160c76ae52dSmrg exit 1 161c76ae52dSmrg fi 162c76ae52dSmrg # It's OK to call `install-sh -d' without argument. 163c76ae52dSmrg # This can happen when creating conditional directories. 164c76ae52dSmrg exit 0 165c76ae52dSmrgfi 166c76ae52dSmrg 167c76ae52dSmrgfor src 168c76ae52dSmrgdo 169c76ae52dSmrg # Protect names starting with `-'. 170c76ae52dSmrg case $src in 171c76ae52dSmrg -*) src=./$src ;; 172c76ae52dSmrg esac 173c76ae52dSmrg 174c76ae52dSmrg if test -n "$dir_arg"; then 175c76ae52dSmrg dst=$src 176c76ae52dSmrg src= 177c76ae52dSmrg 178c76ae52dSmrg if test -d "$dst"; then 179c76ae52dSmrg mkdircmd=: 180c76ae52dSmrg chmodcmd= 181c76ae52dSmrg else 182c76ae52dSmrg mkdircmd=$mkdirprog 183c76ae52dSmrg fi 184c76ae52dSmrg else 185c76ae52dSmrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186c76ae52dSmrg # might cause directories to be created, which would be especially bad 187c76ae52dSmrg # if $src (and thus $dsttmp) contains '*'. 188c76ae52dSmrg if test ! -f "$src" && test ! -d "$src"; then 189c76ae52dSmrg echo "$0: $src does not exist." >&2 190c76ae52dSmrg exit 1 191c76ae52dSmrg fi 192c76ae52dSmrg 193c76ae52dSmrg if test -z "$dstarg"; then 194c76ae52dSmrg echo "$0: no destination specified." >&2 195c76ae52dSmrg exit 1 196c76ae52dSmrg fi 197c76ae52dSmrg 198c76ae52dSmrg dst=$dstarg 199c76ae52dSmrg # Protect names starting with `-'. 200c76ae52dSmrg case $dst in 201c76ae52dSmrg -*) dst=./$dst ;; 202c76ae52dSmrg esac 203c76ae52dSmrg 204c76ae52dSmrg # If destination is a directory, append the input filename; won't work 205c76ae52dSmrg # if double slashes aren't ignored. 206c76ae52dSmrg if test -d "$dst"; then 207c76ae52dSmrg if test -n "$no_target_directory"; then 208c76ae52dSmrg echo "$0: $dstarg: Is a directory" >&2 209c76ae52dSmrg exit 1 210c76ae52dSmrg fi 211c76ae52dSmrg dst=$dst/`basename "$src"` 212c76ae52dSmrg fi 213c76ae52dSmrg fi 214c76ae52dSmrg 215c76ae52dSmrg # This sed command emulates the dirname command. 216c76ae52dSmrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 217c76ae52dSmrg 218c76ae52dSmrg # Make sure that the destination directory exists. 219c76ae52dSmrg 220c76ae52dSmrg # Skip lots of stat calls in the usual case. 221c76ae52dSmrg if test ! -d "$dstdir"; then 222c76ae52dSmrg defaultIFS=' 223c76ae52dSmrg ' 224c76ae52dSmrg IFS="${IFS-$defaultIFS}" 225c76ae52dSmrg 226c76ae52dSmrg oIFS=$IFS 227c76ae52dSmrg # Some sh's can't handle IFS=/ for some reason. 228c76ae52dSmrg IFS='%' 229c76ae52dSmrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230c76ae52dSmrg shift 231c76ae52dSmrg IFS=$oIFS 232c76ae52dSmrg 233c76ae52dSmrg pathcomp= 234c76ae52dSmrg 235c76ae52dSmrg while test $# -ne 0 ; do 236c76ae52dSmrg pathcomp=$pathcomp$1 237c76ae52dSmrg shift 238c76ae52dSmrg if test ! -d "$pathcomp"; then 239c76ae52dSmrg $mkdirprog "$pathcomp" 240c76ae52dSmrg # mkdir can fail with a `File exist' error in case several 241c76ae52dSmrg # install-sh are creating the directory concurrently. This 242c76ae52dSmrg # is OK. 243c76ae52dSmrg test -d "$pathcomp" || exit 244c76ae52dSmrg fi 245c76ae52dSmrg pathcomp=$pathcomp/ 246c76ae52dSmrg done 247c76ae52dSmrg fi 248c76ae52dSmrg 249c76ae52dSmrg if test -n "$dir_arg"; then 250c76ae52dSmrg $doit $mkdircmd "$dst" \ 251c76ae52dSmrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252c76ae52dSmrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253c76ae52dSmrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254c76ae52dSmrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 255c76ae52dSmrg 256c76ae52dSmrg else 257c76ae52dSmrg dstfile=`basename "$dst"` 258c76ae52dSmrg 259c76ae52dSmrg # Make a couple of temp file names in the proper directory. 260c76ae52dSmrg dsttmp=$dstdir/_inst.$$_ 261c76ae52dSmrg rmtmp=$dstdir/_rm.$$_ 262c76ae52dSmrg 263c76ae52dSmrg # Trap to clean up those temp files at exit. 264c76ae52dSmrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 265c76ae52dSmrg trap '(exit $?); exit' 1 2 13 15 266c76ae52dSmrg 267c76ae52dSmrg # Copy the file name to the temp name. 268c76ae52dSmrg $doit $cpprog "$src" "$dsttmp" && 269c76ae52dSmrg 270c76ae52dSmrg # and set any options; do chmod last to preserve setuid bits. 271c76ae52dSmrg # 272c76ae52dSmrg # If any of these fail, we abort the whole thing. If we want to 273c76ae52dSmrg # ignore errors from any of these, just make sure not to ignore 274c76ae52dSmrg # errors from the above "$doit $cpprog $src $dsttmp" command. 275c76ae52dSmrg # 276c76ae52dSmrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277c76ae52dSmrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278c76ae52dSmrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279c76ae52dSmrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280c76ae52dSmrg 281c76ae52dSmrg # Now rename the file to the real destination. 282c76ae52dSmrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 283c76ae52dSmrg || { 284c76ae52dSmrg # The rename failed, perhaps because mv can't rename something else 285c76ae52dSmrg # to itself, or perhaps because mv is so ancient that it does not 286c76ae52dSmrg # support -f. 287c76ae52dSmrg 288c76ae52dSmrg # Now remove or move aside any old file at destination location. 289c76ae52dSmrg # We try this two ways since rm can't unlink itself on some 290c76ae52dSmrg # systems and the destination file might be busy for other 291c76ae52dSmrg # reasons. In this case, the final cleanup might fail but the new 292c76ae52dSmrg # file should still install successfully. 293c76ae52dSmrg { 294c76ae52dSmrg if test -f "$dstdir/$dstfile"; then 295c76ae52dSmrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296c76ae52dSmrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297c76ae52dSmrg || { 298c76ae52dSmrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 299c76ae52dSmrg (exit 1); exit 1 300c76ae52dSmrg } 301c76ae52dSmrg else 302c76ae52dSmrg : 303c76ae52dSmrg fi 304c76ae52dSmrg } && 305c76ae52dSmrg 306c76ae52dSmrg # Now rename the file to the real destination. 307c76ae52dSmrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308c76ae52dSmrg } 309c76ae52dSmrg } 310c76ae52dSmrg fi || { (exit 1); exit 1; } 311c76ae52dSmrgdone 312c76ae52dSmrg 313c76ae52dSmrg# The final little trick to "correctly" pass the exit status to the exit trap. 314c76ae52dSmrg{ 315c76ae52dSmrg (exit 0); exit 0 316c76ae52dSmrg} 317c76ae52dSmrg 318c76ae52dSmrg# Local variables: 319c76ae52dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp) 320c76ae52dSmrg# time-stamp-start: "scriptversion=" 321c76ae52dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H" 322c76ae52dSmrg# time-stamp-end: "$" 323c76ae52dSmrg# End: 324