#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Description=ClipPutIncludes
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=by Oscar (www.autoit.de)
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#Region ;**** Includes created by ClipPutIncludes ****
#include <Array.au3>
#include <File.au3>
#EndRegion ;**** Includes created by ClipPutIncludes ****

Global $sTitle = 'ClipPutIncludes v1.0'

Global $sRegKey = 'HKEY_LOCAL_MACHINE64\SOFTWARE\', $sStablePath, $sStableError, $sBetaPath, $sBetaError
If @OSArch <> 'X86' Then $sRegKey &= 'Wow6432Node\'
$sStablePath = RegRead($sRegKey & 'AutoIt v3\AutoIt', 'InstallDir')
$sStableError = @error
$sBetaPath = RegRead($sRegKey & 'AutoIt v3\AutoIt', 'betaInstallDir')
$sBetaError = @error

Global $oDictStable = ObjCreate('Scripting.Dictionary')
Global $oDictBeta = ObjCreate('Scripting.Dictionary')
If Not IsObj($oDictStable) Or Not IsObj($oDictBeta) Then Exit ConsoleWrite("ERROR! Can't create Scripting.Dictionary!" & @CRLF)

Global $iTimeDiff, $iTimer1, $iTimer2, $aRequired, $sOut
Global $sSourcePath, $sUseAutoItVersion = 'stable'

If $CmdLine[0] = 0 Then
	$sSourcePath = FileOpenDialog($sTitle, @WorkingDir & '\', 'AutoIt-Script (*.au3)', 3)
	If @error Then Exit ConsoleWrite('ERROR! Scriptfile not found!' & @CRLF)
Else
	For $i = 1 To $CmdLine[0]
		If $CmdLine[$i] = '-stable' Then $sUseAutoItVersion = 'stable'
		If $CmdLine[$i] = '-beta' Then $sUseAutoItVersion = 'beta'
		If $CmdLine[$i] = '-s' And $CmdLine[0] > $i Then $sSourcePath = $CmdLine[$i + 1]
	Next
	If $sSourcePath = '' Then
		$sSourcePath = FileOpenDialog($sTitle, @WorkingDir & '\', 'AutoIt-Script (*.au3)', 3)
		If @error Then Exit ConsoleWrite('ERROR! Scriptfile not found!' & @CRLF)
	EndIf
	If Not FileExists($sSourcePath) Then Exit ConsoleWrite('ERROR! Scriptfile not exists!' & @CRLF)
EndIf

$iTimer1 = TimerInit()
ConsoleWrite('# Start: Read Includes' & @CRLF)
Switch $sUseAutoItVersion
	Case 'stable'
		If $sStableError Then Exit ConsoleWrite('ERROR! AutoIt (stable) not installed!' & @CRLF)
		_GetIncludeFuncsAndVars($sStablePath & '\Include', $oDictStable)
	Case 'beta'
		If $sBetaError Then Exit ConsoleWrite('ERROR! AutoIt (beta) not installed!' & @CRLF)
		_GetIncludeFuncsAndVars($sBetaPath & '\Include', $oDictBeta)
EndSwitch
$iTimeDiff = Int(TimerDiff($iTimer1))
ConsoleWrite('# Read Includes complete: ' & $iTimeDiff & ' ms' & @CRLF)

$iTimer2 = TimerInit()
ConsoleWrite('# Start: Analyse Script' & @CRLF)
Switch $sUseAutoItVersion
	Case 'stable'
		$aRequired = _GetRequiredIncludes($sSourcePath, $oDictStable)
		If @error = 2 Then Exit ConsoleWrite("ERROR! Can't open Scriptfile!" & @CRLF)
	Case 'beta'
		$aRequired = _GetRequiredIncludes($sSourcePath, $oDictBeta)
		If @error = 2 Then Exit ConsoleWrite("ERROR! Can't open Scriptfile!" & @CRLF)
EndSwitch
$aRequired = _ArrayUnique($aRequired)
ConsoleWrite('# Analyse Script complete: ' & Int(TimerDiff($iTimer2)) & ' ms' & @CRLF)
ConsoleWrite('# Totaltime: ' & Int(TimerDiff($iTimer1)) & ' ms' & @CRLF)

ConsoleWrite('# Copy includes to Clipboard!' & @CRLF)
$sOut = '#Region ;**** Includes created by ClipPutIncludes ****' & @CRLF
For $i = 1 To $aRequired[0]
	$sOut &= $aRequired[$i] & @CRLF
Next
$sOut &= '#EndRegion ;**** Includes created by ClipPutIncludes ****' & @CRLF
ClipPut($sOut)
ConsoleWrite('# Ready.' & @CRLF)
Exit

Func _GetIncludeFuncsAndVars($sPath, ByRef $oDict)
	If Not IsObj($oDict) Then Return SetError(1, 0, 0)
	Local $aIncludeFiles, $aAll[1][2], $sFile, $aTmp, $iCount
	$aIncludeFiles = _FileListToArray($sPath, '*.au3', 1)
	If @error Then Return SetError(2, 0, 0)
	For $i = 1 To $aIncludeFiles[0]
		$sFile = FileRead($sPath & '\' & $aIncludeFiles[$i])
		$aTmp = StringRegExp($sFile, '(?s)(?:Global|Global Const) (\$.+?)(?:\;|\,|\h)', 3)
		If IsArray($aTmp) Then
			For $j = 0 To UBound($aTmp) - 1
				If Not $oDict.Exists($aTmp[$j]) Then $oDict.add($aTmp[$j], $aIncludeFiles[$i])
			Next
		EndIf
		$aTmp = StringRegExp($sFile, '(?s)\vFunc (.+?)\h*\(', 3)
		If IsArray($aTmp) Then
			For $j = 0 To UBound($aTmp) - 1
				If Not $oDict.Exists($aTmp[$j]) Then $oDict.add($aTmp[$j], $aIncludeFiles[$i])
			Next
		EndIf
	Next
EndFunc   ;==>_GetIncludeFuncsAndVars

Func _GetRequiredIncludes($sScriptPath, ByRef $oDict)
	If Not IsObj($oDict) Then Return SetError(1, 0, 0)
	Local $sFile, $sOut
	$sFile = FileRead($sScriptPath)
	If @error Then Return SetError(2, 0, 0)
	For $strKey In $oDict.Keys
		If StringRegExp($sFile, StringReplace($strKey, '$', '\$')) Then
			ConsoleWrite(StringFormat('     %-40s -> %-40s', $strKey, $oDict.Item($strKey)) & @CR)
			$sOut &= '#include <' & $oDict.Item($strKey) & '>' & '|'
		EndIf
	Next
	If $sOut = '' Then Return
	Return StringSplit(StringTrimRight($sOut, 1), '|', 2)
EndFunc   ;==>_GetRequiredIncludes
