' Prepare a Windows system for running TeX Live from dvd. ' This involves adding or changing some user environment variables ' 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, sTexMain, sTexLocal Dim bGsOnPath, bGsFound, nGsVersion, sGsRoot, sGsRootParent Dim bPerlOnPath Dim i, k, l, oSub, oSubSub, oFile, oTemp, 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 ' convert Ghostscript versions from and to ' an integer between 800 and 999 Function num2version (n) Dim numstr numstr = CStr( n ) num2version = left( numstr, 1 ) & "." & right( numstr, 2 ) 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 & "bin\win32" ' 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 ' Find Ghostscript ' Ghostscript on path? bGsFound = False bGsOnPath = False sGsRoot = "" sGsRootParent = "" sTemp = FindOnPath( "gsdll32.dll" ) If sTemp <> "" Then ' root of this version sGsRoot = RemoveBsl( oFS.GetParentFolderName( RemoveBsl( sTemp ) ) ) sGsRootParent = RemoveBsl(oFS.GetParentFolderName( sGsRoot ) ) bGsFound = True bGsOnPath = True End If ' If not on path, check registry. ' Have to do some guessing here. ' First try most attractive options. ' If that fails, look for a gs directory next to TeX Live nGsVersion = 0 If Not bGsFound Then For l = 999 to 800 step -1 For Each k in Array( "HKCU", "HKLM" ) For Each i in Array( "GPL", "GNU", "AFPL", "ESP" ) Err.Clear() sTemp = oWsh.RegRead( _ k & "\software\" & i & " Ghostscript\" _ & num2version( l ) & "\GS_DLL" ) If ( Not Err ) and ( sTemp <> "" ) Then sGsRoot = left( sTemp, _ len( sTemp ) - len( "\bin\gsdll32.dll" ) ) sGsRootParent = _ RemoveBsl(oFS.GetParentFolderName( sGsRoot ) ) nGsVersion = l bGsFound = True Exit For End If Next If nGsVersion > 0 Then Exit For Next If nGsVersion > 0 Then Exit For Next End If ' final attempt: look for directory sTexRootSl & "gs" If Not bGsFound Then If oFS.FolderExists( sTexRootSl & "gs" ) Then set oSub = oFS.GetFolder( sTexRootSl & "gs" ) set oSubSub = oSub.SubFolders For Each oTemp in oSubSub If oFS.FileExists( _ sTexRootSl & "gs\" & oTemp.name & "\bin\gsdll32.dll" ) Then sGsRoot = sTexRootSl & "gs\" & oTemp.name sGsRootParent = sTexRootSl & "gs" nGsVersion = CInt( _ left( oTemp.name, 1 ) & right( oTemp.name, 2 ) ) bGsFound = True Exit For End If Next End If End If If Not bGsFound Then Wscript.Echo "No Ghostscript found." & vbNewLine & _ "Some functionality will not be available." _ & vbNewLine & _ "If you do have Ghostscript, add it to the search path" _ & vbNewLine & "or rerun the installer." End If ' test for pre-installed Perl. bPerlOnPath = False sTemp = FindOnPath( "perl.exe" ) If ( sTemp <> "" ) and _ ( UCase( AddBsl( sTemp ) ) <> UCase( sTexBin & "\" ) ) Then bPerlOnPath = True End If ' create directories ''''''''''''''''''''''''''''''''''''''''''' sTexMain = AddBsl( oWsh.expandenvironmentstrings( "%USERPROFILE%" ) ) & _ "TeXLive2007" sTexLocal = sTexRootSl & "texmf-local" CreateTry sTexMain CreateTry sTexMain & "\texmf-var" CreateTry sTexMain & "\texmf-var\web2c" oFS.CopyFile sTexRootSl & "texmf\web2c\texmf.cnf-4WIN", _ sTexMain & "\texmf-var\web2c\texmf.cnf" oFS.CopyFile sTexRootSl & "texmf\web2c\fmtutil.cnf", _ sTexMain & "\texmf-var\web2c\fmtutil.cnf" CreateTry sTexMain & "\texmf-var\tex" CreateTry sTexMain & "\texmf-var\tex\generic" CreateTry sTexMain & "\texmf-var\tex\generic\hyphen" oFS.CopyFile sTexRootSl & "texmf\tex\generic\hyphen\hyphen.tex", _ sTexMain & "\texmf-var\tex\generic\hyphen\ushyph.tex" ' CreateTry sTexMain & "\texmf-var\tex\generic\config" ' oFS.CopyFile sTexRootSl & "texmf\tex\generic\config\language.dat", _ ' sTexMain & "\texmf-var\tex\generic\config\language.dat" CreateTry sTexMain & "\temp" ' edit user search path ' Ghostscript and TeXLive are appended to the user searchpath, ' inasfar as necessary. This way, a pre-installed Perl on the old path ' will automatically have priority. ' add TeX, if necessary If Not bTexOnSystemPath Then UpdateNewPath sTexBin End If ' add Ghostscript, if necessary and applicable If bGsFound And ( Not bGsOnPath ) Then UpdateNewPath sGsRoot & "\bin" End If ' write revised user path to registry If InStr( 1, sNewPath, "%" ) > 0 Then oWsh.RegWrite "HKCU\Environment\Path", sNewPath, "REG_EXPAND_SZ" Else oWsh.RegWrite "HKCU\Environment\Path", sNewPath End If ' other environment settings '''''''''''''''''''''''''''''''''''' ' perl If Not bPerlOnPath Then oWsh.RegWrite "HKCU\environment\PERL5LIB", sTexRootSl & _ "perltl\lib;" & sTexRootSl & "perltl\site\lib" End If ' Ghostscript; assume the usual Ghostscript directory layout If bGsFound Then oWsh.RegWrite "HKCU\environment\GS_LIB", _ sGsRoot & "\lib;" & sGsRootParent & "\fonts;" & _ sGsRoot & "\Resource", "REG_SZ" oWsh.RegWrite "HKCU\environment\GS_DLL", _ sGsRoot & "\bin\gsdll32.dll", "REG_SZ" End If ' TeXLive env vars oWsh.RegWrite "HKCU\environment\TEXMFCNF", _ sTexMain & "\texmf-var\web2c" oWsh.RegWrite "HKCU\environment\TEXMFTEMP", _ sTexMain & "\temp" oWsh.RegWrite "HKCU\environment\TEXMFVAR", _ sTexMain & "\texmf-var" oWsh.RegWrite "HKCU\environment\TEXMFLOCAL", _ sTexLocal oWsh.RegWrite "HKCU\environment\TLroot", sTexMain ' generate files MsgBox "Click OK to start Generating files." & vbNewLine & _ "When done, you may have to re-login." Set oFile = oFS.CreateTextFile( sTexMain & "\temp\tl_generate.bat" ) oFile.WriteLine( "@echo off" ) oFile.WriteLine( "path " & sSystemPath & ";" & _ oWsh.ExpandEnvironmentStrings( sNewPath ) ) oFile.WriteLine( "set TEXMFCNF=" & sTexMain & "\texmf-var\web2c" ) oFile.WriteLine( "set TEXMFTEMP=" & sTexMain & "\temp" ) oFile.WriteLine( "set TEXMFVAR=" & sTexMain & "\texmf-var" ) oFile.WriteLine( "set TEXMFLOCAL=" & sTexLocal ) oFile.WriteLine( "set TexRoot=" & sTexMain ) If ( Not bPerlOnPath ) Then oFile.WriteLine( "set PERL5LIB=" & sTexRootSl & _ "perltl\lib;" & sTexRootSl & "perltl\site\lib" ) End If oFile.WriteLine( "mktexlsr" ) 'oFile.WriteLine( "pause" ) oFile.WriteLine( "updmap" ) oFile.WriteLine( "fmtutil --byfmt latex" ) oFile.WriteLine( "fmtutil --byfmt pdflatex" ) 'oFile.WriteLine( "fmtutil --all" ) oFile.WriteLine( "mktexlsr" ) oFile.WriteLine( "echo Done. You may have to re-login." ) oFile.WriteLine( "pause" ) oFile.close oWsh.run """" & sTexMain & "\temp\tl_generate.bat" & """", 1, true 'oFS.deletefile( sTexMain & "\temp\tl_generate.bat" )