install-sh revision 100ae103
1100ae103Smrg#!/bin/sh 2100ae103Smrg# install - install a program, script, or datafile 3100ae103Smrg 4100ae103Smrgscriptversion=2005-05-14.22 5100ae103Smrg 6100ae103Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 7100ae103Smrg# later released in X11R6 (xc/config/util/install.sh) with the 8100ae103Smrg# following copyright and license. 9100ae103Smrg# 10100ae103Smrg# Copyright (C) 1994 X Consortium 11100ae103Smrg# 12100ae103Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 13100ae103Smrg# of this software and associated documentation files (the "Software"), to 14100ae103Smrg# deal in the Software without restriction, including without limitation the 15100ae103Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16100ae103Smrg# sell copies of the Software, and to permit persons to whom the Software is 17100ae103Smrg# furnished to do so, subject to the following conditions: 18100ae103Smrg# 19100ae103Smrg# The above copyright notice and this permission notice shall be included in 20100ae103Smrg# all copies or substantial portions of the Software. 21100ae103Smrg# 22100ae103Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23100ae103Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24100ae103Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25100ae103Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26100ae103Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27100ae103Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28100ae103Smrg# 29100ae103Smrg# Except as contained in this notice, the name of the X Consortium shall not 30100ae103Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 31100ae103Smrg# ings in this Software without prior written authorization from the X Consor- 32100ae103Smrg# tium. 33100ae103Smrg# 34100ae103Smrg# 35100ae103Smrg# FSF changes to this file are in the public domain. 36100ae103Smrg# 37100ae103Smrg# Calling this script install-sh is preferred over install.sh, to prevent 38100ae103Smrg# `make' implicit rules from creating a file called install from it 39100ae103Smrg# when there is no Makefile. 40100ae103Smrg# 41100ae103Smrg# This script is compatible with the BSD install script, but was written 42100ae103Smrg# from scratch. It can only install one file at a time, a restriction 43100ae103Smrg# shared with many OS's install programs. 44100ae103Smrg 45100ae103Smrg# set DOITPROG to echo to test this script 46100ae103Smrg 47100ae103Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 48100ae103Smrgdoit="${DOITPROG-}" 49100ae103Smrg 50100ae103Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 51100ae103Smrg 52100ae103Smrgmvprog="${MVPROG-mv}" 53100ae103Smrgcpprog="${CPPROG-cp}" 54100ae103Smrgchmodprog="${CHMODPROG-chmod}" 55100ae103Smrgchownprog="${CHOWNPROG-chown}" 56100ae103Smrgchgrpprog="${CHGRPPROG-chgrp}" 57100ae103Smrgstripprog="${STRIPPROG-strip}" 58100ae103Smrgrmprog="${RMPROG-rm}" 59100ae103Smrgmkdirprog="${MKDIRPROG-mkdir}" 60100ae103Smrg 61100ae103Smrgchmodcmd="$chmodprog 0755" 62100ae103Smrgchowncmd= 63100ae103Smrgchgrpcmd= 64100ae103Smrgstripcmd= 65100ae103Smrgrmcmd="$rmprog -f" 66100ae103Smrgmvcmd="$mvprog" 67100ae103Smrgsrc= 68100ae103Smrgdst= 69100ae103Smrgdir_arg= 70100ae103Smrgdstarg= 71100ae103Smrgno_target_directory= 72100ae103Smrg 73100ae103Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74100ae103Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 75100ae103Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76100ae103Smrg or: $0 [OPTION]... -d DIRECTORIES... 77100ae103Smrg 78100ae103SmrgIn the 1st form, copy SRCFILE to DSTFILE. 79100ae103SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80100ae103SmrgIn the 4th, create DIRECTORIES. 81100ae103Smrg 82100ae103SmrgOptions: 83100ae103Smrg-c (ignored) 84100ae103Smrg-d create directories instead of installing files. 85100ae103Smrg-g GROUP $chgrpprog installed files to GROUP. 86100ae103Smrg-m MODE $chmodprog installed files to MODE. 87100ae103Smrg-o USER $chownprog installed files to USER. 88100ae103Smrg-s $stripprog installed files. 89100ae103Smrg-t DIRECTORY install into DIRECTORY. 90100ae103Smrg-T report an error if DSTFILE is a directory. 91100ae103Smrg--help display this help and exit. 92100ae103Smrg--version display version info and exit. 93100ae103Smrg 94100ae103SmrgEnvironment variables override the default commands: 95100ae103Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96100ae103Smrg" 97100ae103Smrg 98100ae103Smrgwhile test -n "$1"; do 99100ae103Smrg case $1 in 100100ae103Smrg -c) shift 101100ae103Smrg continue;; 102100ae103Smrg 103100ae103Smrg -d) dir_arg=true 104100ae103Smrg shift 105100ae103Smrg continue;; 106100ae103Smrg 107100ae103Smrg -g) chgrpcmd="$chgrpprog $2" 108100ae103Smrg shift 109100ae103Smrg shift 110100ae103Smrg continue;; 111100ae103Smrg 112100ae103Smrg --help) echo "$usage"; exit $?;; 113100ae103Smrg 114100ae103Smrg -m) chmodcmd="$chmodprog $2" 115100ae103Smrg shift 116100ae103Smrg shift 117100ae103Smrg continue;; 118100ae103Smrg 119100ae103Smrg -o) chowncmd="$chownprog $2" 120100ae103Smrg shift 121100ae103Smrg shift 122100ae103Smrg continue;; 123100ae103Smrg 124100ae103Smrg -s) stripcmd=$stripprog 125100ae103Smrg shift 126100ae103Smrg continue;; 127100ae103Smrg 128100ae103Smrg -t) dstarg=$2 129100ae103Smrg shift 130100ae103Smrg shift 131100ae103Smrg continue;; 132100ae103Smrg 133100ae103Smrg -T) no_target_directory=true 134100ae103Smrg shift 135100ae103Smrg continue;; 136100ae103Smrg 137100ae103Smrg --version) echo "$0 $scriptversion"; exit $?;; 138100ae103Smrg 139100ae103Smrg *) # When -d is used, all remaining arguments are directories to create. 140100ae103Smrg # When -t is used, the destination is already specified. 141100ae103Smrg test -n "$dir_arg$dstarg" && break 142100ae103Smrg # Otherwise, the last argument is the destination. Remove it from $@. 143100ae103Smrg for arg 144100ae103Smrg do 145100ae103Smrg if test -n "$dstarg"; then 146100ae103Smrg # $@ is not empty: it contains at least $arg. 147100ae103Smrg set fnord "$@" "$dstarg" 148100ae103Smrg shift # fnord 149100ae103Smrg fi 150100ae103Smrg shift # arg 151100ae103Smrg dstarg=$arg 152100ae103Smrg done 153100ae103Smrg break;; 154100ae103Smrg esac 155100ae103Smrgdone 156100ae103Smrg 157100ae103Smrgif test -z "$1"; then 158100ae103Smrg if test -z "$dir_arg"; then 159100ae103Smrg echo "$0: no input file specified." >&2 160100ae103Smrg exit 1 161100ae103Smrg fi 162100ae103Smrg # It's OK to call `install-sh -d' without argument. 163100ae103Smrg # This can happen when creating conditional directories. 164100ae103Smrg exit 0 165100ae103Smrgfi 166100ae103Smrg 167100ae103Smrgfor src 168100ae103Smrgdo 169100ae103Smrg # Protect names starting with `-'. 170100ae103Smrg case $src in 171100ae103Smrg -*) src=./$src ;; 172100ae103Smrg esac 173100ae103Smrg 174100ae103Smrg if test -n "$dir_arg"; then 175100ae103Smrg dst=$src 176100ae103Smrg src= 177100ae103Smrg 178100ae103Smrg if test -d "$dst"; then 179100ae103Smrg mkdircmd=: 180100ae103Smrg chmodcmd= 181100ae103Smrg else 182100ae103Smrg mkdircmd=$mkdirprog 183100ae103Smrg fi 184100ae103Smrg else 185100ae103Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186100ae103Smrg # might cause directories to be created, which would be especially bad 187100ae103Smrg # if $src (and thus $dsttmp) contains '*'. 188100ae103Smrg if test ! -f "$src" && test ! -d "$src"; then 189100ae103Smrg echo "$0: $src does not exist." >&2 190100ae103Smrg exit 1 191100ae103Smrg fi 192100ae103Smrg 193100ae103Smrg if test -z "$dstarg"; then 194100ae103Smrg echo "$0: no destination specified." >&2 195100ae103Smrg exit 1 196100ae103Smrg fi 197100ae103Smrg 198100ae103Smrg dst=$dstarg 199100ae103Smrg # Protect names starting with `-'. 200100ae103Smrg case $dst in 201100ae103Smrg -*) dst=./$dst ;; 202100ae103Smrg esac 203100ae103Smrg 204100ae103Smrg # If destination is a directory, append the input filename; won't work 205100ae103Smrg # if double slashes aren't ignored. 206100ae103Smrg if test -d "$dst"; then 207100ae103Smrg if test -n "$no_target_directory"; then 208100ae103Smrg echo "$0: $dstarg: Is a directory" >&2 209100ae103Smrg exit 1 210100ae103Smrg fi 211100ae103Smrg dst=$dst/`basename "$src"` 212100ae103Smrg fi 213100ae103Smrg fi 214100ae103Smrg 215100ae103Smrg # This sed command emulates the dirname command. 216100ae103Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 217100ae103Smrg 218100ae103Smrg # Make sure that the destination directory exists. 219100ae103Smrg 220100ae103Smrg # Skip lots of stat calls in the usual case. 221100ae103Smrg if test ! -d "$dstdir"; then 222100ae103Smrg defaultIFS=' 223100ae103Smrg ' 224100ae103Smrg IFS="${IFS-$defaultIFS}" 225100ae103Smrg 226100ae103Smrg oIFS=$IFS 227100ae103Smrg # Some sh's can't handle IFS=/ for some reason. 228100ae103Smrg IFS='%' 229100ae103Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230100ae103Smrg shift 231100ae103Smrg IFS=$oIFS 232100ae103Smrg 233100ae103Smrg pathcomp= 234100ae103Smrg 235100ae103Smrg while test $# -ne 0 ; do 236100ae103Smrg pathcomp=$pathcomp$1 237100ae103Smrg shift 238100ae103Smrg if test ! -d "$pathcomp"; then 239100ae103Smrg $mkdirprog "$pathcomp" 240100ae103Smrg # mkdir can fail with a `File exist' error in case several 241100ae103Smrg # install-sh are creating the directory concurrently. This 242100ae103Smrg # is OK. 243100ae103Smrg test -d "$pathcomp" || exit 244100ae103Smrg fi 245100ae103Smrg pathcomp=$pathcomp/ 246100ae103Smrg done 247100ae103Smrg fi 248100ae103Smrg 249100ae103Smrg if test -n "$dir_arg"; then 250100ae103Smrg $doit $mkdircmd "$dst" \ 251100ae103Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252100ae103Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253100ae103Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254100ae103Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 255100ae103Smrg 256100ae103Smrg else 257100ae103Smrg dstfile=`basename "$dst"` 258100ae103Smrg 259100ae103Smrg # Make a couple of temp file names in the proper directory. 260100ae103Smrg dsttmp=$dstdir/_inst.$$_ 261100ae103Smrg rmtmp=$dstdir/_rm.$$_ 262100ae103Smrg 263100ae103Smrg # Trap to clean up those temp files at exit. 264100ae103Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 265100ae103Smrg trap '(exit $?); exit' 1 2 13 15 266100ae103Smrg 267100ae103Smrg # Copy the file name to the temp name. 268100ae103Smrg $doit $cpprog "$src" "$dsttmp" && 269100ae103Smrg 270100ae103Smrg # and set any options; do chmod last to preserve setuid bits. 271100ae103Smrg # 272100ae103Smrg # If any of these fail, we abort the whole thing. If we want to 273100ae103Smrg # ignore errors from any of these, just make sure not to ignore 274100ae103Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 275100ae103Smrg # 276100ae103Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277100ae103Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278100ae103Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279100ae103Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280100ae103Smrg 281100ae103Smrg # Now rename the file to the real destination. 282100ae103Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 283100ae103Smrg || { 284100ae103Smrg # The rename failed, perhaps because mv can't rename something else 285100ae103Smrg # to itself, or perhaps because mv is so ancient that it does not 286100ae103Smrg # support -f. 287100ae103Smrg 288100ae103Smrg # Now remove or move aside any old file at destination location. 289100ae103Smrg # We try this two ways since rm can't unlink itself on some 290100ae103Smrg # systems and the destination file might be busy for other 291100ae103Smrg # reasons. In this case, the final cleanup might fail but the new 292100ae103Smrg # file should still install successfully. 293100ae103Smrg { 294100ae103Smrg if test -f "$dstdir/$dstfile"; then 295100ae103Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296100ae103Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297100ae103Smrg || { 298100ae103Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 299100ae103Smrg (exit 1); exit 1 300100ae103Smrg } 301100ae103Smrg else 302100ae103Smrg : 303100ae103Smrg fi 304100ae103Smrg } && 305100ae103Smrg 306100ae103Smrg # Now rename the file to the real destination. 307100ae103Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308100ae103Smrg } 309100ae103Smrg } 310100ae103Smrg fi || { (exit 1); exit 1; } 311100ae103Smrgdone 312100ae103Smrg 313100ae103Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 314100ae103Smrg{ 315100ae103Smrg (exit 0); exit 0 316100ae103Smrg} 317100ae103Smrg 318100ae103Smrg# Local variables: 319100ae103Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 320100ae103Smrg# time-stamp-start: "scriptversion=" 321100ae103Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 322100ae103Smrg# time-stamp-end: "$" 323100ae103Smrg# End: 324