def generate_orca_input(filename, chg, mult):
    """
    From a xyz file generate an ORCA input suitable for calculating RESP charges
    :param filename: name of the xyz file
    :param chg: charge of the molecule
    :param mult: mu,ltiplicity of the molecule
    :return: the name of the .inp file to execute with ORCA
    """

    def get_xyz_list():
        xyz_file = open(filename, 'r')
        n_atoms = int(xyz_file.readline())
        xyz_file.readline()  # Read the title line
        list_atomxyz = []

        for i in range(n_atoms):
            line = xyz_file.readline().split()

            try:
                atom, x, y, z = line
                list_atomxyz.append([atom, x, y, z])
            except ValueError:
                print('It looks like there is an error in the .xyz file')
                return []

        return list_atomxyz

    xyz_list = get_xyz_list()

    def generate_inp():

        keyword_line = '! RHF 6-31G(d) RIJCOSX def2/J PAL2'

        with open(inp_filename, 'w') as inp_file:
            print(keyword_line, file=inp_file)
            print('*xyz', chg, mult, file=inp_file)

            for xyz_line in xyz_list:
                atom, x, y, z = xyz_line
                print("{:<5s}{:10.5f}{:10.5f}{:10.5f}".format(atom, float(x), float(y), float(z)), file=inp_file)

            print('*', file=inp_file)

    inp_filename = filename.replace('.xyz', '.inp')
    generate_inp()

    return inp_filename