summaryrefslogtreecommitdiff
path: root/scripts/dvdarchive.sh
blob: f7bf0662c2996d5e2b2831a5a03d33eb0f752fb7 (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
#!/bin/bash
#
# Version 1.5 2006-04-17
#
# Exitcodes:
#
# exit 0 - no error
# exit 1 - mount/umount error
# exit 2 - no dvd in drive
# exit 3 - wrong dvd in drive / recording not found
# exit 4 - error while linking [0-9]*.vdr
#
# Errorhandling/Symlinking: vejoun@vdr-portal
#
# For dvd-in-drive detection download isodetect.c, compile it and put it into the PATH,
# usually /usr/local/bin/
#
# Tools needed: mount, awk, find, test
# Optional tools: isodetect

#<Configuration>

MOUNTCMD="/usr/bin/sudo /bin/mount"
UMOUNTCMD="/usr/bin/sudo /bin/umount"

MOUNTPOINT="/media/cdrom" # no trailing '/'!

# Eject DVD for exit-codes 2 and 3 (no or wrong dvd). 1 = yes, 0 = no.
EJECTWRONG=0
# Eject DVD after unmounting. 1 = yes, 0 = no.
EJECTUMOUNT=0

#</Configuration>

DEVICE="$(awk '( $1 !~ /^#/ ) && ( $2 == "'$MOUNTPOINT'" ) { printf("%s", $1); exit; }' /etc/fstab)" # dvd-device, used by isodetect if exists

REC="$2"
NAME="$3"

call() {
	echo -e "\nScript $0 needs three parameters for mount and two for umount. The first must be mount or umount, the second is the full path.\n"
	echo -e "Only for mounting the script needs a third parameter, the last part of the recording path.\n"
	echo -e "Example: dvdarchive.sh mount '/video1.0/Music/%Riverdance/2004-06-06.00:10.50.99.rec' '2004-06-06.00:10.50.99.rec'\n"
	echo -e "Example: dvdarchive.sh umount '/video1.0/Music/%Riverdance/2004-06-06.00:10.50.99.rec'\n"
}

[ "$1" = "mount" -o "$1" = "umount" ] || { call; exit 10; }
[ -z "$2" ] && { call; exit 10; }
[ "$1" = mount -a -z "$3" ] && { call; exit 10; }

case "$1" in
mount)
	# check if dvd is in drive, only if isodetect exists
	if [ -n "$(which isodetect)" -a -n "$DEVICE" ]; then
		isodetect -d "$DEVICE" >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "no dvd in drive"
			[ $EJECTWRONG -eq 1 ] && { eject "$DEVICE"; }
			exit 2
		fi
	fi
	# check if not mounted
	$MOUNTCMD | grep "$MOUNTPOINT" >/dev/null && { echo "dvd already mounted"; exit 1; }
	# mount dvd
 	$MOUNTCMD "$MOUNTPOINT" || { echo "dvd mount error"; exit 1; }
 	# is mounted?
	# find recording on dvd
	DIR="$(find "${MOUNTPOINT}/" -name "$NAME")"
	# if not found, umount
	if [ -z "$DIR" ]; then
		$UMOUNTCMD "$MOUNTPOINT" || { echo "dvd umount error"; exit 1; }
		echo "wrong dvd in drive / recording not found on dvd"
		[ $EJECTWRONG -eq 1 ] && { eject "$DEVICE"; }
		exit 3
	fi
	# link index.vdr if not exist
	if [ ! -e "${REC}/index.vdr" ]; then
		cp -s "${DIR}/index.vdr" "${REC}/"
	fi
	# link [0-9]*.vdr files
	cp -s "${DIR}/"[0-9]*.vdr "${REC}/"
	# error while linking [0-9]*.vdr files?
	if [ $? -ne 0 ]; then
		# umount dvd bevor unlinking
		$UMOUNTCMD "$MOUNTPOINT" || { echo "dvd umount error"; exit 1; }
		# unlink broken links
		for LINK in "${REC}/"*.vdr; do
			if [ -L "$LINK" -a ! -s "$LINK" ]; then
				rm "$LINK"
			fi
		done
		echo "error while linking [0-9]*.vdr"
		exit 4
	fi
	;;
umount)
	# check if dvd is mounted
	$MOUNTCMD | grep "$MOUNTPOINT" >/dev/null || { echo "dvd not mounted"; exit 1; }
	# is mounted?
	# umount dvd bevor unlinking
	$UMOUNTCMD "$MOUNTPOINT" || { echo "dvd umount error"; exit 1; }
	# unlink broken links
	for LINK in "${REC}/"*.vdr; do
		if [ -L "$LINK" -a ! -s "$LINK" ]; then
			rm "$LINK"
		fi
	done
	[ $EJECTUMOUNT -eq 1 ] && { eject "$DEVICE"; }
	;;
     *)
        echo -e "\nWrong action."
        call
        ;;
esac

exit 0