#!/usr/bin/env python

#############################################################################
# Author  : Jerome ODIER
#
# Email   : jerome.odier@cern.ch
#           jerome.odier@lpsc.in2p3.fr
#
# Version : 5.X.X for nEMD (2013-2014)
#
#############################################################################

import sys, time, pyAMI.app, nedm.config, nedm.updater, nedm.exception

#############################################################################
# UPDATER                                                                   #
#############################################################################

def mode_update(client, args):
	#####################################################################
	#                                                                   #
	#####################################################################

	print('>>>> %s <<<<' % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))

	#####################################################################
	#                                                                   #
	#####################################################################

	nedm.config.current_site_name = args['site']

	#####################################################################
	#                                                                   #
	#####################################################################

	is_ok = False

	for nedm_site in nedm.config.nedm_sites:

		if nedm_site['name'] == nedm.config.current_site_name:

				nedm.config.current_site_path = nedm_site['path']

				is_ok = True

				break

	if not is_ok:

		try:
			raise nedm.exception.NEDMException('error: site `%s` not valid !' % nedm.config.current_site_name)

		except nedm.exception.NEDMException as e:
			print(e)
			return 1

	#####################################################################
	#                                                                   #
	#####################################################################

	updater = nedm.updater.NEDMUpdater(client)

	if (args['prepare'] == False or updater.prepare() != False) and updater.update() != False:
		return 0
	else:
		return 1

#############################################################################
# ENTRY POINT                                                               #
#############################################################################

def entry_point():
	app = pyAMI.app.App('nedm')

	#####################################################################

	subparser = app.subparsers.add_parser('update', help = 'update runs')
	subparser.set_defaults(func = mode_update)

	subparser.add_argument('-s', '--site',
			dest = 'site', help = 'site',
				choices = [nedm_site['name'] for nedm_site in nedm.config.nedm_sites], default = nedm.config.current_site_name)

        subparser.add_argument('-p', '--prepare',
                        action = 'store_true', dest = 'prepare', help = 'prepare databases')

	#####################################################################

	return app.run()

#############################################################################

if __name__ == '__main__':
	sys.exit(entry_point())

#############################################################################
