'______________________________________________________
'Testaa
' Purpose:	- tallettaa RTF:nä nykyisen tiedoston,
'            - luo tarvittaessa vastaavan .hpj -tiedoston
'            - kääntää help-kääntäjällä 
'            - näyttää virheet
'            - käynnistää helpin
'
'______________________________________________________
'Declare Function WinExec(lpCmdLine As String, nCmdShow As Integer) 'As Integer
'Declare Function GetActiveWindow Lib "user" As Integer
'Declare Function WinHelp Lib "user"(hwnd As Integer, lpFileName As   String, wCommand As Integer, dwData As Long) As Integer
Dim Shared errCompile, errRuntime

'______________________________________________________
Sub MAIN
	BaseName$ = ali.NoPath$(ali.NoExt$)
'    ErrName$ = "ERR.ERR"
    ErrName$ = BaseName$ + ".ERR"
    HelpName$ = BaseName$ + ".hlp"
    PrjName$ = BaseName$ + ".hpj"
	hlpQUIT = 2 : hlpINDEX = 3  
    errCompile = - 1 : errRuntime = - 2
'	hwnd = GetActiveWindow

    ali.CloseApplication("Notepad - " + ErrName$)
    ali.CloseAllWith(ErrName$)

	RTFsave

	' Tapetaan .hlp jotta tied. onko käännös OK
	If WW2_Files$(HelpName$) <> "" Then Kill HelpName$
	' Sammutetaan edellinen WinHelp
'	t = WinHelp(hwnd, HelpName$, hlpQUIT, - 1)

    ' luodaan tarvittaessa .prj
	If WW2_Files$(PrjName$) = "" Then CreateHPJFILE(BaseName$)

	'Compile with Help Compiler.
'   rv = WinExec("hc31 " + BaseName$, 1)
   Shell("hc31 " + BaseName$, 1)
' rv = WinExec(Environ$("COMSPEC") + " /c hc31 " + BaseName$, 1)
'	rv = WinExec(Environ$("COMSPEC") + " /c hc31 " + BaseName$ + \
'           ">" + ErrName$, 0)

'	If rv <= 32 Then SpewErrorMessage(rv) : Goto canceled

	'Wait for compile to complete before continuing.
'	MsgBox("Dismiss this dialog box when compile is complete.", \
'           "HELP.DOT")
'    AppActivate "HC31"
    While (AppIsRunning("HC31") =  - 1)
      Print "Odota, käännetään"
    Wend

	' Shell "notepad " + ErrName$
    On Error Goto DoOpenHelp
    FileOpen .Name = ErrName$, .ConfirmConversions = 0, .ReadOnly = 1
DoOpenHelp:
	If WW2_Files$(HelpName$) = "" Then
		'Compilation failed.
		SpewErrorMessage(errCompile)
		Goto canceled
	End If

   'Invoke Winhelp on the new help file.
'	t = WinHelp(hwnd, BaseName$ + ".hlp", hlpINDEX, 0)
   Shell BaseName$ + ".hlp", 1
'	If t = 0 Then SpewErrorMessage(errRuntime)

canceled:

End Sub

'______________________________________________________
Sub SpewErrorMessage(err)
	Select Case err
		Case errRuntime : msg$ = "Can't run Windows Help."
		Case errCompile : msg$ = "Compilation of help file failed."
		Case 0 : msg$ = "Not enough memory to run the help compiler."
		Case 2, 3 :
			msg$ = "Can't find HC31.EXE."
		Case Else :
			msg$ = "Can't load Help Compiler."
	End Select

	MsgBox(msg$, "HELP.DOT", 48)
End Sub

'______________________________________________________
Sub CreateHPJFIle(PName$)
  FileNew .Template = "Normal", .NewTemplate = 0
  name$ = ali.NoPath$(PName$)
  Insert "; This help project requires hc 3.1" :  InsertPara
  Insert "[OPTIONS]"                    : InsertPara
  Insert "errorlog = " + name$ + ".err" : InsertPara
  Insert "title = " + name$ + " Help"   : InsertPara
'  Insert "contents = CONTEXT"           : InsertPara
  Insert "compress = 0"                 : InsertPara
  Insert "warning = 3"                  : InsertPara
  Insert "report = 1"                   : InsertPara
  Insert ""                             : InsertPara
  Insert "[FILES]"                      : InsertPara
  Insert name$ + ".rtf"                 : InsertPara
  Insert ""                             : InsertPara
  Insert "[BITMAPS]"                    : InsertPara
  Insert ""                             : InsertPara
  Insert "[WINDOWS]"                    : InsertPara
  Insert "main = " + Chr$(34) + name$ + " Help" + Chr$(34) + ", (380,0,600,700 ) ,0,,(255,255,0),"
  InsertPara
  Insert ""                             : InsertPara
  Insert "[CONFIG]"                     : InsertPara
  Insert "BrowseButtons()"              : InsertPara
  FileSaveAs .Name = PName$ + ".HPJ", .Format = 4
  FileClose 2
End Sub