#!/usr/local/monitoring/bin/python

import calendar, time, sys
sys.path.insert (1, '/usr/local/monitoring/princeton/lib')
#sys.path.insert (2, '/usr/local/monitoring/python/lib/python2.7/site-packages')
#sys.path.insert (3, '/usr/local/monitoring/python/lib/python2.7')
#sys.path.insert (4, '/usr/local/monitoring/python/lib64/python2.6/site-packages')
#sys.path.insert (5, '/usr/local/monitoring/python/lib/python2.6/site-packages')  
#sys.path.insert (6, '/usr/local/monitoring/python/lib/python2.6/site-packages/selenium-2.48.0-py2.6.egg')
from puselenium import init_selenium

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions as EXCEP
from random import sample

if len (sys.argv) != 4:
	print "Usage: %s <URL> <username> <password>" % sys.argv[0]
	sys.exit (3)

url = sys.argv[1]
usr = sys.argv[2]
usrpass = sys.argv[3]

#ff, fp = init_selenium (['mon203w'])
ff, fp = init_selenium ()

try:
	prog_start = time.time()
	ff.implicitly_wait (10)

	try:
		ff.get (url)

		WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.ID, "submitButton")))
	except:
		print "Unable to retrieve the initial (Login) webpage \"%s\"" % url
		sys.exit (2)

	fed_login = time.time()

	try:
		uid = ff.find_element_by_id ("userNameInput")
		uid.send_keys (usr)
	except:
		print "Unable to locate the login field. Page likely loaded incorrectly"
		sys.exit (2)

	try:
		pupass = ff.find_element_by_id ("passwordInput")
		pupass.send_keys (usrpass)

		ff.find_element_by_id ("submitButton").click()
	except:
		print "Unable to locate the password field. Page likely loaded incorrectly"
		sys.exit (2)

	try:
		ff.get (url + "/Organization Logos/logoPU.png")
	except:
		print "Unable to retrieve logoPU.png"
		sys.exit (2)

	try:
		ff.back()

		gwmenu = ff.find_element_by_xpath ("//span[@title='Open Menu']")
		gwmenu.click()

		logoutln = ff.find_element_by_xpath ("//*[text()='Sign Out']")
		logoutln.click()

	except:
		print "Unable to navigate back to main page and/or locate the \"Sign Out\" link. The webpage likely did not fully load"
		sys.exit (1)

	prog_finish = time.time()


	try:
		WebDriverWait (ff, 10).until (EC.title_contains("Sign Out"))
	except:
		print "Failed to sign out of SharePoint. This is likely harmless"
		sys.exit (1)

	time.sleep(2)

except EXCEP.ErrorInResponseException, (resp, msg):
	
	print "ERROR: An error occured communicating with the browser server: Response: %s; Message: %s" % (resp, msg)
	sys.exit (1)

except EXCEP.NoSuchElementException, msg:

	print "ERROR: Unable to locate webpage element. Probably the page loaded incompletely or incorrectly: %s" % msg
	sys.exit (1)

except EXCEP.TimeoutException, msg:

	print "ERROR: Request timed out. Network??? Response: %s" % msg
	sys.exit (1)

except EXCEP.WebDriverException, msg:

	print "ERROR: I don't know what happened. Read this - %s" % msg
	sys.exit (1)

finally:
	ff.quit()

print "Successfully logged on to SharePoint (\"%s\") and logged off | 'loginpage_get'=%ss;;; 'fulllogin'=%ss;;; 'total'=%ss;;;" % (url, (fed_login - prog_start), (prog_finish - fed_login), (prog_finish - prog_start))

sys.exit (0)
