~ubuntu-branches/ubuntu/karmic/tovid/karmic

« back to all changes in this revision

Viewing changes to src/tovid-init.in

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-01-24 22:04:40 UTC
  • Revision ID: james.westby@ubuntu.com-20080124220440-x7cheljduf1rdgnq
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
 
 
3
# tovid-init
 
4
# Part of the tovid suite
 
5
# =======================
 
6
# Define global (suite-wide) functions and variables
 
7
# for the tovid suite.
 
8
#
 
9
# Project homepage: http://www.tovid.org
 
10
#
 
11
#
 
12
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
 
13
#
 
14
# This program is free software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either
 
17
# version 2 of the License, or (at your option) any later
 
18
# version.
 
19
#
 
20
# This program is distributed in the hope that it will be useful,
 
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
# GNU General Public License for more details.
 
24
#
 
25
# You should have received a copy of the GNU General Public License
 
26
# along with this program; if not, write to the Free Software
 
27
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
 
28
#
 
29
#           http://www.gnu.org/licenses/gpl.txt
 
30
 
 
31
 
 
32
# ******************************************************************************
 
33
# ******************************************************************************
 
34
#
 
35
#
 
36
# VARIABLES
 
37
#
 
38
#
 
39
# ******************************************************************************
 
40
# ******************************************************************************
 
41
 
 
42
# Reset input field separator to default
 
43
#IFS=
 
44
 
 
45
# Exit with error on undeclared variables
 
46
#set -u
 
47
 
 
48
# Suite version
 
49
TOVID_VERSION="@VERSION@"
 
50
 
 
51
# String used to separate blocks of output
 
52
SEPARATOR="========================================================="
 
53
 
 
54
TOVID_HOME="$HOME/.tovid"
 
55
TOVID_HOME_PAGE="http://www.tovid.org"
 
56
TOVID_FORUMS="http://www.createphpbb.com/tovid/"
 
57
 
 
58
 
 
59
# ******************************************************************************
 
60
# ******************************************************************************
 
61
#
 
62
#
 
63
# FUNCTIONS
 
64
#
 
65
#
 
66
# ******************************************************************************
 
67
# ******************************************************************************
 
68
 
 
69
# ******************************************************************************
 
70
# test if input is a number or not
 
71
# returns true or false exit code ( 0 or 1 )
 
72
# usage: test_is_number INPUT
 
73
# ******************************************************************************
 
74
function test_is_number()
 
75
{
 
76
    [[ $1 && $1 =~ ^[-+]?[0-9]*\(\.[0-9]+\)?$ ]]
 
77
}
 
78
 
 
79
# ******************************************************************************
 
80
# Create a unique temporary directory and return its name
 
81
# Input args: $1 = Base name (i.e., "work_dir")
 
82
# Return value is echoed to stdout (i.e., "work_dir.3")
 
83
# ******************************************************************************
 
84
function tempdir()
 
85
{
 
86
    NUM=0
 
87
    BASENAME="$1"
 
88
    CREATE=:
 
89
    TEMPDIR="$BASENAME.$NUM"
 
90
    [[  -n $2  && $2 = "nocreate" ]] && CREATE=false
 
91
    while test -d "$BASENAME.$NUM"; do
 
92
        ((NUM++))
 
93
    done
 
94
    $CREATE && mkdir "$BASENAME.$NUM"
 
95
    echo "$BASENAME.$NUM"
 
96
}
 
97
 
 
98
# ******************************************************************************
 
99
# Verify that a variable meets certain conditions
 
100
# Usage: verify $VAR set|range "test limits"
 
101
# Input: $1 = the variable to check
 
102
#        $2 = the kind of test to perform (set|range)
 
103
#             set: test if $VAR is in the space-separated set "test limits"
 
104
#             range: test if $VAR is in the range given by "test limits"
 
105
#        $3 = the limits for the test
 
106
#
 
107
# ex: verify $CMD_LN_OPT set "y n Y N"
 
108
#     will return ":" (true) if $CMD_LN_OPT is one of "y n Y N"
 
109
#     or retern "false" if it isn't (so if $CMD_LN_OPT was "no", you'd get "false")
 
110
#
 
111
# ex: verify $CMD_LN_OPT range "0 10"
 
112
#     will return ":" (true) if 0 <= $CMD_LN_OPT <= 10
 
113
# ******************************************************************************
 
114
function verify()
 
115
{
 
116
    VERIFY_VAR="$1"
 
117
    VERIFY_TEST_TYPE="$2"
 
118
    case $VERIFY_TEST_TYPE in
 
119
    "range" )
 
120
        VERIFY_LOW=$(echo "$3" | awk '{ print $1 }')
 
121
        VERIFY_HIGH=$(echo "$3" | awk '{ print $2 }')
 
122
        if test $VERIFY_LOW -le $VERIFY_VAR && \
 
123
        test $VERIFY_HIGH -ge $VERIFY_VAR; then
 
124
            echo ":"
 
125
        else
 
126
            echo "false"
 
127
        fi
 
128
        ;;
 
129
 
 
130
    "set" )
 
131
        VERIFY_SET="$3"
 
132
 
 
133
        if echo "$VERIFY_SET" | grep -q -w "$VERIFY_VAR"; then
 
134
            echo ":"
 
135
        else
 
136
            echo "false"
 
137
        fi
 
138
        ;;
 
139
  esac
 
140
}
 
141
 
 
142
# ******************************************************************************
 
143
# Print a pretty (wrapped) notice message.
 
144
# Args: $@ == text string containing the message
 
145
# ******************************************************************************
 
146
function precho()
 
147
{
 
148
    echo -e "$@" | fold -s -w ${COLUMNS:-80}
 
149
}
 
150
 
 
151
# ******************************************************************************
 
152
# Print error message, then exit.
 
153
# Args: $@ == text string containing error message
 
154
# ******************************************************************************
 
155
function exit_with_error()
 
156
{
 
157
    echo $@
 
158
    exit 1
 
159
}
 
160
 
 
161
# ******************************************************************************
 
162
# Take an integer number of seconds and format it as hours:minutes:seconds
 
163
# Echoes result to stdout, so in order to use the output as a "return value",
 
164
# call the function like this:
 
165
#     RETURN_VALUE=$(format_time $NUM_SECONDS)
 
166
# ******************************************************************************
 
167
function format_time()
 
168
{
 
169
    HMS_HOURS=$(expr $1 / 3600)
 
170
    HMS_MINS=$(expr \( $1 % 3600 \) / 60)
 
171
    HMS_SECS=$(expr $1 % 3600 % 60)
 
172
 
 
173
    test "$HMS_HOURS" -lt 10 && HMS_HOURS="0${HMS_HOURS}"
 
174
    test "$HMS_MINS" -lt 10 && HMS_MINS="0${HMS_MINS}"
 
175
    test "$HMS_SECS" -lt 10 && HMS_SECS="0${HMS_SECS}"
 
176
 
 
177
    echo "${HMS_HOURS}:${HMS_MINS}:${HMS_SECS}"
 
178
}
 
179
 
 
180
# ******************************************************************************
 
181
# Take a string containing a time (like "02:15:25.3") and
 
182
# format it as an integer number of seconds. Fractional seconds
 
183
# are truncated. Result is echoed to stdout, so to use the output
 
184
# as a "return value", call the function like this:
 
185
#     RETURN_VALUE=$(unformat_time $TIME_STRING)
 
186
# ******************************************************************************
 
187
function unformat_time()
 
188
{
 
189
    HMS_HOURS=$(awk -F ':' '{print $1}' <<< $1)
 
190
    HMS_MINS=$(awk -F ':' '{print $2}' <<< $1)
 
191
    HMS_SECS=$(awk -F ':' '{print $3}' <<< $1)
 
192
    # allow integer to be passed, if so it will be echoed unchanged
 
193
    if [[ $1 = *:* ]]; then
 
194
        TOT_SECONDS=$(bc <<< "($HMS_HOURS * 3600) + ($HMS_MINS * 60) + $HMS_SECS")
 
195
    else
 
196
        TOT_SECONDS=$1
 
197
    fi
 
198
    echo $TOT_SECONDS
 
199
}
 
200
 
 
201
 
 
202
# ******************************************************************************
 
203
# Read a space-separated list of arguments, stopping before the next option
 
204
# beginning with '-'. After running, ARGS_ARRAY contains the resulting list.
 
205
# ******************************************************************************
 
206
function get_listargs()
 
207
{
 
208
    unset x ARGS_ARRAY
 
209
    # Hackish list-parsing
 
210
    while test $# -gt 0 && test x"${1:0:1}" != x"-"; do
 
211
        ARGS_ARRAY[x++]="$1"
 
212
        shift
 
213
    done
 
214
    # Do not skip past the next argument
 
215
    if test $# -gt 0 && test x"${1:0:1}" = x"-";then
 
216
        DO_SHIFT=false
 
217
    fi
 
218
}
 
219
 
 
220
 
 
221
# ******************************************************************************
 
222
# Display a progress meter showing MB written to a file
 
223
# Args: $1 = name of file to monitor
 
224
#       $2 = a short message to display, such as "Encoding video"
 
225
# ******************************************************************************
 
226
function file_output_progress()
 
227
{
 
228
    if $FAKE; then
 
229
        return
 
230
    fi
 
231
    FOP_OUTFILE="$1"
 
232
    FOP_BASENAME=$(basename "$FOP_OUTFILE")
 
233
    if test -n "$3"; then
 
234
        FOP_BASENAME_NAME="$3"
 
235
    else
 
236
        FOP_BASENAME_NAME="$FOP_BASENAME"
 
237
    fi
 
238
    FOP_MSG="$2"
 
239
    # A dumb little animation toggle
 
240
    FOP_FLIP=false
 
241
 
 
242
    printf "\n"
 
243
 
 
244
    # Wait for input file to appear
 
245
    # After a 30-second timeout, exit gracefully
 
246
    CUR_TIME=30
 
247
    while test $CUR_TIME -gt 0; do
 
248
        # If file exists, wait a few more seconds, then break out
 
249
        if test -e "$FOP_OUTFILE"; then
 
250
            printf "Processing started. Please wait...                                               "
 
251
            sleep 3s
 
252
            break
 
253
        fi
 
254
        printf "Waiting $CUR_TIME seconds for output file \"$FOP_BASENAME_NAME\" to appear...\r"
 
255
        sleep 1s
 
256
        CUR_TIME=$(expr $CUR_TIME - 1)
 
257
    done
 
258
 
 
259
    printf "\n"
 
260
 
 
261
    # If file does not exist, exit with a runtime error
 
262
    if test ! -e "$FOP_OUTFILE"; then
 
263
        runtime_error "Couldn't create file: \"$FOP_OUTFILE\""
 
264
    fi
 
265
 
 
266
    # File size in bytes
 
267
    FOP_LAST_SIZE=0
 
268
    FOP_CUR_SIZE=$(du -b "$FOP_OUTFILE" | awk '{print $1}')
 
269
 
 
270
    # Keep looping until outfile stops getting bigger
 
271
    while test "$FOP_CUR_SIZE" -gt "$FOP_LAST_SIZE"; do
 
272
        # Display a changing line
 
273
        if $FOP_FLIP; then
 
274
            FOP_FLIP=false
 
275
            ANIM_STR="||| "
 
276
        else
 
277
            FOP_FLIP=:
 
278
            ANIM_STR="--- "
 
279
        fi
 
280
 
 
281
        # Display completion status
 
282
        FOP_CUR_MB=$(expr 1 + $FOP_CUR_SIZE / 1048576)
 
283
        printf "    %s %s: %s MB written to %s        \r" \
 
284
            "$ANIM_STR" "$FOP_MSG" "$FOP_CUR_MB" "$FOP_BASENAME_NAME"
 
285
 
 
286
        # Doze a bit to let the file size increase
 
287
        # (SLEEP_TIME defaults to 1s if unset)
 
288
        sleep ${SLEEP_TIME-"1s"}
 
289
 
 
290
        FOP_LAST_SIZE=$FOP_CUR_SIZE
 
291
        FOP_CUR_SIZE=$(du -b "$FOP_OUTFILE" | awk '{print $1}')
 
292
    done
 
293
    printf "\n\n"
 
294
}
 
295
 
 
296
# ******************************************************************************
 
297
# Check to see if a dependency group exists, quit if missing
 
298
# Input args:
 
299
#    $1 = dependency group to check for; note that this can be a globally
 
300
#         defined group (below), or a space-separated list of executables
 
301
#    $2 = Descriptive message about why the user needs the dependencies
 
302
#
 
303
# Ex:   assert_dep "$dvd" "You cannot make DVDs without these!"
 
304
#       assert_dep "rice sake fish" "You cannot make dinner!"
 
305
#
 
306
# ******************************************************************************
 
307
function assert_dep()
 
308
{
 
309
    DEPS="$1"
 
310
    HELP="$2"
 
311
 
 
312
    # Determine if any group member is missing
 
313
    NO_GROUP=false
 
314
    for dep in $DEPS; do
 
315
        if ! type -a $dep >> /dev/null 2>&1; then
 
316
            echo $SEPARATOR
 
317
            printf "%-13s %s\n" "  $dep" "MISSING!"
 
318
            NO_GROUP=:
 
319
        fi
 
320
    done
 
321
 
 
322
    # Quit if any group member is missing
 
323
    if $NO_GROUP; then
 
324
        echo
 
325
        precho "$HELP $DEP_ERROR_MSG"
 
326
        echo $SEPARATOR
 
327
        exit 1
 
328
    fi
 
329
}
 
330
 
 
331
 
 
332
# ******************************************************************************
 
333
# See what filesystem a directory is in
 
334
# Input args:
 
335
#   $1 = directory to find filesystem type
 
336
#
 
337
# Usage:
 
338
#   FS_TYPE=$(get_filesystem "$WORK_DIR")
 
339
# ******************************************************************************
 
340
function get_filesystem()
 
341
{
 
342
    DIRECTORY="$1"
 
343
    PARTITION=$(df . | tail -n 1 | awk '{print $1}')
 
344
    FILESYSTEM=$(mount | grep "$PARTITION" | awk '{print $5}')
 
345
    echo "$FILESYSTEM"
 
346
}
 
347
 
 
348
# ******************************************************************************
 
349
# Do floating point math with bc
 
350
# Input args:
 
351
#   $1 = The math operation to perfrom, in a quoted string
 
352
#
 
353
# Usage:
 
354
#   ANSWER=$(bc_math "$NUM1 + $NUM2")
 
355
# ******************************************************************************
 
356
function bc_math()
 
357
{
 
358
    echo "scale=2; $1" | bc -l
 
359
}
 
360
 
 
361
 
 
362
# ******************************************************************************
 
363
# Countdown N seconds, then return
 
364
# N defaults to 5 seconds if no args to function
 
365
# usage: countdown [SECONDS]
 
366
# ******************************************************************************
 
367
countdown ()
 
368
{
 
369
    [[ -n $1 ]] && LAST=$1 || LAST=5
 
370
    count=$(seq 1 $LAST)
 
371
    for _CNTR in $(sort -nr <<< "$count")
 
372
    do
 
373
        echo -n -e " in $_CNTR seconds...\r";
 
374
        sleep 1s;
 
375
    done
 
376
}
 
377
 
 
378
# ******************************************************************************
 
379
# ******************************************************************************
 
380
#
 
381
#
 
382
# EXECUTED INITIALIZATION
 
383
#
 
384
#
 
385
# ******************************************************************************
 
386
# ******************************************************************************
 
387
 
 
388
if [[ $UID -eq 0 ]]; then
 
389
    echo "Removing unneeded temporary files in / ..."
 
390
    sleep 10
 
391
    echo "Just kidding! Had this been for real, your hard drive would be"
 
392
    echo "well on its way to oblivion right now, because you're running as root."
 
393
    echo "I hope you know what you're doing..."
 
394
    sleep 5
 
395
fi
 
396
 
 
397
# ******************************************************************************
 
398
# Platform-specific initialization
 
399
# Determines host platform and configures things accordingly
 
400
# ******************************************************************************
 
401
KERNEL=$(uname)
 
402
if test "$KERNEL" = "Linux"; then
 
403
    # Linux should have /proc/cpuinfo
 
404
    CPU_MODEL=$(awk -F ":" '/model name/ {print $2}' /proc/cpuinfo)
 
405
    CPU_SPEED=$(awk 'BEGIN { IGNORECASE = 1 } /MHz/ { print $4 }' /proc/cpuinfo)
 
406
    # Test for multiple CPUs. If they are available, try to use them.
 
407
    if test $(grep "^processor" /proc/cpuinfo | wc -l) -ge "2"; then
 
408
        MULTIPLE_CPUS=:
 
409
    else
 
410
        MULTIPLE_CPUS=false
 
411
    fi
 
412
elif test "$KERNEL" = "Darwin"; then
 
413
    :
 
414
fi
 
415
 
 
416
 
 
417
# ******************************************************************************
 
418
# Find multiple version kludge
 
419
# ******************************************************************************
 
420
INSTALLED_TOVIDS=$(type -a tovid 2>>/dev/null | awk '{print $NF}' | tr '\n' ' ')
 
421
NUM_INSTALLED=0
 
422
INSTALLED_VERS=""
 
423
INSTALLED_PREFS=""
 
424
 
 
425
# Only count tovids that are different versions
 
426
for tovid in $INSTALLED_TOVIDS; do
 
427
    tovid_PREFIX=$(dirname $tovid)
 
428
    tovid_VERSION=$(grep TOVID_VERSION $tovid_PREFIX/tovid-init | \
 
429
        awk -F '"' '{print $2}')
 
430
    INSTALLED_VERS="$INSTALLED_VERS $tovid_VERSION"
 
431
done
 
432
UNIQ_TOVIDS="$(echo $INSTALLED_VERS | tr ' ' '\n' | uniq)"
 
433
NUM_INSTALLED="$(echo $INSTALLED_VERS | tr ' ' '\n' | uniq | wc -l)"
 
434
 
 
435
# Exit when there is more than one tovid installed
 
436
if test $NUM_INSTALLED -ne 1; then
 
437
    echo "Found $NUM_INSTALLED versions of tovid on your system!"
 
438
    echo "I won't run until there is only one of me :)"
 
439
    echo "Installed versions:"
 
440
    i=1
 
441
    while test $i -le $(echo "$INSTALLED_TOVIDS" | awk '{print NF}'); do
 
442
        tovid_ver=$(echo $INSTALLED_VERS | awk '{print $'$i'}')
 
443
        tovid_path=$(echo $INSTALLED_TOVIDS | awk '{print $'$i'}')
 
444
        printf "   %s (%s)\n" $tovid_ver $tovid_path
 
445
        let "i=i+1"
 
446
    done
 
447
    echo "Exiting..."
 
448
    exit 1
 
449
fi
 
450
 
 
451
 
 
452
# ******************************************************************************
 
453
# tovid home setup
 
454
# ******************************************************************************
 
455
 
 
456
# Make home!
 
457
if ! test -d "$TOVID_HOME"; then
 
458
    mkdir "$TOVID_HOME"
 
459
fi
 
460
 
 
461
# Config file configuration and creation
 
462
CONFIG_FILE=$TOVID_HOME/tovid.config
 
463
 
 
464
if ! test -f $CONFIG_FILE; then
 
465
    CONFIG_CONTENTS=`cat << EOF
 
466
tovid
 
467
# Sample tovid configuration file
 
468
# Each line may have one or more tovid options
 
469
# This file is read EVERY time tovid runs
 
470
# DO NOT COMMENT IN LINE
 
471
 
 
472
# See 'man tovid' for a complete list of options
 
473
 
 
474
# Disc type
 
475
#-dvd
 
476
#-half-dvd
 
477
#-dvd-vcd
 
478
#-vcd
 
479
#-svcd
 
480
#-kvcd
 
481
#-ksvcd
 
482
#-kdvd
 
483
 
 
484
# TV system standard
 
485
#-pal
 
486
#-ntsc
 
487
#-ntscfilm
 
488
EOF`
 
489
    printf "$CONFIG_CONTENTS\n" > "$CONFIG_FILE"
 
490
fi
 
491
 
 
492
# Working directory configuration
 
493
USER_PREFS=$TOVID_HOME/preferences
 
494
 
 
495
# Default working/output directories
 
496
WORKING_DIR=$PWD
 
497
OUTPUT_DIR=$PWD
 
498
 
 
499
# If prefs file exists, read it
 
500
if test -f $USER_PREFS; then
 
501
    eval $(grep -v ^# $USER_PREFS)
 
502
# Otherwise, create a default prefs file
 
503
else
 
504
    PREFS_CONTENTS=`cat << EOF
 
505
# tovid preferences
 
506
# Configures working/output directories for tovid
 
507
#WORKING_DIR=/tmp
 
508
#OUTPUT_DIR=/video/outfiles
 
509
EOF`
 
510
    printf "$PREFS_CONTENTS\n" > "$USER_PREFS"
 
511
fi
 
512
 
 
513
# ******************************************************************************
 
514
# Check for run-time dependencies
 
515
# ******************************************************************************
 
516
 
 
517
    DEP_ERROR_MSG="Please install the above MISSING dependencies and try again. See tovid.wikia.com/wiki/Tovid_dependencies or tovid.org for help."
 
518
 
 
519
    # debian based distros install normalize as
 
520
    # "normalize-audio" rather than "normalize"
 
521
    NORMALIZE="normalize"
 
522
    if ! type -a normalize > /dev/null 2>&1; then
 
523
        if type -a normalize-audio > /dev/null 2>&1; then
 
524
            NORMALIZE="normalize-audio"
 
525
        fi
 
526
    fi
 
527
 
 
528
    # Adding (or removing) dependencies:
 
529
    # Does the dependency belong to an existing depdency group below?
 
530
    #   Yes: add the dependency to the list.
 
531
    #   No:  add another group and fill it with members.
 
532
 
 
533
    # *************************************************************************
 
534
    # Required Dependencies
 
535
    # *************************************************************************
 
536
    core="grep sed md5sum mplayer mencoder mplex mpeg2enc yuvfps yuvdenoise ppmtoy4m mp2enc jpeg2yuv ffmpeg"
 
537
 
 
538
    # *************************************************************************
 
539
    # Optional Dependencies
 
540
    # *************************************************************************
 
541
    # Optional dependencies are grouped according to the functionality they
 
542
    # bring to tovid: menu creation, DVD creation, (S)VCD creation, and
 
543
    # post-processing.
 
544
 
 
545
    # -------------------------------------------------------------------------
 
546
    # ImageMagick components
 
547
    magick="composite convert"
 
548
 
 
549
    # -------------------------------------------------------------------------
 
550
    # dvdauthor compononets
 
551
    # (note: growisofs is NOT distributed with dvdauthor, but for tovid's
 
552
    # purposes, it fits in the same catagory, as it burns DVDs!)
 
553
    dvd="spumux dvdauthor growisofs"
 
554
 
 
555
    # -------------------------------------------------------------------------
 
556
    # vcdimager components
 
557
    # (note: cdrdao is NOT distributed with vcdimager, but for tovid's
 
558
    # purposes, it fits in the same catagory, as it burns (S)VCDs!)
 
559
    vcd="vcdxbuild cdrdao"
 
560
 
 
561
    # -------------------------------------------------------------------------
 
562
    # transcode components
 
563
    transcode="tcprobe tcrequant"
 
564
 
 
565
    # -------------------------------------------------------------------------
 
566
    # Plugin tools
 
567
    plugins="sox $NORMALIZE"
 
568
 
 
569
    # -------------------------------------------------------------------------
 
570
    # todisc dependencies
 
571
    todisc_deps="$magick mogrify spumux dvdauthor transcode sox"
 
572
 
 
573
    # Quit and complain if ANY core dependency is missing.
 
574
    assert_dep "$core" "You are missing CORE tovid dependencies!"
 
575
 
 
576
# End tovid-init