' Prepare a Windows system for running MikTeX from cd/dvd or an ' external drive. ' This involves adding or changing some HKCU registry entries ' and placing some files under %USERPROFILE%. ' Copyright (C) 2007 Siep Kroonenberg ' siepo at cybercomm dot nl Option Explicit On Error Resume Next Dim bTexOnSystemPath, sTexRoot, sTexRootSl, sTexBin, sMikData Dim oFile, sTemp, sTemp2, sTemp3 Dim sSystemPath, sNewPath ' load necessary functionality Dim oWsh, oFS Set oWsh = Wscript.CreateObject("WScript.Shell") Set oFS = CreateObject("Scripting.FileSystemObject") ' functions and subroutines '''''''''''''''''''' ' Backslashes Function Bsl( s ) Bsl = Replace( s, "/", "\" ) End Function Function AddBsl(sDir) Dim sTmp sTmp = Bsl( sDir ) If Right( sTmp, 1 ) = "\" Then AddBsl = sTmp Else AddBsl = sTmp & "\" End If End Function Function RemoveBsl(sDir) If ( Right( sDir, 1 ) = "\" ) or ( Right( sDir, 1 ) = "/" ) Then RemoveBsl = Left( sDir, len( sDir ) - 1 ) Else RemoveBsl = sDir End If End Function ' search a program on the searchpath Dim PathHelp PathHelp = vbNewLine & _ "You can edit the system search path as follows:" & vbNewLine & _ "right-click My Computer, choose Properties, Advanced Tab," _ & vbNewLine & _ "Environment Variables button, and edit the Path entry" & _ " in the bottom pane." & vbNewLine & _ "However, you may not have the required privileges." Function FindOnSystemPath( s ) Dim paths, d paths = Split( sSystemPath, ";", -1 ) FindOnSystemPath = "" For Each d in paths If oFS.FileExists( AddBsl( d ) & s ) Then FindOnSystemPath = d ' MsgBox s & " found in " & d Exit For End If Next End Function Function FindOnUserPath( s ) Dim paths, d, tmp Err.clear() tmp = oWsh.ExpandEnvironmentStrings( sNewPath ) paths = Split( tmp, ";", -1 ) FindOnUserPath = "" For Each d in paths If oFS.FileExists( AddBsl( d ) & s ) Then FindOnUserPath = d ' MsgBox s & " found in " & d Exit For End If Next End Function Function FindOnPath( s ) ' system path first, because it has priority Dim tmp tmp = FindOnSystemPath( s ) If tmp = "" Then tmp = FindOnUserPath( s ) FindOnPath = tmp End Function Sub UpdateNewPath( s ) If sNewPath = "" Then sNewPath = s Else sNewPath = sNewPath & ";" & s End If End Sub ' create a folder if it doesn't exist. ' bail out if the folder doesn't exist and can't be created. sub CreateTry( sFolder ) If Not oFS.FolderExists( sFolder ) Then oFS.CreateFolder( sFolder ) If Not oFS.FolderExists( sFolder ) Then MsgBox "Cannot create " & sFolder & "; aborting..." WScript.Quit( 1 ) End If End If End Sub ' end functions and subroutines ''''''''''''''''''''''''''''''''''' ' where are we? '''''''''''''''''''''''''''''''''''''''''''' ' read and expand system path once and for all Err.Clear() sSystemPath = oWsh.ExpandEnvironmentStrings( oWsh.RegRead( _ "HKLM\system\currentcontrolset\control\session manager\environment\path" ) ) sTexRoot = oFS.GetParentFolderName( Wscript.ScriptFullName ) sTexRootSl = AddBsl( sTexRoot ) sTexBin = sTexRootSl & "texmf\miktex\bin" ' diagnostics ''''''''''''''''''''''''''''''''''''''''''''''' ' check path for conflicting TeX bTexOnSystemPath = False Err.Clear() sTemp = FindOnSystemPath( "pdftex.exe" ) If ( sTemp <> "" ) Then ' WARNING: the test below does not take short names into account! If UCase( RemoveBsl( sTemp ) ) <> UCase( sTexBin ) Then Wscript.Echo "Found another TeX in " & sTemp & ";" & vbNewLine & _ "Please uninstall or remove from system search path." _ & vbNewLine & PathHelp Wscript.Quit( 1 ) Else bTexOnSystemPath = True End If End If ' user path minus any existing TeX sNewPath = "" Err.Clear() sTemp = oWsh.RegRead( "HKCU\environment\path" ) For Each sTemp2 in Split( sTemp, ";", -1 ) sTemp3 = AddBsl( oWsh.ExpandEnvironmentStrings( sTemp2 ) ) If Not oFS.FileExists( sTemp3 & "pdftex.exe" ) Then UpdateNewPath sTemp2 End If Next ' create directories ''''''''''''''''''''''''''''''''''''''''''' ' datadir sTemp = RemoveBsl( oWsh.SpecialFolders( "AppData" ) ) sMikData = sTemp & "\MikTeX\2.5" CreateTry sTemp & "\MikTeX" CreateTry sMikData ' create subdirectories if they don't exist For Each sTemp In Array("miktex", "dvipdfm", "dvips", "pdftex") CreateTry sMikData & "\" & sTemp CreateTry sMikData & "\" & sTemp & "\config" Next For Each sTemp In Array("base", "fmt", "mem") CreateTry sMikData & "\miktex\" & sTemp Next ' edit user search path If Not bTexOnSystemPath Then Err.Clear() UpdateNewPath sTexBin If InStr( 1, sNewPath, "%" ) > 0 Then oWsh.RegWrite "HKCU\Environment\Path", sNewPath, "REG_EXPAND_SZ" Else oWsh.RegWrite "HKCU\Environment\Path", sNewPath End If End If ' create registry keys oWsh.RegDelete "HKCU\Software\miktex.org\MikTeX\2.5" oWsh.RegWrite "HKCU\Software\miktex.org\MikTeX\2.5\Core\Install", _ sTexRootSl & "texmf" If oFS.FolderExists( sTexRootSl & "localtexmf" ) Then oWsh.RegWrite "HKCU\Software\miktex.org\MikTeX\2.5\Core\Roots", _ sTexRootSl & "localtexmf;" & sTexRootSl & "texmf" Else oWsh.RegWrite "HKCU\Software\miktex.org\MikTeX\2.5\Core\Roots", _ sTexRootSl & "texmf" End If ' Can't use localtexmf for UserConfig, because it may not be writable. ' The default is ok, but we will set it explicitly anyhow. ' Don't want to use the default setting for UserData, which is under ' %USERPROFILE%\Local Settings, because it won't be written ' to the network copy of %USERPROFILE% in a roaming profile setup oWsh.RegWrite "HKCU\Software\miktex.org\MikTeX\2.5\Core\UserConfig", _ sMikData oWsh.RegWrite "HKCU\Software\miktex.org\MikTeX\2.5\Core\UserData", _ sMikData ' generate fndb, formats and mapfiles MsgBox "Click OK to start Generating files." & vbNewLine & _ "Please be patient and DO NOT INTERRUPT." & vbNewLine & _ "When done, you may have to re-login." sTemp = AddBsl( oWsh.ExpandEnvironmentStrings( "%TEMP%" ) ) _ & "mk_generate.bat" Set oFile = oFS.CreateTextFile( sTemp ) oFile.WriteLine( "@echo off" ) oFile.WriteLine( "path " & sSystemPath & ";" & _ oWsh.ExpandEnvironmentStrings( sNewPath ) ) oFile.WriteLine( "initexmf --update-fndb" ) oFile.WriteLine( "initexmf --dump" ) oFile.WriteLine( "initexmf --mkmaps" ) oFile.WriteLine( "initexmf --update-fndb=" & """" & sMikData & """" ) oFile.WriteLine( "echo Done. You may have to re-login." ) oFile.WriteLine( "pause" ) oFile.close oWsh.run """" & sTemp & """", 1, true 'oFS.deletefile( sTemp )