1102644Snectar#!/bin/sh 255682Smarkm# install - install a program, script, or datafile 3142403Snectar 4178825Sdfrscriptversion=2006-10-14.15 5142403Snectar 6127808Snectar# This originates from X11R5 (mit/util/scripts/install.sh), which was 7127808Snectar# later released in X11R6 (xc/config/util/install.sh) with the 8127808Snectar# following copyright and license. 9102644Snectar# 10127808Snectar# Copyright (C) 1994 X Consortium 11102644Snectar# 12127808Snectar# Permission is hereby granted, free of charge, to any person obtaining a copy 13127808Snectar# of this software and associated documentation files (the "Software"), to 14127808Snectar# deal in the Software without restriction, including without limitation the 15127808Snectar# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16127808Snectar# sell copies of the Software, and to permit persons to whom the Software is 17127808Snectar# furnished to do so, subject to the following conditions: 18127808Snectar# 19127808Snectar# The above copyright notice and this permission notice shall be included in 20127808Snectar# all copies or substantial portions of the Software. 21127808Snectar# 22127808Snectar# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23127808Snectar# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24127808Snectar# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25127808Snectar# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26127808Snectar# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27127808Snectar# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28127808Snectar# 29127808Snectar# Except as contained in this notice, the name of the X Consortium shall not 30127808Snectar# be used in advertising or otherwise to promote the sale, use or other deal- 31127808Snectar# ings in this Software without prior written authorization from the X Consor- 32127808Snectar# tium. 33127808Snectar# 34127808Snectar# 35127808Snectar# FSF changes to this file are in the public domain. 36127808Snectar# 3755682Smarkm# Calling this script install-sh is preferred over install.sh, to prevent 3855682Smarkm# `make' implicit rules from creating a file called install from it 3955682Smarkm# when there is no Makefile. 4055682Smarkm# 4155682Smarkm# This script is compatible with the BSD install script, but was written 42178825Sdfr# from scratch. 4355682Smarkm 44178825Sdfrnl=' 45178825Sdfr' 46178825SdfrIFS=" "" $nl" 47178825Sdfr 4855682Smarkm# set DOITPROG to echo to test this script 4955682Smarkm 5055682Smarkm# Don't use :- since 4.3BSD and earlier shells don't like it. 5155682Smarkmdoit="${DOITPROG-}" 52178825Sdfrif test -z "$doit"; then 53178825Sdfr doit_exec=exec 54178825Sdfrelse 55178825Sdfr doit_exec=$doit 56178825Sdfrfi 5755682Smarkm 58178825Sdfr# Put in absolute file names if you don't have them in your path; 59178825Sdfr# or use environment vars. 6055682Smarkm 6155682Smarkmmvprog="${MVPROG-mv}" 6255682Smarkmcpprog="${CPPROG-cp}" 6355682Smarkmchmodprog="${CHMODPROG-chmod}" 6455682Smarkmchownprog="${CHOWNPROG-chown}" 6555682Smarkmchgrpprog="${CHGRPPROG-chgrp}" 6655682Smarkmstripprog="${STRIPPROG-strip}" 6755682Smarkmrmprog="${RMPROG-rm}" 6855682Smarkmmkdirprog="${MKDIRPROG-mkdir}" 6955682Smarkm 70178825Sdfrposix_glob= 71178825Sdfrposix_mkdir= 72178825Sdfr 73178825Sdfr# Desired mode of installed file. 74178825Sdfrmode=0755 75178825Sdfr 76178825Sdfrchmodcmd=$chmodprog 77142403Snectarchowncmd= 78142403Snectarchgrpcmd= 79142403Snectarstripcmd= 8055682Smarkmrmcmd="$rmprog -f" 8155682Smarkmmvcmd="$mvprog" 82142403Snectarsrc= 83142403Snectardst= 84142403Snectardir_arg= 85178825Sdfrdstarg= 86178825Sdfrno_target_directory= 8755682Smarkm 88178825Sdfrusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 89142403Snectar or: $0 [OPTION]... SRCFILES... DIRECTORY 90178825Sdfr or: $0 [OPTION]... -t DIRECTORY SRCFILES... 91178825Sdfr or: $0 [OPTION]... -d DIRECTORIES... 9255682Smarkm 93178825SdfrIn the 1st form, copy SRCFILE to DSTFILE. 94178825SdfrIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 95178825SdfrIn the 4th, create DIRECTORIES. 9655682Smarkm 97142403SnectarOptions: 98178825Sdfr-c (ignored) 99142403Snectar-d create directories instead of installing files. 100178825Sdfr-g GROUP $chgrpprog installed files to GROUP. 101178825Sdfr-m MODE $chmodprog installed files to MODE. 102178825Sdfr-o USER $chownprog installed files to USER. 103178825Sdfr-s $stripprog installed files. 104178825Sdfr-t DIRECTORY install into DIRECTORY. 105178825Sdfr-T report an error if DSTFILE is a directory. 106142403Snectar--help display this help and exit. 107142403Snectar--version display version info and exit. 10855682Smarkm 109142403SnectarEnvironment variables override the default commands: 110142403Snectar CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 111142403Snectar" 11255682Smarkm 113178825Sdfrwhile test $# -ne 0; do 114142403Snectar case $1 in 115178825Sdfr -c) shift 116142403Snectar continue;; 11755682Smarkm 118142403Snectar -d) dir_arg=true 119142403Snectar shift 120142403Snectar continue;; 12155682Smarkm 122142403Snectar -g) chgrpcmd="$chgrpprog $2" 123142403Snectar shift 124142403Snectar shift 125142403Snectar continue;; 12655682Smarkm 127178825Sdfr --help) echo "$usage"; exit $?;; 12855682Smarkm 129178825Sdfr -m) mode=$2 130142403Snectar shift 131142403Snectar shift 132178825Sdfr case $mode in 133178825Sdfr *' '* | *' '* | *' 134178825Sdfr'* | *'*'* | *'?'* | *'['*) 135178825Sdfr echo "$0: invalid mode: $mode" >&2 136178825Sdfr exit 1;; 137178825Sdfr esac 138142403Snectar continue;; 13955682Smarkm 140142403Snectar -o) chowncmd="$chownprog $2" 141142403Snectar shift 142142403Snectar shift 143142403Snectar continue;; 144127808Snectar 145142403Snectar -s) stripcmd=$stripprog 146142403Snectar shift 147142403Snectar continue;; 14855682Smarkm 149178825Sdfr -t) dstarg=$2 150178825Sdfr shift 151178825Sdfr shift 152178825Sdfr continue;; 15355682Smarkm 154178825Sdfr -T) no_target_directory=true 155178825Sdfr shift 156178825Sdfr continue;; 157127808Snectar 158178825Sdfr --version) echo "$0 $scriptversion"; exit $?;; 159178825Sdfr 160178825Sdfr --) shift 161142403Snectar break;; 162178825Sdfr 163178825Sdfr -*) echo "$0: invalid option: $1" >&2 164178825Sdfr exit 1;; 165178825Sdfr 166178825Sdfr *) break;; 167142403Snectar esac 168142403Snectardone 16955682Smarkm 170178825Sdfrif test $# -ne 0 && test -z "$dir_arg$dstarg"; then 171178825Sdfr # When -d is used, all remaining arguments are directories to create. 172178825Sdfr # When -t is used, the destination is already specified. 173178825Sdfr # Otherwise, the last argument is the destination. Remove it from $@. 174178825Sdfr for arg 175178825Sdfr do 176178825Sdfr if test -n "$dstarg"; then 177178825Sdfr # $@ is not empty: it contains at least $arg. 178178825Sdfr set fnord "$@" "$dstarg" 179178825Sdfr shift # fnord 180178825Sdfr fi 181178825Sdfr shift # arg 182178825Sdfr dstarg=$arg 183178825Sdfr done 184178825Sdfrfi 185178825Sdfr 186178825Sdfrif test $# -eq 0; then 187142403Snectar if test -z "$dir_arg"; then 188142403Snectar echo "$0: no input file specified." >&2 189142403Snectar exit 1 190142403Snectar fi 191142403Snectar # It's OK to call `install-sh -d' without argument. 192142403Snectar # This can happen when creating conditional directories. 193142403Snectar exit 0 19455682Smarkmfi 19555682Smarkm 196178825Sdfrif test -z "$dir_arg"; then 197178825Sdfr trap '(exit $?); exit' 1 2 13 15 198178825Sdfr 199178825Sdfr # Set umask so as not to create temps with too-generous modes. 200178825Sdfr # However, 'strip' requires both read and write access to temps. 201178825Sdfr case $mode in 202178825Sdfr # Optimize common cases. 203178825Sdfr *644) cp_umask=133;; 204178825Sdfr *755) cp_umask=22;; 205178825Sdfr 206178825Sdfr *[0-7]) 207178825Sdfr if test -z "$stripcmd"; then 208178825Sdfr u_plus_rw= 209178825Sdfr else 210178825Sdfr u_plus_rw='% 200' 211178825Sdfr fi 212178825Sdfr cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 213178825Sdfr *) 214178825Sdfr if test -z "$stripcmd"; then 215178825Sdfr u_plus_rw= 216178825Sdfr else 217178825Sdfr u_plus_rw=,u+rw 218178825Sdfr fi 219178825Sdfr cp_umask=$mode$u_plus_rw;; 220178825Sdfr esac 221178825Sdfrfi 222178825Sdfr 223142403Snectarfor src 224142403Snectardo 225142403Snectar # Protect names starting with `-'. 226142403Snectar case $src in 227142403Snectar -*) src=./$src ;; 228142403Snectar esac 22955682Smarkm 230142403Snectar if test -n "$dir_arg"; then 231142403Snectar dst=$src 232178825Sdfr dstdir=$dst 233178825Sdfr test -d "$dstdir" 234178825Sdfr dstdir_status=$? 235178825Sdfr else 23655682Smarkm 237178825Sdfr # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 238142403Snectar # might cause directories to be created, which would be especially bad 239142403Snectar # if $src (and thus $dsttmp) contains '*'. 240142403Snectar if test ! -f "$src" && test ! -d "$src"; then 241142403Snectar echo "$0: $src does not exist." >&2 242142403Snectar exit 1 243142403Snectar fi 24455682Smarkm 245142403Snectar if test -z "$dstarg"; then 246142403Snectar echo "$0: no destination specified." >&2 247142403Snectar exit 1 248142403Snectar fi 24955682Smarkm 250142403Snectar dst=$dstarg 251142403Snectar # Protect names starting with `-'. 252142403Snectar case $dst in 253142403Snectar -*) dst=./$dst ;; 254142403Snectar esac 25555682Smarkm 256142403Snectar # If destination is a directory, append the input filename; won't work 257142403Snectar # if double slashes aren't ignored. 258142403Snectar if test -d "$dst"; then 259178825Sdfr if test -n "$no_target_directory"; then 260178825Sdfr echo "$0: $dstarg: Is a directory" >&2 261178825Sdfr exit 1 262178825Sdfr fi 263178825Sdfr dstdir=$dst 264178825Sdfr dst=$dstdir/`basename "$src"` 265178825Sdfr dstdir_status=0 266178825Sdfr else 267178825Sdfr # Prefer dirname, but fall back on a substitute if dirname fails. 268178825Sdfr dstdir=` 269178825Sdfr (dirname "$dst") 2>/dev/null || 270178825Sdfr expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 271178825Sdfr X"$dst" : 'X\(//\)[^/]' \| \ 272178825Sdfr X"$dst" : 'X\(//\)$' \| \ 273178825Sdfr X"$dst" : 'X\(/\)' \| . 2>/dev/null || 274178825Sdfr echo X"$dst" | 275178825Sdfr sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 276178825Sdfr s//\1/ 277178825Sdfr q 278178825Sdfr } 279178825Sdfr /^X\(\/\/\)[^/].*/{ 280178825Sdfr s//\1/ 281178825Sdfr q 282178825Sdfr } 283178825Sdfr /^X\(\/\/\)$/{ 284178825Sdfr s//\1/ 285178825Sdfr q 286178825Sdfr } 287178825Sdfr /^X\(\/\).*/{ 288178825Sdfr s//\1/ 289178825Sdfr q 290178825Sdfr } 291178825Sdfr s/.*/./; q' 292178825Sdfr ` 293178825Sdfr 294178825Sdfr test -d "$dstdir" 295178825Sdfr dstdir_status=$? 296142403Snectar fi 297142403Snectar fi 29855682Smarkm 299178825Sdfr obsolete_mkdir_used=false 30055682Smarkm 301178825Sdfr if test $dstdir_status != 0; then 302178825Sdfr case $posix_mkdir in 303178825Sdfr '') 304178825Sdfr # Create intermediate dirs using mode 755 as modified by the umask. 305178825Sdfr # This is like FreeBSD 'install' as of 1997-10-28. 306178825Sdfr umask=`umask` 307178825Sdfr case $stripcmd.$umask in 308178825Sdfr # Optimize common cases. 309178825Sdfr *[2367][2367]) mkdir_umask=$umask;; 310178825Sdfr .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 31155682Smarkm 312178825Sdfr *[0-7]) 313178825Sdfr mkdir_umask=`expr $umask + 22 \ 314178825Sdfr - $umask % 100 % 40 + $umask % 20 \ 315178825Sdfr - $umask % 10 % 4 + $umask % 2 316178825Sdfr `;; 317178825Sdfr *) mkdir_umask=$umask,go-w;; 318178825Sdfr esac 31955682Smarkm 320178825Sdfr # With -d, create the new directory with the user-specified mode. 321178825Sdfr # Otherwise, rely on $mkdir_umask. 322178825Sdfr if test -n "$dir_arg"; then 323178825Sdfr mkdir_mode=-m$mode 324178825Sdfr else 325178825Sdfr mkdir_mode= 326178825Sdfr fi 32755682Smarkm 328178825Sdfr posix_mkdir=false 329178825Sdfr case $umask in 330178825Sdfr *[123567][0-7][0-7]) 331178825Sdfr # POSIX mkdir -p sets u+wx bits regardless of umask, which 332178825Sdfr # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 333178825Sdfr ;; 334178825Sdfr *) 335178825Sdfr tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 336178825Sdfr trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 33755682Smarkm 338178825Sdfr if (umask $mkdir_umask && 339178825Sdfr exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 340178825Sdfr then 341178825Sdfr if test -z "$dir_arg" || { 342178825Sdfr # Check for POSIX incompatibilities with -m. 343178825Sdfr # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 344178825Sdfr # other-writeable bit of parent directory when it shouldn't. 345178825Sdfr # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 346178825Sdfr ls_ld_tmpdir=`ls -ld "$tmpdir"` 347178825Sdfr case $ls_ld_tmpdir in 348178825Sdfr d????-?r-*) different_mode=700;; 349178825Sdfr d????-?--*) different_mode=755;; 350178825Sdfr *) false;; 351178825Sdfr esac && 352178825Sdfr $mkdirprog -m$different_mode -p -- "$tmpdir" && { 353178825Sdfr ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 354178825Sdfr test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 355178825Sdfr } 356178825Sdfr } 357178825Sdfr then posix_mkdir=: 358178825Sdfr fi 359178825Sdfr rmdir "$tmpdir/d" "$tmpdir" 360178825Sdfr else 361178825Sdfr # Remove any dirs left behind by ancient mkdir implementations. 362178825Sdfr rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 363178825Sdfr fi 364178825Sdfr trap '' 0;; 365178825Sdfr esac;; 366178825Sdfr esac 367178825Sdfr 368178825Sdfr if 369178825Sdfr $posix_mkdir && ( 370178825Sdfr umask $mkdir_umask && 371178825Sdfr $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 372178825Sdfr ) 373178825Sdfr then : 374178825Sdfr else 375178825Sdfr 376178825Sdfr # The umask is ridiculous, or mkdir does not conform to POSIX, 377178825Sdfr # or it failed possibly due to a race condition. Create the 378178825Sdfr # directory the slow way, step by step, checking for races as we go. 379178825Sdfr 380178825Sdfr case $dstdir in 381178825Sdfr /*) prefix=/ ;; 382178825Sdfr -*) prefix=./ ;; 383178825Sdfr *) prefix= ;; 384178825Sdfr esac 385178825Sdfr 386178825Sdfr case $posix_glob in 387178825Sdfr '') 388178825Sdfr if (set -f) 2>/dev/null; then 389178825Sdfr posix_glob=true 390178825Sdfr else 391178825Sdfr posix_glob=false 392178825Sdfr fi ;; 393178825Sdfr esac 394178825Sdfr 395178825Sdfr oIFS=$IFS 396178825Sdfr IFS=/ 397178825Sdfr $posix_glob && set -f 398178825Sdfr set fnord $dstdir 399142403Snectar shift 400178825Sdfr $posix_glob && set +f 401178825Sdfr IFS=$oIFS 402178825Sdfr 403178825Sdfr prefixes= 404178825Sdfr 405178825Sdfr for d 406178825Sdfr do 407178825Sdfr test -z "$d" && continue 408178825Sdfr 409178825Sdfr prefix=$prefix$d 410178825Sdfr if test -d "$prefix"; then 411178825Sdfr prefixes= 412178825Sdfr else 413178825Sdfr if $posix_mkdir; then 414178825Sdfr (umask=$mkdir_umask && 415178825Sdfr $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 416178825Sdfr # Don't fail if two instances are running concurrently. 417178825Sdfr test -d "$prefix" || exit 1 418178825Sdfr else 419178825Sdfr case $prefix in 420178825Sdfr *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 421178825Sdfr *) qprefix=$prefix;; 422178825Sdfr esac 423178825Sdfr prefixes="$prefixes '$qprefix'" 424178825Sdfr fi 425178825Sdfr fi 426178825Sdfr prefix=$prefix/ 427178825Sdfr done 428178825Sdfr 429178825Sdfr if test -n "$prefixes"; then 430178825Sdfr # Don't fail if two instances are running concurrently. 431178825Sdfr (umask $mkdir_umask && 432178825Sdfr eval "\$doit_exec \$mkdirprog $prefixes") || 433178825Sdfr test -d "$dstdir" || exit 1 434178825Sdfr obsolete_mkdir_used=true 435142403Snectar fi 436178825Sdfr fi 437142403Snectar fi 43855682Smarkm 439142403Snectar if test -n "$dir_arg"; then 440178825Sdfr { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 441178825Sdfr { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 442178825Sdfr { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 443178825Sdfr test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 444142403Snectar else 44555682Smarkm 446142403Snectar # Make a couple of temp file names in the proper directory. 447142403Snectar dsttmp=$dstdir/_inst.$$_ 448142403Snectar rmtmp=$dstdir/_rm.$$_ 44955682Smarkm 450142403Snectar # Trap to clean up those temp files at exit. 451178825Sdfr trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 452127808Snectar 453178825Sdfr # Copy the file name to the temp name. 454178825Sdfr (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 455127808Snectar 456142403Snectar # and set any options; do chmod last to preserve setuid bits. 457142403Snectar # 458142403Snectar # If any of these fail, we abort the whole thing. If we want to 459142403Snectar # ignore errors from any of these, just make sure not to ignore 460178825Sdfr # errors from the above "$doit $cpprog $src $dsttmp" command. 461142403Snectar # 462142403Snectar { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 463142403Snectar && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 464142403Snectar && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 465178825Sdfr && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 46655682Smarkm 467178825Sdfr # Now rename the file to the real destination. 468178825Sdfr { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \ 469178825Sdfr || { 470178825Sdfr # The rename failed, perhaps because mv can't rename something else 471178825Sdfr # to itself, or perhaps because mv is so ancient that it does not 472178825Sdfr # support -f. 47355682Smarkm 474178825Sdfr # Now remove or move aside any old file at destination location. 475178825Sdfr # We try this two ways since rm can't unlink itself on some 476178825Sdfr # systems and the destination file might be busy for other 477178825Sdfr # reasons. In this case, the final cleanup might fail but the new 478178825Sdfr # file should still install successfully. 479178825Sdfr { 480178825Sdfr if test -f "$dst"; then 481178825Sdfr $doit $rmcmd -f "$dst" 2>/dev/null \ 482178825Sdfr || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \ 483178825Sdfr && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\ 484178825Sdfr || { 485178825Sdfr echo "$0: cannot unlink or rename $dst" >&2 486178825Sdfr (exit 1); exit 1 487178825Sdfr } 488178825Sdfr else 489178825Sdfr : 490178825Sdfr fi 491178825Sdfr } && 492178825Sdfr 493178825Sdfr # Now rename the file to the real destination. 494178825Sdfr $doit $mvcmd "$dsttmp" "$dst" 495178825Sdfr } 496178825Sdfr } || exit 1 497178825Sdfr 498178825Sdfr trap '' 0 499178825Sdfr fi 500142403Snectardone 50155682Smarkm 502142403Snectar# Local variables: 503142403Snectar# eval: (add-hook 'write-file-hooks 'time-stamp) 504142403Snectar# time-stamp-start: "scriptversion=" 505142403Snectar# time-stamp-format: "%:y-%02m-%02d.%02H" 506142403Snectar# time-stamp-end: "$" 507142403Snectar# End: 508