138889Sjdp#!/bin/sh 238889Sjdp# install - install a program, script, or datafile 3218822Sdim 4218822Sdimscriptversion=2005-05-14.22 5218822Sdim 6130561Sobrien# This originates from X11R5 (mit/util/scripts/install.sh), which was 7130561Sobrien# later released in X11R6 (xc/config/util/install.sh) with the 8130561Sobrien# following copyright and license. 938889Sjdp# 10130561Sobrien# Copyright (C) 1994 X Consortium 1138889Sjdp# 12130561Sobrien# Permission is hereby granted, free of charge, to any person obtaining a copy 13130561Sobrien# of this software and associated documentation files (the "Software"), to 14130561Sobrien# deal in the Software without restriction, including without limitation the 15130561Sobrien# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16130561Sobrien# sell copies of the Software, and to permit persons to whom the Software is 17130561Sobrien# furnished to do so, subject to the following conditions: 18130561Sobrien# 19130561Sobrien# The above copyright notice and this permission notice shall be included in 20130561Sobrien# all copies or substantial portions of the Software. 21130561Sobrien# 22130561Sobrien# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23130561Sobrien# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24130561Sobrien# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25130561Sobrien# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26130561Sobrien# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27130561Sobrien# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28130561Sobrien# 29130561Sobrien# Except as contained in this notice, the name of the X Consortium shall not 30130561Sobrien# be used in advertising or otherwise to promote the sale, use or other deal- 31130561Sobrien# ings in this Software without prior written authorization from the X Consor- 32130561Sobrien# tium. 33130561Sobrien# 34130561Sobrien# 35130561Sobrien# FSF changes to this file are in the public domain. 36130561Sobrien# 3738889Sjdp# Calling this script install-sh is preferred over install.sh, to prevent 3838889Sjdp# `make' implicit rules from creating a file called install from it 3938889Sjdp# when there is no Makefile. 4038889Sjdp# 4138889Sjdp# This script is compatible with the BSD install script, but was written 4238889Sjdp# from scratch. It can only install one file at a time, a restriction 4338889Sjdp# shared with many OS's install programs. 4438889Sjdp 4538889Sjdp# set DOITPROG to echo to test this script 4638889Sjdp 4738889Sjdp# Don't use :- since 4.3BSD and earlier shells don't like it. 4838889Sjdpdoit="${DOITPROG-}" 4938889Sjdp 5038889Sjdp# put in absolute paths if you don't have them in your path; or use env. vars. 5138889Sjdp 5238889Sjdpmvprog="${MVPROG-mv}" 5338889Sjdpcpprog="${CPPROG-cp}" 5438889Sjdpchmodprog="${CHMODPROG-chmod}" 5538889Sjdpchownprog="${CHOWNPROG-chown}" 5638889Sjdpchgrpprog="${CHGRPPROG-chgrp}" 5738889Sjdpstripprog="${STRIPPROG-strip}" 5838889Sjdprmprog="${RMPROG-rm}" 5938889Sjdpmkdirprog="${MKDIRPROG-mkdir}" 6038889Sjdp 6138889Sjdpchmodcmd="$chmodprog 0755" 62218822Sdimchowncmd= 63218822Sdimchgrpcmd= 64218822Sdimstripcmd= 6538889Sjdprmcmd="$rmprog -f" 6638889Sjdpmvcmd="$mvprog" 67218822Sdimsrc= 68218822Sdimdst= 69218822Sdimdir_arg= 70218822Sdimdstarg= 71218822Sdimno_target_directory= 7238889Sjdp 73218822Sdimusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74218822Sdim or: $0 [OPTION]... SRCFILES... DIRECTORY 75218822Sdim or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76218822Sdim or: $0 [OPTION]... -d DIRECTORIES... 7738889Sjdp 78218822SdimIn the 1st form, copy SRCFILE to DSTFILE. 79218822SdimIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80218822SdimIn the 4th, create DIRECTORIES. 8138889Sjdp 82218822SdimOptions: 83218822Sdim-c (ignored) 84218822Sdim-d create directories instead of installing files. 85218822Sdim-g GROUP $chgrpprog installed files to GROUP. 86218822Sdim-m MODE $chmodprog installed files to MODE. 87218822Sdim-o USER $chownprog installed files to USER. 88218822Sdim-s $stripprog installed files. 89218822Sdim-t DIRECTORY install into DIRECTORY. 90218822Sdim-T report an error if DSTFILE is a directory. 91218822Sdim--help display this help and exit. 92218822Sdim--version display version info and exit. 9338889Sjdp 94218822SdimEnvironment variables override the default commands: 95218822Sdim CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96218822Sdim" 9738889Sjdp 98218822Sdimwhile test -n "$1"; do 99218822Sdim case $1 in 100218822Sdim -c) shift 101218822Sdim continue;; 10238889Sjdp 103218822Sdim -d) dir_arg=true 104218822Sdim shift 105218822Sdim continue;; 10638889Sjdp 107218822Sdim -g) chgrpcmd="$chgrpprog $2" 108218822Sdim shift 109218822Sdim shift 110218822Sdim continue;; 11138889Sjdp 112218822Sdim --help) echo "$usage"; exit $?;; 11338889Sjdp 114218822Sdim -m) chmodcmd="$chmodprog $2" 115218822Sdim shift 116218822Sdim shift 117218822Sdim continue;; 11838889Sjdp 119218822Sdim -o) chowncmd="$chownprog $2" 120218822Sdim shift 121218822Sdim shift 122218822Sdim continue;; 12338889Sjdp 124218822Sdim -s) stripcmd=$stripprog 125218822Sdim shift 126218822Sdim continue;; 127130561Sobrien 128218822Sdim -t) dstarg=$2 129218822Sdim shift 130218822Sdim shift 131218822Sdim continue;; 13238889Sjdp 133218822Sdim -T) no_target_directory=true 134218822Sdim shift 135218822Sdim continue;; 13638889Sjdp 137218822Sdim --version) echo "$0 $scriptversion"; exit $?;; 138130561Sobrien 139218822Sdim *) # When -d is used, all remaining arguments are directories to create. 140218822Sdim # When -t is used, the destination is already specified. 141218822Sdim test -n "$dir_arg$dstarg" && break 142218822Sdim # Otherwise, the last argument is the destination. Remove it from $@. 143218822Sdim for arg 144218822Sdim do 145218822Sdim if test -n "$dstarg"; then 146218822Sdim # $@ is not empty: it contains at least $arg. 147218822Sdim set fnord "$@" "$dstarg" 148218822Sdim shift # fnord 149218822Sdim fi 150218822Sdim shift # arg 151218822Sdim dstarg=$arg 152218822Sdim done 153218822Sdim break;; 154218822Sdim esac 155218822Sdimdone 15638889Sjdp 157218822Sdimif test -z "$1"; then 158218822Sdim if test -z "$dir_arg"; then 159218822Sdim echo "$0: no input file specified." >&2 160218822Sdim exit 1 161218822Sdim fi 162218822Sdim # It's OK to call `install-sh -d' without argument. 163218822Sdim # This can happen when creating conditional directories. 164218822Sdim exit 0 16538889Sjdpfi 16638889Sjdp 167218822Sdimfor src 168218822Sdimdo 169218822Sdim # Protect names starting with `-'. 170218822Sdim case $src in 171218822Sdim -*) src=./$src ;; 172218822Sdim esac 17338889Sjdp 174218822Sdim if test -n "$dir_arg"; then 175218822Sdim dst=$src 176218822Sdim src= 17738889Sjdp 178218822Sdim if test -d "$dst"; then 179218822Sdim mkdircmd=: 180218822Sdim chmodcmd= 181218822Sdim else 182218822Sdim mkdircmd=$mkdirprog 183218822Sdim fi 184218822Sdim else 185218822Sdim # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186218822Sdim # might cause directories to be created, which would be especially bad 187218822Sdim # if $src (and thus $dsttmp) contains '*'. 188218822Sdim if test ! -f "$src" && test ! -d "$src"; then 189218822Sdim echo "$0: $src does not exist." >&2 190218822Sdim exit 1 191218822Sdim fi 19238889Sjdp 193218822Sdim if test -z "$dstarg"; then 194218822Sdim echo "$0: no destination specified." >&2 195218822Sdim exit 1 196218822Sdim fi 19738889Sjdp 198218822Sdim dst=$dstarg 199218822Sdim # Protect names starting with `-'. 200218822Sdim case $dst in 201218822Sdim -*) dst=./$dst ;; 202218822Sdim esac 20338889Sjdp 204218822Sdim # If destination is a directory, append the input filename; won't work 205218822Sdim # if double slashes aren't ignored. 206218822Sdim if test -d "$dst"; then 207218822Sdim if test -n "$no_target_directory"; then 208218822Sdim echo "$0: $dstarg: Is a directory" >&2 209218822Sdim exit 1 210218822Sdim fi 211218822Sdim dst=$dst/`basename "$src"` 212218822Sdim fi 213218822Sdim fi 21438889Sjdp 215218822Sdim # This sed command emulates the dirname command. 216218822Sdim dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 21738889Sjdp 218218822Sdim # Make sure that the destination directory exists. 21938889Sjdp 220218822Sdim # Skip lots of stat calls in the usual case. 221218822Sdim if test ! -d "$dstdir"; then 222218822Sdim defaultIFS=' 223218822Sdim ' 224218822Sdim IFS="${IFS-$defaultIFS}" 22538889Sjdp 226218822Sdim oIFS=$IFS 227218822Sdim # Some sh's can't handle IFS=/ for some reason. 228218822Sdim IFS='%' 229218822Sdim set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230218822Sdim shift 231218822Sdim IFS=$oIFS 23238889Sjdp 233218822Sdim pathcomp= 23438889Sjdp 235218822Sdim while test $# -ne 0 ; do 236218822Sdim pathcomp=$pathcomp$1 237218822Sdim shift 238218822Sdim if test ! -d "$pathcomp"; then 239218822Sdim $mkdirprog "$pathcomp" 240218822Sdim # mkdir can fail with a `File exist' error in case several 241218822Sdim # install-sh are creating the directory concurrently. This 242218822Sdim # is OK. 243218822Sdim test -d "$pathcomp" || exit 244218822Sdim fi 245218822Sdim pathcomp=$pathcomp/ 246218822Sdim done 247218822Sdim fi 24838889Sjdp 249218822Sdim if test -n "$dir_arg"; then 250218822Sdim $doit $mkdircmd "$dst" \ 251218822Sdim && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252218822Sdim && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253218822Sdim && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254218822Sdim && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 25538889Sjdp 256218822Sdim else 257218822Sdim dstfile=`basename "$dst"` 25838889Sjdp 259218822Sdim # Make a couple of temp file names in the proper directory. 260218822Sdim dsttmp=$dstdir/_inst.$$_ 261218822Sdim rmtmp=$dstdir/_rm.$$_ 26238889Sjdp 263218822Sdim # Trap to clean up those temp files at exit. 264218822Sdim trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 265218822Sdim trap '(exit $?); exit' 1 2 13 15 26638889Sjdp 267218822Sdim # Copy the file name to the temp name. 268218822Sdim $doit $cpprog "$src" "$dsttmp" && 269130561Sobrien 270218822Sdim # and set any options; do chmod last to preserve setuid bits. 271218822Sdim # 272218822Sdim # If any of these fail, we abort the whole thing. If we want to 273218822Sdim # ignore errors from any of these, just make sure not to ignore 274218822Sdim # errors from the above "$doit $cpprog $src $dsttmp" command. 275218822Sdim # 276218822Sdim { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277218822Sdim && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278218822Sdim && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279218822Sdim && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280130561Sobrien 281218822Sdim # Now rename the file to the real destination. 282218822Sdim { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 283218822Sdim || { 284218822Sdim # The rename failed, perhaps because mv can't rename something else 285218822Sdim # to itself, or perhaps because mv is so ancient that it does not 286218822Sdim # support -f. 28738889Sjdp 288218822Sdim # Now remove or move aside any old file at destination location. 289218822Sdim # We try this two ways since rm can't unlink itself on some 290218822Sdim # systems and the destination file might be busy for other 291218822Sdim # reasons. In this case, the final cleanup might fail but the new 292218822Sdim # file should still install successfully. 293218822Sdim { 294218822Sdim if test -f "$dstdir/$dstfile"; then 295218822Sdim $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296218822Sdim || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297218822Sdim || { 298218822Sdim echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 299218822Sdim (exit 1); exit 1 300218822Sdim } 301218822Sdim else 302218822Sdim : 303218822Sdim fi 304218822Sdim } && 30538889Sjdp 306218822Sdim # Now rename the file to the real destination. 307218822Sdim $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308218822Sdim } 309218822Sdim } 310218822Sdim fi || { (exit 1); exit 1; } 311218822Sdimdone 31238889Sjdp 313130561Sobrien# The final little trick to "correctly" pass the exit status to the exit trap. 314130561Sobrien{ 315218822Sdim (exit 0); exit 0 316130561Sobrien} 317218822Sdim 318218822Sdim# Local variables: 319218822Sdim# eval: (add-hook 'write-file-hooks 'time-stamp) 320218822Sdim# time-stamp-start: "scriptversion=" 321218822Sdim# time-stamp-format: "%:y-%02m-%02d.%02H" 322218822Sdim# time-stamp-end: "$" 323218822Sdim# End: 324