Index: bin/emerge
===================================================================
--- bin.orig/emerge
+++ bin/emerge
@@ -701,9 +701,8 @@ class search:
 		This makes successive searches faster."""
 		self.installcache = portage.db["/"]["vartree"]
 
-	def execute(self,searchkey):
+	def execute(self,searchkey, myopts):
 		"""Performs the search for the supplied search key"""
-		global myopts
 		match_category = 0
 		self.searchkey=searchkey
 		self.packagematches = []
@@ -892,11 +891,9 @@ def genericdict(mylist):
 		mynewdict[portage.dep_getkey(x)]=x
 	return mynewdict
 
-olddbapi=None
 class depgraph:
 
 	def __init__(self,myaction,myopts):
-		global olddbapi
 		self.pkgsettings = portage.config(clone=portage.settings)
 		if not self.pkgsettings["ARCH"]:
 			portage.writemsg(red("\a!!! ARCH is not set... Are you missing the /etc/make.profile symlink?\n"))
@@ -1369,8 +1366,7 @@ class depgraph:
 				retlist.append(y)
 		return retlist
 
-	def xcreate(self,mode="system"):
-		global syslist
+	def xcreate(self,syslist,mode="system"):
 		world_problems = False
 		if mode=="system":
 			mylist=syslist
@@ -2070,7 +2066,7 @@ class depgraph:
 					if self.pkgsettings["AUTOCLEAN"]=="yes":
 						xsplit=portage.pkgsplit(x[2])
 						emergelog(" >>> AUTOCLEAN: "+xsplit[0])
-						retval=unmerge("clean", [xsplit[0]])
+						retval=unmerge("clean", [xsplit[0]], myopts)
 						if not retval:
 							emergelog(" --- AUTOCLEAN: Nothing unmerged.")
 
@@ -2130,7 +2126,7 @@ class depgraph:
 			else:
 				sys.exit(0)
 
-def unmerge(unmerge_action, unmerge_files):
+def unmerge(unmerge_action, unmerge_files, myopts):
 	candidate_catpkgs=[]
 	global_unmerge=0
 
@@ -2148,7 +2144,6 @@ def unmerge(unmerge_action, unmerge_file
 		else:
 			syslist.append(mycp)
 
-	global myopts
 	mysettings = portage.config(clone=portage.settings)
 
 	if not unmerge_files or "world" in unmerge_files or "system" in unmerge_files:
@@ -2307,7 +2302,6 @@ def unmerge(unmerge_action, unmerge_file
 			print red("\a\n\n!!! '%s' is part of your system profile." % mykey)
 			print yellow("\a!!! Unmerging it may be damaging to your system.\n")
 			if "--pretend" not in myopts and "--ask" not in myopts:
-				global EMERGE_WARNING_DELAY
 				countdown(EMERGE_WARNING_DELAY,red("Press Ctrl-C to Stop"))
 		print "\n "+white(x)
 		for mytype in ["selected","protected","omitted"]:
@@ -2344,7 +2338,6 @@ def unmerge(unmerge_action, unmerge_file
 			return 0
 	#the real unmerging begins, after a short delay....
 
-	global CLEAN_DELAY
 	countdown(CLEAN_DELAY, ">>> Unmerging")
 
 	for x in pkgmap.keys():
@@ -2448,8 +2441,7 @@ def chk_updated_info_files(retval):
 				print " "+green("*")+" Processed",icount,"info files."
 
 
-def post_emerge(retval=0):
-	global myopts
+def post_emerge(myopts, retval=0):
 	os.chdir("/")
 	if "--pretend" in myopts:
 		sys.exit(retval)
@@ -3128,15 +3120,15 @@ elif "search"==myaction:
 		searchinstance = search()
 		for mysearch in myfiles:
 			try:
-				searchinstance.execute(mysearch)
+				searchinstance.execute(mysearch, myopts)
 			except re.error, comment:
 				print "\n!!! Regular expression error in \"%s\": %s" % ( mysearch, comment )
 				sys.exit(1)
 			searchinstance.output()
 elif "unmerge"==myaction or "prune"==myaction or "clean"==myaction:
 	try:
-		if 1==unmerge(myaction, myfiles):
-			post_emerge()
+		if 1==unmerge(myaction, myfiles, myopts):
+			post_emerge(myopts)
 	except portage_exception.PackageNotFound, e:
 		portage.writemsg("--- Couldn't find %s to unmerge.\n" % str(e))
 		sys.exit(1)
@@ -3180,7 +3172,7 @@ elif "depclean"==myaction:
 
 	if not ("--quiet" in myopts):
 		print "\nCalculating dependencies  ",
-	if not mydepgraph.xcreate("world"):
+	if not mydepgraph.xcreate(syslist,"world"):
 		print "\n!!! Failed to create deptree."
 		sys.exit(1)
 	if not ("--quiet" in myopts):
@@ -3241,7 +3233,7 @@ elif "depclean"==myaction:
 	#print
 
 	if len(cleanlist):
-		unmerge("unmerge", cleanlist)
+		unmerge("unmerge", cleanlist, myopts)
 
 	print
 	print "Packages installed:   "+str(len(myvarlist))
@@ -3253,7 +3245,7 @@ elif "depclean"==myaction:
 		print "Number to remove:     "+str(len(cleanlist))
 	else:
 		print "Number removed:       "+str(len(cleanlist))
-		post_emerge()
+		post_emerge(myopts)
 
 # "update", "system", or just process files:
 else:
@@ -3305,7 +3297,7 @@ else:
 			if not ("--quiet" in myopts):
 				print "Calculating",myaction,"dependencies  ",
 				sys.stdout.flush()
-			if not mydepgraph.xcreate(myaction):
+			if not mydepgraph.xcreate(syslist,myaction):
 				print "!!! Depgraph creation failed."
 				sys.exit(1)
 			if not ("--quiet" in myopts):
@@ -3416,5 +3408,5 @@ else:
 			del portage.mtimedb["resume"]
 		if portage.settings["AUTOCLEAN"] and "yes"==portage.settings["AUTOCLEAN"]:
 			print ">>> Auto-cleaning packages..."
-			unmerge("clean", ["world"])
-	post_emerge()
+			unmerge("clean", ["world"], myopts)
+	post_emerge(myopts)

