' Gambas module file

' Copyright(C) 2011-2014, Michał Głowienka aka eloaders <eloaders@linux.pl>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.

Public Logs As New Logger
Public i As Integer
Public database As New Collection
Function _inits()
  Dim buffer As String
  Dim cpucache As String
  Dim z As Integer
  If Application.Env["INEXCPU_VENDOR"] = Null Then
   Logs("Not Use CPU Vendor from ENV", Logger.Info)
  Else
   Logs("Use CPU Vendor from ENV", Logger.Info)
   Finfosys.Label14.Text = Application.Env["INEXCPU_VENDOR"]
  Endif
  ' The use Of the database In JSON format.To decode the information about the processor. 
  ' The database Is Divided into several parts To the model processor manufacturer.
  ' 
  If Finfosys.Label14.Text Like "*Opteron*" Then 'Use Opteron databese if TextBox Like Opteron
   Logs("Use AMD Opteron CPU Database", Logger.Info)
    buffer = File.Load("Database/Opteron.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "AMD", "")
  '
  '
  Else If Finfosys.Label14.Text Like "*amd*A6*" Then
   Logs("Use AMD A6 CPU Database", Logger.Info)
    buffer = File.Load("/etc/i-nex/Database/A6.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "AMD", "")
  '
  '
  Else If Finfosys.Label14.Text Like "*amd*" Then
   Logs("Use AMD CPU Database", Logger.Info)
    buffer = File.Load("Database/amd.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "AMD", "")
  Else If Finfosys.Label14.Text Like "*Xeon*" Then 'Use Xeon databese if TextBox Like Xeon
   Logs("Use Intel Xeon CPU Database", Logger.Info)
    buffer = File.Load("Database/Xeon.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "Intel", "")
  '
  '
  Else If Finfosys.Label14.Text Like "*i3*" Then
   Logs("Use Intel i3 CPU Database", Logger.Info)
    buffer = File.Load("Database/i3.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "Intel", "")
  Else If Finfosys.Label14.Text Like "*i5*" Then
   Logs("Use Intel i5 CPU Database", Logger.Info)
    buffer = File.Load("Database/i5.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "Intel", "")
  Else If Finfosys.Label14.Text Like "*i7*" Then
   Logs("Use Intel i7 CPU Database", Logger.Info)
    buffer = File.Load("Database/i7.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "Intel", "")
  '
  '
  Else If Finfosys.Label14.Text Like "*atom*" Then
   Logs("Use Intel CPU Database (Atom)", Logger.Info)
    buffer = File.Load("Database/atom.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "Intel", "")
  Else If Finfosys.Label14.Text Like "*intel*" Then
   Logs("Use Intel CPU Database (Intel All)", Logger.Info)
    buffer = File.Load("Database/intel.json") 'Load the database to a temporary buffer
    Finfosys.Label14.Text = Replace(Finfosys.Label14.Text, "Intel", "")
  Else If Finfosys.Label14.Text Like "*Pentium*" Then
   Logs("Use Intel CPU Database (Pentium)", Logger.Info)
    buffer = File.Load("Database/intel.json") 'Load the database to a temporary buffer
  Else If Finfosys.Label14.Text Like "*Celeron*" Then
   Logs("Use Intel CPU Database (Celeron)", Logger.Info)
    buffer = File.Load("Database/intel.json") 'Load the database to a temporary buffer
  Endif
  
  'Celeron(R)Dual - Core CPU T3000@1.80 GHz
  Logs("Decode CPU Database", Logger.Info)
  database = JSON.Decode(buffer) 'Convert JSON data to Collection
  buffer = Null
  cpucache = Finfosys.Label14.Text & "*"
  Logs("Replace CPU Strings to Null", Logger.Info)
  
  'For the purpose of the database, certain words have to be converted to an empty value.
  cpucache = Replace(Replace(Replace(Replace(cpucache, "with", "*"), "APU", "*"), "Graphics", "*"), "Radeon", "*")
  'Fix for APU with Radeon(TM) R7 Graphics
   cpucache = Replace(cpucache, "R7", "*")
  '
  cpucache = Replace(Replace(Replace(Replace(Replace(Replace(Replace(cpucache, "Intel", "*"), " ", "*"), "(R)", "*"), "CPU", "*"), "TM", "*"), ")", "*"), "(", "*")
  cpucache = Replace(Replace(Replace(Replace(cpucache, "AMD", "*"), "Dual-Core", "*"), "Processor", "*"), "tm", "*")
  cpucache = Replace(cpucache, "HD", "*")
  
  If cpucache Like "*Eight-Core*" Or "*Triple-Core*" Or "*Six-Core*" Or "*Quad-Core*" Then
   cpucache = Replace(cpucache, "Eight-Core", "*")
   cpucache = Replace(cpucache, "Triple-Core", "*")
   cpucache = Replace(cpucache, "Six-Core", "*")
   cpucache = Replace(cpucache, "Quad-Core", "*")
  Endif
  
  cpucache = Replace(cpucache, "Core2", "Core 2")
  If cpucache Like "*Processor*" Or "*Processo*" Then
    cpucache = Replace(cpucache, "Processo", "*")
  Endif
  If cpucache Like "*Graphic*" Then
    cpucache = Replace(cpucache, "Graphic", "*")
  Endif
  
  If cpucache Like "*Quad*" Or "*Core*" Or "*Dual*" Then
   cpucache = Replace(cpucache, "Quad", "*")
   cpucache = Replace(cpucache, "Core", "*")
   cpucache = Replace(cpucache, "Dual", "*")
  Endif
  
  Logs("CPU Cache (I-Nex)" & cpucache & "", Logger.Info)
  
  For z = 0 To database["DATABASE"].Count - 1
    If database["DATABASE"][z]["PROCESSOR_MODEL_NAME"] Like "*" & cpucache & "*" Then 
      Print database["DATABASE"][z]["PROCESSOR_MODEL_NAME"]
      Finfosys.PictureBox3.Name = database["DATABASE"][z]["PROCESSOR_SOCKET_IMAGE"]
      Finfosys.PictureBox3.Picture = Picture["Database/Logos/" & database["DATABASE"][z]["PROCESSOR_SOCKET_IMAGE"] & ""]
      Finfosys.Label103.Text = " Integrated GPU: " & database["DATABASE"][z]["PROCESSOR_INTEGRATEDGRAPHIC"]
      Finfosys.Label104.Text = " TDP: " & database["DATABASE"][z]["PROCESSOR_TDP"]
      Finfosys.Label110.Text = " PPS: " & database["DATABASE"][z]["PROCESSOR_PROCESS_SIZE"]
      Finfosys.Label111.Text = " Socket: " & database["DATABASE"][z]["PROCESSOR_SOCKET"]
      Finfosys.Label112.Text = " Transistors: " & database["DATABASE"][z]["PROCESSOR_TRANSISTORS"]
      Finfosys.Label113.Text = " Die size: " & database["DATABASE"][z]["PROCESSOR_DIE_SIZE"]
      Finfosys.Label114.Text = " Package: " & database["DATABASE"][z]["PROCESSOR_PACKAGE"]
      Finfosys.Label115.Text = " TCaseMax: " & database["DATABASE"][z]["PROCESSOR_TCASEMAX"]
      Finfosys.Label116.Text = " Turboclock: " & database["DATABASE"][z]["PROCESSOR_TURBOCLOCK"] 
      Finfosys.Label117.Text = " Multiplier: " & database["DATABASE"][z]["PROCESSOR_MULTIPLIER"]
      Finfosys.Label118.Text = " Voltage: " & database["DATABASE"][z]["PROCESSOR_VOLTAGE"]
      Finfosys.Label119.Text = " Codename: " & database["DATABASE"][z]["PROCESSOR_CODENAME"]
      Finfosys.Label120.Text = " Part: " & database["DATABASE"][z]["PROCESSOR_PART"]
      Finfosys.Label121.Text = " Memory support: " & database["DATABASE"][z]["PROCESSOR_MEMORYSUPPORT"]
      i = z
    Endif
  Next
  Logs("Current position of processor = " & cpucache & " is = " & z & "", Logger.Info)
  Logs("Detect CPU As = " & database["DATABASE"][i]["PROCESSOR_MODEL_NAME"] & "", Logger.Info)
  Logs("\nIf Detected CPU is wrong CPU. \n"
       "Please submit report at forum \n"
       "http://forum.i-nex.pl/ \n", Logger.Info)
  database.Clear
End
