#!/usr/bin/python3 -u

from pathlib import Path
import sys

#print "No matches found.\n";
#print "Match found at offset $pos.\n";

def print_usage():
    print("Usage: str_match Search File")
    print("Or: STDIN str_match Search")

def main():
    source_fh = None

    if source_file is not None and Path(source_file).is_dir():
        print("Error: '{0}' is a directory, not a file".format(source_file))
        exit(1)

    if source_file is not None:
        try:
            source_fh = open(source_file, "r")
        except:
            print("Cannot open the file '{0}'".format(source_file))
            exit(1)

    else:
        source_fh = sys.stdin

    input_data = source_fh.read()
    if input_data == "":
        print("Error: No input provided.")
        exit(1)

    if search_str in input_data:
        print("Match found.")
        exit(0)
    else:
        print("No matches found.")
        exit(1)

if len(sys.argv) < 2:
    print_usage()
    exit(1)

search_str = sys.argv[1]
source_file = None
if len(sys.argv) == 3:
    source_file = sys.argv[2]
elif len(sys.argv) > 4:
    print_usage()
    exit(1)

main()