Option Explicit

Const ForReading = 1
Const ForWriting = 2

Dim args
Dim linkFile
Dim fso
Dim inputFile
Dim line
Dim imagePath
Dim imageType
Dim daemonPath
Dim shell
Dim commandLine
Dim result

Set args = WScript.Arguments

If args.Count < 1 Then
    MsgBox "No LCG DiscLink file was supplied.", 48, "LCG DiscLink"
    WScript.Quit 1
End If

linkFile = args.Item(0)

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(linkFile) Then
    MsgBox "The DiscLink file could not be found:" & vbCrLf & vbCrLf & linkFile, 16, "LCG DiscLink"
    WScript.Quit 1
End If

imagePath = ""
imageType = ""

On Error Resume Next
Set inputFile = fso.OpenTextFile(linkFile, ForReading, False)

If Err.Number <> 0 Then
    MsgBox "LCG DiscLink could not read this file:" & vbCrLf & vbCrLf & linkFile, 16, "LCG DiscLink"
    WScript.Quit 1
End If
On Error GoTo 0

Do While Not inputFile.AtEndOfStream
    line = Trim(inputFile.ReadLine)

    If Len(line) >= 6 Then
        If LCase(Left(line, 6)) = "image=" Then
            imagePath = Trim(Mid(line, 7))
        End If
    End If

    If Len(line) >= 10 Then
        If LCase(Left(line, 10)) = "imagetype=" Then
            imageType = Trim(Mid(line, 11))
        End If
    End If
Loop

inputFile.Close

imagePath = RemoveOuterQuotes(imagePath)

If imagePath = "" Then
    MsgBox "This file does not contain an Image= entry.", 16, "LCG DiscLink"
    WScript.Quit 1
End If

If InStr(imagePath, Chr(34)) > 0 Then
    MsgBox "The image path contains an invalid quotation mark.", 16, "LCG DiscLink"
    WScript.Quit 1
End If

If Not IsSupportedImage(fso.GetExtensionName(imagePath)) Then
    MsgBox "Unsupported disc-image format:" & vbCrLf & vbCrLf & imagePath, 16, "LCG DiscLink"
    WScript.Quit 1
End If

If Not fso.FileExists(imagePath) Then
    MsgBox "The disc image could not be reached:" & vbCrLf & vbCrLf & imagePath & vbCrLf & vbCrLf & _
           "Make sure Nexus is online and that Windows is connected to \\Nexus\Retro_Cloud.", 16, "LCG DiscLink"
    WScript.Quit 1
End If

daemonPath = FindDaemonTools(fso)

If daemonPath = "" Then
    daemonPath = InputBox( _
        "DAEMON Tools 3.47 was not found automatically." & vbCrLf & vbCrLf & _
        "Enter the complete path to daemon.exe:", _
        "LCG DiscLink", _
        "C:\Program Files\D-Tools\daemon.exe")

    daemonPath = RemoveOuterQuotes(Trim(daemonPath))

    If daemonPath = "" Then
        WScript.Quit 1
    End If

    If Not fso.FileExists(daemonPath) Then
        MsgBox "daemon.exe was not found at:" & vbCrLf & vbCrLf & daemonPath, 16, "LCG DiscLink"
        WScript.Quit 1
    End If

    SaveDaemonPath fso, daemonPath
End If

Set shell = CreateObject("WScript.Shell")
commandLine = Quote(daemonPath) & " -mount 0," & Quote(imagePath)

On Error Resume Next
result = shell.Run(commandLine, 1, False)

If Err.Number <> 0 Then
    MsgBox "DAEMON Tools could not be started." & vbCrLf & vbCrLf & _
           "Command:" & vbCrLf & commandLine, 16, "LCG DiscLink"
    WScript.Quit 1
End If
On Error GoTo 0

WScript.Quit 0

Function Quote(value)
    Quote = Chr(34) & value & Chr(34)
End Function

Function RemoveOuterQuotes(value)
    If Len(value) >= 2 Then
        If Left(value, 1) = Chr(34) And Right(value, 1) = Chr(34) Then
            RemoveOuterQuotes = Mid(value, 2, Len(value) - 2)
            Exit Function
        End If
    End If

    RemoveOuterQuotes = value
End Function

Function IsSupportedImage(extensionName)
    Dim extensionLower
    extensionLower = LCase(extensionName)

    Select Case extensionLower
        Case "cue", "iso", "ccd", "mds", "nrg", "bwt", "bin", "img"
            IsSupportedImage = True
        Case Else
            IsSupportedImage = False
    End Select
End Function

Function FindDaemonTools(fileSystem)
    Dim installFolder
    Dim configPath
    Dim configFile
    Dim configuredPath
    Dim candidates
    Dim item

    installFolder = fileSystem.GetParentFolderName(WScript.ScriptFullName)
    configPath = installFolder & "\daemon-path.txt"

    If fileSystem.FileExists(configPath) Then
        On Error Resume Next
        Set configFile = fileSystem.OpenTextFile(configPath, ForReading, False)

        If Err.Number = 0 Then
            If Not configFile.AtEndOfStream Then
                configuredPath = RemoveOuterQuotes(Trim(configFile.ReadLine))
                configFile.Close

                If fileSystem.FileExists(configuredPath) Then
                    FindDaemonTools = configuredPath
                    Exit Function
                End If
            Else
                configFile.Close
            End If
        End If
        On Error GoTo 0
    End If

    candidates = Array( _
        "C:\Program Files\D-Tools\daemon.exe", _
        "C:\Program Files\DAEMON Tools\daemon.exe", _
        "C:\Program Files\DAEMON Tools 3.47\daemon.exe", _
        "C:\D-Tools\daemon.exe", _
        "C:\DAEMON Tools\daemon.exe")

    For Each item In candidates
        If fileSystem.FileExists(item) Then
            SaveDaemonPath fileSystem, item
            FindDaemonTools = item
            Exit Function
        End If
    Next

    FindDaemonTools = ""
End Function

Sub SaveDaemonPath(fileSystem, value)
    Dim installFolder
    Dim outputFile

    installFolder = fileSystem.GetParentFolderName(WScript.ScriptFullName)

    On Error Resume Next
    Set outputFile = fileSystem.OpenTextFile(installFolder & "\daemon-path.txt", ForWriting, True)

    If Err.Number = 0 Then
        outputFile.WriteLine value
        outputFile.Close
    End If
    On Error GoTo 0
End Sub
