install-sh revision 7706df26
17706df26Smrg#!/bin/sh 27706df26Smrg# install - install a program, script, or datafile 37706df26Smrg 47706df26Smrgscriptversion=2005-05-14.22 57706df26Smrg 67706df26Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 77706df26Smrg# later released in X11R6 (xc/config/util/install.sh) with the 87706df26Smrg# following copyright and license. 97706df26Smrg# 107706df26Smrg# Copyright (C) 1994 X Consortium 117706df26Smrg# 127706df26Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 137706df26Smrg# of this software and associated documentation files (the "Software"), to 147706df26Smrg# deal in the Software without restriction, including without limitation the 157706df26Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 167706df26Smrg# sell copies of the Software, and to permit persons to whom the Software is 177706df26Smrg# furnished to do so, subject to the following conditions: 187706df26Smrg# 197706df26Smrg# The above copyright notice and this permission notice shall be included in 207706df26Smrg# all copies or substantial portions of the Software. 217706df26Smrg# 227706df26Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 237706df26Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 247706df26Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 257706df26Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 267706df26Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 277706df26Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 287706df26Smrg# 297706df26Smrg# Except as contained in this notice, the name of the X Consortium shall not 307706df26Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 317706df26Smrg# ings in this Software without prior written authorization from the X Consor- 327706df26Smrg# tium. 337706df26Smrg# 347706df26Smrg# 357706df26Smrg# FSF changes to this file are in the public domain. 367706df26Smrg# 377706df26Smrg# Calling this script install-sh is preferred over install.sh, to prevent 387706df26Smrg# `make' implicit rules from creating a file called install from it 397706df26Smrg# when there is no Makefile. 407706df26Smrg# 417706df26Smrg# This script is compatible with the BSD install script, but was written 427706df26Smrg# from scratch. It can only install one file at a time, a restriction 437706df26Smrg# shared with many OS's install programs. 447706df26Smrg 457706df26Smrg# set DOITPROG to echo to test this script 467706df26Smrg 477706df26Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 487706df26Smrgdoit="${DOITPROG-}" 497706df26Smrg 507706df26Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 517706df26Smrg 527706df26Smrgmvprog="${MVPROG-mv}" 537706df26Smrgcpprog="${CPPROG-cp}" 547706df26Smrgchmodprog="${CHMODPROG-chmod}" 557706df26Smrgchownprog="${CHOWNPROG-chown}" 567706df26Smrgchgrpprog="${CHGRPPROG-chgrp}" 577706df26Smrgstripprog="${STRIPPROG-strip}" 587706df26Smrgrmprog="${RMPROG-rm}" 597706df26Smrgmkdirprog="${MKDIRPROG-mkdir}" 607706df26Smrg 617706df26Smrgchmodcmd="$chmodprog 0755" 627706df26Smrgchowncmd= 637706df26Smrgchgrpcmd= 647706df26Smrgstripcmd= 657706df26Smrgrmcmd="$rmprog -f" 667706df26Smrgmvcmd="$mvprog" 677706df26Smrgsrc= 687706df26Smrgdst= 697706df26Smrgdir_arg= 707706df26Smrgdstarg= 717706df26Smrgno_target_directory= 727706df26Smrg 737706df26Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 747706df26Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 757706df26Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 767706df26Smrg or: $0 [OPTION]... -d DIRECTORIES... 777706df26Smrg 787706df26SmrgIn the 1st form, copy SRCFILE to DSTFILE. 797706df26SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 807706df26SmrgIn the 4th, create DIRECTORIES. 817706df26Smrg 827706df26SmrgOptions: 837706df26Smrg-c (ignored) 847706df26Smrg-d create directories instead of installing files. 857706df26Smrg-g GROUP $chgrpprog installed files to GROUP. 867706df26Smrg-m MODE $chmodprog installed files to MODE. 877706df26Smrg-o USER $chownprog installed files to USER. 887706df26Smrg-s $stripprog installed files. 897706df26Smrg-t DIRECTORY install into DIRECTORY. 907706df26Smrg-T report an error if DSTFILE is a directory. 917706df26Smrg--help display this help and exit. 927706df26Smrg--version display version info and exit. 937706df26Smrg 947706df26SmrgEnvironment variables override the default commands: 957706df26Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 967706df26Smrg" 977706df26Smrg 987706df26Smrgwhile test -n "$1"; do 997706df26Smrg case $1 in 1007706df26Smrg -c) shift 1017706df26Smrg continue;; 1027706df26Smrg 1037706df26Smrg -d) dir_arg=true 1047706df26Smrg shift 1057706df26Smrg continue;; 1067706df26Smrg 1077706df26Smrg -g) chgrpcmd="$chgrpprog $2" 1087706df26Smrg shift 1097706df26Smrg shift 1107706df26Smrg continue;; 1117706df26Smrg 1127706df26Smrg --help) echo "$usage"; exit $?;; 1137706df26Smrg 1147706df26Smrg -m) chmodcmd="$chmodprog $2" 1157706df26Smrg shift 1167706df26Smrg shift 1177706df26Smrg continue;; 1187706df26Smrg 1197706df26Smrg -o) chowncmd="$chownprog $2" 1207706df26Smrg shift 1217706df26Smrg shift 1227706df26Smrg continue;; 1237706df26Smrg 1247706df26Smrg -s) stripcmd=$stripprog 1257706df26Smrg shift 1267706df26Smrg continue;; 1277706df26Smrg 1287706df26Smrg -t) dstarg=$2 1297706df26Smrg shift 1307706df26Smrg shift 1317706df26Smrg continue;; 1327706df26Smrg 1337706df26Smrg -T) no_target_directory=true 1347706df26Smrg shift 1357706df26Smrg continue;; 1367706df26Smrg 1377706df26Smrg --version) echo "$0 $scriptversion"; exit $?;; 1387706df26Smrg 1397706df26Smrg *) # When -d is used, all remaining arguments are directories to create. 1407706df26Smrg # When -t is used, the destination is already specified. 1417706df26Smrg test -n "$dir_arg$dstarg" && break 1427706df26Smrg # Otherwise, the last argument is the destination. Remove it from $@. 1437706df26Smrg for arg 1447706df26Smrg do 1457706df26Smrg if test -n "$dstarg"; then 1467706df26Smrg # $@ is not empty: it contains at least $arg. 1477706df26Smrg set fnord "$@" "$dstarg" 1487706df26Smrg shift # fnord 1497706df26Smrg fi 1507706df26Smrg shift # arg 1517706df26Smrg dstarg=$arg 1527706df26Smrg done 1537706df26Smrg break;; 1547706df26Smrg esac 1557706df26Smrgdone 1567706df26Smrg 1577706df26Smrgif test -z "$1"; then 1587706df26Smrg if test -z "$dir_arg"; then 1597706df26Smrg echo "$0: no input file specified." >&2 1607706df26Smrg exit 1 1617706df26Smrg fi 1627706df26Smrg # It's OK to call `install-sh -d' without argument. 1637706df26Smrg # This can happen when creating conditional directories. 1647706df26Smrg exit 0 1657706df26Smrgfi 1667706df26Smrg 1677706df26Smrgfor src 1687706df26Smrgdo 1697706df26Smrg # Protect names starting with `-'. 1707706df26Smrg case $src in 1717706df26Smrg -*) src=./$src ;; 1727706df26Smrg esac 1737706df26Smrg 1747706df26Smrg if test -n "$dir_arg"; then 1757706df26Smrg dst=$src 1767706df26Smrg src= 1777706df26Smrg 1787706df26Smrg if test -d "$dst"; then 1797706df26Smrg mkdircmd=: 1807706df26Smrg chmodcmd= 1817706df26Smrg else 1827706df26Smrg mkdircmd=$mkdirprog 1837706df26Smrg fi 1847706df26Smrg else 1857706df26Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 1867706df26Smrg # might cause directories to be created, which would be especially bad 1877706df26Smrg # if $src (and thus $dsttmp) contains '*'. 1887706df26Smrg if test ! -f "$src" && test ! -d "$src"; then 1897706df26Smrg echo "$0: $src does not exist." >&2 1907706df26Smrg exit 1 1917706df26Smrg fi 1927706df26Smrg 1937706df26Smrg if test -z "$dstarg"; then 1947706df26Smrg echo "$0: no destination specified." >&2 1957706df26Smrg exit 1 1967706df26Smrg fi 1977706df26Smrg 1987706df26Smrg dst=$dstarg 1997706df26Smrg # Protect names starting with `-'. 2007706df26Smrg case $dst in 2017706df26Smrg -*) dst=./$dst ;; 2027706df26Smrg esac 2037706df26Smrg 2047706df26Smrg # If destination is a directory, append the input filename; won't work 2057706df26Smrg # if double slashes aren't ignored. 2067706df26Smrg if test -d "$dst"; then 2077706df26Smrg if test -n "$no_target_directory"; then 2087706df26Smrg echo "$0: $dstarg: Is a directory" >&2 2097706df26Smrg exit 1 2107706df26Smrg fi 2117706df26Smrg dst=$dst/`basename "$src"` 2127706df26Smrg fi 2137706df26Smrg fi 2147706df26Smrg 2157706df26Smrg # This sed command emulates the dirname command. 2167706df26Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 2177706df26Smrg 2187706df26Smrg # Make sure that the destination directory exists. 2197706df26Smrg 2207706df26Smrg # Skip lots of stat calls in the usual case. 2217706df26Smrg if test ! -d "$dstdir"; then 2227706df26Smrg defaultIFS=' 2237706df26Smrg ' 2247706df26Smrg IFS="${IFS-$defaultIFS}" 2257706df26Smrg 2267706df26Smrg oIFS=$IFS 2277706df26Smrg # Some sh's can't handle IFS=/ for some reason. 2287706df26Smrg IFS='%' 2297706df26Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 2307706df26Smrg shift 2317706df26Smrg IFS=$oIFS 2327706df26Smrg 2337706df26Smrg pathcomp= 2347706df26Smrg 2357706df26Smrg while test $# -ne 0 ; do 2367706df26Smrg pathcomp=$pathcomp$1 2377706df26Smrg shift 2387706df26Smrg if test ! -d "$pathcomp"; then 2397706df26Smrg $mkdirprog "$pathcomp" 2407706df26Smrg # mkdir can fail with a `File exist' error in case several 2417706df26Smrg # install-sh are creating the directory concurrently. This 2427706df26Smrg # is OK. 2437706df26Smrg test -d "$pathcomp" || exit 2447706df26Smrg fi 2457706df26Smrg pathcomp=$pathcomp/ 2467706df26Smrg done 2477706df26Smrg fi 2487706df26Smrg 2497706df26Smrg if test -n "$dir_arg"; then 2507706df26Smrg $doit $mkdircmd "$dst" \ 2517706df26Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 2527706df26Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 2537706df26Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 2547706df26Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 2557706df26Smrg 2567706df26Smrg else 2577706df26Smrg dstfile=`basename "$dst"` 2587706df26Smrg 2597706df26Smrg # Make a couple of temp file names in the proper directory. 2607706df26Smrg dsttmp=$dstdir/_inst.$$_ 2617706df26Smrg rmtmp=$dstdir/_rm.$$_ 2627706df26Smrg 2637706df26Smrg # Trap to clean up those temp files at exit. 2647706df26Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 2657706df26Smrg trap '(exit $?); exit' 1 2 13 15 2667706df26Smrg 2677706df26Smrg # Copy the file name to the temp name. 2687706df26Smrg $doit $cpprog "$src" "$dsttmp" && 2697706df26Smrg 2707706df26Smrg # and set any options; do chmod last to preserve setuid bits. 2717706df26Smrg # 2727706df26Smrg # If any of these fail, we abort the whole thing. If we want to 2737706df26Smrg # ignore errors from any of these, just make sure not to ignore 2747706df26Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 2757706df26Smrg # 2767706df26Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 2777706df26Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 2787706df26Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 2797706df26Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 2807706df26Smrg 2817706df26Smrg # Now rename the file to the real destination. 2827706df26Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 2837706df26Smrg || { 2847706df26Smrg # The rename failed, perhaps because mv can't rename something else 2857706df26Smrg # to itself, or perhaps because mv is so ancient that it does not 2867706df26Smrg # support -f. 2877706df26Smrg 2887706df26Smrg # Now remove or move aside any old file at destination location. 2897706df26Smrg # We try this two ways since rm can't unlink itself on some 2907706df26Smrg # systems and the destination file might be busy for other 2917706df26Smrg # reasons. In this case, the final cleanup might fail but the new 2927706df26Smrg # file should still install successfully. 2937706df26Smrg { 2947706df26Smrg if test -f "$dstdir/$dstfile"; then 2957706df26Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 2967706df26Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 2977706df26Smrg || { 2987706df26Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 2997706df26Smrg (exit 1); exit 1 3007706df26Smrg } 3017706df26Smrg else 3027706df26Smrg : 3037706df26Smrg fi 3047706df26Smrg } && 3057706df26Smrg 3067706df26Smrg # Now rename the file to the real destination. 3077706df26Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 3087706df26Smrg } 3097706df26Smrg } 3107706df26Smrg fi || { (exit 1); exit 1; } 3117706df26Smrgdone 3127706df26Smrg 3137706df26Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 3147706df26Smrg{ 3157706df26Smrg (exit 0); exit 0 3167706df26Smrg} 3177706df26Smrg 3187706df26Smrg# Local variables: 3197706df26Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 3207706df26Smrg# time-stamp-start: "scriptversion=" 3217706df26Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 3227706df26Smrg# time-stamp-end: "$" 3237706df26Smrg# End: 324