#!/usr/bin/env python
"""ipkgput
FTPs an ipk to the Zaurus
"""

import ftplib,sys,socket,time,os

def term(msg):
	print "%s: terminating..." % msg
	time.sleep(3)
	sys.exit(1)

if len(sys.argv) <> 2: term("No ipk file specified")

ipk = sys.argv[1]
if not os.path.isfile(ipk): term("Invalid ipk file %s" % ipk)

print "Discovering Zaurus..."
try:
	zIP = socket.gethostbyname("harmony")
except:
	term("Zaurus not found")

ftp = ftplib.FTP()
print "Opening socket..."
try:
	ftp.connect("harmony",4242)
except:
	term("FTP connection failed")

print "Logging in..."
try:
	ftp.login("root","")
except:
	term("Login failed")

print "Changing directory..."
ftp.cwd("Documents")

print "Transferring file..."
basen = os.path.split(ipk)[1]
ipkfp = open(ipk,'rb')
ftp.storbinary("STOR %s" % basen,ipkfp)
ipkfp.close()

print "Closing connection..."
try:
	ftp.quit()
except EOFError:
	pass