#!/bin/bash # $HeadURL: https://svn.pasta.freemyip.com/main/miniade/trunk/bin/nop-sh $ $LastChangedRevision: 10133 $ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Configurable stuff DELAY=5 # Other globals main() { local MY_ARGS SIMULATE NEW_FS FS F #miniade_internal "in anticipation of the need to update my holiday preparation procedure web pages regarding the invocation of this script, this script is disabled. (besides the removal of this error call, no script changes should be required. only changes to my holiday preparation procedure web pages are required)" # Defaults for options # Process options miniade_process_options --help-handler=help MY_ARGS "$@" && set -- "${MY_ARGS[@]}" # Process arguments # Holiday directories like 20YYWW-whatever don't have tracks yet, but they were likely cloned # from real holiday directories, which is correct in order to ensure that they get correctly # populated. if [ $# = 0 ]; then miniade_warning "no tracks provided; nothing generated" return 0 fi FS=( "$@" ) # Sanity checks and derivations NEW_FS=() for F in "${FS[@]}"; do if [[ $F =~ ^2[0-9]{7}.gpx$ ]]; then NEW_FS+=( $F ) elif [[ $F =~ ^2[0-9]{7}-pt[1-9].gpx$ ]]; then NEW_FS+=( $F ) elif [[ $F =~ ^2[0-9]{7}-daytrip.gpx$ ]]; then miniade_warning "$F: skipping daytrip" else miniade_error "$F: non-conformant name" fi done FS=( "${NEW_FS[@]}" ) miniade_get_simulate SIMULATE $SIMULATE || { miniade_warning "you're not working on the only copy of these tracks are you? (hint: press CTRL-C or wait ${DELAY}s)"; sleep ${DELAY}; } # The checks below (e.g. check_one_track_only) cannot be called in simulate mode, # since the action whose success they are checking has not actually been done. # Guts miniade_info "converting multi-track files to single-track files ..." for F in "${FS[@]}"; do miniade_evaler "perl -0777 -pi -e 's/ *<\\/trk>\\n(?: *\\n)* *\\n *[^\\n]+<\\/name>\\n//msg' \"$F\"" $SIMULATE || check_one_track_only "$F" done miniade_info "converting multi-track-segment files to single-track-segment files ..." for F in "${FS[@]}"; do miniade_evaler "perl -0777 -pi -e 's/ *<\\/trkseg>\\n(?: *.*<\\/extensions>\\n){0,1} *\\n//msg' \"$F\"" $SIMULATE || check_one_track_segment_only "$F" done miniade_info "converting multiple single-track-single-track-segment files into one single-track-single-track-segment file ..." miniade_evaler "gpsbabel -t -i gpx $(for F in "${FS[@]}"; do echo -n " -f \"$F\""; done) -x track,merge,discard -o gpx -F merged.gpx" miniade_evaler "perl -0777 -pi -e 's/ *<\\/trkseg>\\n *\\n//msg' merged.gpx" # That previous steps normally puts each track file into a different track segment, so # we then need to strip those again. $SIMULATE || check_one_track_only merged.gpx $SIMULATE || check_one_track_segment_only merged.gpx miniade_info "simplifying ..." miniade_evaler "gpsbabel -i gpx -f merged.gpx -x simplify,count=2000 -o gpx -F simplified.gpx" # Doesn't hurt to check these things again. $SIMULATE || check_one_track_only simplified.gpx $SIMULATE || check_one_track_segment_only simplified.gpx miniade_info "updating track name ..." # '-m' allows simplified.gpx not to have been generated in simulate mode. [[ $(realpath -m simplified.gpx) =~ .*/([^/]+)/tracks/.* ]] # '${1}' used instead of '$1' as the next word starts with digits so # '$1' becomes '$1202433' or whatever. miniade_evaler "perl -0777 -pi -e \"s/(.*?)(.*?)(<\\\\/name>)/\\\${1}${BASH_REMATCH[1]}\\\$3/s\" simplified.gpx" } help() { local PROGNAME miniade_get_progname PROGNAME echo "Usage: $PROGNAME [ ] ..." exit 0 } check_one_track_only() { local F="$1" [ $(grep -c '' "$F") = 1 ] || miniade_error "$F: non-conformant track count" } check_one_track_segment_only() { local F="$1" [ $(grep -c '' "$F") = 1 ] || miniade_error "$F: non-conformant track segment count" } main "$@"