install-sh revision a99f82b8
1a99f82b8Smrg#!/bin/sh 2a99f82b8Smrg# 3a99f82b8Smrg# install - install a program, script, or datafile 4a99f82b8Smrg# 5a99f82b8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 6a99f82b8Smrg# later released in X11R6 (xc/config/util/install.sh) with the 7a99f82b8Smrg# following copyright and license. 8a99f82b8Smrg# 9a99f82b8Smrg# Copyright (C) 1994 X Consortium 10a99f82b8Smrg# 11a99f82b8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 12a99f82b8Smrg# of this software and associated documentation files (the "Software"), to 13a99f82b8Smrg# deal in the Software without restriction, including without limitation the 14a99f82b8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 15a99f82b8Smrg# sell copies of the Software, and to permit persons to whom the Software is 16a99f82b8Smrg# furnished to do so, subject to the following conditions: 17a99f82b8Smrg# 18a99f82b8Smrg# The above copyright notice and this permission notice shall be included in 19a99f82b8Smrg# all copies or substantial portions of the Software. 20a99f82b8Smrg# 21a99f82b8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22a99f82b8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23a99f82b8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24a99f82b8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 25a99f82b8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 26a99f82b8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27a99f82b8Smrg# 28a99f82b8Smrg# Except as contained in this notice, the name of the X Consortium shall not 29a99f82b8Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 30a99f82b8Smrg# ings in this Software without prior written authorization from the X Consor- 31a99f82b8Smrg# tium. 32a99f82b8Smrg# 33a99f82b8Smrg# 34a99f82b8Smrg# FSF changes to this file are in the public domain. 35a99f82b8Smrg# 36a99f82b8Smrg# Calling this script install-sh is preferred over install.sh, to prevent 37a99f82b8Smrg# `make' implicit rules from creating a file called install from it 38a99f82b8Smrg# when there is no Makefile. 39a99f82b8Smrg# 40a99f82b8Smrg# This script is compatible with the BSD install script, but was written 41a99f82b8Smrg# from scratch. It can only install one file at a time, a restriction 42a99f82b8Smrg# shared with many OS's install programs. 43a99f82b8Smrg 44a99f82b8Smrg 45a99f82b8Smrg# set DOITPROG to echo to test this script 46a99f82b8Smrg 47a99f82b8Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 48a99f82b8Smrgdoit="${DOITPROG-}" 49a99f82b8Smrg 50a99f82b8Smrg 51a99f82b8Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 52a99f82b8Smrg 53a99f82b8Smrgmvprog="${MVPROG-mv}" 54a99f82b8Smrgcpprog="${CPPROG-cp}" 55a99f82b8Smrgchmodprog="${CHMODPROG-chmod}" 56a99f82b8Smrgchownprog="${CHOWNPROG-chown}" 57a99f82b8Smrgchgrpprog="${CHGRPPROG-chgrp}" 58a99f82b8Smrgstripprog="${STRIPPROG-strip}" 59a99f82b8Smrgrmprog="${RMPROG-rm}" 60a99f82b8Smrgmkdirprog="${MKDIRPROG-mkdir}" 61a99f82b8Smrg 62a99f82b8Smrgtransformbasename="" 63a99f82b8Smrgtransform_arg="" 64a99f82b8Smrginstcmd="$mvprog" 65a99f82b8Smrgchmodcmd="$chmodprog 0755" 66a99f82b8Smrgchowncmd="" 67a99f82b8Smrgchgrpcmd="" 68a99f82b8Smrgstripcmd="" 69a99f82b8Smrgrmcmd="$rmprog -f" 70a99f82b8Smrgmvcmd="$mvprog" 71a99f82b8Smrgsrc="" 72a99f82b8Smrgdst="" 73a99f82b8Smrgdir_arg="" 74a99f82b8Smrg 75a99f82b8Smrgwhile [ x"$1" != x ]; do 76a99f82b8Smrg case $1 in 77a99f82b8Smrg -c) instcmd=$cpprog 78a99f82b8Smrg shift 79a99f82b8Smrg continue;; 80a99f82b8Smrg 81a99f82b8Smrg -d) dir_arg=true 82a99f82b8Smrg shift 83a99f82b8Smrg continue;; 84a99f82b8Smrg 85a99f82b8Smrg -m) chmodcmd="$chmodprog $2" 86a99f82b8Smrg shift 87a99f82b8Smrg shift 88a99f82b8Smrg continue;; 89a99f82b8Smrg 90a99f82b8Smrg -o) chowncmd="$chownprog $2" 91a99f82b8Smrg shift 92a99f82b8Smrg shift 93a99f82b8Smrg continue;; 94a99f82b8Smrg 95a99f82b8Smrg -g) chgrpcmd="$chgrpprog $2" 96a99f82b8Smrg shift 97a99f82b8Smrg shift 98a99f82b8Smrg continue;; 99a99f82b8Smrg 100a99f82b8Smrg -s) stripcmd=$stripprog 101a99f82b8Smrg shift 102a99f82b8Smrg continue;; 103a99f82b8Smrg 104a99f82b8Smrg -t=*) transformarg=`echo $1 | sed 's/-t=//'` 105a99f82b8Smrg shift 106a99f82b8Smrg continue;; 107a99f82b8Smrg 108a99f82b8Smrg -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 109a99f82b8Smrg shift 110a99f82b8Smrg continue;; 111a99f82b8Smrg 112a99f82b8Smrg *) if [ x"$src" = x ] 113a99f82b8Smrg then 114a99f82b8Smrg src=$1 115a99f82b8Smrg else 116a99f82b8Smrg # this colon is to work around a 386BSD /bin/sh bug 117a99f82b8Smrg : 118a99f82b8Smrg dst=$1 119a99f82b8Smrg fi 120a99f82b8Smrg shift 121a99f82b8Smrg continue;; 122a99f82b8Smrg esac 123a99f82b8Smrgdone 124a99f82b8Smrg 125a99f82b8Smrgif [ x"$src" = x ] 126a99f82b8Smrgthen 127a99f82b8Smrg echo "$0: no input file specified" >&2 128a99f82b8Smrg exit 1 129a99f82b8Smrgelse 130a99f82b8Smrg : 131a99f82b8Smrgfi 132a99f82b8Smrg 133a99f82b8Smrgif [ x"$dir_arg" != x ]; then 134a99f82b8Smrg dst=$src 135a99f82b8Smrg src="" 136a99f82b8Smrg 137a99f82b8Smrg if [ -d "$dst" ]; then 138a99f82b8Smrg instcmd=: 139a99f82b8Smrg chmodcmd="" 140a99f82b8Smrg else 141a99f82b8Smrg instcmd=$mkdirprog 142a99f82b8Smrg fi 143a99f82b8Smrgelse 144a99f82b8Smrg 145a99f82b8Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command 146a99f82b8Smrg# might cause directories to be created, which would be especially bad 147a99f82b8Smrg# if $src (and thus $dsttmp) contains '*'. 148a99f82b8Smrg 149a99f82b8Smrg if [ -f "$src" ] || [ -d "$src" ] 150a99f82b8Smrg then 151a99f82b8Smrg : 152a99f82b8Smrg else 153a99f82b8Smrg echo "$0: $src does not exist" >&2 154a99f82b8Smrg exit 1 155a99f82b8Smrg fi 156a99f82b8Smrg 157a99f82b8Smrg if [ x"$dst" = x ] 158a99f82b8Smrg then 159a99f82b8Smrg echo "$0: no destination specified" >&2 160a99f82b8Smrg exit 1 161a99f82b8Smrg else 162a99f82b8Smrg : 163a99f82b8Smrg fi 164a99f82b8Smrg 165a99f82b8Smrg# If destination is a directory, append the input filename; if your system 166a99f82b8Smrg# does not like double slashes in filenames, you may need to add some logic 167a99f82b8Smrg 168a99f82b8Smrg if [ -d "$dst" ] 169a99f82b8Smrg then 170a99f82b8Smrg dst=$dst/`basename "$src"` 171a99f82b8Smrg else 172a99f82b8Smrg : 173a99f82b8Smrg fi 174a99f82b8Smrgfi 175a99f82b8Smrg 176a99f82b8Smrg## this sed command emulates the dirname command 177a99f82b8Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 178a99f82b8Smrg 179a99f82b8Smrg# Make sure that the destination directory exists. 180a99f82b8Smrg# this part is taken from Noah Friedman's mkinstalldirs script 181a99f82b8Smrg 182a99f82b8Smrg# Skip lots of stat calls in the usual case. 183a99f82b8Smrgif [ ! -d "$dstdir" ]; then 184a99f82b8SmrgdefaultIFS=' 185a99f82b8Smrg ' 186a99f82b8SmrgIFS="${IFS-$defaultIFS}" 187a99f82b8Smrg 188a99f82b8SmrgoIFS=$IFS 189a99f82b8Smrg# Some sh's can't handle IFS=/ for some reason. 190a99f82b8SmrgIFS='%' 191a99f82b8Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 192a99f82b8SmrgIFS=$oIFS 193a99f82b8Smrg 194a99f82b8Smrgpathcomp='' 195a99f82b8Smrg 196a99f82b8Smrgwhile [ $# -ne 0 ] ; do 197a99f82b8Smrg pathcomp=$pathcomp$1 198a99f82b8Smrg shift 199a99f82b8Smrg 200a99f82b8Smrg if [ ! -d "$pathcomp" ] ; 201a99f82b8Smrg then 202a99f82b8Smrg $mkdirprog "$pathcomp" 203a99f82b8Smrg else 204a99f82b8Smrg : 205a99f82b8Smrg fi 206a99f82b8Smrg 207a99f82b8Smrg pathcomp=$pathcomp/ 208a99f82b8Smrgdone 209a99f82b8Smrgfi 210a99f82b8Smrg 211a99f82b8Smrgif [ x"$dir_arg" != x ] 212a99f82b8Smrgthen 213a99f82b8Smrg $doit $instcmd "$dst" && 214a99f82b8Smrg 215a99f82b8Smrg if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi && 216a99f82b8Smrg if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi && 217a99f82b8Smrg if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi && 218a99f82b8Smrg if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi 219a99f82b8Smrgelse 220a99f82b8Smrg 221a99f82b8Smrg# If we're going to rename the final executable, determine the name now. 222a99f82b8Smrg 223a99f82b8Smrg if [ x"$transformarg" = x ] 224a99f82b8Smrg then 225a99f82b8Smrg dstfile=`basename "$dst"` 226a99f82b8Smrg else 227a99f82b8Smrg dstfile=`basename "$dst" $transformbasename | 228a99f82b8Smrg sed $transformarg`$transformbasename 229a99f82b8Smrg fi 230a99f82b8Smrg 231a99f82b8Smrg# don't allow the sed command to completely eliminate the filename 232a99f82b8Smrg 233a99f82b8Smrg if [ x"$dstfile" = x ] 234a99f82b8Smrg then 235a99f82b8Smrg dstfile=`basename "$dst"` 236a99f82b8Smrg else 237a99f82b8Smrg : 238a99f82b8Smrg fi 239a99f82b8Smrg 240a99f82b8Smrg# Make a couple of temp file names in the proper directory. 241a99f82b8Smrg 242a99f82b8Smrg dsttmp=$dstdir/_inst.$$_ 243a99f82b8Smrg rmtmp=$dstdir/_rm.$$_ 244a99f82b8Smrg 245a99f82b8Smrg# Trap to clean up temp files at exit. 246a99f82b8Smrg 247a99f82b8Smrg trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 248a99f82b8Smrg trap '(exit $?); exit' 1 2 13 15 249a99f82b8Smrg 250a99f82b8Smrg# Move or copy the file name to the temp name 251a99f82b8Smrg 252a99f82b8Smrg $doit $instcmd "$src" "$dsttmp" && 253a99f82b8Smrg 254a99f82b8Smrg# and set any options; do chmod last to preserve setuid bits 255a99f82b8Smrg 256a99f82b8Smrg# If any of these fail, we abort the whole thing. If we want to 257a99f82b8Smrg# ignore errors from any of these, just make sure not to ignore 258a99f82b8Smrg# errors from the above "$doit $instcmd $src $dsttmp" command. 259a99f82b8Smrg 260a99f82b8Smrg if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi && 261a99f82b8Smrg if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi && 262a99f82b8Smrg if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi && 263a99f82b8Smrg if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi && 264a99f82b8Smrg 265a99f82b8Smrg# Now remove or move aside any old file at destination location. We try this 266a99f82b8Smrg# two ways since rm can't unlink itself on some systems and the destination 267a99f82b8Smrg# file might be busy for other reasons. In this case, the final cleanup 268a99f82b8Smrg# might fail but the new file should still install successfully. 269a99f82b8Smrg 270a99f82b8Smrg{ 271a99f82b8Smrg if [ -f "$dstdir/$dstfile" ] 272a99f82b8Smrg then 273a99f82b8Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null || 274a99f82b8Smrg $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null || 275a99f82b8Smrg { 276a99f82b8Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 277a99f82b8Smrg (exit 1); exit 278a99f82b8Smrg } 279a99f82b8Smrg else 280a99f82b8Smrg : 281a99f82b8Smrg fi 282a99f82b8Smrg} && 283a99f82b8Smrg 284a99f82b8Smrg# Now rename the file to the real destination. 285a99f82b8Smrg 286a99f82b8Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 287a99f82b8Smrg 288a99f82b8Smrgfi && 289a99f82b8Smrg 290a99f82b8Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 291a99f82b8Smrg 292a99f82b8Smrg{ 293a99f82b8Smrg (exit 0); exit 294a99f82b8Smrg} 295