ProductivityTools.ConvertTcx2Gpx.psm1

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
71
72
73
74
75
76
77
78
79

function Get7zPath{
    return "$PSScriptRoot\GPSBabel.7z"
}

function ValidateGPSBabel
{
    [Cmdletbinding()]
    param()
    
    $gpsBabelPath=Get7zPath
    $directory=Get-ChildItem "$PSScriptRoot\GPSBabel" -ErrorAction Ignore
    if ($directory -eq $null)
    {
        $zip=Get-ChildItem $gpsBabelPath -ErrorAction Ignore
        if($zip -eq $null)
        {
            Write-Verbose "Downloading GPS Babel."
            Invoke-WebRequest -Uri "https://github.com/pwujczyk/ProductivityTools.ConvertTcx2Gpx/raw/master/ProductivityTools.ConvertTcx2Gpx/GPSBabel.7z" -OutFile $gpsBabelPath
        }
        else
        {
            Write-Verbose "GPS babel already downloaded.";
        }
        
        Write-Verbose "Extracting GPS Babel";
        Expand-7Zip -ArchiveFileName $gpsBabelPath -TargetPath GPSBabel
    }    
    else
    {
        Write-Verbose "GPS babel already extracted.";
    }
}

function ConvertTcX(){
    [Cmdletbinding()]
    param(
        [string]$Path
    )
    
    Write-Verbose "Convert $Path"
    
    $targetFile=$Path -replace ".tcx", ".gpx"
    
    $targetFileExists=Get-ChildItem $targetFile -ErrorAction Ignore
    if($targetFileExists -ne $null)
    {
        throw "Target file $targetFile exists."
    }

    $fileExe = "$PSScriptRoot\GPSBabel\gpsbabel.exe"
    & $fileExe  -i gtrnctr -f $Path -o gpx -F $targetFile    
}

function Convert {
    [Cmdletbinding()]
    param(
        [string]$Path
    )

    $tcxs=Get-ChildItem -Path $Path -Filter *.tcx
    foreach($tcx in $tcxs)
    {
        ConvertTcx($tcx.FullName)
    }
}


function Convert-Tcx2Gpx {
    [Cmdletbinding()]
    param(
        [string]$Path
    )
    
    Write-Verbose "Hello";
    
    ValidateGPSBabel
    Convert $Path 
}