annotate kdiff3/admin/install-sh @ 69:8febbfb1148c

KDiff3 0.9.89
author joachim99
date Mon, 10 Apr 2006 08:40:51 +0000
parents efe33e938730
children 1184fc843210
rev   line source
joachim99@2 1 #!/bin/sh
joachim99@69 2 # install - install a program, script, or datafile
joachim99@69 3
joachim99@69 4 scriptversion=2005-05-14.22
joachim99@69 5
joachim99@69 6 # This originates from X11R5 (mit/util/scripts/install.sh), which was
joachim99@69 7 # later released in X11R6 (xc/config/util/install.sh) with the
joachim99@69 8 # following copyright and license.
joachim99@2 9 #
joachim99@69 10 # Copyright (C) 1994 X Consortium
joachim99@2 11 #
joachim99@69 12 # Permission is hereby granted, free of charge, to any person obtaining a copy
joachim99@69 13 # of this software and associated documentation files (the "Software"), to
joachim99@69 14 # deal in the Software without restriction, including without limitation the
joachim99@69 15 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
joachim99@69 16 # sell copies of the Software, and to permit persons to whom the Software is
joachim99@69 17 # furnished to do so, subject to the following conditions:
joachim99@2 18 #
joachim99@69 19 # The above copyright notice and this permission notice shall be included in
joachim99@69 20 # all copies or substantial portions of the Software.
joachim99@69 21 #
joachim99@69 22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
joachim99@69 23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
joachim99@69 24 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
joachim99@69 25 # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
joachim99@69 26 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
joachim99@69 27 # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
joachim99@69 28 #
joachim99@69 29 # Except as contained in this notice, the name of the X Consortium shall not
joachim99@69 30 # be used in advertising or otherwise to promote the sale, use or other deal-
joachim99@69 31 # ings in this Software without prior written authorization from the X Consor-
joachim99@69 32 # tium.
joachim99@69 33 #
joachim99@69 34 #
joachim99@69 35 # FSF changes to this file are in the public domain.
joachim99@2 36 #
joachim99@2 37 # Calling this script install-sh is preferred over install.sh, to prevent
joachim99@2 38 # `make' implicit rules from creating a file called install from it
joachim99@2 39 # when there is no Makefile.
joachim99@2 40 #
joachim99@2 41 # This script is compatible with the BSD install script, but was written
joachim99@2 42 # from scratch. It can only install one file at a time, a restriction
joachim99@2 43 # shared with many OS's install programs.
joachim99@2 44
joachim99@2 45 # set DOITPROG to echo to test this script
joachim99@2 46
joachim99@2 47 # Don't use :- since 4.3BSD and earlier shells don't like it.
joachim99@2 48 doit="${DOITPROG-}"
joachim99@2 49
joachim99@2 50 # put in absolute paths if you don't have them in your path; or use env. vars.
joachim99@2 51
joachim99@2 52 mvprog="${MVPROG-mv}"
joachim99@2 53 cpprog="${CPPROG-cp}"
joachim99@2 54 chmodprog="${CHMODPROG-chmod}"
joachim99@2 55 chownprog="${CHOWNPROG-chown}"
joachim99@2 56 chgrpprog="${CHGRPPROG-chgrp}"
joachim99@2 57 stripprog="${STRIPPROG-strip}"
joachim99@2 58 rmprog="${RMPROG-rm}"
joachim99@2 59 mkdirprog="${MKDIRPROG-mkdir}"
joachim99@2 60
joachim99@2 61 chmodcmd="$chmodprog 0755"
joachim99@69 62 chowncmd=
joachim99@69 63 chgrpcmd=
joachim99@69 64 stripcmd=
joachim99@2 65 rmcmd="$rmprog -f"
joachim99@2 66 mvcmd="$mvprog"
joachim99@69 67 src=
joachim99@69 68 dst=
joachim99@69 69 dir_arg=
joachim99@69 70 dstarg=
joachim99@69 71 no_target_directory=
joachim99@2 72
joachim99@69 73 usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
joachim99@69 74 or: $0 [OPTION]... SRCFILES... DIRECTORY
joachim99@69 75 or: $0 [OPTION]... -t DIRECTORY SRCFILES...
joachim99@69 76 or: $0 [OPTION]... -d DIRECTORIES...
joachim99@2 77
joachim99@69 78 In the 1st form, copy SRCFILE to DSTFILE.
joachim99@69 79 In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
joachim99@69 80 In the 4th, create DIRECTORIES.
joachim99@2 81
joachim99@69 82 Options:
joachim99@69 83 -c (ignored)
joachim99@69 84 -d create directories instead of installing files.
joachim99@69 85 -g GROUP $chgrpprog installed files to GROUP.
joachim99@69 86 -m MODE $chmodprog installed files to MODE.
joachim99@69 87 -o USER $chownprog installed files to USER.
joachim99@69 88 -s $stripprog installed files.
joachim99@69 89 -t DIRECTORY install into DIRECTORY.
joachim99@69 90 -T report an error if DSTFILE is a directory.
joachim99@69 91 --help display this help and exit.
joachim99@69 92 --version display version info and exit.
joachim99@2 93
joachim99@69 94 Environment variables override the default commands:
joachim99@69 95 CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
joachim99@69 96 "
joachim99@2 97
joachim99@69 98 while test -n "$1"; do
joachim99@69 99 case $1 in
joachim99@69 100 -c) shift
joachim99@69 101 continue;;
joachim99@2 102
joachim99@69 103 -d) dir_arg=true
joachim99@69 104 shift
joachim99@69 105 continue;;
joachim99@2 106
joachim99@69 107 -g) chgrpcmd="$chgrpprog $2"
joachim99@69 108 shift
joachim99@69 109 shift
joachim99@69 110 continue;;
joachim99@2 111
joachim99@69 112 --help) echo "$usage"; exit $?;;
joachim99@2 113
joachim99@69 114 -m) chmodcmd="$chmodprog $2"
joachim99@69 115 shift
joachim99@69 116 shift
joachim99@69 117 continue;;
joachim99@69 118
joachim99@69 119 -o) chowncmd="$chownprog $2"
joachim99@69 120 shift
joachim99@69 121 shift
joachim99@69 122 continue;;
joachim99@69 123
joachim99@69 124 -s) stripcmd=$stripprog
joachim99@69 125 shift
joachim99@69 126 continue;;
joachim99@69 127
joachim99@69 128 -t) dstarg=$2
joachim99@69 129 shift
joachim99@69 130 shift
joachim99@69 131 continue;;
joachim99@69 132
joachim99@69 133 -T) no_target_directory=true
joachim99@69 134 shift
joachim99@69 135 continue;;
joachim99@69 136
joachim99@69 137 --version) echo "$0 $scriptversion"; exit $?;;
joachim99@69 138
joachim99@69 139 *) # When -d is used, all remaining arguments are directories to create.
joachim99@69 140 # When -t is used, the destination is already specified.
joachim99@69 141 test -n "$dir_arg$dstarg" && break
joachim99@69 142 # Otherwise, the last argument is the destination. Remove it from $@.
joachim99@69 143 for arg
joachim99@69 144 do
joachim99@69 145 if test -n "$dstarg"; then
joachim99@69 146 # $@ is not empty: it contains at least $arg.
joachim99@69 147 set fnord "$@" "$dstarg"
joachim99@69 148 shift # fnord
joachim99@69 149 fi
joachim99@69 150 shift # arg
joachim99@69 151 dstarg=$arg
joachim99@69 152 done
joachim99@69 153 break;;
joachim99@69 154 esac
joachim99@2 155 done
joachim99@2 156
joachim99@69 157 if test -z "$1"; then
joachim99@69 158 if test -z "$dir_arg"; then
joachim99@69 159 echo "$0: no input file specified." >&2
joachim99@69 160 exit 1
joachim99@69 161 fi
joachim99@69 162 # It's OK to call `install-sh -d' without argument.
joachim99@69 163 # This can happen when creating conditional directories.
joachim99@69 164 exit 0
joachim99@2 165 fi
joachim99@2 166
joachim99@69 167 for src
joachim99@69 168 do
joachim99@69 169 # Protect names starting with `-'.
joachim99@69 170 case $src in
joachim99@69 171 -*) src=./$src ;;
joachim99@69 172 esac
joachim99@66 173
joachim99@69 174 if test -n "$dir_arg"; then
joachim99@69 175 dst=$src
joachim99@69 176 src=
joachim99@2 177
joachim99@69 178 if test -d "$dst"; then
joachim99@69 179 mkdircmd=:
joachim99@69 180 chmodcmd=
joachim99@69 181 else
joachim99@69 182 mkdircmd=$mkdirprog
joachim99@69 183 fi
joachim99@69 184 else
joachim99@69 185 # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
joachim99@69 186 # might cause directories to be created, which would be especially bad
joachim99@69 187 # if $src (and thus $dsttmp) contains '*'.
joachim99@69 188 if test ! -f "$src" && test ! -d "$src"; then
joachim99@69 189 echo "$0: $src does not exist." >&2
joachim99@69 190 exit 1
joachim99@69 191 fi
joachim99@2 192
joachim99@69 193 if test -z "$dstarg"; then
joachim99@69 194 echo "$0: no destination specified." >&2
joachim99@69 195 exit 1
joachim99@69 196 fi
joachim99@66 197
joachim99@69 198 dst=$dstarg
joachim99@69 199 # Protect names starting with `-'.
joachim99@69 200 case $dst in
joachim99@69 201 -*) dst=./$dst ;;
joachim99@69 202 esac
joachim99@2 203
joachim99@69 204 # If destination is a directory, append the input filename; won't work
joachim99@69 205 # if double slashes aren't ignored.
joachim99@69 206 if test -d "$dst"; then
joachim99@69 207 if test -n "$no_target_directory"; then
joachim99@69 208 echo "$0: $dstarg: Is a directory" >&2
joachim99@69 209 exit 1
joachim99@69 210 fi
joachim99@69 211 dst=$dst/`basename "$src"`
joachim99@69 212 fi
joachim99@69 213 fi
joachim99@2 214
joachim99@69 215 # This sed command emulates the dirname command.
joachim99@69 216 dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
joachim99@2 217
joachim99@69 218 # Make sure that the destination directory exists.
joachim99@2 219
joachim99@69 220 # Skip lots of stat calls in the usual case.
joachim99@69 221 if test ! -d "$dstdir"; then
joachim99@69 222 defaultIFS='
joachim99@69 223 '
joachim99@69 224 IFS="${IFS-$defaultIFS}"
joachim99@2 225
joachim99@69 226 oIFS=$IFS
joachim99@69 227 # Some sh's can't handle IFS=/ for some reason.
joachim99@69 228 IFS='%'
joachim99@69 229 set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
joachim99@69 230 shift
joachim99@69 231 IFS=$oIFS
joachim99@2 232
joachim99@69 233 pathcomp=
joachim99@2 234
joachim99@69 235 while test $# -ne 0 ; do
joachim99@69 236 pathcomp=$pathcomp$1
joachim99@69 237 shift
joachim99@69 238 if test ! -d "$pathcomp"; then
joachim99@69 239 $mkdirprog "$pathcomp"
joachim99@69 240 # mkdir can fail with a `File exist' error in case several
joachim99@69 241 # install-sh are creating the directory concurrently. This
joachim99@69 242 # is OK.
joachim99@69 243 test -d "$pathcomp" || exit
joachim99@69 244 fi
joachim99@69 245 pathcomp=$pathcomp/
joachim99@69 246 done
joachim99@69 247 fi
joachim99@2 248
joachim99@69 249 if test -n "$dir_arg"; then
joachim99@69 250 $doit $mkdircmd "$dst" \
joachim99@69 251 && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
joachim99@69 252 && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
joachim99@69 253 && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
joachim99@69 254 && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
joachim99@2 255
joachim99@69 256 else
joachim99@69 257 dstfile=`basename "$dst"`
joachim99@2 258
joachim99@69 259 # Make a couple of temp file names in the proper directory.
joachim99@69 260 dsttmp=$dstdir/_inst.$$_
joachim99@69 261 rmtmp=$dstdir/_rm.$$_
joachim99@69 262
joachim99@69 263 # Trap to clean up those temp files at exit.
joachim99@69 264 trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
joachim99@69 265 trap '(exit $?); exit' 1 2 13 15
joachim99@69 266
joachim99@69 267 # Copy the file name to the temp name.
joachim99@69 268 $doit $cpprog "$src" "$dsttmp" &&
joachim99@69 269
joachim99@69 270 # and set any options; do chmod last to preserve setuid bits.
joachim99@69 271 #
joachim99@69 272 # If any of these fail, we abort the whole thing. If we want to
joachim99@69 273 # ignore errors from any of these, just make sure not to ignore
joachim99@69 274 # errors from the above "$doit $cpprog $src $dsttmp" command.
joachim99@69 275 #
joachim99@69 276 { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
joachim99@69 277 && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
joachim99@69 278 && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
joachim99@69 279 && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
joachim99@69 280
joachim99@69 281 # Now rename the file to the real destination.
joachim99@69 282 { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
joachim99@69 283 || {
joachim99@69 284 # The rename failed, perhaps because mv can't rename something else
joachim99@69 285 # to itself, or perhaps because mv is so ancient that it does not
joachim99@69 286 # support -f.
joachim99@69 287
joachim99@69 288 # Now remove or move aside any old file at destination location.
joachim99@69 289 # We try this two ways since rm can't unlink itself on some
joachim99@69 290 # systems and the destination file might be busy for other
joachim99@69 291 # reasons. In this case, the final cleanup might fail but the new
joachim99@69 292 # file should still install successfully.
joachim99@69 293 {
joachim99@69 294 if test -f "$dstdir/$dstfile"; then
joachim99@69 295 $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
joachim99@69 296 || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
joachim99@69 297 || {
joachim99@69 298 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
joachim99@69 299 (exit 1); exit 1
joachim99@69 300 }
joachim99@69 301 else
joachim99@69 302 :
joachim99@69 303 fi
joachim99@69 304 } &&
joachim99@69 305
joachim99@69 306 # Now rename the file to the real destination.
joachim99@69 307 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
joachim99@69 308 }
joachim99@69 309 }
joachim99@69 310 fi || { (exit 1); exit 1; }
joachim99@2 311 done
joachim99@2 312
joachim99@66 313 # The final little trick to "correctly" pass the exit status to the exit trap.
joachim99@69 314 {
joachim99@69 315 (exit 0); exit 0
joachim99@69 316 }
joachim99@2 317
joachim99@69 318 # Local variables:
joachim99@69 319 # eval: (add-hook 'write-file-hooks 'time-stamp)
joachim99@69 320 # time-stamp-start: "scriptversion="
joachim99@69 321 # time-stamp-format: "%:y-%02m-%02d.%02H"
joachim99@69 322 # time-stamp-end: "$"
joachim99@69 323 # End: