Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace ScanToolUI
   Public Module modScanToolUI
   Private Const MOD_NAME As String = "ScanToolUI"
   Private logFile As String = ""
   Public Enum EShellShowConstants
      essSW_HIDE = 0
      essSW_MAXIMIZE = 3
      essSW_MINIMIZE = 6
      essSW_SHOWMAXIMIZED = 3
      essSW_SHOWMINIMIZED = 2
      essSW_SHOWNORMAL = 1
      essSW_SHOWNOACTIVATE = 4
      essSW_SHOWNA = 8
      essSW_SHOWMINNOACTIVE = 7
      essSW_SHOWDEFAULT = 10
      essSW_RESTORE = 9
      essSW_SHOW = 5
   End Enum
   Private Const ERROR_FILE_NOT_FOUND As Integer = 2&
   Private Const ERROR_PATH_NOT_FOUND As Integer = 3&
   Private Const ERROR_BAD_FORMAT As Integer = 11&
   Private Const SE_ERR_ACCESSDENIED As Integer = 5 ' access denied
   Private Const SE_ERR_ASSOCINCOMPLETE As Integer = 27
   Private Const SE_ERR_DDEBUSY As Integer = 30
   Private Const SE_ERR_DDEFAIL As Integer = 29
   Private Const SE_ERR_DDETIMEOUT As Integer = 28
   Private Const SE_ERR_DLLNOTFOUND As Integer = 32
   Private Const SE_ERR_FNF As Integer = 2 ' file not found
   Private Const SE_ERR_NOASSOC As Integer = 31
   Private Const SE_ERR_PNF As Integer = 3 ' path not found
   Private Const SE_ERR_OOM As Integer = 8 ' out of memory
   Private Const SE_ERR_SHARE As Integer = 26
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer,ByVal lpOperation As String,ByVal lpFile As String,ByVal lpParameters As String,ByVal lpDirectory As String,ByVal nShowCmd As Integer) As Integer
Private Declare Function ShellExecuteForExplore Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer,ByVal lpOperation As String,ByVal lpFile As String,ByVal lpParameters As String,ByVal lpDirectory As String,ByVal nShowCmd As Integer) As Integer
   Public Sub Main(args As string())
      Const FUNC_NAME As String = "Main"
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         Dim arg As String = ""
         arg = Interaction.Command()
         ' to test batch mode
         ' args = "/optRpt=DirList /Root=c:\promis\demo\_demo  /Log=c:\temp\scantool.tdf /FileFilter=*.xml"

         If (Strings.Len(arg) > 0) Then
            runCommand(arg)
         Else
            Dim frm As frmScantool = Nothing
            frm = New frmScantool
            frm.Text = gmRTL.Core.App.DefInstance.Title & " " & gmRTL.Core.App.DefInstance.Path
            System.Windows.Forms.Application.Run(frm)
         End If

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try

      If (Err.Number <> 0) Then
         System.Windows.Forms.MessageBox.Show(MOD_NAME & "::" & FUNC_NAME & " " & Err.Description, "", MessageBoxButtons.OK)
      End If
   End Sub
   Public Sub runCommand(ByVal args_migname As String)
      Const FUNC_NAME As String = "runCommand"
      Dim rpt As String = ""
      Dim scanControl As ScanToolLib.clsScanControl = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         logFile = getArg("/log=",Interaction.Command(),"scantool.tab")

         writeLog("[BGN] **************************************** " & Now)

         scanControl = New ScanToolLib.clsScanControl

         Dim configFile As String = ""
         configFile = getArg("/Config=",args_migname,"")

         If (Strings.Len(configFile) > 0) Then
            scanControl.loadScantoolConfig(configFile)
         Else
            scanControl.txtRootPath = getArg("/Root=",args_migname,".")
            scanControl.optRpt = getArg("/OptRpt=",args_migname,"DirList")
            scanControl.txtFilFilter = getArg("/FileFilter=",args_migname,"*")
            scanControl.txtFolderFilter = getArg("/FolderFilter=",args_migname,"*")
            scanControl.txtSaveTo = scanControl.optRpt & ".tab"
         End If

         scanControl.setFileProcessor()
         scanControl.runReport()


      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try

      If (Err.Number <> 0) Then
         rpt = "ERRORS: " & MOD_NAME & "::" & FUNC_NAME & " " & Err.Description
      End If

      rpt = scanControl.Show() & rpt

      writeLog(rpt & "[END] **************************************** " & Now)

      scanControl = Nothing

   End Sub
   Public Sub writeLog(ByVal rpt As String)
      Const FUNC_NAME As String = "writeLog"
      Dim txt As System.IO.StreamWriter = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         txt = (new System.IO.FileInfo(logFile)).AppendText()
         Debug.WriteLine(rpt)
         txt.WriteLine(rpt)

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try

      txt.Close()

      txt = Nothing

   End Sub
   Private Function getArg(ByVal arg As String,ByVal cml As String,ByVal defVal As String) As String
      getArg = ""
      Const FUNC_NAME As String = "getArg"
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         getArg = defVal

         Dim iStartThisToken As Integer = 0
         iStartThisToken = Strings.InStr(1,cml,arg,CompareMethod.Text)
         If (iStartThisToken > 0) Then
            Dim iStartNextToken As Integer = 0
            iStartNextToken = Strings.InStr(iStartThisToken + Strings.Len(arg),cml," /",CompareMethod.Binary)
            If (iStartNextToken = 0) Then
               iStartNextToken = Strings.Len(cml) + 1
            End If
            getArg = Mid(cml,iStartThisToken + Strings.Len(arg),iStartNextToken - (iStartThisToken + Strings.Len(arg)))
            getArg = Strings.Trim(Strings.Replace(getArg,"""",""))
         End If
         Debug.WriteLine(arg & "[" & getArg & "]")

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try

      If (Err.Number <> 0) Then
         Err.Raise(17,String.Empty,MOD_NAME & "::" & FUNC_NAME & " COMMAND LINE PARSINF ERROR: USAGE " & gmRTL.Core.App.DefInstance.EXEName & " {/Config=file.xml /Root=x /OptRpt=x} /log=ErrLog")
      End If
   End Function
   Public Function ShellEx(ByVal sFIle As String,Optional ByVal eShowCmd As EShellShowConstants = EShellShowConstants.essSW_SHOWDEFAULT,Optional ByVal sParameters As String = "",Optional ByVal sDefaultDir As String = "",Optional ByVal sOperation As String = "open",Optional ByVal Owner As Integer = 0) As Boolean
      ShellEx = False

      Dim lR As Integer = 0
      Dim lErr As Integer = 0
      Dim sErr As String = ""

      If (Strings.InStr(1,Strings.UCase(sFIle),".EXE",CompareMethod.Binary) <> 0) Then
         eShowCmd = EShellShowConstants.essSW_HIDE
      End If
      gmRTL.Core.ErrorHandling.ResumeNext(Sub()
         If (sParameters = "" And sDefaultDir = "") Then
            lR = ShellExecuteForExplore(Owner,sOperation,sFIle,"","",CInt(EShellShowConstants.essSW_SHOWNORMAL))
         Else
            lR = ShellExecute(Owner,sOperation,sFIle,sParameters,sDefaultDir,CInt(eShowCmd))
         End If
         If (lR < 0 Or lR > 32) Then
            ShellEx = True
         Else
            ' raise an appropriate error:
            ShellEx = False
            lErr = Constants.vbObjectError + 1048 + lR
            Select Case lR
               Case 0
                  lErr = 7
                  sErr = "Out of memory"
               Case ERROR_FILE_NOT_FOUND
                  lErr = 53
                  sErr = "File not found"
               Case ERROR_PATH_NOT_FOUND
                  lErr = 76
                  sErr = "Path not found"
               Case ERROR_BAD_FORMAT
                  sErr = "The executable file is invalid or corrupt"
               Case SE_ERR_ACCESSDENIED
                  lErr = 75
                  sErr = "Path/file access error"
               Case SE_ERR_ASSOCINCOMPLETE
                  sErr = "This file type does not have a valid file association."
               Case SE_ERR_DDEBUSY
                  lErr = 285
                  sErr = "The file could not be opened because the target application is busy. Please try again in a moment."
               Case SE_ERR_DDEFAIL
                  lErr = 285
                  sErr = "The file could not be opened because the DDE transaction failed. Please try again in a moment."
               Case SE_ERR_DDETIMEOUT
                  lErr = 286
                  sErr = "The file could not be opened due to time out. Please try again in a moment."
               Case SE_ERR_DLLNOTFOUND
                  lErr = 48
                  sErr = "The specified dynamic-link library was not found."
               Case SE_ERR_NOASSOC
                  sErr = "No application is associated with this file type."
               Case SE_ERR_OOM
                  lErr = 7
                  sErr = "Out of memory"
               Case SE_ERR_SHARE
                  lErr = 75
                  sErr = "A sharing violation occurred."
               Case Else
                  sErr = "An error occurred occurred while trying to" & sOperation & " the selected file."
            End Select

            Err.Raise(lErr,String.Empty,gmRTL.Core.App.DefInstance.EXEName & "ShellEx " & sErr)
         End If

      End Sub)
   End Function
   End Module
End Namespace
