1139368Sobrien#!/bin/sh 268349Sobrien# 3268515Sdelphij# $NetBSD: install-sh.in,v 1.6 2012/01/11 13:07:31 hans Exp $ 4234449Sobrien# This script now also installs multiple files, but might choke on installing 5234449Sobrien# multiple files with spaces in the file names. 6139368Sobrien# 7234449Sobrien# install - install a program, script, or datafile 8234449Sobrien# This comes from X11R5 (mit/util/scripts/install.sh). 9139368Sobrien# 10234449Sobrien# Copyright 1991 by the Massachusetts Institute of Technology 11169942Sobrien# 12234449Sobrien# Permission to use, copy, modify, distribute, and sell this software and its 13234449Sobrien# documentation for any purpose is hereby granted without fee, provided that 14234449Sobrien# the above copyright notice appear in all copies and that both that 15234449Sobrien# copyright notice and this permission notice appear in supporting 16234449Sobrien# documentation, and that the name of M.I.T. not be used in advertising or 17234449Sobrien# publicity pertaining to distribution of the software without specific, 18234449Sobrien# written prior permission. M.I.T. makes no representations about the 19234449Sobrien# suitability of this software for any purpose. It is provided "as is" 20234449Sobrien# without express or implied warranty. 21169942Sobrien# 2268349Sobrien# Calling this script install-sh is preferred over install.sh, to prevent 2368349Sobrien# `make' implicit rules from creating a file called install from it 2468349Sobrien# when there is no Makefile. 2568349Sobrien# 2668349Sobrien# This script is compatible with the BSD install script, but was written 27175296Sobrien# from scratch. 2868349Sobrien 2968349Sobrien# set DOITPROG to echo to test this script 3068349Sobrien 3168349Sobrien# Don't use :- since 4.3BSD and earlier shells don't like it. 3268349Sobriendoit="${DOITPROG-}" 3368349Sobrien 3468349Sobrien 35234449Sobrien# put in absolute paths if you don't have them in your path; or use env. vars. 36234449Sobrien 37234449Sobrienawkprog="${AWKPROG-awk}" 3868349Sobrienmvprog="${MVPROG-mv}" 3968349Sobriencpprog="${CPPROG-cp}" 4068349Sobrienchmodprog="${CHMODPROG-chmod}" 4168349Sobrienchownprog="${CHOWNPROG-chown}" 4268349Sobrienchgrpprog="${CHGRPPROG-chgrp}" 4368349Sobrienstripprog="${STRIPPROG-strip}" 4468349Sobrienrmprog="${RMPROG-rm}" 4568349Sobrienmkdirprog="${MKDIRPROG-mkdir}" 4668349Sobrien 47234449Sobrieninstcmd="$cpprog" 48268515Sdelphijinstflags="" 49234449Sobrienpathcompchmodcmd="$chmodprog 755" 50234449Sobrienchmodcmd="$chmodprog 755" 51234449Sobrienchowncmd="" 52234449Sobrienchgrpcmd="" 53234449Sobrienstripcmd="" 54234449Sobrienstripflags="" 5568349Sobrienrmcmd="$rmprog -f" 5668349Sobrienmvcmd="$mvprog" 57234449Sobriensrc="" 58234449Sobrienmsrc="" 59234449Sobriendst="" 60234449Sobriendir_arg="" 61234449Sobriensuffix="" 62234449Sobriensuffixfmt="" 6368349Sobrien 64234449Sobrienwhile [ x"$1" != x ]; do 65234449Sobrien case $1 in 66234449Sobrien -b) suffix=".old" 67234449Sobrien shift 68234449Sobrien continue;; 6968349Sobrien 70234449Sobrien -B) suffixfmt="$2" 71234449Sobrien shift 72234449Sobrien shift 73234449Sobrien continue;; 7468349Sobrien 75234449Sobrien -c) instcmd="$cpprog" 76234449Sobrien shift 77234449Sobrien continue;; 7868349Sobrien 79234449Sobrien -d) dir_arg=true 80234449Sobrien shift 81234449Sobrien continue;; 8268349Sobrien 83234449Sobrien -m) chmodcmd="$chmodprog $2" 84234449Sobrien shift 85234449Sobrien shift 86234449Sobrien continue;; 8768349Sobrien 88268515Sdelphij -m*) 89268515Sdelphij chmodcmd="$chmodprog ${1#-m}" 90268515Sdelphij shift 91268515Sdelphij continue;; 92268515Sdelphij 93234449Sobrien -o) chowncmd="$chownprog $2" 94234449Sobrien shift 95234449Sobrien shift 96234449Sobrien continue;; 9768349Sobrien 98234449Sobrien -g) chgrpcmd="$chgrpprog $2" 99234449Sobrien shift 100234449Sobrien shift 101234449Sobrien continue;; 10268349Sobrien 103234449Sobrien -s) stripcmd="$stripprog" 104234449Sobrien shift 105234449Sobrien continue;; 10668349Sobrien 107234449Sobrien -S) stripcmd="$stripprog" 108234449Sobrien stripflags="-S $2 $stripflags" 109234449Sobrien shift 110234449Sobrien shift 111234449Sobrien continue;; 11268349Sobrien 113268515Sdelphij -p) instflags="-p" 114268515Sdelphij shift 115268515Sdelphij continue;; 116268515Sdelphij 117234449Sobrien *) if [ x"$msrc" = x ] 118234449Sobrien then 119234449Sobrien msrc="$dst" 120234449Sobrien else 121234449Sobrien msrc="$msrc $dst" 122234449Sobrien fi 123234449Sobrien src="$dst" 124234449Sobrien dst="$1" 125234449Sobrien shift 126234449Sobrien continue;; 127234449Sobrien esac 128169942Sobriendone 129169942Sobrien 130234449Sobrienif [ x"$dir_arg" = x ] 131234449Sobrienthen 132234449Sobrien dstisfile="" 133234449Sobrien if [ ! -d "$dst" ] 134234449Sobrien then 135234449Sobrien if [ x"$msrc" = x"$src" ] 136234449Sobrien then 137234449Sobrien dstisfile=true 138234449Sobrien else 139234449Sobrien echo "install: destination is not a directory" 140234449Sobrien exit 1 141234449Sobrien fi 142234449Sobrien fi 143234449Sobrienelse 144234449Sobrien msrc="$msrc $dst" 145175296Sobrienfi 146175296Sobrien 147234449Sobrienif [ x"$msrc" = x ] 148234449Sobrienthen 149234449Sobrien echo "install: no destination specified" 150234449Sobrien exit 1 151234449Sobrienfi 15268349Sobrien 153234449Sobrienfor srcarg in $msrc; do 154175296Sobrien 155234449Sobrienif [ x"$dir_arg" != x ]; then 156175296Sobrien 157234449Sobrien dstarg="$srcarg" 158234449Sobrienelse 159234449Sobrien dstarg="$dst" 160175296Sobrien 161234449Sobrien# Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command 162234449Sobrien# might cause directories to be created, which would be especially bad 163234449Sobrien# if $src (and thus $dsttmp) contains '*'. 16468349Sobrien 165234449Sobrien if [ -f "$srcarg" ] 166234449Sobrien then 167268515Sdelphij doinst="$instcmd $instflags" 168234449Sobrien elif [ -d "$srcarg" ] 169234449Sobrien then 170234449Sobrien echo "install: $srcarg: not a regular file" 171234449Sobrien exit 1 172234449Sobrien elif [ "$srcarg" = "/dev/null" ] 173234449Sobrien then 174234449Sobrien doinst="$cpprog" 175234449Sobrien else 176234449Sobrien echo "install: $srcarg does not exist" 177234449Sobrien exit 1 178234449Sobrien fi 179234449Sobrien 180234449Sobrien# If destination is a directory, append the input filename; if your system 181234449Sobrien# does not like double slashes in filenames, you may need to add some logic 18268349Sobrien 183234449Sobrien if [ -d "$dstarg" ] 184234449Sobrien then 185234449Sobrien dstarg="$dstarg"/`basename "$srcarg"` 186234449Sobrien fi 187234449Sobrienfi 18868349Sobrien 189234449Sobrien## this sed command emulates the dirname command 190234449Sobriendstdir=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 19168349Sobrien 192234449Sobrien# Make sure that the destination directory exists. 193234449Sobrien# this part is taken from Noah Friedman's mkinstalldirs script 19468349Sobrien 195234449Sobrien# Skip lots of stat calls in the usual case. 196234449Sobrienif [ ! -d "$dstdir" ]; then 197234449SobriendefaultIFS=' 198234449Sobrien' 199234449SobrienIFS="${IFS-${defaultIFS}}" 200175296Sobrien 201234449SobrienoIFS="${IFS}" 202234449Sobrien# Some sh's can't handle IFS=/ for some reason. 203234449SobrienIFS='%' 204234449Sobrienset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` 205234449SobrienIFS="${oIFS}" 20668349Sobrien 207234449Sobrienpathcomp='' 20868349Sobrien 209234449Sobrienwhile [ $# -ne 0 ] ; do 210234449Sobrien pathcomp="${pathcomp}${1}" 211234449Sobrien shift 21268349Sobrien 213234449Sobrien if [ ! -d "${pathcomp}" ] ; 214234449Sobrien then 215234449Sobrien $doit $mkdirprog "${pathcomp}" 216234449Sobrien if [ x"$chowncmd" != x ]; then $doit $chowncmd "${pathcomp}"; else true ; fi && 217234449Sobrien if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "${pathcomp}"; else true ; fi && 218234449Sobrien if [ x"$pathcompchmodcmd" != x ]; then $doit $pathcompchmodcmd "${pathcomp}"; else true ; fi 21968349Sobrien 220175296Sobrien else 221234449Sobrien true 222175296Sobrien fi 22368349Sobrien 224234449Sobrien pathcomp="${pathcomp}/" 225234449Sobriendone 226234449Sobrienfi 22768349Sobrien 228234449Sobrien if [ x"$dir_arg" != x ] 229234449Sobrien then 230234449Sobrien if [ -d "$dstarg" ]; then 231234449Sobrien true 232234449Sobrien else 233234449Sobrien $doit $mkdirprog "$dstarg" && 234175296Sobrien 235234449Sobrien if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dstarg"; else true ; fi && 236234449Sobrien if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dstarg"; else true ; fi && 237234449Sobrien if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dstarg"; else true ; fi 238234449Sobrien fi 239234449Sobrien else 240175296Sobrien 241234449Sobrien if [ x"$dstisfile" = x ] 242234449Sobrien then 243234449Sobrien file=$srcarg 244234449Sobrien else 245234449Sobrien file=$dst 246234449Sobrien fi 247175296Sobrien 248234449Sobrien dstfile=`basename "$file"` 249234449Sobrien dstfinal="$dstdir/$dstfile" 250175296Sobrien 251234449Sobrien# Make a temp file name in the proper directory. 252175296Sobrien 253234449Sobrien dsttmp=$dstdir/#inst.$$# 254175296Sobrien 255234449Sobrien# Make a backup file name in the proper directory. 256234449Sobrien case x$suffixfmt in 257234449Sobrien *%*) suffix=`echo x | 258234449Sobrien $awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" ' 259234449Sobrien { cnt = 0; 260234449Sobrien do { 261234449Sobrien sfx = sprintf(fmt, cnt++); 262234449Sobrien name = bname sfx; 263234449Sobrien } while (system("test -f " name) == 0); 264234449Sobrien print sfx; }' -`;; 265234449Sobrien x) ;; 266234449Sobrien *) suffix="$suffixfmt";; 267234449Sobrien esac 268234449Sobrien dstbackup="$dstfinal$suffix" 269175296Sobrien 270234449Sobrien# Move or copy the file name to the temp name 271175296Sobrien 272234449Sobrien $doit $doinst $srcarg "$dsttmp" && 273175296Sobrien 274234449Sobrien trap "rm -f ${dsttmp}" 0 && 27568349Sobrien 276234449Sobrien# and set any options; do chmod last to preserve setuid bits 27768349Sobrien 278234449Sobrien# If any of these fail, we abort the whole thing. If we want to 279234449Sobrien# ignore errors from any of these, just make sure not to ignore 280234449Sobrien# errors from the above "$doit $instcmd $src $dsttmp" command. 28168349Sobrien 282234449Sobrien if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else true;fi && 283234449Sobrien if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else true;fi && 284234449Sobrien if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripflags "$dsttmp"; else true;fi && 285234449Sobrien if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else true;fi && 28668349Sobrien 287234449Sobrien# Now rename the file to the real destination. 28868349Sobrien 289234449Sobrien if [ x"$suffix" != x ] && [ -f "$dstfinal" ] 290234449Sobrien then 291234449Sobrien $doit $mvcmd "$dstfinal" "$dstbackup" 292234449Sobrien else 293234449Sobrien $doit $rmcmd -f "$dstfinal" 294234449Sobrien fi && 295234449Sobrien $doit $mvcmd "$dsttmp" "$dstfinal" 296234449Sobrien fi 29768349Sobrien 298234449Sobriendone && 29968349Sobrien 30068349Sobrien 301234449Sobrienexit 0 302