-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp-optical.sh
executable file
·117 lines (99 loc) · 2.64 KB
/
cp-optical.sh
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
#!/bin/bash
usage() {
echo -e "Usage: cp-optical.sh [ <options> ]\n"
echo -e " <options> are any of:"
echo -e " -h\t\tprint full usage"
echo -e " -d\t\tpath to destination root"
echo -e " \t\tif not provided, current directory is used"
echo -e " -m\t\tcopy multiple optical discs"
echo -e " -i\t\tinteractive mode during multiple optical disc copy"
}
if [[ "$(uname)" != "Darwin" ]]; then
echo "$0 currently only runs on Mac OS X.\n"
exit 1
fi
OPTIND=1
readopt='getopts $opts opt; rc=$?; [ $rc$opt == 0? ] && exit 1; [ $rc == 0 ] || { shift $[OPTIND - 1]; false; }'
opts=himd:
opt_root_folder=
loop=false
interactive=false
while eval $readopt; do
case "$opt" in
h)
usage
exit 0
;;
d)
opt_root_folder=$OPTARG
if [ ! -d "$opt_root_folder" ]; then
echo -e "Root output folder, $opt_root_folder, does not exist.\n"
exit 1
fi
;;
m)
loop=true
;;
i)
loop=true
interactive=true
;;
esac
done
echo "cp-optical"
while [ : ]; do
printf "Waiting for optical disc to be inserted..."
optical_found=false
while [ "$optical_found" = false ]; do
for disk in $(diskutil list | grep ^/); do
printf "."
diskutil info "$disk" | grep -q Optical >/dev/null 2>&1
[ "$?" = "0" ] && optical_found=true && break
done
printf "."
sleep 3
done
printf "\n"
# Retrieves the first mount point and device node of a CD/DVD/BD.
dev_node=
mnt_point=
vol_name=
for disk in $(diskutil list | grep ^/); do
if diskutil info "$disk" | grep -q Optical; then
dev_node="$disk"
mnt_point=`df | sed -ne "s,^$disk.*\(/Volumes.*\)$,\1,p"`
vol_name=`echo "$mnt_point" | sed "s,^/Volumes/,,"`
break
fi
done
if [ -z "$mnt_point" ]; then
echo -e "\tNo optical disc found.\n"
fi
dst_folder="$vol_name"
if [ -n "$opt_root_folder" ]; then
dst_folder="$opt_root_folder"/"$dst_folder"
else
dst_folder="`pwd`"/"$dst_folder"
fi
shopt -s extglob
dst_folder="${dst_folder//+(\/)//}"
shopt -u extglob
output_format="\t%-20s %s\n"
printf "$output_format" "Mount point:" "$mnt_point"
printf "$output_format" "Volume name:" "$vol_name"
printf "$output_format" "Destination folder:" "$dst_folder"
if [ ! -d "$dst_folder" ]; then
mkdir "$dst_folder"
fi
ditto "$mnt_point" "$dst_folder"
diskutil eject "$mnt_point"
if [ "$loop" != true ]; then
break
fi
if [ "$interactive" = true ]; then
read -p "Copy another disc? [Y|n] " _answer
_answer=${_answer:-Y}
echo ${_answer} | grep -E -i -e '^y$' > /dev/null 2>&1
[ "$?" != "0" ] && break
fi
done