install-sh revision 7da8b7e3
17da8b7e3Smrg#!/bin/sh 27da8b7e3Smrg# install - install a program, script, or datafile 37da8b7e3Smrg 47da8b7e3Smrgscriptversion=2005-05-14.22 57da8b7e3Smrg 67da8b7e3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 77da8b7e3Smrg# later released in X11R6 (xc/config/util/install.sh) with the 87da8b7e3Smrg# following copyright and license. 97da8b7e3Smrg# 107da8b7e3Smrg# Copyright (C) 1994 X Consortium 117da8b7e3Smrg# 127da8b7e3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 137da8b7e3Smrg# of this software and associated documentation files (the "Software"), to 147da8b7e3Smrg# deal in the Software without restriction, including without limitation the 157da8b7e3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 167da8b7e3Smrg# sell copies of the Software, and to permit persons to whom the Software is 177da8b7e3Smrg# furnished to do so, subject to the following conditions: 187da8b7e3Smrg# 197da8b7e3Smrg# The above copyright notice and this permission notice shall be included in 207da8b7e3Smrg# all copies or substantial portions of the Software. 217da8b7e3Smrg# 227da8b7e3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 237da8b7e3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 247da8b7e3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 257da8b7e3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 267da8b7e3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 277da8b7e3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 287da8b7e3Smrg# 297da8b7e3Smrg# Except as contained in this notice, the name of the X Consortium shall not 307da8b7e3Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 317da8b7e3Smrg# ings in this Software without prior written authorization from the X Consor- 327da8b7e3Smrg# tium. 337da8b7e3Smrg# 347da8b7e3Smrg# 357da8b7e3Smrg# FSF changes to this file are in the public domain. 367da8b7e3Smrg# 377da8b7e3Smrg# Calling this script install-sh is preferred over install.sh, to prevent 387da8b7e3Smrg# `make' implicit rules from creating a file called install from it 397da8b7e3Smrg# when there is no Makefile. 407da8b7e3Smrg# 417da8b7e3Smrg# This script is compatible with the BSD install script, but was written 427da8b7e3Smrg# from scratch. It can only install one file at a time, a restriction 437da8b7e3Smrg# shared with many OS's install programs. 447da8b7e3Smrg 457da8b7e3Smrg# set DOITPROG to echo to test this script 467da8b7e3Smrg 477da8b7e3Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 487da8b7e3Smrgdoit="${DOITPROG-}" 497da8b7e3Smrg 507da8b7e3Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 517da8b7e3Smrg 527da8b7e3Smrgmvprog="${MVPROG-mv}" 537da8b7e3Smrgcpprog="${CPPROG-cp}" 547da8b7e3Smrgchmodprog="${CHMODPROG-chmod}" 557da8b7e3Smrgchownprog="${CHOWNPROG-chown}" 567da8b7e3Smrgchgrpprog="${CHGRPPROG-chgrp}" 577da8b7e3Smrgstripprog="${STRIPPROG-strip}" 587da8b7e3Smrgrmprog="${RMPROG-rm}" 597da8b7e3Smrgmkdirprog="${MKDIRPROG-mkdir}" 607da8b7e3Smrg 617da8b7e3Smrgchmodcmd="$chmodprog 0755" 627da8b7e3Smrgchowncmd= 637da8b7e3Smrgchgrpcmd= 647da8b7e3Smrgstripcmd= 657da8b7e3Smrgrmcmd="$rmprog -f" 667da8b7e3Smrgmvcmd="$mvprog" 677da8b7e3Smrgsrc= 687da8b7e3Smrgdst= 697da8b7e3Smrgdir_arg= 707da8b7e3Smrgdstarg= 717da8b7e3Smrgno_target_directory= 727da8b7e3Smrg 737da8b7e3Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 747da8b7e3Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 757da8b7e3Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 767da8b7e3Smrg or: $0 [OPTION]... -d DIRECTORIES... 777da8b7e3Smrg 787da8b7e3SmrgIn the 1st form, copy SRCFILE to DSTFILE. 797da8b7e3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 807da8b7e3SmrgIn the 4th, create DIRECTORIES. 817da8b7e3Smrg 827da8b7e3SmrgOptions: 837da8b7e3Smrg-c (ignored) 847da8b7e3Smrg-d create directories instead of installing files. 857da8b7e3Smrg-g GROUP $chgrpprog installed files to GROUP. 867da8b7e3Smrg-m MODE $chmodprog installed files to MODE. 877da8b7e3Smrg-o USER $chownprog installed files to USER. 887da8b7e3Smrg-s $stripprog installed files. 897da8b7e3Smrg-t DIRECTORY install into DIRECTORY. 907da8b7e3Smrg-T report an error if DSTFILE is a directory. 917da8b7e3Smrg--help display this help and exit. 927da8b7e3Smrg--version display version info and exit. 937da8b7e3Smrg 947da8b7e3SmrgEnvironment variables override the default commands: 957da8b7e3Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 967da8b7e3Smrg" 977da8b7e3Smrg 987da8b7e3Smrgwhile test -n "$1"; do 997da8b7e3Smrg case $1 in 1007da8b7e3Smrg -c) shift 1017da8b7e3Smrg continue;; 1027da8b7e3Smrg 1037da8b7e3Smrg -d) dir_arg=true 1047da8b7e3Smrg shift 1057da8b7e3Smrg continue;; 1067da8b7e3Smrg 1077da8b7e3Smrg -g) chgrpcmd="$chgrpprog $2" 1087da8b7e3Smrg shift 1097da8b7e3Smrg shift 1107da8b7e3Smrg continue;; 1117da8b7e3Smrg 1127da8b7e3Smrg --help) echo "$usage"; exit $?;; 1137da8b7e3Smrg 1147da8b7e3Smrg -m) chmodcmd="$chmodprog $2" 1157da8b7e3Smrg shift 1167da8b7e3Smrg shift 1177da8b7e3Smrg continue;; 1187da8b7e3Smrg 1197da8b7e3Smrg -o) chowncmd="$chownprog $2" 1207da8b7e3Smrg shift 1217da8b7e3Smrg shift 1227da8b7e3Smrg continue;; 1237da8b7e3Smrg 1247da8b7e3Smrg -s) stripcmd=$stripprog 1257da8b7e3Smrg shift 1267da8b7e3Smrg continue;; 1277da8b7e3Smrg 1287da8b7e3Smrg -t) dstarg=$2 1297da8b7e3Smrg shift 1307da8b7e3Smrg shift 1317da8b7e3Smrg continue;; 1327da8b7e3Smrg 1337da8b7e3Smrg -T) no_target_directory=true 1347da8b7e3Smrg shift 1357da8b7e3Smrg continue;; 1367da8b7e3Smrg 1377da8b7e3Smrg --version) echo "$0 $scriptversion"; exit $?;; 1387da8b7e3Smrg 1397da8b7e3Smrg *) # When -d is used, all remaining arguments are directories to create. 1407da8b7e3Smrg # When -t is used, the destination is already specified. 1417da8b7e3Smrg test -n "$dir_arg$dstarg" && break 1427da8b7e3Smrg # Otherwise, the last argument is the destination. Remove it from $@. 1437da8b7e3Smrg for arg 1447da8b7e3Smrg do 1457da8b7e3Smrg if test -n "$dstarg"; then 1467da8b7e3Smrg # $@ is not empty: it contains at least $arg. 1477da8b7e3Smrg set fnord "$@" "$dstarg" 1487da8b7e3Smrg shift # fnord 1497da8b7e3Smrg fi 1507da8b7e3Smrg shift # arg 1517da8b7e3Smrg dstarg=$arg 1527da8b7e3Smrg done 1537da8b7e3Smrg break;; 1547da8b7e3Smrg esac 1557da8b7e3Smrgdone 1567da8b7e3Smrg 1577da8b7e3Smrgif test -z "$1"; then 1587da8b7e3Smrg if test -z "$dir_arg"; then 1597da8b7e3Smrg echo "$0: no input file specified." >&2 1607da8b7e3Smrg exit 1 1617da8b7e3Smrg fi 1627da8b7e3Smrg # It's OK to call `install-sh -d' without argument. 1637da8b7e3Smrg # This can happen when creating conditional directories. 1647da8b7e3Smrg exit 0 1657da8b7e3Smrgfi 1667da8b7e3Smrg 1677da8b7e3Smrgfor src 1687da8b7e3Smrgdo 1697da8b7e3Smrg # Protect names starting with `-'. 1707da8b7e3Smrg case $src in 1717da8b7e3Smrg -*) src=./$src ;; 1727da8b7e3Smrg esac 1737da8b7e3Smrg 1747da8b7e3Smrg if test -n "$dir_arg"; then 1757da8b7e3Smrg dst=$src 1767da8b7e3Smrg src= 1777da8b7e3Smrg 1787da8b7e3Smrg if test -d "$dst"; then 1797da8b7e3Smrg mkdircmd=: 1807da8b7e3Smrg chmodcmd= 1817da8b7e3Smrg else 1827da8b7e3Smrg mkdircmd=$mkdirprog 1837da8b7e3Smrg fi 1847da8b7e3Smrg else 1857da8b7e3Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 1867da8b7e3Smrg # might cause directories to be created, which would be especially bad 1877da8b7e3Smrg # if $src (and thus $dsttmp) contains '*'. 1887da8b7e3Smrg if test ! -f "$src" && test ! -d "$src"; then 1897da8b7e3Smrg echo "$0: $src does not exist." >&2 1907da8b7e3Smrg exit 1 1917da8b7e3Smrg fi 1927da8b7e3Smrg 1937da8b7e3Smrg if test -z "$dstarg"; then 1947da8b7e3Smrg echo "$0: no destination specified." >&2 1957da8b7e3Smrg exit 1 1967da8b7e3Smrg fi 1977da8b7e3Smrg 1987da8b7e3Smrg dst=$dstarg 1997da8b7e3Smrg # Protect names starting with `-'. 2007da8b7e3Smrg case $dst in 2017da8b7e3Smrg -*) dst=./$dst ;; 2027da8b7e3Smrg esac 2037da8b7e3Smrg 2047da8b7e3Smrg # If destination is a directory, append the input filename; won't work 2057da8b7e3Smrg # if double slashes aren't ignored. 2067da8b7e3Smrg if test -d "$dst"; then 2077da8b7e3Smrg if test -n "$no_target_directory"; then 2087da8b7e3Smrg echo "$0: $dstarg: Is a directory" >&2 2097da8b7e3Smrg exit 1 2107da8b7e3Smrg fi 2117da8b7e3Smrg dst=$dst/`basename "$src"` 2127da8b7e3Smrg fi 2137da8b7e3Smrg fi 2147da8b7e3Smrg 2157da8b7e3Smrg # This sed command emulates the dirname command. 2167da8b7e3Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 2177da8b7e3Smrg 2187da8b7e3Smrg # Make sure that the destination directory exists. 2197da8b7e3Smrg 2207da8b7e3Smrg # Skip lots of stat calls in the usual case. 2217da8b7e3Smrg if test ! -d "$dstdir"; then 2227da8b7e3Smrg defaultIFS=' 2237da8b7e3Smrg ' 2247da8b7e3Smrg IFS="${IFS-$defaultIFS}" 2257da8b7e3Smrg 2267da8b7e3Smrg oIFS=$IFS 2277da8b7e3Smrg # Some sh's can't handle IFS=/ for some reason. 2287da8b7e3Smrg IFS='%' 2297da8b7e3Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 2307da8b7e3Smrg shift 2317da8b7e3Smrg IFS=$oIFS 2327da8b7e3Smrg 2337da8b7e3Smrg pathcomp= 2347da8b7e3Smrg 2357da8b7e3Smrg while test $# -ne 0 ; do 2367da8b7e3Smrg pathcomp=$pathcomp$1 2377da8b7e3Smrg shift 2387da8b7e3Smrg if test ! -d "$pathcomp"; then 2397da8b7e3Smrg $mkdirprog "$pathcomp" 2407da8b7e3Smrg # mkdir can fail with a `File exist' error in case several 2417da8b7e3Smrg # install-sh are creating the directory concurrently. This 2427da8b7e3Smrg # is OK. 2437da8b7e3Smrg test -d "$pathcomp" || exit 2447da8b7e3Smrg fi 2457da8b7e3Smrg pathcomp=$pathcomp/ 2467da8b7e3Smrg done 2477da8b7e3Smrg fi 2487da8b7e3Smrg 2497da8b7e3Smrg if test -n "$dir_arg"; then 2507da8b7e3Smrg $doit $mkdircmd "$dst" \ 2517da8b7e3Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 2527da8b7e3Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 2537da8b7e3Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 2547da8b7e3Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 2557da8b7e3Smrg 2567da8b7e3Smrg else 2577da8b7e3Smrg dstfile=`basename "$dst"` 2587da8b7e3Smrg 2597da8b7e3Smrg # Make a couple of temp file names in the proper directory. 2607da8b7e3Smrg dsttmp=$dstdir/_inst.$$_ 2617da8b7e3Smrg rmtmp=$dstdir/_rm.$$_ 2627da8b7e3Smrg 2637da8b7e3Smrg # Trap to clean up those temp files at exit. 2647da8b7e3Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 2657da8b7e3Smrg trap '(exit $?); exit' 1 2 13 15 2667da8b7e3Smrg 2677da8b7e3Smrg # Copy the file name to the temp name. 2687da8b7e3Smrg $doit $cpprog "$src" "$dsttmp" && 2697da8b7e3Smrg 2707da8b7e3Smrg # and set any options; do chmod last to preserve setuid bits. 2717da8b7e3Smrg # 2727da8b7e3Smrg # If any of these fail, we abort the whole thing. If we want to 2737da8b7e3Smrg # ignore errors from any of these, just make sure not to ignore 2747da8b7e3Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 2757da8b7e3Smrg # 2767da8b7e3Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 2777da8b7e3Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 2787da8b7e3Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 2797da8b7e3Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 2807da8b7e3Smrg 2817da8b7e3Smrg # Now rename the file to the real destination. 2827da8b7e3Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 2837da8b7e3Smrg || { 2847da8b7e3Smrg # The rename failed, perhaps because mv can't rename something else 2857da8b7e3Smrg # to itself, or perhaps because mv is so ancient that it does not 2867da8b7e3Smrg # support -f. 2877da8b7e3Smrg 2887da8b7e3Smrg # Now remove or move aside any old file at destination location. 2897da8b7e3Smrg # We try this two ways since rm can't unlink itself on some 2907da8b7e3Smrg # systems and the destination file might be busy for other 2917da8b7e3Smrg # reasons. In this case, the final cleanup might fail but the new 2927da8b7e3Smrg # file should still install successfully. 2937da8b7e3Smrg { 2947da8b7e3Smrg if test -f "$dstdir/$dstfile"; then 2957da8b7e3Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 2967da8b7e3Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 2977da8b7e3Smrg || { 2987da8b7e3Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 2997da8b7e3Smrg (exit 1); exit 1 3007da8b7e3Smrg } 3017da8b7e3Smrg else 3027da8b7e3Smrg : 3037da8b7e3Smrg fi 3047da8b7e3Smrg } && 3057da8b7e3Smrg 3067da8b7e3Smrg # Now rename the file to the real destination. 3077da8b7e3Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 3087da8b7e3Smrg } 3097da8b7e3Smrg } 3107da8b7e3Smrg fi || { (exit 1); exit 1; } 3117da8b7e3Smrgdone 3127da8b7e3Smrg 3137da8b7e3Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 3147da8b7e3Smrg{ 3157da8b7e3Smrg (exit 0); exit 0 3167da8b7e3Smrg} 3177da8b7e3Smrg 3187da8b7e3Smrg# Local variables: 3197da8b7e3Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 3207da8b7e3Smrg# time-stamp-start: "scriptversion=" 3217da8b7e3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 3227da8b7e3Smrg# time-stamp-end: "$" 3237da8b7e3Smrg# End: 324