forked from ednl/shellscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfittime
executable file
·70 lines (59 loc) · 1.85 KB
/
fittime
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
#!/bin/bash
#######################################################################
# Made by : Ewoud Dronkert
# Licence : GNU GPL v3
# Platform : Mac OS X
# Requires : bash
# Location : ~/bin/
# Name : fittime
# Version : 2.1.0
# Date : 2016-07-28
# Purpose : Align timestamp with file name for Garmin .fit files.
# Also remove DOS 'execute' and OS X extended attributes.
# Parameters : none
# Settings : FITDIR = location of Garmin Activity .fit files
# Exit 0 : No errors (but chmod/xattr/touch errors not monitored)
# 1 : FITDIR not found or not a directory
# 2 : No .fit files in FITDIR directory
#######################################################################
# Use backslashes to escape spaces, if any, in the directory name
FITDIR=~/Documents/Garmin/Activities
# Directory exists?
if [ -d "$FITDIR" ]; then
echo "Using directory : $FITDIR"
else
echo "FIT-file directory not found: $FITDIR" 1>&2
exit 1
fi
# Exit early if no files found
tot=$(ls "$FITDIR"/*.fit | wc -l | grep -oE '\d+')
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 "$FITDIR"/*.fit | cut -d':' -f1 | sort | uniq | tr \\n \\0 | xargs -0 xattr -c
# Change file mode to RW only for owner
find "$FITDIR" -depth 1 -name *.fit -perm +177 -print0 | xargs -0 chmod 600
# Set file modification time according to file name
err=0
adj=0
for f in "$FITDIR"/*.fit; do
# File modification time in touch format
m=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' "$f")
# 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"
exit 0