aboutsummaryrefslogtreecommitdiff
path: root/mass-ripper
blob: 0e7ceecb9f6a807d70ee0a1b2abb2346b978be76 (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
#!/bin/bash
#
# mass-ripper: sequentially copy CDs and DVDs.
#

# Parameters
BASENAME="`basename $0`"
DRIVE="$1"
ITERATION="$2"
INFO="0"
MOUNT="/media/cdrom"

# Disk drive
if [ -z "$DRIVE" ]; then
  DRIVE="/dev/sr0"
fi

# Iteration
if [ -z "$ITERATION" ]; then
  ITERATION="1"
fi

# Set sudo config
if [ "`whoami`" != 'root' ]; then
  sudo="sudo"
fi

# Greeter
echo "Mass CD/DVD copier, press Ctrl-C to abort..."

# Thanks http://askubuntu.com/questions/197662/how-do-i-auto-copy-cd-contents-on-insertion
while true; do
  # Print just once per disk
  if [ "$INFO" == "0" ]; then
    echo "Waiting for disk #$ITERATION..."
  fi

  # Check if media is available
  #HAS_MEDIA=$(grep $DRIVE /proc/self/mounts)
  HAS_MEDIA=$(udisks --show-info $DRIVE | grep "has media" | cut -d : -f 2 | sed -e 's/ //g')

  # If it doesn't exist, loop until it does with 1 second pause
  if [ "$HAS_MEDIA" == "0" ]; then
    #echo -ne "."
    INFO="1"
    sleep 1
  else
    echo "Got it! Mounting..."
    mkdir -p $MOUNT              || exit 1
    $sudo mount -r $DRIVE $MOUNT || exit 1
    #mkdir -p $ITERATION         || exit 1

    echo "Copying data..."
    cp -vr $MOUNT $ITERATION  || exit 1

    echo "Fixing permissions..."
    find $ITERATION/ -type d -exec chmod 755 {} \;
    find $ITERATION/ -type f -exec chmod 644 {} \;

    echo "Umounting..."
    $sudo umount $MOUNT

    # Eject the CD with suitable pauses to avoid any buffer problems
    echo "Ejecting media..."
    sleep 1
    eject $DRIVE
    sleep 2

    # Increase counter
    let ITERATION++
    INFO="0"
  fi
  # Still looping! Go back and wait for another CD!
done