-
Notifications
You must be signed in to change notification settings - Fork 31
/
CopyToSD
executable file
·154 lines (127 loc) · 2.07 KB
/
CopyToSD
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
#!/bin/sh
# copy files to SD card
ERROR()
{
echo error: $*
exit 1
}
USAGE()
{
[ -z "$1" ] || echo error: $*
echo usage $(basename "$0") '[--sdcard=/media/disk]' '<files...>'
exit 1
}
CopyFiles()
{
local item mountpoint src dst
mountpoint=$1; shift
for item in "$@"
do
item="${item}:"
src="${item%%:*}"
item="${item#*:}:"
dst="${item%%:*}"
item="${item#*:}:"
cp "${src}" "${mountpoint}/${dst}"
done
ls -l "${mountpoint}"
}
SpinCount=0
Spinner()
{
SpinCount=$((${SpinCount} + 1))
case "${SpinCount}" in
1)
printf -- '|\x08'
;;
2)
printf -- '/\x08'
;;
3)
printf -- '-\x08'
;;
4)
printf -- '\\\x08'
SpinCount=0
;;
esac
}
InsertCard()
{
local dev mountpoint fstype
dev="$1"; shift
mountpoint="$1"; shift
fstype=vfat
umount "${mountpoint}" "${dev}" 2> /dev/null
echo -n 'Insert SD Card '
while [ ! -b "${dev}" ]
do
Spinner
sleep 1
done
echo
fsck.vfat -a "${dev}"
mount -t "${fstype}" "${dev}" "${mountpoint}" || ERROR mount failed
rm -f "${mountpoint}"/fsck*.rec
df -h "${mountpoint}"
}
RemoveCard()
{
local dev mountpoint fstype
dev="$1"; shift
mountpoint="$1"; shift
umount "${mountpoint}" || ERROR umount failed
sleep 1
echo -n ""
echo 'SD Card Unmounted'
echo
}
main()
{
dev=/dev/sdb1
mountpoint=/mnt
InsertCard "${dev}" "${mountpoint}"
CopyFiles "${mountpoint}" "$@"
df -h "${mountpoint}"
RemoveCard "${dev}" "${mountpoint}"
}
sdcard=''
# if a mounted sdcard is given then no need for sudo
while [ $#0 -gt 0 ]
do
case "$1" in
--sdcard=*)
sdcard="${1#*=}"
;;
--)
shift
break
;;
-*)
USAGE invalid option: $1
;;
*)
break
;;
esac
shift
done
if [ -d "${sdcard}" ]
then
CopyFiles "${sdcard}" "$@"
exit
fi
# nee sudo to mount
if [ -z "${SUDO_UID}" -o -z "${SUDO_GID}" ]
then
echo re-run with sudo
sudo -K
exec sudo "$0" "$@"
exit 1
fi
if [ -n "${SUDO_UID}" -a -n "${SUDO_GID}" ]
then
main "$@"
else
echo sudo failed
fi