1*01e196c8SJohn Marino: 2*01e196c8SJohn Marino# NAME: 3*01e196c8SJohn Marino# install.sh - portable version of install(1) 4*01e196c8SJohn Marino# 5*01e196c8SJohn Marino# SYNOPSIS: 6*01e196c8SJohn Marino# install [-CNcs] [-f flags] [-i errs] [-o owner] [-g group] [-m mode] file1 file2 ... 7*01e196c8SJohn Marino# install -d [-i errs] [-o owner] [-g group] [-m mode] directory ... 8*01e196c8SJohn Marino# 9*01e196c8SJohn Marino# DESCRIPTION: 10*01e196c8SJohn Marino# Compatible with BSD install(1). Except that '-c' is always 11*01e196c8SJohn Marino# true and we always move an already installed target aside as 12*01e196c8SJohn Marino# this is important on many systems. Recent BSD install(1) 13*01e196c8SJohn Marino# versions have a '-b' option for this. 14*01e196c8SJohn Marino# 15*01e196c8SJohn Marino# 16*01e196c8SJohn Marino# OPTIONS: 17*01e196c8SJohn Marino# -b move previous target file aside (always true). 18*01e196c8SJohn Marino# 19*01e196c8SJohn Marino# -B "suffix" 20*01e196c8SJohn Marino# use "suffix" instead of .old for saving existing target. 21*01e196c8SJohn Marino# 22*01e196c8SJohn Marino# -c copy rather than move the file into place (always true). 23*01e196c8SJohn Marino# 24*01e196c8SJohn Marino# -C compare. Only install if target is missing or 25*01e196c8SJohn Marino# different. 26*01e196c8SJohn Marino# 27*01e196c8SJohn Marino# -N newer. Only install if target is missing or older. 28*01e196c8SJohn Marino# 29*01e196c8SJohn Marino# -s strip target 30*01e196c8SJohn Marino# 31*01e196c8SJohn Marino# -o "owner" 32*01e196c8SJohn Marino# make target owned by "owner" 33*01e196c8SJohn Marino# 34*01e196c8SJohn Marino# -g "group" 35*01e196c8SJohn Marino# make target group owned by "group" 36*01e196c8SJohn Marino# 37*01e196c8SJohn Marino# -m "mode" 38*01e196c8SJohn Marino# set permissions to "mode" 39*01e196c8SJohn Marino# 40*01e196c8SJohn Marino# -f "flags" 41*01e196c8SJohn Marino# Pass "flags" onto chflags(1) 42*01e196c8SJohn Marino# 43*01e196c8SJohn Marino# -i "errs" 44*01e196c8SJohn Marino# Ignore errors from steps indicated by "errs" (``s,o,g,m''). 45*01e196c8SJohn Marino# 46*01e196c8SJohn Marino# BUGS: 47*01e196c8SJohn Marino# The '-i' option is to save your sanity when 'bsd.prog.mk' 48*01e196c8SJohn Marino# insists on haveing a '-o' "owner" option which is doomed to 49*01e196c8SJohn Marino# fail on many systems. We ignore '-b', '-B' and '-c' options. 50*01e196c8SJohn Marino# 51*01e196c8SJohn Marino# AUTHOR: 52*01e196c8SJohn Marino# Simon J. Gerraty <sjg@quick.com.au> 53*01e196c8SJohn Marino# 54*01e196c8SJohn Marino 55*01e196c8SJohn Marino# RCSid: 56*01e196c8SJohn Marino# $Id: install-sh,v 1.18 2001/03/16 17:33:02 sjg Exp $ 57*01e196c8SJohn Marino# 58*01e196c8SJohn Marino# @(#) Copyright (c) 1993 Simon J. Gerraty 59*01e196c8SJohn Marino# 60*01e196c8SJohn Marino# This file is provided in the hope that it will 61*01e196c8SJohn Marino# be of use. There is absolutely NO WARRANTY. 62*01e196c8SJohn Marino# Permission to copy, redistribute or otherwise 63*01e196c8SJohn Marino# use this file is hereby granted provided that 64*01e196c8SJohn Marino# the above copyright notice and this notice are 65*01e196c8SJohn Marino# left intact. 66*01e196c8SJohn Marino# 67*01e196c8SJohn Marino# Please send copies of changes and bug-fixes to: 68*01e196c8SJohn Marino# sjg@quick.com.au 69*01e196c8SJohn Marino# 70*01e196c8SJohn Marino 71*01e196c8SJohn Marinoset -- `getopt B:bpxCNcsdo:g:m:i:f: $*` 72*01e196c8SJohn Marino 73*01e196c8SJohn MarinoMydir=`dirname $0` 74*01e196c8SJohn Marino[ -s $Mydir/.installrc ] && . $Mydir/.installrc 75*01e196c8SJohn Marino 76*01e196c8SJohn Marinoowner=: 77*01e196c8SJohn Marinogroup=: 78*01e196c8SJohn Marinomode=: 79*01e196c8SJohn Marinostrip=: 80*01e196c8SJohn Marinomkdirs= 81*01e196c8SJohn Marinocompare=: 82*01e196c8SJohn Marinonewer=: 83*01e196c8SJohn Marinochflags=: 84*01e196c8SJohn MarinoLS1= 85*01e196c8SJohn MarinoCP_P= 86*01e196c8SJohn Marino 87*01e196c8SJohn Marinowhile [ $# -gt 1 ] 88*01e196c8SJohn Marinodo 89*01e196c8SJohn Marino case $1 in 90*01e196c8SJohn Marino --) shift; break;; 91*01e196c8SJohn Marino -p) CP_P=-p;; 92*01e196c8SJohn Marino -x) set -x;; 93*01e196c8SJohn Marino -B) OLD_EXT=$2; shift;; 94*01e196c8SJohn Marino -C) compare=Different;; 95*01e196c8SJohn Marino -N) newer=Newer; 96*01e196c8SJohn Marino # check if /bin/ls supports -1 97*01e196c8SJohn Marino /bin/ls -1 $0 >/dev/null 2>&1 && LS1=1 98*01e196c8SJohn Marino ;; 99*01e196c8SJohn Marino -o) owner="${CHOWN:-chown} $2 "; shift;; 100*01e196c8SJohn Marino -g) group="${CHGRP:-chgrp} $2 "; shift;; 101*01e196c8SJohn Marino -m) mode="${CHMOD:-chmod} $2 "; shift;; 102*01e196c8SJohn Marino -s) strip=${STRIP:-strip};; 103*01e196c8SJohn Marino -d) mkdirs="mkdir -p";; 104*01e196c8SJohn Marino -i) ignore_err="$ignore_err$2"; shift;; 105*01e196c8SJohn Marino -f) chflags="${CHFLAGS:-chflags} $2 "; shift;; 106*01e196c8SJohn Marino esac 107*01e196c8SJohn Marino shift 108*01e196c8SJohn Marinodone 109*01e196c8SJohn Marino 110*01e196c8SJohn MarinoNewer() { 111*01e196c8SJohn Marino n=`/bin/ls -t$LS1 $* 2>/dev/null | head -1` 112*01e196c8SJohn Marino [ $1 = $n ] 113*01e196c8SJohn Marino} 114*01e196c8SJohn Marino 115*01e196c8SJohn MarinoDifferent() { 116*01e196c8SJohn Marino cmp -s $* 117*01e196c8SJohn Marino [ $? != 0 ] 118*01e196c8SJohn Marino} 119*01e196c8SJohn Marino 120*01e196c8SJohn MarinoErr() { 121*01e196c8SJohn Marino case "$ignore_err" in 122*01e196c8SJohn Marino *$1*) ;; 123*01e196c8SJohn Marino *) exit 1;; 124*01e196c8SJohn Marino esac 125*01e196c8SJohn Marino} 126*01e196c8SJohn Marino 127*01e196c8SJohn MarinoSetem() { 128*01e196c8SJohn Marino # the order is important 129*01e196c8SJohn Marino if [ ! -d $1 ]; then 130*01e196c8SJohn Marino $strip $1 || Err s 131*01e196c8SJohn Marino fi 132*01e196c8SJohn Marino $group $1 || Err g 133*01e196c8SJohn Marino $owner $1 || Err o 134*01e196c8SJohn Marino $mode $1 || Err m 135*01e196c8SJohn Marino $chflags $1 || Err f 136*01e196c8SJohn Marino return 0 137*01e196c8SJohn Marino} 138*01e196c8SJohn Marino 139*01e196c8SJohn Marino# a bug in HP-UX's /bin/sh, means we need to re-set $* 140*01e196c8SJohn Marino# after any calls to add_path() 141*01e196c8SJohn Marinoargs="$*" 142*01e196c8SJohn Marino 143*01e196c8SJohn Marino# all this just for chown! 144*01e196c8SJohn Marinoadd_path () { [ -d $1 ] && eval ${2:-PATH}="\$${2:-PATH}:$1"; } 145*01e196c8SJohn Marinoadd_path /etc 146*01e196c8SJohn Marinoadd_path /usr/etc 147*01e196c8SJohn Marinoadd_path /sbin 148*01e196c8SJohn Marinoadd_path /usr/sbin 149*01e196c8SJohn Marino 150*01e196c8SJohn Marino# restore saved $* 151*01e196c8SJohn Marinoset -- $args 152*01e196c8SJohn Marino 153*01e196c8SJohn Marino# make directories if needed 154*01e196c8SJohn Marino# and ensure mode etc are as desired 155*01e196c8SJohn Marinoif [ "$mkdirs" ]; then 156*01e196c8SJohn Marino for d in $* 157*01e196c8SJohn Marino do 158*01e196c8SJohn Marino [ ! -d $d ] && $mkdirs $d 159*01e196c8SJohn Marino Setem $d 160*01e196c8SJohn Marino done 161*01e196c8SJohn Marino exit 0 # that's all we do 162*01e196c8SJohn Marinofi 163*01e196c8SJohn Marino 164*01e196c8SJohn Marino# install files 165*01e196c8SJohn Marinoif [ $# -gt 2 ]; then 166*01e196c8SJohn Marino dest_dir=yes 167*01e196c8SJohn Marinoelif [ $# -eq 1 ]; then 168*01e196c8SJohn Marino echo "what should I do with $*?" >&2 169*01e196c8SJohn Marino exit 1 170*01e196c8SJohn Marinofi 171*01e196c8SJohn Marino 172*01e196c8SJohn Marino# get list of files 173*01e196c8SJohn Marinowhile [ $# -gt 1 ] 174*01e196c8SJohn Marinodo 175*01e196c8SJohn Marino files="$files $1" 176*01e196c8SJohn Marino shift 177*01e196c8SJohn Marinodone 178*01e196c8SJohn Marino# last one is dest 179*01e196c8SJohn Marinodest=$1 180*01e196c8SJohn Marinoshift 181*01e196c8SJohn Marino 182*01e196c8SJohn Marino 183*01e196c8SJohn Marinoif [ "$dest_dir" = yes -a ! -d $dest ]; then 184*01e196c8SJohn Marino echo "no directory $dest" >&2 185*01e196c8SJohn Marino exit 1 186*01e196c8SJohn Marinofi 187*01e196c8SJohn Marino 188*01e196c8SJohn Marinofor f in $files 189*01e196c8SJohn Marinodo 190*01e196c8SJohn Marino b=`basename $f` 191*01e196c8SJohn Marino if [ -d $dest ]; then 192*01e196c8SJohn Marino t=$dest/$b 193*01e196c8SJohn Marino else 194*01e196c8SJohn Marino t=$dest 195*01e196c8SJohn Marino fi 196*01e196c8SJohn Marino $newer $f $t || continue 197*01e196c8SJohn Marino $compare $f $t || continue 198*01e196c8SJohn Marino [ -f $t ] && { mv -f $t $t.old || exit 1; } 199*01e196c8SJohn Marino { cp $CP_P $f $t && Setem $t; } || exit 1 200*01e196c8SJohn Marinodone 201*01e196c8SJohn Marinoexit 0 202