clear
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$egInstallpath=Get-ChildItem Env:EGURKHA_INSTALL_DIR |Select Value
$egurkhaPath=$egInstallpath.Value.ToString()
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$egEncryPath=$scriptPath+"\EGFileEncryption.psm1"
$egDatnKy=$scriptPath+"\GetDatnKeyFiles.psm1"
Import-Module $egEncryPath,$egDatnKy


$testargs=$args
$reportingname='EXO/'+$testargs[0]
$username=$testargs[1]
$Syntime=$testargs[2]

try{
    $readfiles=Eg-ReadFile -ComntRptPath $reportingname -FileName "devicestats" -keyFileName "kdevicestats" -EgPath $egurkhaPath
    $datafile=$readfiles[1] -replace (".csv",".dat") 
    $csvfile=Unprotect-File $datafile -Algorithm AES -KeyAsPlainText $readfiles[0]	
    $users= import-csv $csvfile
    Remove-Item $csvfile
    Eg-DeleteFiles -FilePath $readfiles[2] -Pattern 'devicestats' -InputFile $readfiles[3]
    Eg-DeleteFiles -FilePath $readfiles[2] -Pattern 'kdevicestats' -InputFile $readfiles[4]
    Write-Host 'ReadFile:'$readfiles[3]
    }
    catch [System.Management.Automation.RuntimeException] {
        Write-Error -Message "File not found. The $reportingname location does not contain devicestats files."
    }
    catch{
        $errmsg=$_.Exception.Message
        $errstack=$_.Exception.StackTrace
        Write-Error -Message $errmsg
    }
	$useragents=@{}
	$devicetypes=@{}
	$deviceOss=@{}
	$clienttypes=@{}
	$devicelanges=@{}
	$totaldevicecount=0
    #$domainname=$username.split('@')[1]
	foreach($user in $users)
	{
		$totaldevicecount++
	    $useragent=$user.DeviceUserAgent
	       if($useragent -ne "" -and $useragent -ne $null)
	       {
		
			 if($useragents.ContainsKey($useragent))
			 {
			      $useragents[$useragent]+=1
			 }
			 else
			 {
			      $useragents.Add($useragent,1) 
			 }
	       }


	    $devicetype=$user.DeviceType
	       if($devicetype -ne "" -and $devicetype -ne $null)
	       {
		     
			 if($devicetypes.ContainsKey($devicetype))
			 {
			      $devicetypes[$devicetype]+=1
			 }
			 else
			 {
			      $devicetypes.Add($devicetype,1) 
			 }
	       }


	    $deviceos=$user.DeviceOS
	       if($deviceos -ne "" -and $deviceos -ne $null -and -not($deviceos -match "^[A-Z]$") -and -not($deviceos -match "^\d+$")  -and -not($deviceos -match "^\d+.+\d+.+\d+$")  -and -not($deviceos -match "^\d+.+\d+$") )
	       {
		     
			 if($deviceOss.ContainsKey($deviceos))
			 {
			      $deviceOss[$deviceos]+=1
			 }
			 else
			 {
			      $deviceOss.Add($deviceos,1) 
			 }
	       }


	    $clienttype=$user.ClientType
	       if($clienttype -ne "" -and $clienttype -ne $null)
	       { 
		
			 if($clienttypes.ContainsKey($clienttype))
			 {
			      $clienttypes[$clienttype]+=1
			 }
			 else
			 {
			      $clienttypes.Add($clienttype,1) 
			 }
	       }


	    $devicelang=$user.DeviceOSLanguage
	       if($devicelang -ne "" -and $devicelang -ne $null)
	       { 
		
			 if($devicelanges.ContainsKey($devicelang))
			 {
			      $devicelanges[$devicelang]+=1
			 }
			 else
			 {
			      $devicelanges.Add($devicelang,1) 
			 }
	       }

	}

	    Write-Host "useragentscount "$useragents.Count
	    Write-Host "devicetypescount "$devicetypes.Count
	    Write-Host "operatingsystemscount "$deviceOss.Count
	    Write-Host "clientscount "$clienttypes.Count
	    Write-Host "deviceOSlangcount "$devicelanges.Count
	    Write-Host "Sychdevicescount "$totaldevicecount

	    foreach($entry in $useragents.GetEnumerator() | sort -Property value  -Descending | select -first 20)
	    {
		Write-Host '#useragents' $entry.Name ~!~ $entry.Value
	    }


	    foreach($entry in $devicetypes.GetEnumerator() | sort -Property value  -Descending | select -first 20)
	    {
		Write-Host '#devicetypes' $entry.Name ~!~ $entry.Value
	    }


	    foreach($entry in $deviceOss.GetEnumerator() | sort -Property value  -Descending | select -first 20)
	    {
		Write-Host '#deviceOss' $entry.Name ~!~ $entry.Value
	    }


	    foreach($entry in $clienttypes.GetEnumerator() | sort -Property value  -Descending | select -first 20)
	    {
		Write-Host '#clienttypes' $entry.Name ~!~ $entry.Value
	    }


	    foreach($entry in $devicelanges.GetEnumerator() | sort -Property value  -Descending | select -first 20)
	    {
		Write-Host '#devicelanges' $entry.Name ~!~ $entry.Value
	    }
