1106424Sroberto#!/bin/sh 254359Sroberto# install - install a program, script, or datafile 3182007Sroberto 4182007Srobertoscriptversion=2005-05-14.22 5182007Sroberto 6132451Sroberto# This originates from X11R5 (mit/util/scripts/install.sh), which was 7132451Sroberto# later released in X11R6 (xc/config/util/install.sh) with the 8132451Sroberto# following copyright and license. 9106424Sroberto# 10132451Sroberto# Copyright (C) 1994 X Consortium 11106424Sroberto# 12132451Sroberto# Permission is hereby granted, free of charge, to any person obtaining a copy 13132451Sroberto# of this software and associated documentation files (the "Software"), to 14132451Sroberto# deal in the Software without restriction, including without limitation the 15132451Sroberto# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16132451Sroberto# sell copies of the Software, and to permit persons to whom the Software is 17132451Sroberto# furnished to do so, subject to the following conditions: 18132451Sroberto# 19132451Sroberto# The above copyright notice and this permission notice shall be included in 20132451Sroberto# all copies or substantial portions of the Software. 21132451Sroberto# 22132451Sroberto# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23132451Sroberto# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24132451Sroberto# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25132451Sroberto# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26132451Sroberto# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27132451Sroberto# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28132451Sroberto# 29132451Sroberto# Except as contained in this notice, the name of the X Consortium shall not 30132451Sroberto# be used in advertising or otherwise to promote the sale, use or other deal- 31132451Sroberto# ings in this Software without prior written authorization from the X Consor- 32132451Sroberto# tium. 33132451Sroberto# 34132451Sroberto# 35132451Sroberto# FSF changes to this file are in the public domain. 36132451Sroberto# 3754359Sroberto# Calling this script install-sh is preferred over install.sh, to prevent 3854359Sroberto# `make' implicit rules from creating a file called install from it 3954359Sroberto# when there is no Makefile. 4054359Sroberto# 4154359Sroberto# This script is compatible with the BSD install script, but was written 42106424Sroberto# from scratch. It can only install one file at a time, a restriction 43106424Sroberto# shared with many OS's install programs. 4454359Sroberto 4554359Sroberto# set DOITPROG to echo to test this script 4654359Sroberto 4754359Sroberto# Don't use :- since 4.3BSD and earlier shells don't like it. 4854359Srobertodoit="${DOITPROG-}" 4954359Sroberto 5054359Sroberto# put in absolute paths if you don't have them in your path; or use env. vars. 5154359Sroberto 5254359Srobertomvprog="${MVPROG-mv}" 5354359Srobertocpprog="${CPPROG-cp}" 5454359Srobertochmodprog="${CHMODPROG-chmod}" 5554359Srobertochownprog="${CHOWNPROG-chown}" 5654359Srobertochgrpprog="${CHGRPPROG-chgrp}" 5754359Srobertostripprog="${STRIPPROG-strip}" 5854359Srobertormprog="${RMPROG-rm}" 5954359Srobertomkdirprog="${MKDIRPROG-mkdir}" 6054359Sroberto 6154359Srobertochmodcmd="$chmodprog 0755" 62182007Srobertochowncmd= 63182007Srobertochgrpcmd= 64182007Srobertostripcmd= 6554359Srobertormcmd="$rmprog -f" 6654359Srobertomvcmd="$mvprog" 67182007Srobertosrc= 68182007Srobertodst= 69182007Srobertodir_arg= 70182007Srobertodstarg= 71182007Srobertono_target_directory= 7254359Sroberto 73182007Srobertousage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74182007Sroberto or: $0 [OPTION]... SRCFILES... DIRECTORY 75182007Sroberto or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76182007Sroberto or: $0 [OPTION]... -d DIRECTORIES... 7754359Sroberto 78182007SrobertoIn the 1st form, copy SRCFILE to DSTFILE. 79182007SrobertoIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80182007SrobertoIn the 4th, create DIRECTORIES. 8154359Sroberto 82182007SrobertoOptions: 83182007Sroberto-c (ignored) 84182007Sroberto-d create directories instead of installing files. 85182007Sroberto-g GROUP $chgrpprog installed files to GROUP. 86182007Sroberto-m MODE $chmodprog installed files to MODE. 87182007Sroberto-o USER $chownprog installed files to USER. 88182007Sroberto-s $stripprog installed files. 89182007Sroberto-t DIRECTORY install into DIRECTORY. 90182007Sroberto-T report an error if DSTFILE is a directory. 91182007Sroberto--help display this help and exit. 92182007Sroberto--version display version info and exit. 9354359Sroberto 94182007SrobertoEnvironment variables override the default commands: 95182007Sroberto CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96182007Sroberto" 9754359Sroberto 98182007Srobertowhile test -n "$1"; do 99182007Sroberto case $1 in 100182007Sroberto -c) shift 101182007Sroberto continue;; 10254359Sroberto 103182007Sroberto -d) dir_arg=true 104182007Sroberto shift 105182007Sroberto continue;; 10654359Sroberto 107182007Sroberto -g) chgrpcmd="$chgrpprog $2" 108182007Sroberto shift 109182007Sroberto shift 110182007Sroberto continue;; 11154359Sroberto 112182007Sroberto --help) echo "$usage"; exit $?;; 11354359Sroberto 114182007Sroberto -m) chmodcmd="$chmodprog $2" 115182007Sroberto shift 116182007Sroberto shift 117182007Sroberto continue;; 11854359Sroberto 119182007Sroberto -o) chowncmd="$chownprog $2" 120182007Sroberto shift 121182007Sroberto shift 122182007Sroberto continue;; 12354359Sroberto 124182007Sroberto -s) stripcmd=$stripprog 125182007Sroberto shift 126182007Sroberto continue;; 127132451Sroberto 128182007Sroberto -t) dstarg=$2 129182007Sroberto shift 130182007Sroberto shift 131182007Sroberto continue;; 13254359Sroberto 133182007Sroberto -T) no_target_directory=true 134182007Sroberto shift 135182007Sroberto continue;; 13654359Sroberto 137182007Sroberto --version) echo "$0 $scriptversion"; exit $?;; 138132451Sroberto 139182007Sroberto *) # When -d is used, all remaining arguments are directories to create. 140182007Sroberto # When -t is used, the destination is already specified. 141182007Sroberto test -n "$dir_arg$dstarg" && break 142182007Sroberto # Otherwise, the last argument is the destination. Remove it from $@. 143182007Sroberto for arg 144182007Sroberto do 145182007Sroberto if test -n "$dstarg"; then 146182007Sroberto # $@ is not empty: it contains at least $arg. 147182007Sroberto set fnord "$@" "$dstarg" 148182007Sroberto shift # fnord 149182007Sroberto fi 150182007Sroberto shift # arg 151182007Sroberto dstarg=$arg 152182007Sroberto done 153182007Sroberto break;; 154182007Sroberto esac 155182007Srobertodone 15654359Sroberto 157182007Srobertoif test -z "$1"; then 158182007Sroberto if test -z "$dir_arg"; then 159182007Sroberto echo "$0: no input file specified." >&2 160182007Sroberto exit 1 161182007Sroberto fi 162182007Sroberto # It's OK to call `install-sh -d' without argument. 163182007Sroberto # This can happen when creating conditional directories. 164182007Sroberto exit 0 16554359Srobertofi 16654359Sroberto 167182007Srobertofor src 168182007Srobertodo 169182007Sroberto # Protect names starting with `-'. 170182007Sroberto case $src in 171182007Sroberto -*) src=./$src ;; 172182007Sroberto esac 17354359Sroberto 174182007Sroberto if test -n "$dir_arg"; then 175182007Sroberto dst=$src 176182007Sroberto src= 17754359Sroberto 178182007Sroberto if test -d "$dst"; then 179182007Sroberto mkdircmd=: 180182007Sroberto chmodcmd= 181182007Sroberto else 182182007Sroberto mkdircmd=$mkdirprog 183182007Sroberto fi 184182007Sroberto else 185182007Sroberto # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186182007Sroberto # might cause directories to be created, which would be especially bad 187182007Sroberto # if $src (and thus $dsttmp) contains '*'. 188182007Sroberto if test ! -f "$src" && test ! -d "$src"; then 189182007Sroberto echo "$0: $src does not exist." >&2 190182007Sroberto exit 1 191182007Sroberto fi 19254359Sroberto 193182007Sroberto if test -z "$dstarg"; then 194182007Sroberto echo "$0: no destination specified." >&2 195182007Sroberto exit 1 196182007Sroberto fi 19754359Sroberto 198182007Sroberto dst=$dstarg 199182007Sroberto # Protect names starting with `-'. 200182007Sroberto case $dst in 201182007Sroberto -*) dst=./$dst ;; 202182007Sroberto esac 20354359Sroberto 204182007Sroberto # If destination is a directory, append the input filename; won't work 205182007Sroberto # if double slashes aren't ignored. 206182007Sroberto if test -d "$dst"; then 207182007Sroberto if test -n "$no_target_directory"; then 208182007Sroberto echo "$0: $dstarg: Is a directory" >&2 209182007Sroberto exit 1 210182007Sroberto fi 211182007Sroberto dst=$dst/`basename "$src"` 212182007Sroberto fi 213182007Sroberto fi 21454359Sroberto 215182007Sroberto # This sed command emulates the dirname command. 216182007Sroberto dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 21754359Sroberto 218182007Sroberto # Make sure that the destination directory exists. 21954359Sroberto 220182007Sroberto # Skip lots of stat calls in the usual case. 221182007Sroberto if test ! -d "$dstdir"; then 222182007Sroberto defaultIFS=' 223182007Sroberto ' 224182007Sroberto IFS="${IFS-$defaultIFS}" 22554359Sroberto 226182007Sroberto oIFS=$IFS 227182007Sroberto # Some sh's can't handle IFS=/ for some reason. 228182007Sroberto IFS='%' 229182007Sroberto set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230182007Sroberto shift 231182007Sroberto IFS=$oIFS 23254359Sroberto 233182007Sroberto pathcomp= 23454359Sroberto 235182007Sroberto while test $# -ne 0 ; do 236182007Sroberto pathcomp=$pathcomp$1 237182007Sroberto shift 238182007Sroberto if test ! -d "$pathcomp"; then 239182007Sroberto $mkdirprog "$pathcomp" 240182007Sroberto # mkdir can fail with a `File exist' error in case several 241182007Sroberto # install-sh are creating the directory concurrently. This 242182007Sroberto # is OK. 243182007Sroberto test -d "$pathcomp" || exit 244182007Sroberto fi 245182007Sroberto pathcomp=$pathcomp/ 246182007Sroberto done 247182007Sroberto fi 24854359Sroberto 249182007Sroberto if test -n "$dir_arg"; then 250182007Sroberto $doit $mkdircmd "$dst" \ 251182007Sroberto && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252182007Sroberto && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253182007Sroberto && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254182007Sroberto && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 25554359Sroberto 256182007Sroberto else 257182007Sroberto dstfile=`basename "$dst"` 25854359Sroberto 259182007Sroberto # Make a couple of temp file names in the proper directory. 260182007Sroberto dsttmp=$dstdir/_inst.$$_ 261182007Sroberto rmtmp=$dstdir/_rm.$$_ 26254359Sroberto 263182007Sroberto # Trap to clean up those temp files at exit. 264182007Sroberto trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 265182007Sroberto trap '(exit $?); exit' 1 2 13 15 26654359Sroberto 267182007Sroberto # Copy the file name to the temp name. 268182007Sroberto $doit $cpprog "$src" "$dsttmp" && 269132451Sroberto 270182007Sroberto # and set any options; do chmod last to preserve setuid bits. 271182007Sroberto # 272182007Sroberto # If any of these fail, we abort the whole thing. If we want to 273182007Sroberto # ignore errors from any of these, just make sure not to ignore 274182007Sroberto # errors from the above "$doit $cpprog $src $dsttmp" command. 275182007Sroberto # 276182007Sroberto { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277182007Sroberto && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278182007Sroberto && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279182007Sroberto && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280132451Sroberto 281182007Sroberto # Now rename the file to the real destination. 282182007Sroberto { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 283182007Sroberto || { 284182007Sroberto # The rename failed, perhaps because mv can't rename something else 285182007Sroberto # to itself, or perhaps because mv is so ancient that it does not 286182007Sroberto # support -f. 28754359Sroberto 288182007Sroberto # Now remove or move aside any old file at destination location. 289182007Sroberto # We try this two ways since rm can't unlink itself on some 290182007Sroberto # systems and the destination file might be busy for other 291182007Sroberto # reasons. In this case, the final cleanup might fail but the new 292182007Sroberto # file should still install successfully. 293182007Sroberto { 294182007Sroberto if test -f "$dstdir/$dstfile"; then 295182007Sroberto $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296182007Sroberto || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297182007Sroberto || { 298182007Sroberto echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 299182007Sroberto (exit 1); exit 1 300182007Sroberto } 301182007Sroberto else 302182007Sroberto : 303182007Sroberto fi 304182007Sroberto } && 30554359Sroberto 306182007Sroberto # Now rename the file to the real destination. 307182007Sroberto $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308182007Sroberto } 309182007Sroberto } 310182007Sroberto fi || { (exit 1); exit 1; } 311182007Srobertodone 31254359Sroberto 313132451Sroberto# The final little trick to "correctly" pass the exit status to the exit trap. 314132451Sroberto{ 315182007Sroberto (exit 0); exit 0 316132451Sroberto} 317182007Sroberto 318182007Sroberto# Local variables: 319182007Sroberto# eval: (add-hook 'write-file-hooks 'time-stamp) 320182007Sroberto# time-stamp-start: "scriptversion=" 321182007Sroberto# time-stamp-format: "%:y-%02m-%02d.%02H" 322182007Sroberto# time-stamp-end: "$" 323182007Sroberto# End: 324