' Gambas module file

Private $nKill As Integer
Private $bVerbose As Boolean
Private $aOutput As Integer[]
Private $aStyles As String[]
Private $fLast As Float

Private Sub MakeDir(Path As String)

  Dim sElt As String
  Dim sMake As String = "/"

  If Path Begins "~/" Then Path = User.Home &/ Mid$(Path, 3)

  For Each sElt In Split(Path, "/")
    sMake &/= sElt
    If IsDir(sMake) Then Continue
    Try Mkdir sMake
  Next
  
  If Not Exist(Path) Or If Not IsDir(Path) Then Error.Raise("Cannot create directory")

End

Public Sub Process_Kill()
  
  Inc $nKill
  
End

Private Sub PrintProgress(Optional bForce As Boolean)

  Dim fNext As Float = Timer
  Dim I As Integer
  
  If Not $bVerbose Then Return
  
  If Not bForce Then
    If fNext - $fLast < 0.1 Then Return
  Endif
  
  $fLast = fNext
  
  Print "\e[2J";
  For I = 0 To $aOutput.Max
    Print $aStyles[I]; String$(20 - Len($aStyles[I]), ".");; $aOutput[I]
  Next

End

Public Sub Process_Read()

  Dim sData As String
  Dim N As Integer
  
  sData = Read #Last, Lof(Last)
  If Not $bVerbose Then Return
  
  Try N = CInt(Trim(sData))
  If N = 0 Then Return
  
  $aOutput[Last.Tag] = N
  
  PrintProgress
  
End

Public Sub Run(Optional sStyle As String, bVerbose As Boolean)

  Dim sSize As String
  Dim hFile As File
  Dim sLine As String
  Dim iPos As Integer
  Dim sIcon As String
  Dim aTarget As String[]
  Dim sIconPath As String
  Dim sTarget As String
  Dim sTargetDir As String
  Dim cMime As New Collection
  Dim sTargetPath As String
  Dim sRoot As String
  Dim aDirectories As New String[]
  Dim sDir As String
  Dim sName As String
  Dim hImage As Image
  Dim sMakeLink As String
  Dim sLink As String
  Dim cTarget As Collection
  Dim bDark As Boolean
  Dim sOrigin As String
  Dim sFile As String
  Dim hIcon As Image
  Dim aSize As String[]
  Dim sTargetRoot As String
  Dim N As Integer
  Dim sSuffix As String
  Dim sFullIcon As String
  Dim hSymbolic As Image
  Dim hProcess As Process
  Dim I As Integer
  
  'Dim sSymbolicPath As String
  
  'Dim sPath As String

  If Not sStyle Then
    
    $bVerbose = bVerbose
    
    $aStyles = ["gambas", "gambas-mono", "gambas-dark", "gambas-mono-dark", "gambas-thin", "gambas-thin-dark", "gambas-glass", "gambas-glass-dark"]
    $aOutput = New Integer[$aStyles.Count]
    
    For I = 0 To $aStyles.Max
      hProcess = Exec [Component.Path &/ "gb.form.stock.gambas", $aStyles[I]] For Input As "Process"
      hProcess.Tag = I
    Next
    
    While $nKill < $aStyles.Count
      Wait Next
    Wend
    
    PrintProgress(True)
    
    Return
    
  Endif

  Component.Load("gb.image.io")

  aSize = ["16", "24", "32", "48", "64", "128"]

  hFile = Open "links" For Input 
  While Not Eof(hFile)
    
    Line Input #hFile, sLine
    sLine = Trim(sLine)
    If Not sLine Then Continue
    
    iPos = InStr(sLine, "->")
    If iPos = 0 Then Continue
    
    sIcon = Trim(Mid(sLine, iPos + 2))
    sTarget = Trim(Left(sLine, iPos - 1))
    
    aTarget = cMime[sIcon]
    If Not aTarget Then 
      aTarget = New String[]
      cMime[sIcon] = aTarget
    Endif
    
    aTarget.Add("$(mime)" &/ sTarget)
    
  Wend
  
  Close #hFile

  'For Each sStyle In ["gambas", "gambas-mono", "gambas-dark", "gambas-mono-dark", "gambas-thin", "gambas-thin-dark", "gambas-glass", "gambas-glass-dark"]
  'For Each sStyle In ["gambas-thin-dark"]

    sTargetRoot = "~/.local/share/icons/" & sStyle
    sRoot = sTargetRoot & ".tmp"
    Shell "rm -rf " & sRoot Wait
    
    bDark = sStyle Ends "-dark"
    sOrigin = sStyle
    If bDark Then sOrigin = Left(sOrigin, -5)
  
    N = 0
  
    hFile = Open "icon.map" For Input 
    While Not Eof(hFile)
  
      Line Input #hFile, sLine
      sLine = Trim(sLine)
      If Not sLine Then Continue
      
      iPos = InStr(sLine, " ")
      If iPos = 0 Then Continue
      
      GoSub PRINT_PROGRESSION
      
      sIcon = Left(sLine, iPos - 1)
      aTarget = Split(Trim(Mid(sLine, iPos + 1)), ";", "", True)
      If cMime.Exist(sIcon) Then aTarget.Insert(cMime[sIcon])
  
      'For Each sSuffix In ["", "-symbolic"]
      sSuffix = ""
  
      sFullIcon = sIcon & sSuffix

      For Each sSize In aSize
        
        sIconPath = sOrigin &/ sSize &/ sFullIcon & ".png"
        If Exist(sIconPath) Then
          
          If bDark Then
            
            hImage = Image.Load(sIconPath)
            hSymbolic = hImage.Copy().Brightness(1).Colorize(&H606060)
            _DefaultStock.Invert(hImage)
            sIconPath = Temp$("image") & ".png"
            hImage.Save(sIconPath)
            
          Endif
          
        Else
          
          sIconPath = sOrigin &/ "128" &/ sFullIcon & ".png"
          If Not Exist(sIconPath) Then sIconPath = sOrigin &/ "32" &/ sFullIcon & ".png"
          If Not Exist(sIconPath) Then
            If sSuffix Then Continue
          Endif
          
          hImage = Image.Load(sIconPath).Stretch(CInt(sSize), CInt(sSize))
          
          If bDark Then _DefaultStock.Invert(hImage)
          
          sIconPath = Temp$("image") & ".png"
          hImage.Save(sIconPath)
        
        ' Else If sSize = "48" Or If sSize = "64" Then
        '   sIconPath = sStyle &/ "128" &/ sIcon & ".png"
        '   If Not Exist(sIconPath) Then Continue
        '   hImage = Image.Load(sIconPath).Stretch(CInt(sSize), CInt(sSize))
        '   sIconPath = Temp$("image") & ".png"
        '   hImage.Save(sIconPath)
        Endif
      
        If Not Exist(sIconPath) Then Continue
        
        If bDark And If hSymbolic Then _DefaultStock.Invert(hSymbolic)
        
        sMakeLink = ""
        cTarget = New Collection
        
        For Each sTarget In aTarget
          If sTarget = "#" Then Continue
          
          If sTarget Begins "[" Then
            If sTarget Begins "[-" Then Continue
            iPos = InStr(sTarget, "]")
            sTarget = Mid(sTarget, iPos + 1)
          Endif
          
          If cTarget.Exist(sTarget) Then Continue
          cTarget[sTarget] = True
          
          iPos = InStr(sTarget, "/")
          sTargetDir = Left(sTarget, iPos - 1)
          sTarget = Mid(sTarget, iPos + 1)
          
          If sTargetDir = "$(mime)" Then sTargetDir = "mimetypes"
          
          sTargetDir = sSize & "x" & sSize &/ sTargetDir
          If Not aDirectories.Exist(sTargetDir) Then aDirectories.Add(sTargetDir)
          sTargetPath = sRoot &/ sTargetDir &/ sTarget & sSuffix & ".png"
          'Print sTargetPath
          
          MakeDir(File.Dir(sTargetPath))
          Try Kill sTargetPath
          
          If sMakeLink Then
            sLink = sMakeLink
            If sLink & "/" Begins sTargetDir & "/" Then
              sLink = File.Name(sLink)
            Else 
              sLink = ".." &/ sMakeLink
            Endif
            Link sLink To sTargetPath
          Else
            Copy sIconPath To sTargetPath
            sMakeLink = Mid$(sTargetDir, InStr(sTargetDir, "/") + 1) &/ sTarget & sSuffix & ".png"
          Endif
          
          ' If ["actions", "categories", "devices", "places", "status"].Exist(File.Name(sTargetDir)) Then
          '   sSymbolicPath = File.Dir(sTargetPath) &/ File.BaseName(sTargetPath) & "-symbolic.png"
          '   If Not hSymbolic Then
          '     Try Link File.Name(sTargetPath) To sSymbolicPath
          '   Else
          '     hSymbolic.Save(sSymbolicPath)
          '   Endif
          ' Endif
          
        Next
        
      Next
        
      'Next
      
    Wend
    
    Close #hFile
    
    For Each sFile In Dir(sOrigin &/ "app")
      If sFile Ends "~" Then Continue
      GoSub PRINT_PROGRESSION
      hImage = Image.Load(sOrigin &/ "app" &/ sFile)
      For Each sSize In aSize
        sTargetPath = sRoot &/ (sSize & "x" & sSize) &/ "apps" &/ sFile
        'Print sTargetPath
        hIcon = hImage.Stretch(CInt(sSize), CInt(sSize))
        If bDark Then _DefaultStock.Invert(hIcon)
        hIcon.Save(sTargetPath)
        'Try Link File.Name(sTargetPath) To File.Dir(sTargetPath) &/ File.BaseName(sTargetPath) & "-symbolic.png"
      Next
    Next
    
    hFile = Open "app-links"
    For Each sLine In hFile.Lines
      
      sLine = Trim(sLine)
      If Not sLine Then Continue
      
      iPos = InStr(sLine, ": ")
      If iPos = 0 Then Continue
      
      GoSub PRINT_PROGRESSION
      sIcon = Left(sLine, iPos - 1)
      
      For Each sTarget In Split(Mid$(sLine, iPos + 2), " ", "", True)
        For Each sSize In aSize
          sTargetPath = sRoot &/ (sSize & "x" & sSize) &/ "apps" &/ sTarget & ".png"
          'Print sTargetPath
          Try Kill sTargetPath
          Link sIcon & ".png" To sTargetPath
          'Try Link sIcon & "-symbolic.png" To File.Dir(sTargetPath) &/ File.BaseName(sTargetPath) & "-symbolic.png"
        Next
      Next
      
    Next
    
    ' For Each sFile In RDir(sRoot, "*.png")
    '   sIcon = File.BaseName(sFile)
    '   If sIcon Ends "-symbolic" Then Continue
    '   sPath = sRoot &/ File.Dir(sFile) &/ File.BaseName(sFile) & "-symbolic.png"
    '   If Exist(sPath) Then Continue
    '   Link File.Name(sFile) To sPath
    ' Next
  
    hFile = Open sRoot &/ "index.theme" For Output Create
    
    Print #hFile, "[Icon Theme]"
    Print #hFile, "Name="; sStyle
    Print #hFile, "Comment="; sStyle; " icon theme"
    Print #hFile, "Inherits=";
    Select Case sStyle
      Case "gambas"
        Print #hFile, "Papirus,breeze,hicolor"
      Case "gambas-thin", "gambas-glass"
        Print #hFile, "breeze,hicolor"
      Case "gambas-dark", "gambas-glass-dark"
        Print #hFile, "Papirus-Dark,breeze-dark,hicolor"
      Case "gambas-thin-dark"
        Print #hFile, "breeze-dark,hicolor"
      Case "gambas-mono", "gambas-mono-dark"
        Print #hFile, "HighContrast"
    End Select
    
    Print #hFile, "Directories="; aDirectories.Sort(gb.Natural).Join(",")
    Print #hFile
    ' Print #hFile, "DesktopDefault=48"
    ' Print #hFile, "ToolbarDefault=16"
    ' Print #hFile, "ToolbarSizes=16,22,32,48"
    ' Print #hFile, "MainToolbarDefault=16"
    ' Print #hFile, "MainToolbarSizes=16,22,32,48"
    ' Print #hFile, "PanelDefault=22"
    ' Print #hFile, "PanelSizes=16,22,32,48,64,128,256"
    Print #hFile, "FollowsColorScheme=true"
    Print #hFile
    
    For Each sDir In aDirectories
      Print #hFile, "["; sDir; "]"
      iPos = InStr(sDir, "x")
      Print #hFile, "Size="; Left(sDir, iPos - 1)
      Print #hFile, "Type=Fixed"
      sName = File.Name(sDir)
      Select Case sName
        Case "mimetypes"
          Print #hFile, "Context=MimeTypes"
        Case "apps", "preferences", "applets"
          Print #hFile, "Context=Applications"
        Case Else
          Print #hFile, "Context="; UCase(Left(sName)); Mid(sName, 2)
      End Select
      Print #hFile
    Next
    
    Close #hFile
    
    Shell "rm -rf " & sTargetRoot Wait
    Move sRoot To sTargetRoot
    
  'Next

  Return
  
PRINT_PROGRESSION:

  Inc N
  Print N
  Return

End
