4
# Part of the tovid suite
5
# =======================
6
# Define global (suite-wide) functions and variables
9
# Project homepage: http://www.tovid.org
12
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
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
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.
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:
29
# http://www.gnu.org/licenses/gpl.txt
32
# ******************************************************************************
33
# ******************************************************************************
39
# ******************************************************************************
40
# ******************************************************************************
42
# Reset input field separator to default
45
# Exit with error on undeclared variables
49
TOVID_VERSION="@VERSION@"
51
# String used to separate blocks of output
52
SEPARATOR="========================================================="
54
TOVID_HOME="$HOME/.tovid"
55
TOVID_HOME_PAGE="http://www.tovid.org"
56
TOVID_FORUMS="http://www.createphpbb.com/tovid/"
59
# ******************************************************************************
60
# ******************************************************************************
66
# ******************************************************************************
67
# ******************************************************************************
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()
76
[[ $1 && $1 =~ ^[-+]?[0-9]*\(\.[0-9]+\)?$ ]]
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
# ******************************************************************************
89
TEMPDIR="$BASENAME.$NUM"
90
[[ -n $2 && $2 = "nocreate" ]] && CREATE=false
91
while test -d "$BASENAME.$NUM"; do
94
$CREATE && mkdir "$BASENAME.$NUM"
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
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")
111
# ex: verify $CMD_LN_OPT range "0 10"
112
# will return ":" (true) if 0 <= $CMD_LN_OPT <= 10
113
# ******************************************************************************
117
VERIFY_TEST_TYPE="$2"
118
case $VERIFY_TEST_TYPE in
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
133
if echo "$VERIFY_SET" | grep -q -w "$VERIFY_VAR"; then
142
# ******************************************************************************
143
# Print a pretty (wrapped) notice message.
144
# Args: $@ == text string containing the message
145
# ******************************************************************************
148
echo -e "$@" | fold -s -w ${COLUMNS:-80}
151
# ******************************************************************************
152
# Print error message, then exit.
153
# Args: $@ == text string containing error message
154
# ******************************************************************************
155
function exit_with_error()
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()
169
HMS_HOURS=$(expr $1 / 3600)
170
HMS_MINS=$(expr \( $1 % 3600 \) / 60)
171
HMS_SECS=$(expr $1 % 3600 % 60)
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}"
177
echo "${HMS_HOURS}:${HMS_MINS}:${HMS_SECS}"
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()
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")
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()
209
# Hackish list-parsing
210
while test $# -gt 0 && test x"${1:0:1}" != x"-"; do
214
# Do not skip past the next argument
215
if test $# -gt 0 && test x"${1:0:1}" = x"-";then
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()
232
FOP_BASENAME=$(basename "$FOP_OUTFILE")
233
if test -n "$3"; then
234
FOP_BASENAME_NAME="$3"
236
FOP_BASENAME_NAME="$FOP_BASENAME"
239
# A dumb little animation toggle
244
# Wait for input file to appear
245
# After a 30-second timeout, exit gracefully
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... "
254
printf "Waiting $CUR_TIME seconds for output file \"$FOP_BASENAME_NAME\" to appear...\r"
256
CUR_TIME=$(expr $CUR_TIME - 1)
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\""
268
FOP_CUR_SIZE=$(du -b "$FOP_OUTFILE" | awk '{print $1}')
270
# Keep looping until outfile stops getting bigger
271
while test "$FOP_CUR_SIZE" -gt "$FOP_LAST_SIZE"; do
272
# Display a changing line
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"
286
# Doze a bit to let the file size increase
287
# (SLEEP_TIME defaults to 1s if unset)
288
sleep ${SLEEP_TIME-"1s"}
290
FOP_LAST_SIZE=$FOP_CUR_SIZE
291
FOP_CUR_SIZE=$(du -b "$FOP_OUTFILE" | awk '{print $1}')
296
# ******************************************************************************
297
# Check to see if a dependency group exists, quit if missing
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
303
# Ex: assert_dep "$dvd" "You cannot make DVDs without these!"
304
# assert_dep "rice sake fish" "You cannot make dinner!"
306
# ******************************************************************************
307
function assert_dep()
312
# Determine if any group member is missing
315
if ! type -a $dep >> /dev/null 2>&1; then
317
printf "%-13s %s\n" " $dep" "MISSING!"
322
# Quit if any group member is missing
325
precho "$HELP $DEP_ERROR_MSG"
332
# ******************************************************************************
333
# See what filesystem a directory is in
335
# $1 = directory to find filesystem type
338
# FS_TYPE=$(get_filesystem "$WORK_DIR")
339
# ******************************************************************************
340
function get_filesystem()
343
PARTITION=$(df . | tail -n 1 | awk '{print $1}')
344
FILESYSTEM=$(mount | grep "$PARTITION" | awk '{print $5}')
348
# ******************************************************************************
349
# Do floating point math with bc
351
# $1 = The math operation to perfrom, in a quoted string
354
# ANSWER=$(bc_math "$NUM1 + $NUM2")
355
# ******************************************************************************
358
echo "scale=2; $1" | bc -l
362
# ******************************************************************************
363
# Countdown N seconds, then return
364
# N defaults to 5 seconds if no args to function
365
# usage: countdown [SECONDS]
366
# ******************************************************************************
369
[[ -n $1 ]] && LAST=$1 || LAST=5
371
for _CNTR in $(sort -nr <<< "$count")
373
echo -n -e " in $_CNTR seconds...\r";
378
# ******************************************************************************
379
# ******************************************************************************
382
# EXECUTED INITIALIZATION
385
# ******************************************************************************
386
# ******************************************************************************
388
if [[ $UID -eq 0 ]]; then
389
echo "Removing unneeded temporary files in / ..."
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..."
397
# ******************************************************************************
398
# Platform-specific initialization
399
# Determines host platform and configures things accordingly
400
# ******************************************************************************
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
412
elif test "$KERNEL" = "Darwin"; then
417
# ******************************************************************************
418
# Find multiple version kludge
419
# ******************************************************************************
420
INSTALLED_TOVIDS=$(type -a tovid 2>>/dev/null | awk '{print $NF}' | tr '\n' ' ')
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"
432
UNIQ_TOVIDS="$(echo $INSTALLED_VERS | tr ' ' '\n' | uniq)"
433
NUM_INSTALLED="$(echo $INSTALLED_VERS | tr ' ' '\n' | uniq | wc -l)"
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:"
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
452
# ******************************************************************************
454
# ******************************************************************************
457
if ! test -d "$TOVID_HOME"; then
461
# Config file configuration and creation
462
CONFIG_FILE=$TOVID_HOME/tovid.config
464
if ! test -f $CONFIG_FILE; then
465
CONFIG_CONTENTS=`cat << EOF
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
472
# See 'man tovid' for a complete list of options
489
printf "$CONFIG_CONTENTS\n" > "$CONFIG_FILE"
492
# Working directory configuration
493
USER_PREFS=$TOVID_HOME/preferences
495
# Default working/output directories
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
504
PREFS_CONTENTS=`cat << EOF
506
# Configures working/output directories for tovid
508
#OUTPUT_DIR=/video/outfiles
510
printf "$PREFS_CONTENTS\n" > "$USER_PREFS"
513
# ******************************************************************************
514
# Check for run-time dependencies
515
# ******************************************************************************
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."
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"
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.
533
# *************************************************************************
534
# Required Dependencies
535
# *************************************************************************
536
core="grep sed md5sum mplayer mencoder mplex mpeg2enc yuvfps yuvdenoise ppmtoy4m mp2enc jpeg2yuv ffmpeg"
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
545
# -------------------------------------------------------------------------
546
# ImageMagick components
547
magick="composite convert"
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"
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"
561
# -------------------------------------------------------------------------
562
# transcode components
563
transcode="tcprobe tcrequant"
565
# -------------------------------------------------------------------------
567
plugins="sox $NORMALIZE"
569
# -------------------------------------------------------------------------
570
# todisc dependencies
571
todisc_deps="$magick mogrify spumux dvdauthor transcode sox"
573
# Quit and complain if ANY core dependency is missing.
574
assert_dep "$core" "You are missing CORE tovid dependencies!"