#!/usr/bin/env python

import os
import sys
import shutil

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from runtest_dirac import Filter, TestRun

test = TestRun(__file__, sys.argv)

f = Filter()
f.add(from_string = ' TOTAL ENERGY',
      num_lines   = 11,
      rel_tolerance   = 1.0e-8)

test.run(['atoms.inp'], ['atoms.xyz'],
                 args='--replace closed_occ="4" --replace open_e="5" --replace open_orb="6" --replace myatom="F" --outcmo')
if os.path.isfile('CHECKPOINT.h5'):
    ext = '.h5'
elif os.path.isfile('CHECKPOINT.noh5.tar.gz'):
    ext = '.noh5.tar.gz'
shutil.move('CHECKPOINT'+ext, 'X2CMAT_009'+ext)

test.run(['atoms.inp'], ['atoms.xyz'],
                 args='--replace closed_occ="12" --replace open_e="4" --replace open_orb="6" --replace myatom="S" --outcmo')
shutil.move('CHECKPOINT'+ext, 'X2CMAT_016'+ext)

test.run(['large-molecular-x2c-spinfree.inp', 'large-molecular-x2c-spinfree-end.inp', 'large-molecular-x2c-spinfree-beg.inp'],
                 ['sf6.xyz'], f)

test.run(['large-atomic-x2c-spinfree.inp'],
                 ['sf6.xyz'], f, args='--put="X2CMAT_009.h5 X2CMAT_016.h5"')

# cleanup
os.unlink('X2CMAT_016'+ext)
os.unlink('X2CMAT_009'+ext)

sys.exit(test.return_code)
