install-sh revision 04bdb234571448ed6194e1d4048e6512f2446f1c
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews#! /bin/sh
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews#
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# install - install a program, script, or datafile
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# This comes from X11R5 (mit/util/scripts/install.sh).
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# Copyright 1991 by the Massachusetts Institute of Technology
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# Permission to use, copy, modify, distribute, and sell this software and its
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# documentation for any purpose is hereby granted without fee, provided that
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# the above copyright notice appear in all copies and that both that
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# copyright notice and this permission notice appear in supporting
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# documentation, and that the name of M.I.T. not be used in advertising or
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# publicity pertaining to distribution of the software without specific,
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# written prior permission. M.I.T. makes no representations about the
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# suitability of this software for any purpose. It is provided "as is"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews# without express or implied warranty.
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User#
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# Calling this script install-sh is preferred over install.sh, to prevent
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# `make' implicit rules from creating a file called install from it
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# when there is no Makefile.
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews#
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# This script is compatible with the BSD install script, but was written
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# from scratch. It can only install one file at a time, a restriction
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# shared with many OS's install programs.
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# set DOITPROG to echo to test this script
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews# Don't use :- since 4.3BSD and earlier shells don't like it.
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrewsdoit="${DOITPROG-}"
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews
95317501208f3bf5b159e6a40801b7069f68c486Mark Andrews
73cac2175470e9068829589476dda8bd6d88036fMark Andrews# put in absolute paths if you don't have them in your path; or use env. vars.
73cac2175470e9068829589476dda8bd6d88036fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsmvprog="${MVPROG-mv}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewscpprog="${CPPROG-cp}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewschmodprog="${CHMODPROG-chmod}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewschownprog="${CHOWNPROG-chown}"
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrewschgrpprog="${CHGRPPROG-chgrp}"
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrewsstripprog="${STRIPPROG-strip}"
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrewsrmprog="${RMPROG-rm}"
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrewsmkdirprog="${MKDIRPROG-mkdir}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
01163d188b89911c3a23fe1125a4cab6764a408cMark Andrewstransformbasename=""
01163d188b89911c3a23fe1125a4cab6764a408cMark Andrewstransform_arg=""
01163d188b89911c3a23fe1125a4cab6764a408cMark Andrewsinstcmd="$mvprog"
01163d188b89911c3a23fe1125a4cab6764a408cMark Andrewschmodcmd="$chmodprog 0755"
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrewschowncmd=""
648ba62b1f156cbca14d54d06c535385f1193d13Mark Andrewschgrpcmd=""
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsstripcmd=""
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsrmcmd="$rmprog -f"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsmvcmd="$mvprog"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewssrc=""
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsdst=""
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrewsdir_arg=""
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrewswhile [ x"$1" != x ]; do
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews case $1 in
35665db4e49e3e4c0e3776e635449f931f3732cfMark Andrews -c) instcmd="$cpprog"
c6313caa6cd9d011ac075048d2b62fd3410162dfMark Andrews shift
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews continue;;
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews -d) dir_arg=true
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews shift
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews continue;;
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews -m) chmodcmd="$chmodprog $2"
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews shift
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews shift
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews continue;;
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews -o) chowncmd="$chownprog $2"
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews shift
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews shift
bdfd62f497fe0d5281c25b61271595a4c821a040Mark Andrews continue;;
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews -g) chgrpcmd="$chgrpprog $2"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews shift
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews shift
64cde9d94a2f6c7050f00d9651df6b05e63d09f2Mark Andrews continue;;
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
cbb94d52f98b48e8c3a8866dbf8c67860764f349Mark Andrews -s) stripcmd="$stripprog"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews shift
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews continue;;
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews -t=*) transformarg=`echo $1 | sed 's/-t=//'`
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews shift
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews continue;;
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews shift
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews continue;;
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews *) if [ x"$src" = x ]
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews then
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews src=$1
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrews else
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews # this colon is to work around a 386BSD /bin/sh bug
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews :
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews dst=$1
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews fi
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews shift
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews continue;;
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews esac
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrewsdone
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrewsif [ x"$src" = x ]
11b07ea523314b123b4e503ce3813443a018c8d3Mark Andrewsthen
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews echo "install: no input file specified"
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews exit 1
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrewselse
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews true
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsfi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrewsif [ x"$dir_arg" != x ]; then
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews dst=$src
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews src=""
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ -d $dst ]; then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews instcmd=:
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews else
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews instcmd=mkdir
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews fi
61a03692ab84504fb2bd85b71facfe0f6456b466Mark Andrewselse
61a03692ab84504fb2bd85b71facfe0f6456b466Mark Andrews
61a03692ab84504fb2bd85b71facfe0f6456b466Mark Andrews# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
61a03692ab84504fb2bd85b71facfe0f6456b466Mark Andrews# might cause directories to be created, which would be especially bad
61a03692ab84504fb2bd85b71facfe0f6456b466Mark Andrews# if $src (and thus $dsttmp) contains '*'.
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews if [ -f $src -o -d $src ]
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews true
8f8634e66351e292925dde8ab6b0418a0141f86aMark Andrews else
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews echo "install: $src does not exist"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews exit 1
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews fi
61a03692ab84504fb2bd85b71facfe0f6456b466Mark Andrews
68843c99b695bf194b019d465f6d33e6297fd02aMark Andrews if [ x"$dst" = x ]
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews echo "install: no destination specified"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews exit 1
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews else
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews true
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews fi
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews# If destination is a directory, append the input filename; if your system
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews# does not like double slashes in filenames, you may need to add some logic
514f6f14245f42c96f3d5e90462ce2ebe0597d2eMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ -d $dst ]
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews dst="$dst"/`basename $src`
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews else
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews true
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews fi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsfi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews## this sed command emulates the dirname command
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews# Make sure that the destination directory exists.
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews# this part is taken from Noah Friedman's mkinstalldirs script
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews# Skip lots of stat calls in the usual case.
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrewsif [ ! -d "$dstdir" ]; then
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark AndrewsdefaultIFS='
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews'
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark AndrewsIFS="${IFS-${defaultIFS}}"
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark Andrews
d6dc0d4f584352d2e4305435599ae8c93776d9b4Mark AndrewsoIFS="${IFS}"
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews# Some sh's can't handle IFS=/ for some reason.
d9b4174233b951f25cd53a2787b9f14314258c2fMark AndrewsIFS='%'
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
d9b4174233b951f25cd53a2787b9f14314258c2fMark AndrewsIFS="${oIFS}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewspathcomp=''
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewswhile [ $# -ne 0 ] ; do
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews pathcomp="${pathcomp}${1}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews shift
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ ! -d "${pathcomp}" ] ;
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews $mkdirprog "${pathcomp}"
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews else
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews true
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews fi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews pathcomp="${pathcomp}/"
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrewsdone
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsfi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsif [ x"$dir_arg" != x ]
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewsthen
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews $doit $instcmd $dst &&
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrewselse
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews# If we're going to rename the final executable, determine the name now.
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ x"$transformarg" = x ]
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews dstfile=`basename $dst`
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews else
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews dstfile=`basename $dst $transformbasename |
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews sed $transformarg`$transformbasename
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews fi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews# don't allow the sed command to completely eliminate the filename
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews if [ x"$dstfile" = x ]
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews then
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews dstfile=`basename $dst`
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews else
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews true
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews fi
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews# Make a temp file name in the proper directory.
b08e3be5dbfba22719ae9c428bd6853ac6f09798Mark Andrews
f91671c7dc877a52adc06d0a7d0ed1c7f6391e6eMark Andrews dsttmp=$dstdir/#inst.$$#
f91671c7dc877a52adc06d0a7d0ed1c7f6391e6eMark Andrews
d9b4174233b951f25cd53a2787b9f14314258c2fMark Andrews# Move or copy the file name to the temp name
$doit $instcmd $src $dsttmp &&
trap "rm -f ${dsttmp}" 0 &&
# and set any options; do chmod last to preserve setuid bits
# If any of these fail, we abort the whole thing. If we want to
# ignore errors from any of these, just make sure not to ignore
# errors from the above "$doit $instcmd $src $dsttmp" command.
if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
# Now rename the file to the real destination.
$doit $rmcmd -f $dstdir/$dstfile &&
$doit $mvcmd $dsttmp $dstdir/$dstfile
fi &&
exit 0