#!/usr/bin/env bash ####################################################################### # Made by : Ewoud Dronkert # Licence : GNU GPL v3 # Platform : Mac OS X # Requires : bash # Location : ~/bin/ # Name : fittime # Version : 2.2.0 # Date : 2017-04-29 # 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 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