install-sh revision 139368
1139368Sobrien#!/bin/sh 268349Sobrien# 368349Sobrien# install - install a program, script, or datafile 4139368Sobrien# This comes from X11R5 (mit/util/scripts/install.sh). 568349Sobrien# 6139368Sobrien# Copyright 1991 by the Massachusetts Institute of Technology 7139368Sobrien# 8139368Sobrien# Permission to use, copy, modify, distribute, and sell this software and its 9139368Sobrien# documentation for any purpose is hereby granted without fee, provided that 10139368Sobrien# the above copyright notice appear in all copies and that both that 11139368Sobrien# copyright notice and this permission notice appear in supporting 12139368Sobrien# documentation, and that the name of M.I.T. not be used in advertising or 13139368Sobrien# publicity pertaining to distribution of the software without specific, 14139368Sobrien# written prior permission. M.I.T. makes no representations about the 15139368Sobrien# suitability of this software for any purpose. It is provided "as is" 16139368Sobrien# without express or implied warranty. 17139368Sobrien# 1868349Sobrien# Calling this script install-sh is preferred over install.sh, to prevent 1968349Sobrien# `make' implicit rules from creating a file called install from it 2068349Sobrien# when there is no Makefile. 2168349Sobrien# 2268349Sobrien# This script is compatible with the BSD install script, but was written 23139368Sobrien# from scratch. It can only install one file at a time, a restriction 24139368Sobrien# shared with many OS's install programs. 2568349Sobrien 2668349Sobrien 2768349Sobrien# set DOITPROG to echo to test this script 2868349Sobrien 2968349Sobrien# Don't use :- since 4.3BSD and earlier shells don't like it. 3068349Sobriendoit="${DOITPROG-}" 3168349Sobrien 3268349Sobrien 3368349Sobrien# put in absolute paths if you don't have them in your path; or use env. vars. 3468349Sobrien 3568349Sobrienmvprog="${MVPROG-mv}" 3668349Sobriencpprog="${CPPROG-cp}" 3768349Sobrienchmodprog="${CHMODPROG-chmod}" 3868349Sobrienchownprog="${CHOWNPROG-chown}" 3968349Sobrienchgrpprog="${CHGRPPROG-chgrp}" 4068349Sobrienstripprog="${STRIPPROG-strip}" 4168349Sobrienrmprog="${RMPROG-rm}" 4268349Sobrienmkdirprog="${MKDIRPROG-mkdir}" 4368349Sobrien 44139368Sobrientransformbasename="" 4568349Sobrientransform_arg="" 4668349Sobrieninstcmd="$mvprog" 4768349Sobrienchmodcmd="$chmodprog 0755" 4868349Sobrienchowncmd="" 4968349Sobrienchgrpcmd="" 5068349Sobrienstripcmd="" 5168349Sobrienrmcmd="$rmprog -f" 5268349Sobrienmvcmd="$mvprog" 5368349Sobriensrc="" 5468349Sobriendst="" 5568349Sobriendir_arg="" 5668349Sobrien 5768349Sobrienwhile [ x"$1" != x ]; do 5868349Sobrien case $1 in 5968349Sobrien -c) instcmd="$cpprog" 6068349Sobrien shift 6168349Sobrien continue;; 6268349Sobrien 6368349Sobrien -d) dir_arg=true 6468349Sobrien shift 6568349Sobrien continue;; 6668349Sobrien 6768349Sobrien -m) chmodcmd="$chmodprog $2" 6868349Sobrien shift 6968349Sobrien shift 7068349Sobrien continue;; 7168349Sobrien 7268349Sobrien -o) chowncmd="$chownprog $2" 7368349Sobrien shift 7468349Sobrien shift 7568349Sobrien continue;; 7668349Sobrien 7768349Sobrien -g) chgrpcmd="$chgrpprog $2" 7868349Sobrien shift 7968349Sobrien shift 8068349Sobrien continue;; 8168349Sobrien 8268349Sobrien -s) stripcmd="$stripprog" 8368349Sobrien shift 8468349Sobrien continue;; 8568349Sobrien 8668349Sobrien -t=*) transformarg=`echo $1 | sed 's/-t=//'` 8768349Sobrien shift 8868349Sobrien continue;; 8968349Sobrien 9068349Sobrien -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 9168349Sobrien shift 9268349Sobrien continue;; 9368349Sobrien 9468349Sobrien *) if [ x"$src" = x ] 9568349Sobrien then 9668349Sobrien src=$1 9768349Sobrien else 9868349Sobrien # this colon is to work around a 386BSD /bin/sh bug 9968349Sobrien : 10068349Sobrien dst=$1 10168349Sobrien fi 10268349Sobrien shift 10368349Sobrien continue;; 10468349Sobrien esac 10568349Sobriendone 10668349Sobrien 10768349Sobrienif [ x"$src" = x ] 10868349Sobrienthen 10968349Sobrien echo "install: no input file specified" 11068349Sobrien exit 1 11168349Sobrienelse 112139368Sobrien : 11368349Sobrienfi 11468349Sobrien 11568349Sobrienif [ x"$dir_arg" != x ]; then 11668349Sobrien dst=$src 11768349Sobrien src="" 11868349Sobrien 11968349Sobrien if [ -d $dst ]; then 12068349Sobrien instcmd=: 121139368Sobrien chmodcmd="" 12268349Sobrien else 123139368Sobrien instcmd=$mkdirprog 12468349Sobrien fi 12568349Sobrienelse 12668349Sobrien 12768349Sobrien# Waiting for this to be detected by the "$instcmd $src $dsttmp" command 12868349Sobrien# might cause directories to be created, which would be especially bad 12968349Sobrien# if $src (and thus $dsttmp) contains '*'. 13068349Sobrien 131139368Sobrien if [ -f "$src" ] || [ -d "$src" ] 13268349Sobrien then 133139368Sobrien : 13468349Sobrien else 13568349Sobrien echo "install: $src does not exist" 13668349Sobrien exit 1 13768349Sobrien fi 13868349Sobrien 13968349Sobrien if [ x"$dst" = x ] 14068349Sobrien then 14168349Sobrien echo "install: no destination specified" 14268349Sobrien exit 1 14368349Sobrien else 144139368Sobrien : 14568349Sobrien fi 14668349Sobrien 14768349Sobrien# If destination is a directory, append the input filename; if your system 14868349Sobrien# does not like double slashes in filenames, you may need to add some logic 14968349Sobrien 15068349Sobrien if [ -d $dst ] 15168349Sobrien then 15268349Sobrien dst="$dst"/`basename $src` 15368349Sobrien else 154139368Sobrien : 15568349Sobrien fi 15668349Sobrienfi 15768349Sobrien 15868349Sobrien## this sed command emulates the dirname command 15968349Sobriendstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 16068349Sobrien 16168349Sobrien# Make sure that the destination directory exists. 16268349Sobrien# this part is taken from Noah Friedman's mkinstalldirs script 16368349Sobrien 16468349Sobrien# Skip lots of stat calls in the usual case. 16568349Sobrienif [ ! -d "$dstdir" ]; then 166139368SobriendefaultIFS=' 167139368Sobrien ' 16868349SobrienIFS="${IFS-${defaultIFS}}" 16968349Sobrien 17068349SobrienoIFS="${IFS}" 17168349Sobrien# Some sh's can't handle IFS=/ for some reason. 17268349SobrienIFS='%' 17368349Sobrienset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` 17468349SobrienIFS="${oIFS}" 17568349Sobrien 17668349Sobrienpathcomp='' 17768349Sobrien 17868349Sobrienwhile [ $# -ne 0 ] ; do 17968349Sobrien pathcomp="${pathcomp}${1}" 18068349Sobrien shift 18168349Sobrien 18268349Sobrien if [ ! -d "${pathcomp}" ] ; 18368349Sobrien then 18468349Sobrien $mkdirprog "${pathcomp}" 18568349Sobrien else 186139368Sobrien : 18768349Sobrien fi 18868349Sobrien 18968349Sobrien pathcomp="${pathcomp}/" 19068349Sobriendone 19168349Sobrienfi 19268349Sobrien 19368349Sobrienif [ x"$dir_arg" != x ] 19468349Sobrienthen 19568349Sobrien $doit $instcmd $dst && 19668349Sobrien 197139368Sobrien if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else : ; fi && 198139368Sobrien if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else : ; fi && 199139368Sobrien if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else : ; fi && 200139368Sobrien if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else : ; fi 20168349Sobrienelse 20268349Sobrien 20368349Sobrien# If we're going to rename the final executable, determine the name now. 20468349Sobrien 20568349Sobrien if [ x"$transformarg" = x ] 20668349Sobrien then 20768349Sobrien dstfile=`basename $dst` 20868349Sobrien else 20968349Sobrien dstfile=`basename $dst $transformbasename | 21068349Sobrien sed $transformarg`$transformbasename 21168349Sobrien fi 21268349Sobrien 21368349Sobrien# don't allow the sed command to completely eliminate the filename 21468349Sobrien 21568349Sobrien if [ x"$dstfile" = x ] 21668349Sobrien then 21768349Sobrien dstfile=`basename $dst` 21868349Sobrien else 219139368Sobrien : 22068349Sobrien fi 22168349Sobrien 22268349Sobrien# Make a temp file name in the proper directory. 22368349Sobrien 22468349Sobrien dsttmp=$dstdir/#inst.$$# 22568349Sobrien 22668349Sobrien# Move or copy the file name to the temp name 22768349Sobrien 22868349Sobrien $doit $instcmd $src $dsttmp && 22968349Sobrien 23068349Sobrien trap "rm -f ${dsttmp}" 0 && 23168349Sobrien 23268349Sobrien# and set any options; do chmod last to preserve setuid bits 23368349Sobrien 23468349Sobrien# If any of these fail, we abort the whole thing. If we want to 23568349Sobrien# ignore errors from any of these, just make sure not to ignore 23668349Sobrien# errors from the above "$doit $instcmd $src $dsttmp" command. 23768349Sobrien 238139368Sobrien if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else :;fi && 239139368Sobrien if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else :;fi && 240139368Sobrien if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else :;fi && 241139368Sobrien if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else :;fi && 24268349Sobrien 24368349Sobrien# Now rename the file to the real destination. 24468349Sobrien 24568349Sobrien $doit $rmcmd -f $dstdir/$dstfile && 24668349Sobrien $doit $mvcmd $dsttmp $dstdir/$dstfile 24768349Sobrien 24868349Sobrienfi && 24968349Sobrien 25068349Sobrien 25168349Sobrienexit 0 252