~ubuntu-branches/ubuntu/karmic/kid3/karmic

« back to all changes in this revision

Viewing changes to admin/install-sh

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-20 16:12:30 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090520161230-qetp532r8ydujkz2
Tags: upstream-1.2
Import upstream version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/sh
2
2
# install - install a program, script, or datafile
3
3
 
4
 
scriptversion=2005-05-14.22
 
4
scriptversion=2005-11-07.23
5
5
 
6
6
# This originates from X11R5 (mit/util/scripts/install.sh), which was
7
7
# later released in X11R6 (xc/config/util/install.sh) with the
58
58
rmprog="${RMPROG-rm}"
59
59
mkdirprog="${MKDIRPROG-mkdir}"
60
60
 
61
 
chmodcmd="$chmodprog 0755"
 
61
posix_glob=
 
62
posix_mkdir=
 
63
 
 
64
# Symbolic mode for testing mkdir with directories.
 
65
# It is the same as 755, but also tests that "u+" works.
 
66
test_mode=u=rwx,g=rx,o=rx,u+wx
 
67
 
 
68
# Desired mode of installed file.
 
69
mode=0755
 
70
 
 
71
# Desired mode of newly created intermediate directories.
 
72
# It is empty if not known yet.
 
73
intermediate_mode=
 
74
 
 
75
chmodcmd=$chmodprog
62
76
chowncmd=
63
77
chgrpcmd=
64
78
stripcmd=
111
125
 
112
126
    --help) echo "$usage"; exit $?;;
113
127
 
114
 
    -m) chmodcmd="$chmodprog $2"
 
128
    -m) mode=$2
115
129
        shift
116
130
        shift
117
131
        continue;;
164
178
  exit 0
165
179
fi
166
180
 
 
181
test -n "$dir_arg" || trap '(exit $?); exit' 1 2 13 15
 
182
 
167
183
for src
168
184
do
169
185
  # Protect names starting with `-'.
173
189
 
174
190
  if test -n "$dir_arg"; then
175
191
    dst=$src
176
 
    src=
 
192
    dstdir=$dst
 
193
    test -d "$dstdir"
 
194
    dstdir_status=$?
 
195
  else
177
196
 
178
 
    if test -d "$dst"; then
179
 
      mkdircmd=:
180
 
      chmodcmd=
181
 
    else
182
 
      mkdircmd=$mkdirprog
183
 
    fi
184
 
  else
185
197
    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
186
198
    # might cause directories to be created, which would be especially bad
187
199
    # if $src (and thus $dsttmp) contains '*'.
208
220
        echo "$0: $dstarg: Is a directory" >&2
209
221
        exit 1
210
222
      fi
211
 
      dst=$dst/`basename "$src"`
 
223
      dstdir=$dst
 
224
      dst=$dstdir/`basename "$src"`
 
225
      dstdir_status=0
 
226
    else
 
227
      # Prefer dirname, but fall back on a substitute if dirname fails.
 
228
      dstdir=`
 
229
        (dirname "$dst") 2>/dev/null ||
 
230
        expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
 
231
             X"$dst" : 'X\(//\)[^/]' \| \
 
232
             X"$dst" : 'X\(//\)$' \| \
 
233
             X"$dst" : 'X\(/\)' \| \
 
234
             .       : '\(.\)' 2>/dev/null ||
 
235
        echo X"$dst" |
 
236
            sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
 
237
                  /^X\(\/\/\)[^/].*/{ s//\1/; q; }
 
238
                  /^X\(\/\/\)$/{ s//\1/; q; }
 
239
                  /^X\(\/\).*/{ s//\1/; q; }
 
240
                  s/.*/./; q'
 
241
      `
 
242
 
 
243
      test -d "$dstdir"
 
244
      dstdir_status=$?
212
245
    fi
213
246
  fi
214
247
 
215
 
  # This sed command emulates the dirname command.
216
 
  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
217
 
 
218
 
  # Make sure that the destination directory exists.
219
 
 
220
 
  # Skip lots of stat calls in the usual case.
221
 
  if test ! -d "$dstdir"; then
222
 
    defaultIFS='
223
 
         '
224
 
    IFS="${IFS-$defaultIFS}"
225
 
 
226
 
    oIFS=$IFS
227
 
    # Some sh's can't handle IFS=/ for some reason.
228
 
    IFS='%'
229
 
    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
230
 
    shift
231
 
    IFS=$oIFS
232
 
 
233
 
    pathcomp=
234
 
 
235
 
    while test $# -ne 0 ; do
236
 
      pathcomp=$pathcomp$1
 
248
  obsolete_mkdir_used=false
 
249
 
 
250
  if test $dstdir_status != 0; then
 
251
    case $posix_mkdir in
 
252
      '')
 
253
        posix_mkdir=false
 
254
        if $mkdirprog -m $test_mode -p -- / >/dev/null 2>&1; then
 
255
          posix_mkdir=true
 
256
        else
 
257
          # Remove any dirs left behind by ancient mkdir implementations.
 
258
          rmdir ./-m "$test_mode" ./-p ./-- 2>/dev/null
 
259
        fi ;;
 
260
    esac
 
261
 
 
262
    if
 
263
      $posix_mkdir && {
 
264
 
 
265
        # With -d, create the new directory with the user-specified mode.
 
266
        # Otherwise, create it using the same intermediate mode that
 
267
        # mkdir -p would use when creating intermediate directories.
 
268
        # POSIX says that this mode is "$(umask -S),u+wx", so use that
 
269
        # if umask -S works.
 
270
 
 
271
        if test -n "$dir_arg"; then
 
272
          mkdir_mode=$mode
 
273
        else
 
274
          case $intermediate_mode in
 
275
            '')
 
276
              if umask_S=`(umask -S) 2>/dev/null`; then
 
277
                intermediate_mode=$umask_S,u+wx
 
278
              else
 
279
                intermediate_mode=$test_mode
 
280
              fi ;;
 
281
          esac
 
282
          mkdir_mode=$intermediate_mode
 
283
        fi
 
284
 
 
285
        $mkdirprog -m "$mkdir_mode" -p -- "$dstdir"
 
286
      }
 
287
    then :
 
288
    else
 
289
 
 
290
      # mkdir does not conform to POSIX, or it failed possibly due to
 
291
      # a race condition.  Create the directory the slow way, step by
 
292
      # step, checking for races as we go.
 
293
 
 
294
      case $dstdir in
 
295
        /*) pathcomp=/ ;;
 
296
        -*) pathcomp=./ ;;
 
297
        *)  pathcomp= ;;
 
298
      esac
 
299
 
 
300
      case $posix_glob in
 
301
        '')
 
302
          if (set -f) 2>/dev/null; then
 
303
            posix_glob=true
 
304
          else
 
305
            posix_glob=false
 
306
          fi ;;
 
307
      esac
 
308
 
 
309
      oIFS=$IFS
 
310
      IFS=/
 
311
      $posix_glob && set -f
 
312
      set fnord $dstdir
237
313
      shift
238
 
      if test ! -d "$pathcomp"; then
239
 
        $mkdirprog "$pathcomp"
240
 
        # mkdir can fail with a `File exist' error in case several
241
 
        # install-sh are creating the directory concurrently.  This
242
 
        # is OK.
243
 
        test -d "$pathcomp" || exit
244
 
      fi
245
 
      pathcomp=$pathcomp/
246
 
    done
 
314
      $posix_glob && set +f
 
315
      IFS=$oIFS
 
316
 
 
317
      for d
 
318
      do
 
319
        test "x$d" = x && continue
 
320
 
 
321
        pathcomp=$pathcomp$d
 
322
        if test ! -d "$pathcomp"; then
 
323
          $mkdirprog "$pathcomp"
 
324
          # Don't fail if two instances are running concurrently.
 
325
          test -d "$pathcomp" || exit 1
 
326
        fi
 
327
        pathcomp=$pathcomp/
 
328
      done
 
329
      obsolete_mkdir_used=true
 
330
    fi
247
331
  fi
248
332
 
249
333
  if test -n "$dir_arg"; then
250
 
    $doit $mkdircmd "$dst" \
251
 
      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
252
 
      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
253
 
      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
254
 
      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
255
 
 
 
334
    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
 
335
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
 
336
    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
 
337
      test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dst"; } || exit 1
256
338
  else
257
 
    dstfile=`basename "$dst"`
258
339
 
259
340
    # Make a couple of temp file names in the proper directory.
260
341
    dsttmp=$dstdir/_inst.$$_
262
343
 
263
344
    # Trap to clean up those temp files at exit.
264
345
    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
265
 
    trap '(exit $?); exit' 1 2 13 15
266
346
 
267
347
    # Copy the file name to the temp name.
268
348
    $doit $cpprog "$src" "$dsttmp" &&
276
356
    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
277
357
      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
278
358
      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
279
 
      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
 
359
      && { test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dsttmp"; } &&
280
360
 
281
361
    # Now rename the file to the real destination.
282
 
    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
 
362
    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
283
363
      || {
284
364
           # The rename failed, perhaps because mv can't rename something else
285
365
           # to itself, or perhaps because mv is so ancient that it does not
291
371
           # reasons.  In this case, the final cleanup might fail but the new
292
372
           # file should still install successfully.
293
373
           {
294
 
             if test -f "$dstdir/$dstfile"; then
295
 
               $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
296
 
               || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
 
374
             if test -f "$dst"; then
 
375
               $doit $rmcmd -f "$dst" 2>/dev/null \
 
376
               || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
 
377
                     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
297
378
               || {
298
 
                 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
 
379
                 echo "$0: cannot unlink or rename $dst" >&2
299
380
                 (exit 1); exit 1
300
381
               }
301
382
             else
304
385
           } &&
305
386
 
306
387
           # Now rename the file to the real destination.
307
 
           $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
 
388
           $doit $mvcmd "$dsttmp" "$dst"
308
389
         }
309
 
    }
310
 
  fi || { (exit 1); exit 1; }
 
390
    } || exit 1
 
391
 
 
392
    trap '' 0
 
393
  fi
311
394
done
312
395
 
313
 
# The final little trick to "correctly" pass the exit status to the exit trap.
314
 
{
315
 
  (exit 0); exit 0
316
 
}
317
 
 
318
396
# Local variables:
319
397
# eval: (add-hook 'write-file-hooks 'time-stamp)
320
398
# time-stamp-start: "scriptversion="