﻿var lineReader = require('line-reader');
var fs = require('fs');

var output = "";
var i = 1;
var trkOpenTagFound = false;

NAMEOPENTAG = "<name>";
NAMECLOSETAG = "</name>";
TRACKOPENTAG = "<trk>";
TRACKCLOSETAG = "</trk>";

// read all lines:
lineReader.eachLine('./passit/passit_2014.gpx', function(line) {
	var nameOpenTag = line.search(NAMEOPENTAG);
	var nameCloseTag = line.search(NAMECLOSETAG);
	var trackOpenTag = line.search(TRACKOPENTAG);
	var trackCloseTag = line.search(TRACKCLOSETAG);
	
	if (trackOpenTag >= 0) {
		trkOpenTagFound = true;
	}

	// If track is not found yet, check the line content
	// If track is found then skip the line
	if (trkOpenTagFound === false) {
		if (nameOpenTag >= 0) {
			var nameOrig = line.slice(nameOpenTag + NAMEOPENTAG.length, nameCloseTag);
			var tmp = nameOrig.replace(/[,:.]/gi, "");
			var newLine = line.replace(nameOrig, tmp);
			output += newLine;
			i++;
		} else {
			output += line;
		}
	}
	// Track closed
	if ((trackCloseTag >= 0) && (trkOpenTagFound === true)) {
		trkOpenTagFound = false;
	}
}).then(function () {
	console.log("I'm done!!");
    // All lines are read, file is closed now.
	fs.writeFile('./passit/passit_2014-mod.gpx', output, function (err) {
		if (err) throw err;
		console.log('It\'s saved!');
	});
});

@@ -92,31 +92,24 @@
  1;%0Avar 
-trkOpenTagFound
+skipLine
  = false
@@ -210,16 +210,76 @@
 %3C/trk%3E%22;
+%0AEXTOPENTAG = %22%3Cextensions%3E%22;%0AEXTCLOSETAG = %22%3C/extensions%3E%22;
 %0A%0A// rea
@@ -548,14 +548,103 @@
 G);%0A
+%09var extOpenTag = line.search(EXTOPENTAG);%0A%09var extCloseTag = line.search(EXTCLOSETAG);%0A
 %09%0A%09if 
+(
 (tra
@@ -659,35 +659,50 @@
 g %3E= 0) 
-%7B%0A%09%09trkOpenTagFound
+%7C%7C (extOpenTag %3E= 0)) %7B%0A%09%09skipLine
  = true;
@@ -806,31 +806,24 @@
 ne%0A%09if (
-trkOpenTagFound
+skipLine
  === fal
@@ -1110,24 +1110,25 @@
 losed%0A%09if ((
+(
 trackCloseTa
@@ -1139,27 +1139,43 @@
  0) 
-&& (trkOpenTagFound
+%7C%7C (extCloseTag %3E= 0)) && (skipLine
  ===
@@ -1190,23 +1190,16 @@
 %7B%0A%09%09
-trkOpenTagFound
+skipLine
  = f

Remove <extension> fields.