-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintdcp
145 lines (137 loc) · 3.4 KB
/
printdcp
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
#!/bin/bash
#
# PrintDCP
#
# GNU GPLv3
# (c)2019 Jeff Billings
# Command-line usage
help () {
echo "PrintDCP - automates the process of creating DCP drives"
echo " "
echo "Usage: printdcp [source folder path] [drive device path]"
echo " "
echo " -g --gui Graphical Interface"
echo " -h --help Operating Instructions"
}
# Completion handling
error () {
echo "(Error) "$1
exit 1
}
finish () {
echo "(Finished)"
exit 0
}
# Check for root privileges
root () {
if [ $UID -ne 0 ]; then
error "This script must be run as root."
fi
}
# Check for user cancel
check () {
if [[ $? -eq 1 ]]; then
error "Cancelled by user"
fi
}
# Disk preparation commands
init () {
drive=$1
volume="${drive}1"
echo "Unmounting disk..."
umount $drive* 2>/dev/null
echo "Creating partition..."
parted -s $drive mklabel msdos
parted -s $drive mkpart primary 0% 100%
echo "Waiting for partition..."
while [ ! -e $volume ]; do continue; done
echo "Creating filesystem..."
until format=$(mkfs -t ext2 -I 128 -m 0 -F $volume); do continue; done 2>/dev/null
echo "$format"
echo "Mounting volume..."
until mount=$(su -c "gio mount -d ${volume}" $SUDO_USER); do continue; done 2>/dev/null
echo "$mount"
}
# Disk copying commands
copy () {
drive=$1
source=$2
volume="${drive}1"
mount=$(lsblk $volume -o MOUNTPOINT -n)
echo "Copying DCP, performing checksum, setting permissions..."
sudo -S rsync -a --progress --checksum --chmod 755 $source $mount
}
# Graphical user interface
gui () {
zenity --info \
--title="PrintDCP" \
--width=300 --height=150 \
--icon-name='drive-harddisk' \
--text="This utility prepares DCP drives\n\nBefore continuing:\n1. Connect destination drive\n2. Have access to source DCP" \
2>/dev/null
check
IFS=$'\n'
os=$(lsblk -n -p --output PKNAME $(findmnt / -o SOURCE -n) )
drive=$(lsblk -I 8 -d -n -p -P --output NAME,MODEL,SIZE |
awk -v os="$os" -F '"' '{ if ($2 != os) print $2"\n"$4"\n"$6 }' |
zenity --list \
--title="PrintDCP" \
--width=400 --height=300 \
--text="Select a drive to <b>erase</b> and prepare:" \
--column="Device" --column="Model" --column="Size" \
2>/dev/null )
check
source=$(zenity --file-selection --directory \
--title="Choose source DCP folder ..." \
--filename=$HOME/Desktop/ \
2>/dev/null
)
check
init $drive & PIPED_PID=$!
tail -f /dev/null --pid $PIPED_PID | (
zenity --progress --pulsate --auto-close \
--width=300 --height=150 \
--title="PrintDCP" \
--text="Initializing drive..." \
2>/dev/null || ( kill $PIPED_PID && exit 1 ) );
check
copy $drive $source & PIPED_PID=$!
tail -f /dev/null --pid $PIPED_PID | (
zenity --progress --pulsate --auto-close \
--width=300 --height=150 \
--title="PrintDCP" \
--text="Copying DCP..." \
2>/dev/null || ( kill $PIPED_PID && exit 1 ) );
check
zenity --info \
--width=300 --height=150 \
--title="PrintDCP" \
--text="Drive preparation finished." \
2>/dev/null
}
# Default to help
if [ "$#" -eq 0 ]; then
help
fi
# Parse input
while [ "$#" -gt 0 ]; do case $1 in
-h|--help)
help
shift;;
-g|--gui)
root
gui
finish
shift;;
*)
if [ $# -ne 2 ]; then
error "Invalid arguments"
else
source=$1
drive=$2
root
init $drive
copy $drive $source
finish
fi
esac; shift; done