#!/usr/bin/env python
from __future__ import division

"""
intersect: DESCRIPTION
"""

__version__ = "$Revision: 1.2 $"

# Copyright 2007 Michael M. Hoffman <hoffman+software@ebi.ac.uk>

import sys
import textinput

def get_intersection(filenames):
    res = None

    for textfilename, textfile in textinput.files(filenames):
        new_set = set(line.rstrip() for line in textfile)
        if res is None:
            res = new_set
        else:
            res &= new_set

    return res

def intersect(*filenames):
    intersection = get_intersection(filenames)

    print "\n".join(intersection)

def parse_options(args):
    from optparse import OptionParser

    global options

    usage = "%prog [OPTION]... [FILE]..."
    version = "%%prog %s" % __version__
    parser = OptionParser(usage=usage, version=version)

    options, args = parser.parse_args(args)

    return args

def main(args):
    args = parse_options(args)

    return intersect(*args)

def _test(*args, **kwargs):
    import doctest
    doctest.testmod(sys.modules[__name__], *args, **kwargs)

if __name__ == "__main__":
    if __debug__:
        _test()
    sys.exit(main(sys.argv[1:]))
