#!/usr/bin/env python3

#  Copyright (C) 2019 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

__author__  = 'You Lu <you.lu@stfc.ac.uk>'
__version__ = '2019.0'

from sys        import stdout

def run(args):
    ''''''

    from subprocess import run
    from os         import path

    atenexec   = path.join('@ATEN_DIR@', 'bin', 'aten')
    datadir    = path.join('@ATEN_DIR@', 'data')
    pluginsdir = path.join('@ATEN_DIR@', 'data', 'plugins')

    try:
        command = ' '.join([ atenexec,
                             '--atendata={}'.format(datadir),
                             '--atenplugins={}'.format(pluginsdir),
                             args.filenames])
        print(" >>> Executing command: %s\n"%command)
        stdout.flush()

        run(command,
            shell=True,
            check=True)

    except:
        raise

def parseArguments():
    ''''''

    from argparse import ArgumentParser, REMAINDER, RawTextHelpFormatter
    from os       import path
    from sys      import argv

    # file containing saved command line arguments starts with '@'
    parser = ArgumentParser(prog='aten-chemsh',
                            description='ChemShell script to invoke Aten GUI',
                            fromfile_prefix_chars='@',
                            formatter_class=RawTextHelpFormatter)

    parser.add_argument('-v', '--version',
                         action='version',
                         default=__version__,
                         version='%(prog)s: ChemShell version {}'.format(__version__))

    parser.add_argument('filenames',
                         nargs='?')

    # parse
    args = parser.parse_args()

    return args


def main():
    '''Main'''

    args = parseArguments()

    if not args.filenames:
        " >>> Aten for Py-ChemShell: please specify a structure file"
        exit(999)

    # select platform
    run(args)


if __name__ == '__main__':

    main()


