-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-pictures.sh
executable file
·57 lines (53 loc) · 1.27 KB
/
copy-pictures.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
#!/bin/bash
#
# copy the pictures into the photo frame
#
# see also https://askubuntu.com/questions/284224/autorun-a-script-after-i-plugged-or-unplugged-a-usb-device
# settings
# absolute path to the pictures
export SOURCE="/data/share/phone/picture-frame"
# constants
export log="$0.log"
export device="$1"
# for mount
sleep 1
( (
if ! [ -e "$SOURCE" ]; then
echo "source $SOURCE does not exist"
exit
fi
touch "$SOURCE/picture-frame-recognized"
if ! echo "$device" | grep -qE '^sd[a-z][0-9]$'; then
echo "ignoring $device"
exit
fi
(
set -e
cd "`dirname \"$0\"`"
echo "$0" "$@"
# /home/pi/picture-frame/copy-pictures.sh sdc1
date
echo "picture frame attached!"
dir="`mktemp -d`"
echo "created $dir, mounting $device"
if mount "/dev/$device" "$dir"; then
echo "copy '$SOURCE' into '$dir'"
cp -r "$SOURCE"/* "$dir"
echo "copy '$log' into '$dir'"
cp "$log" "$dir"
echo "unmount '$dir' and sync"
umount "$dir"
sync
else
echo "ERROR: could not mount $dev"
fi
# remove only if empty
# see https://stackoverflow.com/a/23122112/1320237
echo "removing $dir"
rmdir "$dir"
)
echo "Exit code: $?"
) 2>&1 ) | tee -a "$log"
if [ -d "$SOURCE" ]; then
cp "$log" "$SOURCE"
fi