summaryrefslogtreecommitdiffstats
path: root/burncd
blob: e8d503fd5b09abf04e77fc097b4340fcc74c5c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#! /bin/bash

CYAN="\033[3;36m"
RED="\033[3;31m"
RESET="\033[0m"

READ_SPEED=52
WRITE_SPEED=40
MOUNT='/media/cd'
# DEVICES=`ls -1l /dev/cdr* | gawk '{ print $8; }'`
DEVICE='/dev/cdrom'
WAV_TMP='WAV_tmp'
ISO_TMP='ISO_tmp'
NBR=1
DVD=0
VERIFY=0
VERSION="1.1"

# set files and directory mode to usuall values
# allow full 31 characters filename
# Generate Joliet directory records
ISO_OPTS='-input-charset iso8859-15 -r -l -J'

# Disk At Once == Session At Once
BURN_WAV_OPTS='-v -eject -pad -dao -audio *.wav'
BURN_ISO_OPTS='-v -eject -dao -data '


# TODO	MOUNT && DEVICE
#
#	cdrecord -scanbus
#
#	doesn't work => multi lines ...
#	`cat /etc/fstab | grep cdrom | awk '{ if ($1 != "") print $2; else print "N" }'`
#
#	catch trap SIGINT => clean
#

BIN=`basename $0`

REC=0
REC_2=1
MP3=2
MP3_2=3
MPC=4
MPC_2=5
OGG=6
CD2W=7
MKISO=8
SOX=9
CMP=10
MY_BIN=(
    `which cdrecord 2>/dev/null || echo 'N'`
    `which dvdrecord 2>/dev/null || echo 'N'`
    `which mpg321 2>/dev/null || echo 'N'`
    `which mpg123 2>/dev/null || echo 'N'`
    `which mppdec 2>/dev/null || echo 'N'`
    `which mppdec-static 2>/dev/null || echo 'N'`
    `which oggdec 2>/dev/null || echo 'N'`
    `which cdda2wav 2>/dev/null || echo 'N'`
    `which mkisofs 2>/dev/null || echo 'N'`
    `which sox 2>/dev/null || echo 'N'`
    `which cmp 2>/dev/null || echo 'N'`
)

verify() {
    if [ "${MY_BIN[$1]}" = "N" ]; then return 1; fi
}

error() {
    echo -e "${RED}$@ => exit 1${RESET} (temporary files may still exists)." && exit 1;
}


mktmp() {
    if test -d $1; then error "$1 already exists"; fi
    echo "mkdir $1" && mkdir $1 2>/dev/null || error "unable to mkdir $1"
}

need_one() {
    GET=""
    if verify $1 ; then
        GET=$1
    else
        if ! verify $2 ; then error $3; fi
        GET=$2
    fi
    GET=${MY_BIN[$GET]}
}

mp3_to_wav() {
    need_one $MP3 $MP3_2 "neither mpg321 nore mpg123 are available"
    for i in *.MP3; do mv $i `basename $i .MP3`.mp3 2>/dev/null; done
    for i in *.mp3; do mv $i `echo $i | tr ' ' '_'` 2>/dev/null; done
    if verify $SOX; then # convert to 44100 signed linear 16-bit double channel wav file
	#for i in *.mp3; do ${GET} -s $i | ${MY_BIN[$SOX]} -t raw -r 44100 -u -c 2 - -t wav ${WAV_TMP}/`basename $i .mp3`.wav  || error; done
	for i in *.mp3; do ${MY_BIN[$SOX]} ${i} ${WAV_TMP}/`basename $i .mp3`.wav  || error; done
    else
	for i in *.mp3; do ${GET} -w ${WAV_TMP}/`basename $i .mp3`.wav $i || error; done
    fi
    #for i in *.mp3; do mplayer -vo null -ao pcm:file=${WAV_TMP}/`basename $i .mp3`.wav $i || error; done
}

mpc_to_wav() {
    need_one $MPC $MPC_2 "neither mppcdec nore mppcdec-static are available"
    for i in *.MPC; do mv $i `basename $i .MPC`.mpc 2>/dev/null; done
    for i in *.mpc; do mv $i `echo $i | tr ' ' '_'` 2>/dev/null; done
    for i in *.mpc; do ${GET} $i ${WAV_TMP}/`basename $i .mpc`.wav || error; done
}

ogg_to_wav() {
    if ! verify $OGG ; then error "oggdec unavailable"; fi
    for i in *.OGG; do mv $i `basename $i .OGG`.ogg 2>/dev/null; done
    for i in *.ogg; do mv $i `echo $i | tr ' ' '_'` 2>/dev/null; done
    for i in *.ogg; do ${MY_BIN[${OGG}]} $i -o ${WAV_TMP}/`basename $i .ogg`.wav || error; done
}


cd_to_wav() {
    if ! verify $CD2W ; then error "cdda2wav unavailable"; fi
    # -P 0 : initial number of overlap sectors for jitter correction
    # -n 75 : read 75 sectors per request
    # -B copy each track info in a separate file
    ${MY_BIN[$CD2W]} dev=${DEVICE} speed=${READ_SPEED} -v all cddb=0  -P 0 -n 75 -B -O wav || error "cdda2wav failure"
}

iso_from_cd() {
    if ! verify $MKISO ; then error "mkisofs unavailable"; fi
    mount -t iso9660 -o ro ${DEVICE} ${MOUNT} || error "unable to mount ${DEVICE} on ${MOUNT}"
    ${MY_BIN[$MKISO]} ${ISO_OPTS} -o ${ISO_TMP} ${MOUNT} || error "mkisofs failure"
    umount ${MOUNT}
}

iso_from_files() {
    echo "${MY_BIN[$MKISO]} ${ISO_OPTS} -o ${ISO_TMP} $@"
    ${MY_BIN[$MKISO]} ${ISO_OPTS} -o ${ISO_TMP} $@ || error "mkisofs failure"
}

burn_wav() {
    if [ $DVD -eq 1 ]; then
        need_one $REC_2 $REC_2 "dvdrecord is not available"
    else
        need_one $REC $REC_2 "neighter cdrecord or dvdrecord is available"
    fi
    while [ $NBR -gt 0 ]; do
        need_cd
        echo "$GET dev=${DEVICE} speed=${WRITE_SPEED} ${OVERBURN} ${BURN_WAV_OPTS} ${INFO}"
        $GET dev=${DEVICE} speed=${WRITE_SPEED} ${OVERBURN} ${BURN_WAV_OPTS} ${INFO} || error "cdrecord failure"
        let NBR--
    done
}

burn_iso() {
    if [ $DVD -eq 1 ]; then
        need_one $REC_2 $REC_2 "dvdrecord is not available"
    else
        need_one $REC $REC_2 "neighter cdrecord or dvdrecord is available"
    fi
    while [ $NBR -gt 0 ]; do
        need_cd
        echo "$GET dev=${DEVICE} speed=${WRITE_SPEED} ${OVERBURN} ${BURN_ISO_OPTS} ${ISO_TMP}"
        $GET dev=${DEVICE} speed=${WRITE_SPEED} ${OVERBURN} ${BURN_ISO_OPTS} ${ISO_TMP} || error "cdrecord failure"
        let NBR--
    done
}

eject() {
    need_one $REC $REC_2 "neighter cdrecord or dvdrecord is available"
    $GET dev=${DEVICE} -eject
}

clean() {
    echo "cleanning" && rm -fr ${ISO_TMP} ${WAV_TMP} 2>/dev/null
}

need_cd() {
    echo ""
    echo "would you please insert one of those marvelous recordable compat discs and hit ENTER, thanks a lot !"
    echo  ""
    read NOPE
}

usage (){
    echo "usage : $BIN  (3|w|m|o|a|c|f|i|I|V) [-Od:D:r:w:t:i:n:]"
    echo " burn from :"
    echo "              3 => mp3 files"
    echo "              w => wav files"
    echo "              m => mpc files"
    echo "              o => ogg files"
    echo "              a => audio cd"
    echo "              c => data cd"
    echo "              f => files"
    echo "              i => iso file"
    echo "              I => create iso from CD/DVD"
    echo "              V => print version and exit"
    echo ""
    echo " options  :"
    echo "              d => burn a dvd"
    echo "              o => overburn"
    echo "              m => cdrom mounting moint   - default : ${MOUNT}"
    echo "              e => cdrom device           - default : ${DEVICE}"
    echo "              r => reading speed          - default : ${READ_SPEED}"
    echo "              w => writing speed          - default : ${WRITE_SPEED}"
    echo "              t => temporary directory    - default : ${WAV_TMP}"
    echo "              i => temporary iso file     - default : ${ISO_TMP}"
    echo "              n => n copies               - default : 1"
    echo "              v => verify burned files    - default : 0"
    exit 1
}
version () {
    echo " $BIN $VERSION"
    echo ""
    echo " Copyright (C) Jeremy Zurcher 2004-2007"
    echo " This is free software; There is NO warranty; "
    echo " not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
    exit 0
}

with_options() {
    OPTERR=1
    while [ 1 ]; do
        getopts "dom:e:r:w:t:i:n:v" OPT
        if test $? -eq 1; then break; fi
        case $OPT in
            d)
            DVD=1
            ;;
            o)
            OVERBURN="-overburn"
            ;;
            m)
            MOUNT=$OPTARG
            ;;
            e)
            DEVICE=$OPTARG
            ;;
            r)
            READ_SPEED=$OPTARG
            ;;
            w)
            WRITE_SPEED=$OPTARG
            ;;
            t)
            WAV_TMP=$OPTARG
            ;;
            i)
            ISO_TMP=$OPTARG
            ;;
            n)
            NBR=$OPTARG
            ;;
            v)
            VERIFY=1
            ;;
            *)
            exit 1
            ;;
        esac
    done
}

change_option() {
    echo -n "new value for $1 : "
    read RET
}

interactive() {
    OPTIONS="mp3 mpc ogg wav files audio_cd data_cd iso ISO quit"
    select opt in $OPTIONS; do
	if [ "$opt" = mp3 ]; then choice="3" && break; fi
	if [ "$opt" = mpc ]; then choice="m" && break; fi
	if [ "$opt" = ogg ]; then choice="o" && break; fi
	if [ "$opt" = wav ]; then choice="w" && break; fi
	if [ "$opt" = files ]; then choice="f" && break; fi
	if [ "$opt" = audio_cd ]; then choice="a" && break; fi
	if [ "$opt" = data_cd ]; then choice="c" && break; fi
	if [ "$opt" = iso ]; then choice="i" && break; fi
	if [ "$opt" = ISO ]; then choice="I" && break; fi
	if [ "$opt" = quit ]; then exit 0; fi
    done
    OPTIONS="dvd overburn mount_point device read_speed write_speed wav_tmp iso_tmp copies verify done quit"
    while [ 1 ]; do
        echo
        echo -e "${RED}OPTIONS :  ${RESET}"
        echo "   burnd a  dvd  :  $DVD"
        echo "   overburn      :  $OVERBURN"
        echo "   mount_point   :  $MOUNT"
        echo "   device        :  $DEVICE"
        echo "   read_speed    :  $READ_SPEED"
        echo "   write_speed   :  $WRITE_SPEED"
        echo "   wav_tmp       :  $WAV_TMP"
        echo "   iso_tmp       :  $ISO_TMP"
        echo "   copies        :  $NBR"
        echo "   verify        :  $VERIFY"
        echo
        echo -e "${CYAN}which options do you want to change ?${RESET}"
        select opt in $OPTIONS; do
            if [ "$opt" = overburn ]; then
                if [ "$OVERBURN" = " -overburn" ]; then
                    OVERBURN="";
                else
                    OVERBURN=" -overburn";
                fi
                break;
	    fi
	    if [ "$opt" = dvd ]; then DVD=1 && break; fi
            if [ "$opt" = mount_point ]; then change_option "mount_point" && MOUNT=$RET && break; fi
            if [ "$opt" = device ]; then change_option "device" && DEVICE=$RET && break; fi
            if [ "$opt" = read_speed ]; then change_option "read_speed" && READ_SPEED=$RET && break; fi
            if [ "$opt" = write_speed ]; then change_option "write_speed" && WRITE_SPEED=$RET && break; fi
            if [ "$opt" = wav_tmp ]; then change_option "wav_tmp" && WAV_TMP=$RET && break; fi
            if [ "$opt" = iso_tmp ]; then change_option "iso_tmp" && ISO_TMP=$RET && break; fi
            if [ "$opt" = copies ]; then change_option "copies" && NBR=$RET && break; fi
            if [ "$opt" = verify ]; then
                if test "$VERIFY" == "1" ; then
                    VERIFY=0
                else
                    VERIFY=1
                fi
                break
            fi
            if [ "$opt" = done ]; then break; fi
            if [ "$opt" = quit ]; then exit 0; fi
        done
        if [ "$opt" = done ]; then break; fi
    done
}

int_handler () {
    echo "SIGINT caught !!"
    echo "cleaning..."
    clean
    exit 1
}

if [ ${UID} -ne 0 ]; then
    if [ "$1" = "V" ]; then
        version
    fi
    echo "$BIN says : you must be root" && exit 1;
fi

trap int_handler SIGINT

need_one $REC $REC_2 "neighter cdrecord or dvdrecord is available"

if [ "${0##*/}" = "burncdi" ]; then
    interactive
else
    if [ $# -lt 1 ]; then usage; fi
    choice=$1; shift;
    with_options $@
    let OPTIND--		# eat choice
    # eat options
    while [ $OPTIND -gt 0 ]; do
        let OPTIND--
        shift;
    done
fi

case $choice in

    3)
    #echo "burn mp3 files"
    mktmp $WAV_TMP
    mp3_to_wav
    cd $WAV_TMP
    burn_wav
    cd ..
    ;;

    m)
    #echo "burn mpc files"
    mktmp $WAV_TMP
    mpc_to_wav
    cd $WAV_TMP
    burn_wav
    cd ..
    ;;

    o)
    #echo "burn ogg files"
    mktmp $WAV_TMP
    ogg_to_wav
    cd $WAV_TMP
    burn_wav
    cd ..
    ;;

    w)
    #echo "burn wav files"
    burn_wav
    ;;

    f)
    #echo "burn data files"
    if [ $# -lt 1 ]; then error "file missing"; fi
    iso_from_files $@
    burn_iso
    # TODO VERIFY files
    ;;

    a)
    #echo "copy audio cd"
    mktmp $WAV_TMP
    cd $WAV_TMP
    cd_to_wav
    eject
    INFO=" -useinfo -text *.inf"
    burn_wav
    cd ..
    ;;

    c)
    #echo "copy data cd"
    iso_from_cd
    eject
    burn_iso
    # TODO VERIFY files
    ;;

    i)
    #echo "burn an iso"
    if [ $# -lt 1 ]; then error "file missing"; fi
    ISO_TMP=$1
    burn_iso
    # TODO VERIFY files
    exit 0     # don't remove ISO file
    ;;

    I)
    #echo "copy data cd"
    iso_from_cd
    eject
    exit 0     # don't remove ISO file
    ;;

    V)
    version
    ;;

    *)
    usage
    ;;
esac

clean