Annotation of tme/install-sh, revision 1.1.1.2

1.1       root        1: #!/bin/sh
                      2: #
1.1.1.2 ! root        3: # $NetBSD: install-sh.in,v 1.4 2007/07/12 18:32:50 jlam Exp $
        !             4: # This script now also installs multiple files, but might choke on installing
        !             5: # multiple files with spaces in the file names.
        !             6: #
1.1       root        7: # install - install a program, script, or datafile
                      8: # This comes from X11R5 (mit/util/scripts/install.sh).
                      9: #
                     10: # Copyright 1991 by the Massachusetts Institute of Technology
                     11: #
                     12: # Permission to use, copy, modify, distribute, and sell this software and its
                     13: # documentation for any purpose is hereby granted without fee, provided that
                     14: # the above copyright notice appear in all copies and that both that
                     15: # copyright notice and this permission notice appear in supporting
                     16: # documentation, and that the name of M.I.T. not be used in advertising or
                     17: # publicity pertaining to distribution of the software without specific,
                     18: # written prior permission.  M.I.T. makes no representations about the
                     19: # suitability of this software for any purpose.  It is provided "as is"
                     20: # without express or implied warranty.
                     21: #
                     22: # Calling this script install-sh is preferred over install.sh, to prevent
                     23: # `make' implicit rules from creating a file called install from it
                     24: # when there is no Makefile.
                     25: #
                     26: # This script is compatible with the BSD install script, but was written
1.1.1.2 ! root       27: # from scratch.
1.1       root       28: 
                     29: # set DOITPROG to echo to test this script
                     30: 
                     31: # Don't use :- since 4.3BSD and earlier shells don't like it.
                     32: doit="${DOITPROG-}"
                     33: 
                     34: 
                     35: # put in absolute paths if you don't have them in your path; or use env. vars.
                     36: 
1.1.1.2 ! root       37: awkprog="${AWKPROG-awk}"
1.1       root       38: mvprog="${MVPROG-mv}"
                     39: cpprog="${CPPROG-cp}"
                     40: chmodprog="${CHMODPROG-chmod}"
                     41: chownprog="${CHOWNPROG-chown}"
                     42: chgrpprog="${CHGRPPROG-chgrp}"
                     43: stripprog="${STRIPPROG-strip}"
                     44: rmprog="${RMPROG-rm}"
                     45: mkdirprog="${MKDIRPROG-mkdir}"
                     46: 
                     47: instcmd="$mvprog"
1.1.1.2 ! root       48: pathcompchmodcmd="$chmodprog 755"
        !            49: chmodcmd="$chmodprog 755"
1.1       root       50: chowncmd=""
                     51: chgrpcmd=""
                     52: stripcmd=""
1.1.1.2 ! root       53: stripflags=""
1.1       root       54: rmcmd="$rmprog -f"
                     55: mvcmd="$mvprog"
                     56: src=""
1.1.1.2 ! root       57: msrc=""
1.1       root       58: dst=""
                     59: dir_arg=""
1.1.1.2 ! root       60: suffix=""
        !            61: suffixfmt=""
1.1       root       62: 
                     63: while [ x"$1" != x ]; do
                     64:     case $1 in
1.1.1.2 ! root       65:        -b) suffix=".old"
        !            66:            shift
        !            67:            continue;;
        !            68: 
        !            69:        -B) suffixfmt="$2"
        !            70:            shift
        !            71:            shift
        !            72:            continue;;
        !            73: 
1.1       root       74:        -c) instcmd="$cpprog"
                     75:            shift
                     76:            continue;;
                     77: 
                     78:        -d) dir_arg=true
                     79:            shift
                     80:            continue;;
                     81: 
                     82:        -m) chmodcmd="$chmodprog $2"
                     83:            shift
                     84:            shift
                     85:            continue;;
                     86: 
                     87:        -o) chowncmd="$chownprog $2"
                     88:            shift
                     89:            shift
                     90:            continue;;
                     91: 
                     92:        -g) chgrpcmd="$chgrpprog $2"
                     93:            shift
                     94:            shift
                     95:            continue;;
                     96: 
                     97:        -s) stripcmd="$stripprog"
                     98:            shift
                     99:            continue;;
                    100: 
1.1.1.2 ! root      101:        -S) stripcmd="$stripprog"
        !           102:            stripflags="-S $2 $stripflags"
1.1       root      103:            shift
                    104:            shift
                    105:            continue;;
                    106: 
1.1.1.2 ! root      107:        *)  if [ x"$msrc" = x ]
1.1       root      108:            then
1.1.1.2 ! root      109:                msrc="$dst"
1.1       root      110:            else
1.1.1.2 ! root      111:                msrc="$msrc $dst"
1.1       root      112:            fi
1.1.1.2 ! root      113:            src="$dst"
        !           114:            dst="$1"
1.1       root      115:            shift
                    116:            continue;;
                    117:     esac
                    118: done
                    119: 
1.1.1.2 ! root      120: if [ x"$dir_arg" = x ]
1.1       root      121: then
1.1.1.2 ! root      122:        dstisfile=""
        !           123:        if [ ! -d "$dst" ]
        !           124:        then
        !           125:                if [ x"$msrc" = x"$src" ]
        !           126:                then
        !           127:                        dstisfile=true
        !           128:                else
        !           129:                        echo "install: destination is not a directory"
        !           130:                        exit 1
        !           131:                fi
        !           132:        fi
1.1       root      133: else
1.1.1.2 ! root      134:        msrc="$msrc $dst"
1.1       root      135: fi
                    136: 
1.1.1.2 ! root      137: if [ x"$msrc" = x ]
        !           138: then
        !           139:        echo "install: no destination specified"
        !           140:        exit 1
        !           141: fi      
        !           142: 
        !           143: for srcarg in $msrc; do
        !           144: 
1.1       root      145: if [ x"$dir_arg" != x ]; then
1.1.1.2 ! root      146: 
        !           147:        dstarg="$srcarg"
1.1       root      148: else
1.1.1.2 ! root      149:        dstarg="$dst"
1.1       root      150: 
1.1.1.2 ! root      151: # Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
1.1       root      152: # might cause directories to be created, which would be especially bad 
                    153: # if $src (and thus $dsttmp) contains '*'.
                    154: 
1.1.1.2 ! root      155:        if [ -f "$srcarg" ]
1.1       root      156:        then
1.1.1.2 ! root      157:                doinst="$instcmd"
        !           158:        elif [ -d "$srcarg" ]
1.1       root      159:        then
1.1.1.2 ! root      160:                echo "install: $srcarg: not a regular file"
1.1       root      161:                exit 1
1.1.1.2 ! root      162:        elif [ "$srcarg" = "/dev/null" ]
        !           163:        then
        !           164:                doinst="$cpprog"
1.1       root      165:        else
1.1.1.2 ! root      166:                echo "install:  $srcarg does not exist"
        !           167:                exit 1
1.1       root      168:        fi
1.1.1.2 ! root      169:        
1.1       root      170: # If destination is a directory, append the input filename; if your system
                    171: # does not like double slashes in filenames, you may need to add some logic
                    172: 
1.1.1.2 ! root      173:        if [ -d "$dstarg" ]
1.1       root      174:        then
1.1.1.2 ! root      175:                dstarg="$dstarg"/`basename "$srcarg"`
1.1       root      176:        fi
                    177: fi
                    178: 
                    179: ## this sed command emulates the dirname command
1.1.1.2 ! root      180: dstdir=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
1.1       root      181: 
                    182: # Make sure that the destination directory exists.
                    183: #  this part is taken from Noah Friedman's mkinstalldirs script
                    184: 
                    185: # Skip lots of stat calls in the usual case.
                    186: if [ ! -d "$dstdir" ]; then
1.1.1.2 ! root      187: defaultIFS='   
        !           188: '
1.1       root      189: IFS="${IFS-${defaultIFS}}"
                    190: 
                    191: oIFS="${IFS}"
                    192: # Some sh's can't handle IFS=/ for some reason.
                    193: IFS='%'
                    194: set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
                    195: IFS="${oIFS}"
                    196: 
                    197: pathcomp=''
                    198: 
                    199: while [ $# -ne 0 ] ; do
                    200:        pathcomp="${pathcomp}${1}"
                    201:        shift
                    202: 
                    203:        if [ ! -d "${pathcomp}" ] ;
                    204:         then
1.1.1.2 ! root      205:                $doit $mkdirprog "${pathcomp}"
        !           206:                if [ x"$chowncmd" != x ]; then $doit $chowncmd "${pathcomp}"; else true ; fi &&
        !           207:                if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "${pathcomp}"; else true ; fi &&
        !           208:                if [ x"$pathcompchmodcmd" != x ]; then $doit $pathcompchmodcmd "${pathcomp}"; else true ; fi
        !           209: 
1.1       root      210:        else
1.1.1.2 ! root      211:                true
1.1       root      212:        fi
                    213: 
                    214:        pathcomp="${pathcomp}/"
                    215: done
                    216: fi
                    217: 
1.1.1.2 ! root      218:        if [ x"$dir_arg" != x ]
1.1       root      219:        then
1.1.1.2 ! root      220:                if [ -d "$dstarg" ]; then
        !           221:                        true
        !           222:                else
        !           223:                        $doit $mkdirprog "$dstarg" &&
        !           224: 
        !           225:                        if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dstarg"; else true ; fi &&
        !           226:                        if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dstarg"; else true ; fi &&
        !           227:                        if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dstarg"; else true ; fi
        !           228:                fi
1.1       root      229:        else
                    230: 
1.1.1.2 ! root      231:                if [ x"$dstisfile" = x ]
        !           232:                then
        !           233:                        file=$srcarg
        !           234:                else
        !           235:                        file=$dst
        !           236:                fi
1.1       root      237: 
1.1.1.2 ! root      238:                dstfile=`basename "$file"`
        !           239:                dstfinal="$dstdir/$dstfile"
1.1       root      240: 
                    241: # Make a temp file name in the proper directory.
                    242: 
1.1.1.2 ! root      243:                dsttmp=$dstdir/#inst.$$#
        !           244: 
        !           245: # Make a backup file name in the proper directory.
        !           246:                case x$suffixfmt in
        !           247:                *%*)    suffix=`echo x |
        !           248:                        $awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
        !           249:                        { cnt = 0;
        !           250:                          do {
        !           251:                                sfx = sprintf(fmt, cnt++);
        !           252:                                name = bname sfx;
        !           253:                          } while (system("test -f " name) == 0);
        !           254:                          print sfx; }' -`;;
        !           255:                x)      ;;
        !           256:                *)      suffix="$suffixfmt";;
        !           257:                esac
        !           258:                dstbackup="$dstfinal$suffix"
1.1       root      259: 
                    260: # Move or copy the file name to the temp name
                    261: 
1.1.1.2 ! root      262:                $doit $doinst $srcarg "$dsttmp" &&
1.1       root      263: 
1.1.1.2 ! root      264:                trap "rm -f ${dsttmp}" 0 &&
1.1       root      265: 
                    266: # and set any options; do chmod last to preserve setuid bits
                    267: 
                    268: # If any of these fail, we abort the whole thing.  If we want to
                    269: # ignore errors from any of these, just make sure not to ignore
                    270: # errors from the above "$doit $instcmd $src $dsttmp" command.
                    271: 
1.1.1.2 ! root      272:                if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else true;fi &&
        !           273:                if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else true;fi &&
        !           274:                if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripflags "$dsttmp"; else true;fi &&
        !           275:                if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else true;fi &&
1.1       root      276: 
                    277: # Now rename the file to the real destination.
                    278: 
1.1.1.2 ! root      279:                if [ x"$suffix" != x ] && [ -f "$dstfinal" ]
        !           280:                then
        !           281:                        $doit $mvcmd "$dstfinal" "$dstbackup"
        !           282:                else
        !           283:                        $doit $rmcmd -f "$dstfinal"
        !           284:                fi &&
        !           285:                $doit $mvcmd "$dsttmp" "$dstfinal"
        !           286:        fi
1.1       root      287: 
1.1.1.2 ! root      288: done &&
1.1       root      289: 
                    290: 
                    291: exit 0

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.