#!/usr/bin/python
# -*- coding: latin1 -*-
#!/usr/bin/python2
#code par Jerome KIEFFER
#le 11/04/2005
# ce programme prend deux fichiers  CIF en entree et ajoute les donnees manquantes 
#au premier a partir des infos du second.
 


import sys,os
sys.path.append(os.getenv("HOME")+"/bin")
import libcif as C





if __name__=="__main__":
	ciffile="kcd.cif"
	if len(sys.argv)<3 : 
		try:
			CIF1=C.LoadCIF(ciffile)
			CIF2=C.LoadCIF("sg.cif")
		except:
			print "Veuillez entrer sur la ligne de commande les DEUX fichier CIF a fusionner.\n  Le contenu du second sera ajoute intelligement au premier.\nLe troisieme fichier correspond au fichier destination (faculatif). "
			sys.exit(1)
	else:
		try:
			CIF1=C.LoadCIF(sys.argv[1])
			ciffile=sys.argv[1]
			CIF2=C.LoadCIF(sys.argv[2])	
		except:
			print "oups ... certains fichiers ne sont pas lisibles"
			sys.exit(2)

	if len(sys.argv)==4 :
		name=sys.argv[3]

	else:
		struc=os.path.abspath(ciffile).split("/")
#		print struc
#		i=1
		i=struc.index("Xtal")
		name="-".join(struc[i+1:-1])

	cif=C.MergeSg(name,CIF1,CIF2)
	print "Completing the CIF file with the data generated by PLATON"
	cif2=C.mergeplaton(cif,C.platon(ciffile))
	print "Checking for the data needed to generate a report"
	cif,mod=C.CheckForRST(cif2)
	if not mod: print "OK"
	print "Checking for the data needed to enter the CSD-database"
	cif2,mod=C.CheckForCSD(cif,name)
	if not mod: print "OK"
	cifout=name+".cif"
	print cifout
	C.SaveCIF(cif2,cifout)
	C.SaveCIF(cif2,os.getenv("HOME")+"/CIF/"+cifout)

