#!/usr/bin/env python3

# ----
# MAIN
# ----
if __name__ == '__main__':
	import argparse
	import sys
	argument_parser = argparse.ArgumentParser(
		formatter_class = argparse.ArgumentDefaultsHelpFormatter,
		description = 'Periodically output/forward the input strings, while at the same time logging to stderr or a log file',
	)
	argument_parser.add_argument(
		'--input_file', '-i',
		type = argparse.FileType('r'),
		default = '-',
		help = 'the input file to read from',
	)
	argument_parser.add_argument(
		'--output_file', '-o',
		type = argparse.FileType('w'),
		default = '-',
		help = 'the output file to write to',
	)
	argument_parser.add_argument(
		'--log_file', '-l',
		type = argparse.FileType('w'),
		default = sys.stderr,
		help = 'the log file to log to',
	)
	argument_parser.add_argument(
		'--pause', '-p',
		type = float,
		default = 1.0,
		help = 'how many seconds to pause between outputting lines',
	)
	argument_parser.add_argument(
		'strings',
		metavar = 'string',
		type = str,
		nargs = '*',
		help = 'the string(s) to send. If omitted, will use lines read from <input_file> instead',
	)
	arguments = argument_parser.parse_args()

	for string in (arguments.strings if arguments.strings else arguments.input_file):
		string = string.strip()
		print(string, file = arguments.log_file)
		arguments.log_file.flush()
		print(string, file = arguments.output_file)
		arguments.output_file.flush()
		if (arguments.pause > 0):
			import time
			time.sleep(arguments.pause)
