.KEY inifile/a,sectionname/a,parametername/a
.BRA {
.KET }

; Ini File Get Script
; -------------------
; Author: Henrik Noerfjand Stengaard
; Date: 2018-01-14
;
; This script gets a parameter from a section within an ini file.

; End, if ini file doesn't exist
IF NOT EXISTS "{inifile}"
  SKIP end
ENDIF

; Copy ini file to temp
Copy >NIL: "{inifile}" T:_inifile

; Set ini file section to false
set inifilesection 0

; Get next line from ini file
LAB nextline
set inifileline ""
set inifileline `Execute INSTALLDIR:S/FirstLine T:_inifile`

; Goto end, if line is empty
IF "$inifileline" EQ ""
  SKIP end
ENDIF

echo "$inifileline" >T:_inifileline

IF $inifilesection eq 1 val
  ; End, if line contains next section
  search T:_inifileline "[" >NIL:
  IF NOT WARN  
    SKIP end
  ENDIF

  ; End, if line contains parameter
  search T:_inifileline "{parametername}=" >NIL:
  IF NOT WARN
    ; Replace equal with newline and print 2nd line with parameter value
    rep T:_inifileline "=" "*N"
    sed "2q;d" T:_inifileline
    SKIP end
  ENDIF
ENDIF

; Set ini file section true, if line contains section name
search T:_inifileline "[{sectionname}]" >NIL:
IF NOT WARN  
  set inifilesection 1
ENDIF

SKIP BACK nextline


; End
LAB end

; Delete temp files, if they exist
IF EXISTS T:_inifile
  delete >NIL: T:_inifile
ENDIF
IF EXISTS T:_inifileline
  delete >NIL: T:_inifileline
ENDIF