'*****************************************
' KARL'S AUTOMATIC PDF GENERATION SCRIPT
' Dec 9, 2002
'*****************************************


Dim FramePath, InstallPath, NumBooks, Books(), fso, WWPath, ExamplesPath, DocSet, BasePath
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.shell")
Dim DoOpen, DoBuild, DoUpdate, DoPDF, DoWW, DoLog

NumBooks = 0

DoOpen = True
DoPDF = True
DoWW = True
DoUpdate = True
DoBuild = True
DoLog = True
DoClose = True

DocSet = WScript.Arguments(0)
BasePath = "S:\Temp\DocSource6\Build\"
FramePath = "..\Source\books\" & DocSet & "\"
InstallPath = "..\Output\" & DocSet & "\docs\"
WWPath = FramePath
ExamplesPath = "..\Output\" & DocSet & "\chart_root\examples\"
logfile = "log.txt"


'-----------------------------
'  PROCESS COMMAND LINE ARGS
'-----------------------------
If WScript.Arguments.Count = 0 Then
  NoArgs "ERROR: Script Started without Proper Command Line Arguments.  See " & logfile & " for correct syntax."
Else
  Dim NextAction
   For Each ARG In WScript.Arguments
    'LogIt "Processing Command Line Arg: " & ARG
    If String(1,ARG) = "/" THEN
      NextAction = ""
    End If
    If NextAction = "logfile" THEN
      logfile = ARG
      NextAction = ""
    ElseIf NextAction = "framepath" THEN
      FramePath = ARG
      NextAction = ""
    ElseIf NextAction = "basepath" THEN
      BasePath = ARG
      NextAction = ""
    ElseIf NextAction = "installpath" THEN
      InstallPath = ARG
      NextAction = ""
    ElseIf NextAction = "examplepath" THEN
      ExamplesPath = ARG
      NextAction = ""
    ElseIf NextAction = "wwpath" THEN
      WWPath = ARG
      NextAction = ""
    ElseIf NextAction = "books" THEN
      ReDim Preserve Books(NumBooks)
      Books(NumBooks) = ARG
      'LogIt "Adding Book " & ARG & " Which is " & Books(NumBooks)
      NumBooks = NumBooks + 1
    Else
      Select Case ARG
        Case "/?"
          NoArgs("HELP: See " & logfile & " for syntax.")
        Case "/nolog"
          DoLog = False
        Case "/nopdf"
          DoPDF = False
        Case "/nobuild"
          DoBuild = False
        Case "/noww"
          DoWW = False
        Case "/noopen"
          DoOpen = False
        Case "/noupdate"
          DoUpdate = False
        Case "/logfile"
          NextAction = "logfile"
        Case "/books"
          NextAction = "books"
        Case "/framepath"
          NextAction = "framepath"
        Case "/installpath"
          NextAction = "installpath"
        Case "/examplepath"
          NextAction = "examplepath"
        Case "/basepath"
          NextAction = "basepath"
        Case "/wwpath"
          NextAction = "wwpath"
        Case "/close"
          DoClose = True
        Case "/noclose"
          DoClose = False
      End Select
    End If
	Next
End If
If NumBooks = 0 Then
  NoArgs "ERROR: No Books Were Specified. See " & logfile & " for correct syntax."
End If

Sub NoArgs(msg)
    MsgBox msg
	  LogIt msg
	  LogIt " Syntax:		MAKE_PDF.VBS /?"
	  LogIt " Example:	make_pdf.vbs"
    CloseLog
    WScript.Quit
End Sub

'--------------------------------------
'  WAIT FOR WINDOW
'--------------------------------------

Sub WaitForWindow(WindowName)
	While Not WshShell.AppActivate(WindowName)
		LogIt " Waiting for " & WindowName 'Debug
		WScript.Sleep 500
	Wend
  WScript.Sleep 500
End Sub


'---------------------------------------
'  UPDATEBOOK
'  - opens and updates specified book
'---------------------------------------

Sub UpdateBook(BookName)
  'LogIt "Trying to Open " & FramePath & BookName & "\" & BookName & ".book"
	WshShell.run FramePath & BookName & "\" & BookName & ".book",1
	LogIt "Opened " & FramePath & BookName & "\" & BookName & ".book"
	WaitForWindow "Adobe FrameMaker"

	If DoUpdate Then
		WScript.Sleep 3000
		WshShell.SendKeys "%EB"
		
    WaitForWindow "Update Book"
    
		WshShell.SendKeys "{Enter}"
		WScript.Sleep 6000
		If WshShell.AppActivate("Inconsistent Numbering Properties") Then
			WshShell.SendKeys " {Tab}{Enter}"
		End If
		WScript.Sleep 10000
		LogIt "Updated " & FramePath & BookName & "\" & BookName & ".book"
	End If
End Sub

'------------------------------------------
'  UPDATEWWBOOK
'  - opens and updates a book for webworks
'------------------------------------------

Sub UpdateWWBook(BookName)
  'LogIt "Trying to Open " & FramePath & BookName & "\" & BookName & "_ww.book"
	WshShell.run FramePath & BookName & "\" & BookName & "_ww.book",1
	LogIt "Opened " & FramePath & BookName & "\" & BookName & "_ww.book"
	WaitForWindow "Adobe FrameMaker"

	If DoUpdate Then
		WScript.Sleep 2000
		WshShell.SendKeys "%EB"
		
    WaitForWindow "Update Book"
    
		WshShell.SendKeys "{Enter}"
		WScript.Sleep 6000
		If WshShell.AppActivate("Inconsistent Numbering Properties") Then
			WshShell.SendKeys " {Tab}{Enter}"
		End If
		WScript.Sleep 10000
		LogIt "Updated " & FramePath & BookName & "\" & BookName & "_ww.book"
	End If

End Sub

'----------------------------------------
'  OPENPROJECT
'  - opens a webworks project
'----------------------------------------

Sub OpenProject(BookName)
  On Error Resume Next
  WshShell.run WWPath & BookName & "\" & BookName & ".wdt",1
  LogIt "Opened " & WWPath & BookName & "\" & BookName & ".wdt" & ""
  WScript.Sleep 4000
  While WshShell.AppActivate("Progress Indicator")
	WScript.Sleep 1000
  Wend
  WScript.Sleep 2000
End Sub


'---------------------------------------
'  GENERATEPDF
'  - generates PDF from specified book
'---------------------------------------

Sub GeneratePDF(BookName)
If DoBuild Then

	Dim fso
	Set fso = CreateObject("Scripting.FileSystemObject")


	'MsgBox "Generating PDF" 
	If fso.FileExists(InstallPath & BookName & "\" & BookName & ".pdf") Then
		fso.DeleteFile InstallPath & BookName & "\" & BookName & ".pdf", True
		LogIt "Deleted " & InstallPath & BookName & "\" & BookName & ".pdf" & ""
	End If

	WScript.Sleep 2000
	LogIt "Opened " & FramePath & BookName & "\" & BookName & ".book" & ""
	WshShell.run FramePath & BookName & "\" & BookName & ".book",1
  
	WaitForWindow "Adobe FrameMaker"
	WScript.Sleep 8000
	WshShell.SendKeys "%Fa"
  
	WaitForWindow "Save Book"
	WshShell.SendKeys BasePath & InstallPath & BookName & "\" & BookName & ".pdf{Tab}p{Enter}"

	WaitForWindow "PDF Setup for Selected Files"

	WshShell.SendKeys "{Enter}"
	
	While Not fso.FileExists(InstallPath & BookName & "\" & BookName & ".pdf")
		WScript.Sleep 1000
		If WshShell.AppActivate("Inconsistencies in Book") Then
			WshShell.SendKeys " {Tab}{Enter}"
		End If
		'LogIt "Waiting for PDF Generation " & InstallPath & BookName & "\" & BookName & ".pdf" 'Debug
		LogIt "Waiting for PDF Generation " & BasePath & InstallPath & BookName & "\" & BookName & ".pdf"
	Wend

	LogIt "" & FramePath & BookName & "\" & BookName & ".pdf" & " Was Succesfully Generated (" & Count & ")" & ""

	WScript.Sleep 1000
	WshShell.AppActivate "Adobe Framemaker"
	WshShell.SendKeys "^s"
	WScript.Sleep 1000
End If
End Sub


'-------------------------------------------
'  GENERATEWW
'  - generates WebWorks from specified book
'-------------------------------------------

Sub GenerateWW(CBook,InstallPath)
Logit "Generating WW for " & CBook & ": " & Books(CBook-1)
'MsgBox "Generating WW for " & CBook & ": " & Books(CBook-1)
If DoBuild Then
	WScript.Sleep 500
	WshShell.AppActivate "WebWorks Publisher"
	WshShell.SendKeys "{Enter}"
	WScript.Sleep 500
	WshShell.AppActivate "WebWorks Publisher Professional Edition"
  WshShell.SendKeys "%W"
  If NumBooks > 9 Then
	  WshShell.SendKeys "M"
	  WaitForWindow "Select Window"
	Dim DownPresses
	If CBook = NumBooks Then
		DownPresses = 9
	ElseIf CBook > 8 Then
		DownPresses = CBook
	Else
		DownPresses = CBook-1
	End If
  	For Loop1 = 1 to NumBooks
		  WshShell.SendKeys "{UP}"
	  Next
  	For Loop1 = 1 to DownPresses
		  WshShell.SendKeys "{DOWN}"
	  Next
	  WshShell.SendKeys "{Enter}"
  Else
	  WshShell.SendKeys CBook
  End If
  WScript.Sleep 500
  WshShell.SendKeys "%PD"
  WaitForWindow "WebWorks Publisher"
  WshShell.SendKeys "{Enter}"
  WScript.Sleep 3000
  WshShell.SendKeys "^a"
  While Not fso.FileExists(InstallPath & "\images\tabledashedline.gif")
    Logit "Waiting for " & InstallPath & "\images\tabledashedline.gif"
    WScript.Sleep 1000
  Wend

  While WshShell.AppActivate("Progress Indicator")
	LogIt "Waiting for Progress Indicator to disappear"
	WScript.Sleep 1000
  Wend
  WScript.Sleep 1000

End If
End Sub


'---------------------------------------
' log routine
'---------------------------------------

Sub LogIt(str)
  If DoLog Then
	Dim f
	Set f = fso.OpenTextFile(logfile, 8, True)
	f.WriteLine Now & ": " & str
	f.Close
  End If
End Sub

Sub CloseLog()
  If DoLog Then
	Dim f
	Set f = fso.OpenTextFile(logfile, 8, True)
	f.WriteLine "---------------------------------------------------------"
	f.WriteLine ""
	f.WriteLine ""
	f.Close
  End If
End Sub


'---------------------------------------
' close porgram routines
'---------------------------------------

Sub CloseWW()
    If WshShell.AppActivate("WebWorks Publisher Professional Edition") Then
	WScript.Sleep 1000
    	WshShell.SendKeys "%{F4}"
      WScript.Sleep 2000
      While WshShell.AppActivate("WebWorks Publisher")
        WshShell.SendKeys "%{F4}{Enter}"
        WScript.Sleep 2000
      Wend
    End If
End Sub


Sub CloseFM()
	If WshShell.AppActivate("Adobe FrameMaker") Then
		WScript.Sleep 3000
		'MsgBox "Closing Framemaker"
		If WshShell.AppActivate("FrameMaker Console") Then
			'MsgBox "Closing Console"
			WScript.Sleep 1000
			WshShell.SendKeys "%{F4}"
		End If
		WScript.Sleep 1000
		WshShell.AppActivate("Adobe FrameMaker")
		WScript.Sleep 1000
		WshShell.SendKeys "%{F4}"
		WScript.Sleep 1000
		While WshShell.AppActivate("FrameMaker")
			'MsgBox "Closing Program"
			'If WshShell.AppActivate("FrameMaker Console") Then
				'MsgBox "Closing Console"
				'WshShell.SendKeys "%{F4}"
				'WScript.Sleep 1000
			'End If
			'WshShell.AppActivate("FrameMaker")
			WScript.Sleep 1000
			WshShell.SendKeys "{Enter}"
			WScript.Sleep 1000
		Wend
	End If
End Sub

'---------------------------------------
' MAIN
'---------------------------------------

Dim Book, CBook
WScript.Sleep 500


'Start PDF Script
If DoPDF Then
  If DoClose Then CloseFM

  LogIt "PDF Generation Script Started"
  If WshShell.AppActivate("Acrobat Distiller") Then
	  WshShell.SendKeys "%{F4}"
  End If
  For Each Book in Books
    UpdateBook Book
  Next
	For Each Book in Books
		GeneratePDF Book
	Next
	LogIt "Finished Generating PDF"
  If DoClose Then CloseFM
End If


'Start WW Script
If DoWW Then
  LogIt "WebWorks Doc Generation Script Started"
  For Each Book in Books
    UpdateWWBook Book
  Next  
  If DoOpen Then
    WScript.Sleep 1000
    CloseWW
    For CBook = 1 to NumBooks
      OpenProject Books(CBook-1)
    Next  
  End If
  For CBook = 1 to NumBooks
	GenerateWW CBook,InstallPath & Books(CBook-1)
  Next
  
  LogIt "Finished Generating WWHelp Docs"
End If


If DoClose Then
	CloseFM
	CloseWW
End If

CloseLog