#!/usr/bin/python

####################################################################################################
### IMPORTS ###
import sys
sys.path.append( "~/server2/adaptiveSampling" )
import FidoTools
from FAHProject import FAHProject 
####################################################################################################

####################################################################################################
### USEFUL SETTINGS ###
fidopath = "/home/server_c/.fido/"
server2dir = "/home/server_c/server2/"
serverBin = "%s/%s" % ( server2dir, "serverV3M_c" )
debug = True
serverAdministrationAllowed = False # if False, script not allowed to run server binaries
####################################################################################################

####################################################################################################
### READ FIDO CONFIGURATION ###
# see documentation in FidoTools for details
print "-- parsing configuration --"
config = FidoTools.FidoConfig( debug = debug, path = fidopath, server2path = server2dir )
scriptPath = config.scriptPath
print
####################################################################################################

####################################################################################################
### LOOP THROUGH AND TEST THE PROJECTS ###
print "-- testing projects --"
projectsToUse = [] 
for project in config.projects:
	project.debug = debug # use same setting as the script

	if debug:
		print "Found project named '%s' using script '%s'" % ( project.name, project.script )

	# how many WUs exist?
	nWUs = project.countReturnedWUs()
	nThreshold = project.min
	nToAdd = project.add
	if debug: 
		print "Found %d returned WUs, need %d to build model with add threshold %d" % ( nWUs, nThreshold, nToAdd )

	# does a model already exist?
	if project.modelExists :
		# do I have enough additional data to justify building a new model?
		if nWUs - project.lastnWUs > nToAdd :
			project.lastnWUs = nWUs
			projectsToUse.append( project )

	elif nWUs > nThreshold :
		project.lastnWUs = nWUs
		projectsToUse.append( project )

	print "- project end -"

if debug:
	print "Found %d projects for model building and updating" % len( projectsToUse )
print

# throw away config.projects ; we don't care about them until next time the script runs
del( config ) 
####################################################################################################

####################################################################################################
### BUILD A MODEL FOR EACH PROJECT THAT REQUIRES IT ###
print "-- building and updating models --"
for project in projectsToUse :
	if project.modelExists :
		if debug :
			print "Model exists!"

		project.updateModel()
	else:
		if debug:
			print "Generating model ..."
		project.buildModel()
print 
####################################################################################################

####################################################################################################
### INTERPRET THE RESULTING MODELS ###
print "-- interpreting models --"
for project in projectsToUse :
	project.analyze()
print
####################################################################################################

####################################################################################################
### MODIFY THE PROJECT BY ADDING NEW RUNs ###
print "-- adding new runs --"
for project in projectsToUse :
	project.addNewRuns()
print
####################################################################################################

####################################################################################################
### RUN ./SetupDataDirs ###
FidoTools.runSetupDataDirs( server2dir, justKidding = not serverAdministrationAllowed )

### RESTART THE SERVER ###
FidoTools.restartServer( serverBin, justKidding = not serverAdministrationAllowed )
####################################################################################################

####################################################################################################
""" don't do this yet
### SAVE FIDO PROJECTS ###
print "-- saving projects --"
for project in projectsToUse:
	project.save() """
print 
####################################################################################################
print "Done."

