#!/usr/bin/env python3
"""
Script to concatenate the MS files from Linc for input to Rapthor
"""
import argparse
import glob
import os
import sys
from rapthor.scripts.concat_ms import concat_ms


def main(input_path, output_file, overwrite=False):
    """
    Concatenate MS files from Linc for input to Rapthor

    Parameters
    ----------
    input_path : str
        Full path to the directory with the Linc MS files
    output_file : str
        Filename of output file
    overwrite : bool, optional
        If True and output_file points to an existing file, the file is
        overwritten
    """
    msfiles = []
    for pattern in ["*.ms", "*.MS"]:
        msfiles.extend(glob.glob(os.path.join(input_path, pattern)))
    return concat_ms(msfiles, output_file, overwrite=overwrite)


if __name__ == "__main__":
    descriptiontext = "Concatenate Linc MS files.\n"
    parser = argparse.ArgumentParser(
        description=descriptiontext, formatter_class=argparse.RawTextHelpFormatter
    )
    parser.add_argument(
        "input_path", help="Full path to the directory with the Linc MS files"
    )
    parser.add_argument("output_file", help="Output filename")
    parser.add_argument('--overwrite', help='Overwrite existing output file', type=bool,
                        default=False)

    args = parser.parse_args()
    try:
        sys.exit(main(args.input_path, args.output_file, overwrite=args.overwrite))
    except Exception as e:
        print('Error: {}'.format(e))
