'--------------------------------------------------------------------------------------------
' Reparer par ligne de commande ver 1.1
' Auteur : IDEOVA\vodiem - 13 janvier 2012
'--------------------------------------------------------------------------------------------
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
CmdArgs = " /NoStartup /Repair /Compact /Decompile "

If WScript.Arguments.Count <> 0 Then
	'sauvegarde
	accfile = WScript.Arguments.Item(0)
	accfilesrc = oFSO.BuildPath(oFSO.GetParentFolderName(accfile),oFSO.GetBaseName(accfile) & " - Sauvegarde" & "." & oFSO.GetExtensionName(accfile))
	oFSO.CopyFile accfile, accfilesrc, true
	
	'execution
	Set re = New RegExp
	re.IgnoreCase = True
	re.Pattern = "((mdb)|(accdb))$"
	Set matches = re.Execute(accfile)
	fileExtension = matches(0)
	assoc = WshShell.RegRead ("HKCR\." & fileExtension & "\")
	ftype = WshShell.RegRead ("HKCR\" & assoc & "\shell\Open\command\")
	re.Pattern = "("".*MSACCESS.EXE"")"
	Set matches = re.Execute(ftype)
	If matches.Count > 0 Then
		WshShell.Run matches(0) & CmdArgs & chr(34) & accfile & chr(34), 0, true
		MsgBox "Commande terminée."
	End if
Else
	Set WshProcessEnv = WshShell.Environment("Process")
	VBSPathSrc = WScript.ScriptFullName
	VBSPathDst = WshProcessEnv("ProgramFiles") & "\IDEOVA"
	If Not oFSO.FolderExists(VBSPathDst) Then oFSO.CreateFolder VBSPathDst
	VBSPathDst = WshProcessEnv("ProgramFiles") & "\IDEOVA\VBS"
	If Not oFSO.FolderExists(VBSPathDst) Then oFSO.CreateFolder VBSPathDst
	oFSO.CopyFile VBSPathSrc, VBSPathDst & "\", true

	AddTo "mdb"
	AddTo "accdb"
	
	MsgBox "Installation terminée."
End If

Sub AddTo(fileExtension)
	On Error Resume Next
	assoc = WshShell.RegRead ("HKCR\." & fileExtension & "\")
	ftype = WshShell.RegRead ("HKCR\" & assoc & "\shell\Open\command\")
	
	Set re = New RegExp
	re.IgnoreCase = True
	re.Pattern = "("".*MSACCESS.EXE"")"
	Set matches = re.Execute(ftype)
	If matches.Count > 0 Then
		WshShell.RegWrite "HKCR\" & assoc & "\shell\RepairByCMD\", "Réparer par ligne de commande", "REG_SZ"
		WshShell.RegWrite "HKCR\" & assoc & "\shell\RepairByCMD\command\", "wscript.exe """ & VBSPathDst & "\" & WScript.ScriptName & """ ""%1""", "REG_SZ"
	Else
		MsgBox "Extension Access non trouvé : """ & fileExtension & """"
	End If
End Sub
