import sys

timestamps = []
emails = []
names = []
phones = []
affiliations = []
designations = []

def readAll(datafile):
    df = open(datafile)
    header = df.readline()
    line = df.readline()

    while not line == '':
        line = line.strip()         # Check what happens if you comment out this line.
        fields = line.split('\t')
        ff = 0
        timestamps.append(fields[ff]);      ff = ff + 1
        emails.append(fields[ff]);          ff = ff + 1
        names.append(fields[ff]);           ff = ff + 1
        phones.append(fields[ff]);          ff = ff + 1
        affiliations.append(fields[ff]);    ff = ff + 1
        designations.append(fields[ff]);    ff = ff + 1

        line = df.readline()

    df.close()

def printOne(timestamp, email, name, phone, affiliation, designation):
    print(name, affiliation, designation, email, phone, sep=':::')

def printRecord(ii):
    printOne(timestamps[ii], emails[ii], names[ii], phones[ii], affiliations[ii], designations[ii])

def printAll():
    for ii in range(len(timestamps)):
        printRecord(ii)

def nonStudents():
    for ii in range(len(designations)):
        if not designations[ii] == 'Student':
            printRecord(ii)

def iit():
    for ii in range(len(timestamps)):
        lowaffil = affiliations[ii].lower()
        if ("iit" in lowaffil and not "iiit" in lowaffil) or \
            "indian institute of technology" in lowaffil:
            printRecord(ii)

def match(ii, jj):
    return (names[ii].upper() == names[jj].upper() and emails[ii].upper() == emails[jj].upper())

def duplicates():
    for ii in range(len(timestamps)):
        for jj in range(ii + 1, len(timestamps)):
            if match(ii, jj):
                print("Records", ii, "and", jj, "are duplicates")
                printRecord(ii)
                printRecord(jj)

def groupByAffiliation():
    grouped = zip(affiliations, names, emails)
    sg = sorted(grouped)
    for record in sg:
        print(record[0], record[1], record[2], sep=":::")

if not len(sys.argv) == 2:
    print("Usage:", sys.argv[0], "<datafile.csv>")
    sys.exit(1)

readAll(sys.argv[1])
print("----------- All records ------------")
printAll()
print("----------- F1: Nonstudents ------------")
nonStudents()       # Functionality 1
print("----------- F2: IIT ------------")
iit()               # Functionality 2
print("----------- F3: Duplicates ------------")
duplicates()               # Functionality 3
print("----------- F4: Group by affiliation ------------")
groupByAffiliation()               # Functionality 4
