Index: src/port/port.tcl
===================================================================
--- src/port/port.tcl	(revision 36509)
+++ src/port/port.tcl	(working copy)
@@ -1494,16 +1494,12 @@
         return 1
     }
     foreachport $portlist {
-        # Merge global variations into the variations specified for this port
-        foreach { variation value } [array get global_variations] {
-            if { ![info exists variations($variation)] } {
-                set variations($variation) $value
+		# Global variations will have to be merged into the specified
+	        # variations, but perhaps after the installed variations are
+	        # merged. So we pass them into upgrade:
+		macports::upgrade $portname "port:$portname" [array get global_variations] [array get variations] [array get options]
             }
-        }
 
-        macports::upgrade $portname "port:$portname" [array get variations] [array get options]
-    }
-
     return 0
 }
 
Index: src/macports1.0/macports.tcl
===================================================================
--- src/macports1.0/macports.tcl	(revision 36509)
+++ src/macports1.0/macports.tcl	(working copy)
@@ -1850,10 +1850,14 @@
 }
 
 # upgrade procedure
-proc macports::upgrade {portname dspec variationslist optionslist {depscachename ""}} {
+proc macports::upgrade {portname dspec globalvarlist variationslist optionslist {depscachename ""}} {
     global macports::registry.installtype
     global macports::portarchivemode
     array set options $optionslist
+	# Note $variationslist is left alone and so retains the original
+        # requested variations, which should be passed to recursive calls to
+	# upgrade; while variations gets existing variants and global variations
+        # merged in later on, so it applies only to this port's upgrafe
     array set variations $variationslist
     if {![string match "" $depscachename]} {
         upvar $depscachename depscache
@@ -1890,6 +1894,10 @@
     # if this is set (not 0) we dont follow the deps
     set depflag 0
 
+        # Sooner or later we may have to open this port to update the portinfo
+	# by evaluating the variants. Keep track of whether this has happened
+	set portwasopened 0
+
     # set version_installed and revision_installed
     set ilist {}
     if { [catch {set ilist [registry::installed $portname ""]} result] } {
@@ -1900,12 +1908,23 @@
             if {![info exists porturl]} {
                 set porturl file://./    
             }    
-            if {[catch {set workername [mportopen $porturl [array get options] ]} result]} {
+			# Merge the global variations into the specified
+			foreach { variation value } $globalvarlist {
+				if { ![info exists variations($variation)] } {
+					set variations($variation) $value
+				}
+			}
+		
+			if {[catch {set workername [mportopen $porturl [array get options] [array get variations]]} result]} {
                     global errorInfo
                     ui_debug "$errorInfo"
                     ui_error "Unable to open port: $result"        
                     return 1
             }
+			# While we're at it, update the portinfo
+			array unset portinfo
+			array set portinfo [mportinfo $workername]
+			set portwasopened 1
 
             if {![_mportispresent $workername $dspec ] } {
                 # port in not installed - install it!
@@ -1915,12 +1934,18 @@
                     ui_error "Unable to exec port: $result"
                     return 1
                 }
+				if {$result > 0} {
+					ui_error "Problem while installing $portname"
+					return $result
+				}
             } else {
                 # port installed outside MacPorts
                 ui_debug "$portname installed outside the MacPorts system"
                 set depflag 1
             }
 
+			# update ilist since we just did an installation
+			set ilist [registry::installed $portname ""]
         } else {
             ui_error "Checking installed version failed: $result"
             exit 1
@@ -1972,6 +1997,8 @@
                 }
             }
         }
+		# record the variant of the latest version
+		set variant [lindex $num 3]
         if { [lindex $num 4] == 0 && 0 == [string compare "image" ${macports::registry.installtype}] } {
             # activate the latest installed version
             if {[catch {portimage::activate $portname ${version_installed}_${revision_installed}$variant $optionslist} result]} {
@@ -1986,7 +2013,7 @@
     # output version numbers
     ui_debug "epoch: in tree: $epoch_in_tree installed: $epoch_installed"
     ui_debug "$portname ${version_in_tree}_$revision_in_tree exists in the ports tree"
-    ui_debug "$portname ${version_installed}_$revision_installed is installed"
+    ui_debug "$portname ${version_installed}_$revision_installed $variant is installed"
 
     # set the nodeps option  
     if {![info exists options(ports_nodeps)]} {
@@ -1995,6 +2022,63 @@
         set nodeps yes
     }
 
+	# save existing variant for later use
+	set oldvariant $variant
+	if {$portwasopened == 0} {
+		# If the port has not been opened, then before we do 
+		# dependencies, we need to figure out the final variants, 
+		# open the port, and update the portinfo. (If it was opened,
+		# then the variants must still be the ones we opened it with,
+		# and the portinfo is correct.)
+
+		set porturl $portinfo(porturl)
+		if {![info exists porturl]} {
+			set porturl file://./
+		}
+
+		# check if the variants is present in $version_in_tree
+		set variant [split $variant +]
+		ui_debug "Merging existing variants $variant into variants"
+		if {[info exists portinfo(variants)]} {
+			set avariants $portinfo(variants)
+		} else {
+			set avariants {}
+		}
+		ui_debug "available variants are : $avariants"
+		foreach v $variant {
+			if {[lsearch $avariants $v] == -1} {
+			} else {
+				ui_debug "variant $v is present in $portname $version_in_tree"
+				if { ![info exists variations($v)]} {
+					set variations($v) "+"
+				}
+			}
+		}
+		
+		# Now merge in the global (i.e. variants.conf) variations.
+		# We wait until now so that existing variants for this port
+		# override global variations
+		foreach { variation value } $globalvarlist {
+			if { ![info exists variations($variation)] } {
+				set variations($variation) $value
+			}
+		}
+		
+		ui_debug "new fully merged portvariants: [array get variations]"
+	
+		if {[catch {set workername [mportopen $porturl [array get options] [array get variations]]} result]} {
+			global errorInfo
+			ui_debug "$errorInfo"
+			ui_error "Unable to open port: $result"
+			return 1
+		}
+
+		array unset portinfo
+		array set portinfo [mportinfo $workername]
+		set portwasopened 1
+	}
+		
+
     if {$nodeps == "yes" || $depflag == 1} {
         ui_debug "Not following dependencies"
         set depflag 0
@@ -2005,7 +2089,7 @@
                 if {![llength [array get depscache $i]]} {
                 set d [lindex [split $i :] end]
                     set depscache($i) 1
-                    upgrade $d $i $variationslist $optionslist depscache
+					upgrade $d $i $globalvarlist $variationslist $optionslist depscache
                 } 
             }
         }
@@ -2015,7 +2099,7 @@
                 if {![llength [array get depscache $i]]} {
                 set d [lindex [split $i :] end]
                     set depscache($i) 1
-                    upgrade $d $i $variationslist $optionslist depscache
+					upgrade $d $i $globalvarlist $variationslist $optionslist depscache
                 } 
             }
         }
@@ -2025,7 +2109,7 @@
                 if {![llength [array get depscache $i]]} {
                 set d [lindex [split $i :] end]
                     set depscache($i) 1
-                    upgrade $d $i $variationslist $optionslist depscache
+					upgrade $d $i $globalvarlist $variationslist $optionslist depscache
                 } 
             }
         }
@@ -2037,6 +2121,10 @@
                 && [rpm-vercomp $revision_installed $revision_in_tree] >= 0 ))
         && ![info exists options(ports_force)] } {
         ui_debug "No need to upgrade! $portname ${version_installed}_$revision_installed >= $portname ${version_in_tree}_$revision_in_tree"
+		if {[info exists portinfo(portvariants)]
+		    && $portinfo(portvariants) != $oldvariant} {
+			ui_warn "Skipping upgrdae since $portname $version_installed >= $portname $version_in_tree, even though installed variant $oldvariant differs from $portinfo(portvariants). Specify -f to force upgrade."
+		}
         if { $epoch_installed >= $epoch_in_tree } {
             # Check if we have to do dependents
             if {[info exists options(ports_do_dependents)]} {
@@ -2049,7 +2137,7 @@
                 if { [llength deplist] > 0 } {
                     foreach dep $deplist {
                         set mpname [lindex $dep 2] 
-                        macports::upgrade $mpname "port:$mpname" [array get variations] [array get options]
+						macports::upgrade $mpname "port:$mpname" $globalvarlist $variationslist [array get options]
                     }
                 }
             }
@@ -2061,38 +2149,7 @@
         }
     }
 
-    # open porthandle
-    set porturl $portinfo(porturl)
-    if {![info exists porturl]} {
-        set porturl file://./
-    }
 
-    # check if the variants is present in $version_in_tree
-    set oldvariant $variant
-    set variant [split $variant +]
-    ui_debug "variants to install $variant"
-    if {[info exists portinfo(variants)]} {
-        set avariants $portinfo(variants)
-    } else {
-        set avariants {}
-    }
-    ui_debug "available variants are : $avariants"
-    foreach v $variant {
-        if {[lsearch $avariants $v] == -1} {
-        } else {
-            ui_debug "variant $v is present in $portname ${version_in_tree}_$revision_in_tree"
-            set variations($v) "+"
-        }
-    }
-    ui_debug "new portvariants: [array get variations]"
-    
-    if {[catch {set workername [mportopen $porturl [array get options] [array get variations]]} result]} {
-        global errorInfo
-        ui_debug "$errorInfo"
-        ui_error "Unable to open port: $result"
-        return 1
-    }
-
     # install version_in_tree
     if {0 == [string compare "yes" ${macports::portarchivemode}]} {
         set upgrade_action "archive"
@@ -2145,7 +2202,7 @@
         if { [llength deplist] > 0 } {
             foreach dep $deplist {
                 set mpname [lindex $dep 2] 
-                macports::upgrade $mpname "port:$mpname" [array get variations] [array get options]
+				macports::upgrade $mpname "port:$mpname" $globalvarlist $variationslist [array get options]
             }
         }
     }
