. .\LogbookScan.ConvertFromGzip.ps1

# Edit me
# Types: RVM, NTX, DC
$type = 'NTX'

# Also edit me
$folders = @()
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\14"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\15"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\16"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\17"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\18"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\19"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\20"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\21"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\22"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\23"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\24"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\25"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\26"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\27"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\28"
$folders += Get-Item "\\rejk-p-int003\E-Drive\FTP Repository\EW\LogBooks\$type\2017\08\29"


# Don't edit me unless you know what you're doing

$i = 1
foreach($folder in $folders) {
    Write-Progress -Activity "Folders $($folder.FullName)" -Status "$i of $($folders.Count)" -Id 1 -PercentComplete (100 / $folders.Count * $i)
    
    # Get all files in current folder
    $items = Get-ChildItem -Path $folder.FullName

    $y = 1
    $result = foreach($item in $items) {
        try {
            Write-Progress -Activity 'Files' -Status "$y of $($items.Count)" -ParentId 1 -PercentComplete (100 / $items.Count * $y)
                
            # Unpack gzip and get logbook file
            $file = $item.FullName | ConvertFrom-Gzip
        
        
            if($type -eq 'RVM') { 
                $file | Select-String -Pattern 'Ticket processing operation error;.*?;29'
            }
            elseif($type -eq 'NTX') {
                $file | Select-String -Pattern 'Error type : 120'
            }
            elseif($type -eq 'DC') {
                throw "Type $type not implemented"
            }
            else {
                Write-Warning "Unknown type $type"
                break
            }
        
            $file = $null
            $y++
        }
        catch {
            Write-Warning "Failed to process $($item.FullName): $_"
        }
    }

    $result | Out-File -Append -FilePath "errortype29$type.csv"

    $result = $null

    [System.GC]::Collect()
    "Last folder operated on: $i : $($folder.FullName)" | Out-File -FilePath "errortype29$type.count" -Append
    
    $i++
}



if($type -eq 'NTX') { 
    $csv = Import-Csv .\errortype29NTX.csv -Delimiter ';' -Header 'Date','Time','DeviceType','DeviceId','Unknown1','Unknown2','ErrorDescription','TransactionType','MediaSerialNumber','ErrorType' 
    $csv | where { $_.ErrorType -eq 'Error type : 120' } | % { ($_.MediaserialNumber -split ' : ')[1] } | sort -Unique > ntx.txt

    #SELECT omi.CardEngravedID FROM trk.ObjMediaInstance omi with(nolock)
    #WHERE omi.MediaSerialNumberID IN ( ... )
}
elseif($type -eq 'RVM') { 
    $csv = Import-Csv .\errortype29RVM.csv -Delimiter ';' -Header 'Date','Time','DeviceType','DeviceId','Unknown1','Unknown2','ErrorDescription','MediaSerialNumber','ErrorType' 
    $csv | where { $_.ErrorType -eq '120' } | % { $_.MediaSerialNumber } | sort -Unique | % { $_ -replace '308430','' } | % { $_ -replace ' ','' } > rvm.txt
}




