#!/usr/bin/python
import urllib
import urllib2
import sys
#from vim import *

lang = 'D'
sourcecode = ''
problemNo =  '0029'
pswd = 'Kotarou0781940'
username = 'kotarou777775'

def submit():
    global lang,sourcecode,problemNo,pswd,username
    loginurl = 'http://judge.u-aizu.ac.jp/onlinejudge/index.jsp'
    url = 'http://judge.u-aizu.ac.jp/onlinejudge/servlet/Submit'

    values = { 'loginUserID':username,
	     'problemNO':problemNo,
	     'loginPassword':pswd,
	     'language':lang,
	     'sourceCode':sourcecode,
	     'password':pswd,
	     'userID':username
	     }

    data = urllib.urlencode(values)
    req = urllib2.Request(loginurl,data)
    cont = urllib2.urlopen(req).read()
    req.add_header('Cookie',cont)

    req = urllib2.Request(url,data)
    response = urllib2.urlopen(req)
    #the_page = response.read()


def selectlang():
    global lang
    ext = sys.argv[1]
    if ext == 'c':
	lang = 'C'
    elif ext == 'cpp':
	lang = 'C++'
    elif ext == 'cpp11':
	lang = 'C++11'
    elif ext == 'java':
	lang = 'JAVA'
    elif ext == 'cs':
	lang = 'C#'
    elif ext == 'd':
	lang = 'D'
    elif ext == 'rb':
	lang = 'Ruby'
    elif ext == 'py':
	lang = 'Python'
    elif ext == 'py3':
	lang = 'Python3'
    elif ext == 'php':
	lang = 'PHP'
    elif ext == 'js':
	lang = 'JavaScript'
    elif ext == 'scala':
	lang = 'Scala'
    elif ext == 'hs':
	lang = 'Haskell'
    elif ext == 'ml':
	lang = 'OCaml'

def main():
    global sourcecode
    f = open(sys.argv[0],"r")
    sourcecode = f.read()
    #sourcecode = current.buffer
    selectlang()
    submit()

if __name__ == '__main__':
    main()