; Video to MKV Convert
; by Lehmden 2013
; Version 0.2.0

$version = "0.2.0"

; GUI Includes
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <Array.au3>
#include <ListBoxConstants.au3>
#include <ProgressConstants.au3>
#include <FileEx.au3>

Global $indir, $outdir

; Ini Read Main Config
If FileExists(@ScriptDir & "\Video2mkv_convert.ini") Then
	$indir = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "folder", "input", @MyDocumentsDir)
	$outdir = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "folder", "output", @MyDocumentsDir)
	$language = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "languages", "language", "English")
	$tvs = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "settings", "MPTvSeries2MKV", "")
	$deleteorg = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "settings", "deleteorg", "4")
	$deletetag = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "settings", "deletetag", "4")
	$ffm = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "settings", "FFMpeg", @ScriptDir & "\ffmpeg.exe")
	$mkvm = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "settings", "mkvmerge", @ScriptDir & "\mkvmerge.exe")
	$inandout = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "settings", "inandout", "4")
	
Else
	$indir = @MyDocumentsDir
	$outdir = @MyDocumentsDir
	$language = "English"
	$tvs = ""
	$deleteorg = "4"
	$deletetag = "4"
	$ffm = @ScriptDir & "\ffmpeg.exe"
	$mkvm = @ScriptDir & "\mkvmerge.exe"
	$inandout = "4"
	
Endif

; Ini Read Localized Strings
$InputFolderText = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "InputFolderText", "Choose Source Directory (with Videos to convert)")
$OuputFolderText = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "OuputFolderText", "Choose Destination Directory (for converted Videos)")
$Start = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Start", "Start")
$Pleasewait = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Pleasewait", "Please wait")
$cancel = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "cancel", "Cancel")
$StartConversion = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "StartConversion", "Start Conversion?")
$ConversionText1 = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "ConversionText1", "By pressing OK all Videos in")
$ConversionText2 = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "ConversionText2", "\ will be converted to MKV. It will take a while. Once started, process can't be aborted. If you don't want this please press Exit")
$Conversion = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Conversion", "Conversion")
$isstarted = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "isstarted", "is startet")
$isabort = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "isabort", "is aborted")
$errmsg = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "errmsg", "An Error occured")
$NoSrcDir = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "NoSrcDir", "No Source Directory choosen")
$NoDestDir = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "NoDestDir", "No Destination Directory choosen")
$NoFiles = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "NoFiles", "No (Video-) files found in Source Directory")
$Files = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Files", "File")
$Title = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Title", "Video2MKV Convert Version")
$ok = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "OK", "OK")
$config = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "config", "Configuration")
$starttvs2m = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "starttvs2m", "Run MPTvSeries2MKV")
$about = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "About", "About")
$exit = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Exit", "Exit")
$SaveConfig = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "SaveConfig", "Save Settings")
$Langchange = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Langchange", "If you change language you need to restart program")
$langselect = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "langselect", "Select Language")
$error = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Error", "Error")
$save = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "Save", "Save")
$NotSave = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "NotSave", "Aborted, Settings not saved")
$areyousure = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "areyousure", "Are you sure?")
$tvs2m = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "tvs2m", "Path to MPTvSeries2MKV.exe")
$search = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "search", "Search")
$choose = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "choose", "Choose")
$delorg = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "delorg", "Delete Originals?")
$danger1 = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "danger1", "Highly Dangerous! use at own risk!")
$deltag = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "deltag", "Delete Tagfiles?")
$danger2 = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "danger2", "Use at own risk! Maybe you need them later.")
$ffmpeg = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "ffmpeg", "Path to FFMpeg.exe")
$mkvmerge = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "mkvmerge", "Path to mkvmerge.exe")
$inout = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "inout", "One Folder?")
$inoutmsg = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "inoutmsg", "Source folder is target folder")
$runmsg = IniRead(@ScriptDir & "\Language\" & $language & ".ini", "Translation", "runmsg", "Pressing OK will convert")

Global $ffmfile2 = $indir
Global $mkvfile2 = $outdir
Global $InputFolder = $indir
Global $OutputFolder = $outdir

; Get initial working dir
$wd = @WorkingDir & "\"

;Generate Main Window
$style = $WS_SYSMENU + $WS_CAPTION + $WS_MINIMIZEBOX + $WS_EX_ACCEPTFILES 

$mainwindow = GUICreate($Title & ": " & $version, 700, 500, -1, -1, $style) 
GUISetIcon(@ScriptDir & "\logo-128x128.ico")

;Generate Generic Controls
GUICtrlCreateLabel($Title, 10, 10, 500, 90, $SS_ETCHEDFRAME)
GUICtrlCreatePic (@ScriptDir & "\header.bmp", 15, 15, 500, 75)
GUICtrlCreatePic (@ScriptDir & "\logo.bmp", 550, 10, 128, 128)

;Generate Main Menu
$filemenu = GUICtrlCreateMenu($Files)
$fileitem = GUICtrlCreateMenuItem($starttvs2m, $filemenu)
$separator1 = GUICtrlCreateMenuItem("", $filemenu, 2)
$exititem = GUICtrlCreateMenuItem($exit, $filemenu)
$helpmenu = GUICtrlCreateMenu("?")
$infoitem = GUICtrlCreateMenuItem($about, $helpmenu)
$configmenu = GUICtrlCreateMenu($config, -1, 1)
$configitem = GUICtrlCreateMenuItem($config, $configmenu)

;Show Main Windows
GUISetState(@SW_SHOW)


;Function Declaration

;Function to run MPTvSeries2MKV.exe
Func MPTvSeries2MKV()
	ShellExecuteWait ($tvs)	
EndFunc

;Function to close Program
Func Closeprogram()
	IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "folder", "input", GUICtrlRead($ffmfile2))
	IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "folder", "output", GUICtrlRead($mkvfile2))
	Exit
EndFunc

;Function to Create Main GUI
Func Main()

	$okbutton = GUICtrlCreateButton($ok, 560, 440, 60)
	GUICtrlSetState(-1, $GUI_DEFBUTTON)
	$cancelbutton = GUICtrlCreateButton($exit, 630, 440, 60)

	$label5 = GUICtrlCreateLabel($InputFolderText, 150, 210)
	$ffmfile2 = GUICtrlCreateInput($indir, 150, 230, 300, 20)
	$button1a = GUICtrlCreateButton($search, 460, 227, 60)
	$label6 = GUICtrlCreateLabel($OuputFolderText, 150, 270)
	$mkvfile2 = GUICtrlCreateInput($outdir, 150, 290, 300, 20)
	$button2a = GUICtrlCreateButton($search, 460, 287, 60)
	$label4 = GUICtrlCreateLabel($ConversionText1 & " " & $indir & $ConversionText2, 50, 340, 600, 56, $SS_CENTER)
	GUICtrlSetFont(-1, 12)

	if $inandout = "1" then
		$outdir = $indir
		GUICtrlSetState($button2a, $GUI_HIDE)
		GUICtrlSetState($label6, $GUI_HIDE)
		GUICtrlSetState($mkvfile2, $GUI_HIDE)
	endif

	While 1
		$msg1 = GUIGetMsg(1)
		Select
			Case $msg1[0] = $okbutton
				$box = MsgBox(1, $StartConversion, $areyousure)
				if $box = "1" then
					GUICtrlDelete($okbutton)
					GUICtrlDelete($cancelbutton)
					GUICtrlDelete($button1a)
					GUICtrlDelete($button2a)
					GUICtrlDelete($label4)
					GUICtrlDelete($label5)
					GUICtrlDelete($label6)
					GUICtrlDelete($ffmfile2)
					GUICtrlDelete($mkvfile2)
					Conversation()
				endif

			Case $msg1[0] = $cancelbutton
				Closeprogram()
				Return
			
			Case $msg1[0] = $configitem
				GUICtrlDelete($okbutton)
				GUICtrlDelete($cancelbutton)
				GUICtrlDelete($button1a)
				GUICtrlDelete($button2a)
				GUICtrlDelete($label4)
				GUICtrlDelete($label5)
				GUICtrlDelete($label6)
				GUICtrlDelete($ffmfile2)
				GUICtrlDelete($mkvfile2)
				Configuration()
				Return

			Case $msg1[0] = $button1a
				GUICtrlDelete($ffmfile2)
				$InputFolder = FileSelectFolder($InputFolderText, "",6, $indir)
				$ffmfile2 = GUICtrlCreateInput($InputFolder, 150, 230, 300, 20)
				if $inandout = "1" then
					$outdir = $indir
					$OutputFolder = $InputFolder
					GUICtrlSetState($button2a, $GUI_HIDE)
					GUICtrlSetState($label6, $GUI_HIDE)
					GUICtrlSetState($mkvfile2, $GUI_HIDE)
				Endif
			Case $msg1[0] = $button2a
				GUICtrlDelete($mkvfile2)
				$OutputFolder = FileSelectFolder($OuputFolderText, "",6, $outdir)
				$mkvfile2 = GUICtrlCreateInput($OutputFolder, 150, 290, 300, 20)
			
			Case $msg1[0] = $GUI_EVENT_CLOSE OR $msg1[0] = $exititem
				Closeprogram()
			
			case $msg1[0] = $infoitem 
				MsgBox (0, $about & " " & $Title, "Video-2-MKV Convert GUI Version" &  @CRLF & @CRLF & "(C) 2013 Lehmden" &  @CRLF & @CRLF & "Version: " & $version)
			
			case $msg1[0] = $fileitem 
				MPTvSeries2MKV()
			
		EndSelect

	WEnd

	GUICtrlDelete($okbutton)
	GUICtrlDelete($cancelbutton)
	GUICtrlDelete($button1a)
	GUICtrlDelete($button2a)
	GUICtrlDelete($label4)
	GUICtrlDelete($label5)
	GUICtrlDelete($label6)
	GUICtrlDelete($ffmfile2)
	GUICtrlDelete($mkvfile2)

EndFunc

;Function to Create Configuration GUI
Func Configuration()
	DIM $Arraylng[1]
	$search1 = FileFindFirstFile(@ScriptDir & "\language\*.ini")  
		If $search1 = -1 Then
			MsgBox(0, $error, $errmsg)
			Exit
		EndIf

		While 1
			$file = FileFindNextFile($search1) 
			If @error Then ExitLoop
			_ArrayAdd($Arraylng, $file)
		WEnd
	FileClose($search1)

	$okbutton = GUICtrlCreateButton($save, 560, 440, 60)
	GUICtrlSetState(-1, $GUI_DEFBUTTON)
	$cancelbutton = GUICtrlCreateButton($cancel, 630, 440, 60)

	$label = GUICtrlCreateLabel($config, 15, 120, 300)
	$label1 = GUICtrlCreateLabel($langselect, 15, 150)
	$combo = GUICtrlCreateCombo($Language, 15, 170)
	$label2 = GUICtrlCreateLabel($tvs2m, 150, 150)
	$tvsfile = GUICtrlCreateInput($tvs, 150, 170, 300, 20)
	$button = GUICtrlCreateButton($search, 460, 167, 60)
	$label5 = GUICtrlCreateLabel($ffmpeg, 150, 210)
	$ffmfile = GUICtrlCreateInput($ffm, 150, 230, 300, 20)
	$button1 = GUICtrlCreateButton($search, 460, 227, 60)
	$label6 = GUICtrlCreateLabel($mkvmerge, 150, 270)
	$mkvfile = GUICtrlCreateInput($mkvm, 150, 290, 300, 20)
	$button2 = GUICtrlCreateButton($search, 460, 287, 60)
	$label7 = GUICtrlCreateCheckbox($inout, 15, 210)
	$label8 = GUICtrlCreateLabel($inoutmsg, 15, 230, 110, 28)
	$label3a = GUICtrlCreateCheckbox($deltag, 15, 270)
	$label4a = GUICtrlCreateLabel($danger2, 15, 290, 110, 28)
	$label3 = GUICtrlCreateCheckbox($delorg, 15, 330)
	$label4 = GUICtrlCreateLabel($danger1, 15, 350, 110, 28)

	;$combo = GUICtrlCreateCombo($Language, 15, 170)

	if $deleteorg = "1" Then
		$check = $GUI_CHECKED
	Else
		$check = $GUI_UNCHECKED
	Endif

	if $deletetag = "1" Then
		$check2 = $GUI_CHECKED
	Else
		$check2 = $GUI_UNCHECKED
	Endif
	
	if $inandout = "1" Then
		$check1 = $GUI_CHECKED
	Else
		$check1 = $GUI_UNCHECKED
	Endif

	GUICtrlSetFont($label, 12)
	GUICtrlSetColor($label4, 0xff0000)
	GUICtrlSetState($label3, $check)
	GUICtrlSetState($label7, $check1)
	GUICtrlSetState($label3a, $check2)


	for $I = 1 to _ArrayMaxIndex($Arraylng)
		GUICtrlSetData($combo, StringTrimRight($Arraylng[$I], 4), $Language)
	Next

	dim $tvsfile1, $ffmfile1, $mkvfile1
	While 1
		$msg1 = GUIGetMsg(1)
		Select
			Case $msg1[0] = $okbutton
				MsgBox(0, $Save, $SaveConfig & @CRLF & @CRLF & $Langchange)
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "languages", "language", GUICtrlRead($combo))
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "settings", "MPTvSeries2MKV", $tvs)
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "settings", "deleteorg", GUICtrlRead($label3))
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "settings", "deletetag", GUICtrlRead($label3a))
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "settings", "ffmpeg", $ffm)
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "settings", "mkvmerge", $mkvm)
				IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "settings", "inandout", GUICtrlRead($label7))
				GUICtrlDelete($label)
				GUICtrlDelete($label1)
				GUICtrlDelete($combo)
				GUICtrlDelete($tvsfile1)
				GUICtrlDelete($tvsfile)
				GUICtrlDelete($okbutton)
				GUICtrlDelete($cancelbutton)
				GUICtrlDelete($button)
				GUICtrlDelete($button1)
				GUICtrlDelete($button2)
				GUICtrlDelete($label2)
				GUICtrlDelete($label3)
				GUICtrlDelete($label4)
				GUICtrlDelete($label3a)
				GUICtrlDelete($label4a)
				GUICtrlDelete($label5)
				GUICtrlDelete($label6)
				GUICtrlDelete($label7)
				GUICtrlDelete($label8)
				GUICtrlDelete($ffmfile1)
				GUICtrlDelete($ffmfile)
				GUICtrlDelete($mkvfile1)
				GUICtrlDelete($mkvfile)
				main()

			Case $msg1[0] = $cancelbutton
				MsgBox(0, $cancel, $NotSave)
				GUICtrlDelete($label)
				GUICtrlDelete($label1)
				GUICtrlDelete($combo)
				GUICtrlDelete($tvsfile1)
				GUICtrlDelete($tvsfile)
				GUICtrlDelete($okbutton)
				GUICtrlDelete($cancelbutton)
				GUICtrlDelete($button)
				GUICtrlDelete($button1)
				GUICtrlDelete($button2)
				GUICtrlDelete($label2)
				GUICtrlDelete($label3)
				GUICtrlDelete($label4)
				GUICtrlDelete($label3a)
				GUICtrlDelete($label4a)
				GUICtrlDelete($label5)
				GUICtrlDelete($label6)
				GUICtrlDelete($label7)
				GUICtrlDelete($label8)
				GUICtrlDelete($ffmfile1)
				GUICtrlDelete($ffmfile)
				GUICtrlDelete($mkvfile1)
				GUICtrlDelete($mkvfile)
				main()

			Case $msg1[0] = $button
				GUICtrlDelete($tvsfile)
				$tvs = FileOpenDialog($choose, @ProgramFilesDir & "\Team MediaPortal\", "Exe (*.exe)")
				$tvsfile1 = GUICtrlCreateInput($tvs, 150, 170, 300, 20)

			Case $msg1[0] = $button1
				GUICtrlDelete($ffmfile)
				$ffm = FileOpenDialog($choose, @ScriptDir & "\", "Exe (*.exe)")
				$ffmfile1 = GUICtrlCreateInput($ffm, 150, 230, 300, 20)
				
			Case $msg1[0] = $button2
				GUICtrlDelete($mkvfile)
				$mkvm = FileOpenDialog($choose, @ScriptDir & "\", "Exe (*.exe)")
				$mkvfile1 = GUICtrlCreateInput($mkvm, 150, 290, 300, 20)
				
			Case $msg1[0] = $GUI_EVENT_CLOSE OR $msg1[0] = $exititem
				Closeprogram()
			
			case $msg1[0] = $fileitem 
				MPTvSeries2MKV()

			case $msg1[0] = $infoitem 
				MsgBox (0, $about & " " & $Title, "Video-2-MKV Convert GUI Version" &  @CRLF & @CRLF & "(C) 2013 Lehmden" &  @CRLF & @CRLF & "Version: " & $version)

		EndSelect

	WEnd

EndFunc

;Main Function 
Func Conversation()

	; Change to Input Folder
	FileChangeDir ( $InputFolder )

	$label4 = GUICtrlCreateLabel("Umwandlung läuft, Bitte warten", 50, 140, 600, 56, $SS_CENTER)
	GUICtrlSetFont(-1, 12)
	$progressbar1 = GUICtrlCreateProgress(50, 250, 600, 20, $SS_CENTER)
    GUICtrlSetColor(-1, 32250)


	if $inandout = "1" then
		$outdir = $indir
		$OutputFolder = $InputFolder
	Endif

	$Array =  _FileListTreeToArray($InputFolder)

 	$step1 = 100 / $Array[0]
	$p=0
	GUICtrlSetData($progressbar1, $p)
	
	Dim $genpath = ""
				
	For $i=1 to $Array[0]

		$InFile = $Array[$i]
		$lenA = StringLen($InFile)  
		$len0 = StringLen($InputFolder) +2 
		$lenB = $lenA - $len0 +1
		$subpath = StringRight($InFile, $lenB)
		$day0 = StringSplit($subpath, "\")
  		$lenC = $day0[0] -1
		$posx = StringInStr($subpath, "\", 0, -1)
		$posy = StringLen($subpath) - $posx
		$genpath = Stringleft($subpath, $posx)
		DirCreate ($OutputFolder & "\" & $genpath)
		$days = StringSplit($InFile, "\")
		$len = $days[0] 
		$FileNew1 = $days[$len]
		$day = StringSplit($FileNew1, ".")
		$len1 = $day[0] 
		$FileNew2 = $day[$len1]
		$pos = StringInStr($FileNew1, ".", 0, -1)
		$FileNew = StringLeft($FileNew1, $pos) & "mkv"
		$FileTag = StringLeft($FileNew1, $pos) & "xml"
		$OutFile = $OutputFolder & "\" & $genpath & $FileNew
		$ext = StringLower ($FileNew2)

		if $ext = "wmv" OR $ext = "mov" then
			$options = ' -i "' & $InFile & '" -acodec copy -vcodec copy "' & $OutputFolder & "\" & "temp.mkv" & '"'
			ShellExecuteWait ($ffm, $options, "", "", @SW_HIDE)
			If FileExists($InputFolder & "\" & $FileTag) Then
				$option = ' -o "' & $OutFile & '" --global-tags ' & '"' & $InputFolder & "\" & $FileTag & '"' & ' "'& $OutputFolder & "\" & "temp.mkv" & '"'
				ShellExecuteWait ($mkvm, $option, "", "", @SW_HIDE)
				FileDelete ( $OutputFolder & "\" & "temp.mkv" )
			Else
				$option = ' -o "' & $OutFile & '"' & ' "'& $OutputFolder & "\" & "temp.mkv" & '"'
				ShellExecuteWait ($mkvm, $option, "", "", @SW_HIDE)	
				FileDelete ( $OutputFolder & "\" & "temp.mkv" )
			EndIf
			if $deleteorg = "1" Then
				Sleep(5)
				FileDelete ( $Infile )	
			Endif
			if $deletetag = "1" Then									
				FileDelete ( $InputFolder & "\" & $FileTag )	
			Endif
		Else
			If $ext = "avi" OR $ext = "mpg" OR $ext = "ts" OR $ext = "mpeg" OR $ext = "mp4" OR $ext = "flv" then
				If FileExists($InputFolder & "\" & $FileTag) Then
					$option = ' -o "' & $OutFile & '" --global-tags ' & '"' & $InputFolder & "\" & $FileTag & '"' & ' "' & $InFile & '"'
					ShellExecuteWait ($mkvm, $option, "", "", @SW_HIDE)
				Else
					$option = ' -o "' & $OutFile & '"' & ' "'& $InFile & '"'
					ShellExecuteWait ($mkvm, $option, "", "", @SW_HIDE)	
				EndIf
				if $deleteorg = "1" Then
					FileDelete ( $Infile )	
				Endif
				if $deletetag = "1" Then									
					FileDelete ( $InputFolder & "\" & $FileTag )	
				Endif
			Endif
		EndIf
		GUICtrlSetData($progressbar1, $p)
		$p += $step1

	Next

	GUICtrlDelete($label4)
	GUICtrlDelete($progressbar1)
	Main()
	return

Endfunc

Main()

;Main GUI Loop
While 1
	$msg = GUIGetMsg(1)

	Select
		Case $msg[0] = $configitem
			Configuration()

		Case $msg[0] = $GUI_EVENT_CLOSE
			Closeprogram()

		case $msg[0] = $exititem 
			Closeprogram()

		case $msg[0] = $fileitem 
			MPTvSeries2MKV()

		case $msg[0] = $infoitem 
			MsgBox (0, $about & " " & $Title, "Video-2-MKV Convert GUI Version" &  @CRLF & @CRLF & "(C) 2013 Lehmden" &  @CRLF & @CRLF & "Version: " & $version)
	EndSelect
WEnd 

