Ryan Rueger

ryan@rueg.re / picture / key / home
aboutsummaryrefslogtreecommitdiff
path: root/combine-gpx
blob: a440e299f0e69046163e54b8c4d69cb5032c3a40 (plain) (blame)
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
#!/bin/bash

combine-gpx.log() {
    1>&2 echo "combine-gpx: $*"
}

if [[ "$1" =~ ""|-h|--help ]]
then
    combine-gpx.log "Combine GPX files (in order) and print to stdout"
    combine-gpx.log "Usage: combine-gpx <FILE 1> <FILE 2> [FILE N ...]"
    exit 2
fi

USABLE_FILES=()

for FILE in "$@"
do
    if [[ ! -r "$FILE" ]]
    then
        combine-gpx.log "[WARNING] Cannot read file '$FILE' skipping."
    else
        USABLE_FILES+=("$FILE")
    fi
done

if ((${#USABLE_FILES[@]} == 0))
then
    combine-gpx.log "[Error] None of the supplied filenames are readable files."
    exit 1
fi

# First get headers from the first file
awk '{if ($0 ~ /trkpt/) {exit} else {print}}' "${USABLE_FILES[0]}"

# Now get the trackpoints from all the usable files
for FILE in "${USABLE_FILES[@]}"
do
    awk '/trkpt/,/\/trkpt/' "$FILE"
done

# Finally, get footers from the final file
# (These should be identical to those of the first file)
tac "${USABLE_FILES[${#USABLE_FILES[@]}-1]}" \
    | awk '{if ($0 ~ /trkpt/) {exit} else {print}}' \
    | tac