# -*- Mode: Python; tab-width: 4 -*-

import asyncore
import consumer
import ftp

class nlst_test (ftp.ftp_client):

	def data_callback (self, data):
		print data

	def done_fun (self, status):
		if not status:
			print status
			self.close()

	def nlst_done_fun (self, status):
		if not status:
			print status
			self.close()
		self.test_nlst()

	def final_done_fun (self, status):
		print 'quit:', status

	def quitting_done_fun (self, status):
		if not status:
			print status
			self.close()
			
		self.quit (self.final_done_fun)

	def test_nlst (self, arg='*'):
		# pop another directory name off the list
		arg = self.dirs[0]
		self.dirs = self.dirs[1:]

		if not self.dirs:
			df = self.quitting_done_fun
		else:
			df = self.nlst_done_fun
			
		self.nlst (
			'-l ' + arg,
			self.done_fun,
			self.data_callback,
			df
			)

	def go (self, dirs=['*']):
		self.dirs = dirs
		self.connect (self.nlst_done_fun)

if __name__ == '__main__':
	import sys
	[host, username, password] = sys.argv[1:4]
	fc = nlst_test (host, username, password)
	if len(sys.argv) < 5:
		fc.go ()
	else:
		fc.go (sys.argv[4:])

	asyncore.loop()
