global env

set revn 140
set config 1
set unconfig 0
set runFromWithInInstaller 0

set i 0
foreach arg $argv {
   switch -glob -- $arg {
      -install        { set runFromWithInInstaller 1 }
      -noconfig       { set config 0; set unconfig 0 }
      -unconfig       { set unconfig 1; set config 0 }
      -installdir     { set installDir       [lindex $argv [expr $i + 1]] }
      -rev            { set revn             [lindex $argv [expr $i + 1]] }
      -scripttosource { set scripttosource   [lindex $argv [expr $i + 1]] }
      -os_lang        { set os_lang          [lindex $argv [expr $i + 1]] }
   }
   incr i
}

catch {source $scripttosource}

if { $config == 1 || $unconfig == 1 } {
   set config_dir "$installDir\\v$revn\\commonfiles\\configs"
   if { [ string compare -nocase "$env(ANSYS_SYSDIR)" "intel" ] == 0 } {
      set config_dir "$config_dir\\win32"
   } else {
      set config_dir "$config_dir\\$env(ANSYS_SYSDIR)"
   }

   set cmdPath ""
   if { $config == 1 } {
      set config_dir "$config_dir\\config"
      set cmdPath "$config_dir\\AnsConfigANSYS.exe"
   } else {
      #$unconfig
      set config_dir "$config_dir\\unconfig"
      set cmdPath "$config_dir\\AnsUnconfigANSYS.exe"
   }
   puts "\nRunning $cmdPath"
   exec $cmdPath

   if { $config == 1 } {
      #Add write permissions to certain directories for Users groups ...
      puts "\n\nAdding write permissions for Users and Administrators group ..."

      set PATH_TO_CACLS_EXE "$env(SYSTEMDRIVE)\\Windows\\system32\\cacls.exe"

      set adminsGroupName ""
      set usersGroupName ""
      if { [ string compare -nocase "$os_lang" "en-us" ] == 0 || [ string compare -nocase "$os_lang" "ja" ] == 0 } {
         set adminsGroupName "BUILTIN\\Administrators"
         set usersGroupName "BUILTIN\\Users"
      } else {
         if { [ string compare -nocase "$os_lang" "fr" ] == 0 } {
            set adminsGroupName "BUILTIN\\Administrateurs"
            set usersGroupName "BUILTIN\\Utilisateurs"
         } else {
            if { [ string compare -nocase "$os_lang" "de" ] == 0 } {
               set adminsGroupName "Administratoren"
               set usersGroupName "Benutzer"
            }
         }
      }
      if { $runFromWithInInstaller == 1 } {
         catch { exec "$PATH_TO_CACLS_EXE" "$installDir\\v$revn\\commonfiles\\*" /C /T /E /G ${adminsGroupName}:W ${usersGroupName}:W }
      }
   }
}
