; This module is responsible for creating/reading/saving of common for
; the whole IDE parameters from the main .ini file.
;

uses advapi32


cParamSection text 'IDE_settings'
cDebugerKey   text 'Debugger'

cAutoHideKey  text 'AutoHideEnable'
cAutoHideTime text 'AutoHideTime'


uglobal
  flagAutoHide dd ?
  timeAutoHide dd ?
  blockAutoHide dd ?
endg



proc CheckParams
begin
        invoke  SendMessageW, [frmMsg], MWM_CLEARMESSAGES, 0, 0

; Check ini file consistency...
        stdcall FileExists, [hCfgFileName]
        jnc     .doCheckParams  ; if the file exists, go check params.

        stdcall FirstTimeInstall       ; try to create .ini file automatically

.doCheckParams:
        stdcall CheckFileParam, cDebugerKey, cDebuggerNotSet, cDebuggerNotExists
;        stdcall CheckFileParam, cWebBrowserKey, cBrowserNotSet, cBrowserNotExists

; Load IDE options from the .ini file
        stdcall CfgGetInt, [hCfgFileName], cParamSection, cAutoHideKey, 0
        mov     [flagAutoHide], eax

        stdcall CfgGetInt, [hCfgFileName], cParamSection, cAutoHideTime, 5000
        mov     [timeAutoHide], eax

        stdcall RebuildHelpFilesList

        call SetAutoHideTimer
        return

  cDebuggerNotExists text  "Specified external debugger doesn't exists."
  cDebuggerNotSet    text  "No external debugger is set in IDE options."

;  cBrowserNotExists text "Specified web browser doesn't exists."
;  cBrowserNotSet    text "No web browser is set in IDE options."
endp



proc CheckFileParam, .ptrKeyName, .msgNotSet, .msgNotExists
begin
        stdcall GetParamFromINI, [.ptrKeyName]
        jc      .filenotset

        push    eax
        stdcall StrPtr, eax
        stdcall FileExists, eax
        jc      .notexists

        stdcall StrDel ; from the stack
        return

.notexists:
        stdcall StrDel ; from the stack
        stdcall AddCheckMessage, [.msgNotExists], mtError
        return

.filenotset:
        stdcall AddCheckMessage, [.msgNotSet], mtWarning
        return
endp


iglobal
  cHTTPOpenCommand du 'http\shell\open\command\', 0
endg


cTargetOSLinux text "Linux"
cTargetOSWin32 text "Win32"


;-------------------------------------
; It tries to create proper ini file
; in the case that ini file does not
; exists.
;-------------------------------------
proc FirstTimeInstall
  .key  dd ?
  .data rb 1024
  .len  dd ?
  .dir  dd ?
begin

        stdcall GetCurrentDir
        mov     [.dir], eax

        stdcall ChangeDir, [hCfgFileName]


        stdcall SearchTablePath, cPathsTableInc, _section_environment, cIncludeKey, TRUE, cNotFoundMsg1, [hCfgFileName]
        stdcall SearchTablePath, cPathsTableMain, _section_environment, cFreshKey, TRUE, cNotFoundMsg2, [hCfgFileName]
        stdcall SearchTablePath, cPathsTableHelp, _section_environment, cFreshHelpKey, TRUE, cNotFoundMsg4, [hCfgFileName]
        stdcall SearchTablePath, cPathsTableLib,  _section_environment, cLibKey, TRUE, cNotFoundMsg3, [hCfgFileName]

; set TargetOS

        mov     eax, cTargetOSLinux
        mov     ecx, cTargetOSWin32
        cmp     [procLinuxPath], 0
        cmove   eax, ecx

        stdcall CfgSetStr, [hCfgFileName], _section_environment, cTargetKey, eax

        cmp     [procLinuxPath], 0
        je      .set_win_browser

; set Linux browser...

        stdcall CfgSetStr, [hCfgFileName], cParamSection, cWebBrowserKey, '/usr/bin/xdg-open'
        jmp     .end_first_time

.set_win_browser:
; set Windows browser...

        lea     eax, [.key]
        invoke  RegOpenKeyExW, HKEY_CLASSES_ROOT, cHTTPOpenCommand, 0, KEY_QUERY_VALUE, eax

        lea     eax, [.data]
        lea     ecx, [.len]
        invoke  RegQueryValueExW, [.key], 0, 0, 0, eax, ecx

; search the path for the filename...

        lea     eax, [.data]
        stdcall WideCharToUtf8, eax
        push    eax

        stdcall StrSplitList, eax, ' ', FALSE
        stdcall StrDel ; from the stack

        push    StrDel  ; preparation for ListFree
        push    eax

        cmp     [eax+TArray.count], 0
        je      .end_browser_set

        stdcall CfgSetStr, [hCfgFileName], cParamSection, cWebBrowserKey, [eax+TArray.array]
        invoke  RegCloseKey, [.key]

.end_browser_set:
        stdcall ListFree ; from the stack

.end_first_time:

        stdcall SetCurrentDir, [.dir]
        return
endp





proc SearchTablePath, .ptrTable, .hSection, .hKey, .fPathOnly, .ptrErrorMsg, .hIniFile
begin
        pushad

        mov     ebx, [.ptrTable]
        xor     edi, edi

.findloop:
        stdcall StrDel, edi

        mov     ecx, [ebx]
        add     ebx, 4

        test    ecx, ecx
        jz      .notfound

        stdcall GetFullPathNameUtf8, ecx
        mov     edi, eax

        stdcall FileExists, edi
        jc      .findloop

        DebugMsg "Path has been found!"

        cmp     [.fPathOnly], 0
        je      @f

        stdcall StrSplitFilename, edi
        stdcall StrDel, eax

        stdcall UnFixPathEnd, edi

@@:
        stdcall CfgSetStr, [.hIniFile], [.hSection], [.hKey], edi

        clc
        popad
        return

.notfound:
        DebugMsg "The path was not found!"

;        int3    ; it should never happen at all...
        invoke  MessageBoxA, [hMainForm], [.ptrErrorMsg], "Auto setup error", MB_ICONWARNING or MB_OK
        stc
        popad
        return
endp


iglobal
  align 4
  cPathsTableInc dd cAutoPath1, cAutoPath2, 0
  cAutoPath2  db '..\'
  cAutoPath1  db 'include'
  cFincTestFile db '\finc.txt'
                dd 0

  align 4
  cPathsTableMain dd cAutoPath4, cAutoPath3, 0
  cAutoPath3  db '..\'
  cAutoPath4  db 'Fresh.exe'
              dd 0

  align 4
  cPathsTableHelp dd cAutoPath7, cAutoPath8, 0
  cAutoPath7  db '..\'
  cAutoPath8  db 'doc\index.help'
              dd 0

  align 4
  cPathsTableLib dd cAutoPath6, cAutoPath5, 0
  cAutoPath5   db '..\'
  cAutoPath6   db 'freshlib'
  cLibTestFile db '\freshlib.inc'
               dd 0
endg

cIncludeKey   text 'finc'
cFreshKey     text 'Fresh'
cFreshHelpKey text 'FreshHelp'
cLibKey       text 'lib'
cTargetKey    text 'TargetOS'

cNotFoundMsg1 text "First time run procedure can't find include path automanically. Try to set it manualy in 'options|IDE params' menu.",     \
                     "You have to use 'finc' name for this path."
cNotFoundMsg2 text "First time run procedure can't find main program directory. Try to set it manually in 'options|IDE params' menu.",        \
                     "You have to use 'Fresh' name for this path.",0
cNotFoundMsg3 text "First time run procedure can't find FreshLib directory. Try to set it manually in 'options|IDE params' menu.",        \
                     "You have to use 'Lib' name for this path.",0
cNotFoundMsg4 text "First time run procedure can't find FreshHelp directory. Try to set it manually in 'options|IDE params' menu.",        \
                     "You have to use 'FreshHelp' name for this path.",0


proc AddCheckMessage, .ptrMessage, .msgType
.msg TFreshMsg
begin
        mov     eax, [.msgType]
        mov     ecx, [.ptrMessage]
        mov     [.msg.type], eax
        mov     [.msg.hMessage], ecx
        mov     [.msg.link.hFileName], 0

        lea     eax, [.msg]
        invoke  SendMessageW, [frmMsg], MWM_ADDMESSAGE, eax, TVI_ROOT
        return
endp




;------------------------------------------------
; Parces command line and opens the files.
;------------------------------------------------
proc ParseCommandLine
.num dd ?
begin
        pushad

        stdcall GetCmdArguments
        mov     esi, eax

        cmp     [esi+TArray.count], 2
        jb      .finish

        xor     ebx, ebx
        inc     ebx

.open_loop:
        stdcall OpenFileByName, [esi+TArray.array+4*ebx], NULL, TRUE
        inc     ebx
        cmp     ebx, [esi+TArray.count]
        jne     .open_loop

.finish:
        stdcall ListFree, esi, StrDel
        popad
        return
endp



proc GetParamFromINI, .ptrKeyName
begin
        stdcall CfgGetStr, [hCfgFileName], cParamSection, [.ptrKeyName]
        return
endp

