/*
Function to replace any environment variable references in a String

Additionally, converts from Pre-vista path to the Vista equivalent - when running a Vista computer
This allows using the same Path on a Vista and non-vista computer - path portability
*/

EnvVars(String, ExtraEnvVars = "", CaseSensitive = false)
{
    /*
    ExtraEnvVars is a list of any extra EnvVar replacements
    
    ex.
    
    ExtraEnvVars =
    (LTrim Commments
        Var1 = Value1
        Var2 = Value2
    */
    
    static EnvVars, VistaSpecific, Vista

    if (!EnvVars)
    {
        ;Note: Vista running in compatability mode isn't seen as Vista
        Vista := A_OSVersion = "WIN_VISTA"

        AllUsersProfile := Vista ? "C:\ProgramData" : "C:\Documents and Settings\All Users"
        Public := Vista ? "C:\Users\Public" : ""
        EnvGet, ProgramFilesX86, PROGRAMFILES(X86)
        UserProfile := (Vista ? "C:\Users\" : "C:\Documents and Settings\") . A_UserName
        EnvGet , tcp , COMMANDER_PATH
 
        EnvVars =
        (LTrim Comments
            ALLUSERSPROFILE = %AllUsersProfile%
            APPDATA = %A_AppData%
            COMPUTERNAME = %A_ComputerName%
            COMMONPROGRAMFILES = C:\Program Files\Common Files
            COMMONPROGRAMFILES(X86) = C:\Program Files (x86)\Common Files
            COMSPEC = %ComSpec%
            ;HOMEDRIVE = %A_h%
            ;HOMEPATH = %A_HOMEPATH%
            ;PATH
            ;PATHEXT
            PROGRAMFILES = %ProgramFiles%
            PROGRAMFILES(X86) = %ProgramFilesX86%
            ;PROMPT
            ;SYSTEMDRIVE = %A_SYSTEMDRIVE%
            ;SystemRoot
            TMP = %A_Temp%
            TEMP = %A_Temp%
            USERNAME = %A_UserName%
            USERPROFILE = %UserProfile%
            WINDIR = %A_WinDir%
            PUBLIC = %PUBLIC%
            COMMANDER_PATH = %tcp%
        )
        

        ;vista modifications for the basic paths
        ;ex. %USERPROFILE%\Start Menu -> %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu
        ;replaced only on a Vista machine

        VistaSpecific =
        (LTrim Comments
            C:\Documents and Settings = C:\Users
            C:\Users\All Users = C:\ProgramData
            C:\ProgramData\Application Data = C:\ProgramData
            C:\ProgramData\Desktop = C:\Users\Public\Desktop
            C:\ProgramData\Documents = C:\Users\Public\Documents
            C:\ProgramData\Favorites = C:\Users\Public\Favorites
            C:\ProgramData\Start Menu = C:\ProgramData\Microsoft\Windows\Start Menu
            C:\ProgramData\Templates = C:\ProgramData\Microsoft\Windows\Templates
            C:\Users\Default User = C:\Users\Default
        )
    }

    oldStringCaseSense := A_StringCaseSense
    
    if (CaseSensitive)
        StringCaseSense, On
    else if (A_StringCaseSense = "On")
        StringCaseSense, Locale

    UsedEnvVars := EnvVars . (ExtraEnvVars ? "`n" . ExtraEnvVars : "")
    
    Loop, Parse, UsedEnvVars, `n
    {
    	CurrentField := A_LoopField
    	
    	if !RegExMatch(CurrentField, "^(?<Var>.*?)\s*+=\s*+(?<Value>.*)$", Env)
            continue

        if inStr(String, "%" EnvVar "%", CaseSensitive)
            StringReplace, String, String, % "%" EnvVar "%", %EnvValue%, All
    }

    if (Vista)
    {
        Loop, Parse, VistaSpecific, `n
        {
    	   CurrentField := A_LoopField

    	   if !RegExMatch(CurrentField, "^(?<Find>.*?)\s*+=\s*+(?<Replace>.*)$", Env)
                continue

            if inStr(String, EnvFind, CaseSensitive)
                StringReplace, String, String, %EnvFind%, %EnvReplace%, All
        }

        ;in the below replaces, any "valid" username is allowed
        ;a "valid" username is [\w ]++
        String := RegExReplace(String, "i)C:\\Users\\(?!Public)([\w ]++)\\Application Data"
            , "C:\Users\$1\AppData\Roaming", "", -1)
            
        String := RegExReplace(String, "i)C:\\Users\\(?!Public)([\w ]++)\\"
            . "(Cookies|Recent|SendTo|Start Menu|Templates)"
            , "C:\Users\$1\AppData\Roaming\Microsoft\Windows\$2", "", -1)

        String := RegExReplace(String, "i)C:\\Users\\(?!Public)([\w ]++)\\Local Settings"
            , "C:\Users\$1\AppData\Local", "", -1)
            
        String := RegExReplace(String, "i)C:\\Users\\(?!Public)([\w ]++)\\My Documents"
            , "C:\Users\$1\Documents", "", -1)
            
        String := RegExReplace(String, "i)C:\\Users\\([\w ]++)\\Documents\\"
            . "My (Music|Pictures|Videos)"
            , "C:\Users\$1\$2", "", -1)
            
        String := RegExReplace(String, "i)C:\\Users\\(?!Public)([\w ]++)\\NetHood"
            , "C:\Users\$1\AppData\Roaming\Microsoft\Windows\Network Shortcuts", "", -1)
            
        String := RegExReplace(String, "i)C:\\Users\\(?!Public)([\w ]++)\\PrintHood"
            , "C:\Users\$1\AppData\Roaming\Microsoft\Windows\Printer Shortcuts", "", -1)
    }

    StringCaseSense, %oldStringCaseSense%
    return String
}