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