-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgarmin
executable file
·90 lines (77 loc) · 2.34 KB
/
garmin
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
#!/usr/bin/env bash
#######################################################################
# Made by : Ewoud Dronkert
# Licence : GNU GPL v3
# Platform : macOS
# Requires : bash
# Location : ~/bin/
# Name : garmin
# Version : 2.2.0
# Date : 2017-05-09
# Purpose : Copies new .fit files from Garmin device to disk. Ejects
# Garmin device. Align timestamps with file names. Also
# remove DOS 'execute' and macOS extended attributes.
# Parameters : none
# Settings : DST = location of saved Garmin Activity .fit files
# Exit 0 : No errors (but chmod/xattr/touch errors not monitored)
# 1 : DST not found or not a directory
# 2 : No .fit files in DST directory
#######################################################################
# Use backslashes to escape spaces, if any, in the directory name
DEV=/Volumes/GARMIN
SRC="$DEV/Garmin/Activities"
DST=~/Documents/Garmin/Activities
# Directory exists?
if [ -d "$DST" ]; then
echo "Using local directory : $DST"
else
echo "Local FIT-file directory not found: $DST" 1>&2
exit 1
fi
# Device connected?
if [ -d "$SRC" ]; then
echo "Copying new FIT files from Garmin ..."
rsync -a --ignore-existing --info=name "$SRC"/*.fit "$DST" 2>/dev/null
if [ $? -eq 0 ]; then
DSK=$(diskutil info "$DEV" | grep -oP '/dev/disk\d+')
if [ -n "$DSK" ]; then
echo "Ejecting Garmin ..."
diskutil eject "$DSK"
fi
fi
else
echo "Garmin device not connected at $DEV"
fi
# Exit early if no files found
tot=$(\ls "$DST"/*.fit | grep -E '\.fit$' | wc -l | grep -oE '[0-9]+')
if (( tot >= 1 )); then
echo "FIT-files found : $tot"
else
echo "No FIT-files found." 1>&2
exit 2
fi
# Remove macOS extended attributes
xattr "$DST"/*.fit | cut -d':' -f1 | sort | uniq | xargs xattr -c
# Change file mode to RW only for owner
find "$DST"/*.fit -perm +177 -print0 | xargs -0 chmod 600
# Set file modification time according to file name
err=0
adj=0
for f in "$DST"/*.fit; do
# File modification time in touch format
m=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' "$f")
# Much faster than "basename $f"
t=${f:(-23)}
# Remove dashes
t=${t//-/}
# Format as YYYYMMDDhhmm.SS for touch
t=${t:0:12}.${t:12:2}
if [[ "$m" != "$t" ]]; then
(( err++ ))
touch -cfm -t $t "$f" && (( adj++ ))
fi
done
echo "Wrong file time : $err"
echo "Time adjusted : $adj"
open "$DST"
exit 0