198937Sdes#!/bin/sh
298937Sdes#
398937Sdes# install - install a program, script, or datafile
498937Sdes# This comes from X11R5 (mit/util/scripts/install.sh).
598937Sdes#
698937Sdes# Copyright 1991 by the Massachusetts Institute of Technology
798937Sdes#
898937Sdes# Permission to use, copy, modify, distribute, and sell this software and its
998937Sdes# documentation for any purpose is hereby granted without fee, provided that
1098937Sdes# the above copyright notice appear in all copies and that both that
1198937Sdes# copyright notice and this permission notice appear in supporting
1298937Sdes# documentation, and that the name of M.I.T. not be used in advertising or
1398937Sdes# publicity pertaining to distribution of the software without specific,
1498937Sdes# written prior permission.  M.I.T. makes no representations about the
1598937Sdes# suitability of this software for any purpose.  It is provided "as is"
1698937Sdes# without express or implied warranty.
1798937Sdes#
1898937Sdes# Calling this script install-sh is preferred over install.sh, to prevent
1998937Sdes# `make' implicit rules from creating a file called install from it
2098937Sdes# when there is no Makefile.
2198937Sdes#
2298937Sdes# This script is compatible with the BSD install script, but was written
2398937Sdes# from scratch.  It can only install one file at a time, a restriction
2498937Sdes# shared with many OS's install programs.
2598937Sdes
2698937Sdes
2798937Sdes# set DOITPROG to echo to test this script
2898937Sdes
2998937Sdes# Don't use :- since 4.3BSD and earlier shells don't like it.
3098937Sdesdoit="${DOITPROG-}"
3198937Sdes
3298937Sdes
3398937Sdes# put in absolute paths if you don't have them in your path; or use env. vars.
3498937Sdes
3598937Sdesmvprog="${MVPROG-mv}"
3698937Sdescpprog="${CPPROG-cp}"
3798937Sdeschmodprog="${CHMODPROG-chmod}"
3898937Sdeschownprog="${CHOWNPROG-chown}"
3998937Sdeschgrpprog="${CHGRPPROG-chgrp}"
4098937Sdesstripprog="${STRIPPROG-strip}"
4198937Sdesrmprog="${RMPROG-rm}"
4298937Sdesmkdirprog="${MKDIRPROG-mkdir}"
4398937Sdes
4498937Sdestransformbasename=""
4598937Sdestransform_arg=""
4698937Sdesinstcmd="$mvprog"
4798937Sdeschmodcmd="$chmodprog 0755"
4898937Sdeschowncmd=""
4998937Sdeschgrpcmd=""
5098937Sdesstripcmd=""
5198937Sdesrmcmd="$rmprog -f"
5298937Sdesmvcmd="$mvprog"
5398937Sdessrc=""
5498937Sdesdst=""
5598937Sdesdir_arg=""
5698937Sdes
5798937Sdeswhile [ x"$1" != x ]; do
5898937Sdes    case $1 in
5998937Sdes	-c) instcmd="$cpprog"
6098937Sdes	    shift
6198937Sdes	    continue;;
6298937Sdes
6398937Sdes	-d) dir_arg=true
6498937Sdes	    shift
6598937Sdes	    continue;;
6698937Sdes
6798937Sdes	-m) chmodcmd="$chmodprog $2"
6898937Sdes	    shift
6998937Sdes	    shift
7098937Sdes	    continue;;
7198937Sdes
7298937Sdes	-o) chowncmd="$chownprog $2"
7398937Sdes	    shift
7498937Sdes	    shift
7598937Sdes	    continue;;
7698937Sdes
7798937Sdes	-g) chgrpcmd="$chgrpprog $2"
7898937Sdes	    shift
7998937Sdes	    shift
8098937Sdes	    continue;;
8198937Sdes
8298937Sdes	-s) stripcmd="$stripprog"
8398937Sdes	    shift
8498937Sdes	    continue;;
8598937Sdes
8698937Sdes	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8798937Sdes	    shift
8898937Sdes	    continue;;
8998937Sdes
9098937Sdes	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9198937Sdes	    shift
9298937Sdes	    continue;;
9398937Sdes
9498937Sdes	*)  if [ x"$src" = x ]
9598937Sdes	    then
9698937Sdes		src=$1
9798937Sdes	    else
9898937Sdes		# this colon is to work around a 386BSD /bin/sh bug
9998937Sdes		:
10098937Sdes		dst=$1
10198937Sdes	    fi
10298937Sdes	    shift
10398937Sdes	    continue;;
10498937Sdes    esac
10598937Sdesdone
10698937Sdes
10798937Sdesif [ x"$src" = x ]
10898937Sdesthen
10998937Sdes	echo "install:	no input file specified"
11098937Sdes	exit 1
11198937Sdeselse
11298937Sdes	true
11398937Sdesfi
11498937Sdes
11598937Sdesif [ x"$dir_arg" != x ]; then
11698937Sdes	dst=$src
11798937Sdes	src=""
11898937Sdes	
11998937Sdes	if [ -d $dst ]; then
12098937Sdes		instcmd=:
12198937Sdes		chmodcmd=""
12298937Sdes	else
12398937Sdes		instcmd=mkdir
12498937Sdes	fi
12598937Sdeselse
12698937Sdes
12798937Sdes# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
128126274Sdes# might cause directories to be created, which would be especially bad
12998937Sdes# if $src (and thus $dsttmp) contains '*'.
13098937Sdes
13198937Sdes	if [ -f $src -o -d $src ]
13298937Sdes	then
13398937Sdes		true
13498937Sdes	else
13598937Sdes		echo "install:  $src does not exist"
13698937Sdes		exit 1
13798937Sdes	fi
13898937Sdes	
13998937Sdes	if [ x"$dst" = x ]
14098937Sdes	then
14198937Sdes		echo "install:	no destination specified"
14298937Sdes		exit 1
14398937Sdes	else
14498937Sdes		true
14598937Sdes	fi
14698937Sdes
14798937Sdes# If destination is a directory, append the input filename; if your system
14898937Sdes# does not like double slashes in filenames, you may need to add some logic
14998937Sdes
15098937Sdes	if [ -d $dst ]
15198937Sdes	then
15298937Sdes		dst="$dst"/`basename $src`
15398937Sdes	else
15498937Sdes		true
15598937Sdes	fi
15698937Sdesfi
15798937Sdes
15898937Sdes## this sed command emulates the dirname command
15998937Sdesdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
16098937Sdes
16198937Sdes# Make sure that the destination directory exists.
16298937Sdes#  this part is taken from Noah Friedman's mkinstalldirs script
16398937Sdes
16498937Sdes# Skip lots of stat calls in the usual case.
16598937Sdesif [ ! -d "$dstdir" ]; then
16698937SdesdefaultIFS='	
16798937Sdes'
16898937SdesIFS="${IFS-${defaultIFS}}"
16998937Sdes
17098937SdesoIFS="${IFS}"
17198937Sdes# Some sh's can't handle IFS=/ for some reason.
17298937SdesIFS='%'
17398937Sdesset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17498937SdesIFS="${oIFS}"
17598937Sdes
17698937Sdespathcomp=''
17798937Sdes
17898937Sdeswhile [ $# -ne 0 ] ; do
17998937Sdes	pathcomp="${pathcomp}${1}"
18098937Sdes	shift
18198937Sdes
18298937Sdes	if [ ! -d "${pathcomp}" ] ;
183126274Sdes	then
18498937Sdes		$mkdirprog "${pathcomp}"
18598937Sdes	else
18698937Sdes		true
18798937Sdes	fi
18898937Sdes
18998937Sdes	pathcomp="${pathcomp}/"
19098937Sdesdone
19198937Sdesfi
19298937Sdes
19398937Sdesif [ x"$dir_arg" != x ]
19498937Sdesthen
19598937Sdes	$doit $instcmd $dst &&
19698937Sdes
19798937Sdes	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19898937Sdes	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19998937Sdes	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
20098937Sdes	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
20198937Sdeselse
20298937Sdes
20398937Sdes# If we're going to rename the final executable, determine the name now.
20498937Sdes
205126274Sdes	if [ x"$transformarg" = x ]
20698937Sdes	then
20798937Sdes		dstfile=`basename $dst`
20898937Sdes	else
209126274Sdes		dstfile=`basename $dst $transformbasename |
21098937Sdes			sed $transformarg`$transformbasename
21198937Sdes	fi
21298937Sdes
21398937Sdes# don't allow the sed command to completely eliminate the filename
21498937Sdes
215126274Sdes	if [ x"$dstfile" = x ]
21698937Sdes	then
21798937Sdes		dstfile=`basename $dst`
21898937Sdes	else
21998937Sdes		true
22098937Sdes	fi
22198937Sdes
22298937Sdes# Make a temp file name in the proper directory.
22398937Sdes
22498937Sdes	dsttmp=$dstdir/#inst.$$#
22598937Sdes
22698937Sdes# Move or copy the file name to the temp name
22798937Sdes
22898937Sdes	$doit $instcmd $src $dsttmp &&
22998937Sdes
23098937Sdes	trap "rm -f ${dsttmp}" 0 &&
23198937Sdes
23298937Sdes# and set any options; do chmod last to preserve setuid bits
23398937Sdes
23498937Sdes# If any of these fail, we abort the whole thing.  If we want to
23598937Sdes# ignore errors from any of these, just make sure not to ignore
23698937Sdes# errors from the above "$doit $instcmd $src $dsttmp" command.
23798937Sdes
23898937Sdes	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23998937Sdes	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
24098937Sdes	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
24198937Sdes	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
24298937Sdes
24398937Sdes# Now rename the file to the real destination.
24498937Sdes
24598937Sdes	$doit $rmcmd -f $dstdir/$dstfile &&
246126274Sdes	$doit $mvcmd $dsttmp $dstdir/$dstfile
24798937Sdes
24898937Sdesfi &&
24998937Sdes
25098937Sdes
25198937Sdesexit 0
252