executable_shell = '/bin/csh'

# create outputFilePath in case it does not yet exist
def createFilePath_recursively(filePath):
    filePath_items = filePath.split('/')
    currentFilePath = "/"
    for filePath_item in filePath_items:
        currentFilePath = os.path.join(currentFilePath, filePath_item)
        if len(currentFilePath) <= 1:
            continue
        if not os.path.exists(currentFilePath):
            os.mkdir(currentFilePath)

if outputFilePath.find("/castor") != -1:
    try:
        castor.rfstat(outputFilePath)
    except RuntimeError:
        print "outputFilePath does not yet exist, creating it."
        os.system("rfmkdir %s" % outputFilePath)
        os.system("rfchmod 777 %s" % outputFilePath)
elif outputFilePath.find("/store") != -1:
    try:
        eos.lsl(outputFilePath)
    except IOError:
        print "outputFilePath does not yet exist, creating it."
        eos.mkdir(outputFilePath)
else:
    hostname = socket.gethostname()
    numChars = min(len(hostname), len(outputFileMachine))
    if outputFileMachine[:numChars] != hostname[:numChars]:
        raise ValueError("Cannot create output file path = %s on machine %s !!" % (outputFilePath, outputFileMachine))
    for filePath_item in filePath_items:
        currentFilePath = os.path.join(currentFilePath, filePath_item)
        if len(currentFilePath) <= 1:
            continue
        if not os.path.exists(currentFilePath):
            #sys.stdout.write("creating directory %s\n" % currentFilePath)
            os.mkdir(currentFilePath)


hostname = socket.gethostname()
print "hostname = %s" % hostname
if hostname == 'ucdavis.cern.ch':
    print "Running on %s" % hostname

try:
    castor.rfstat(harvestingFilePath)
except RuntimeError:
    # harvestingFilePath does not yet exist, create it
    print "harvestingFilePath does not yet exist, creating it."
    os.system("rfmkdir %s" % harvestingFilePath)
    os.system("rfchmod 777 %s" % harvestingFilePath)

outputFilePath = os.path.join(outputFilePath, version)
print "outputFilePath = %s" % outputFilePath
createFilePath_recursively(outputFilePath)
for channelToAnalyze in channelsToAnalyze:
    outputFilePath_channel = os.path.join(outputFilePath, channelToAnalyze)
    createFilePath_recursively(outputFilePath_channel)

configFilePath = os.path.join(os.getcwd(), "lxbatch")
print "configFilePath = %s" % configFilePath
# create outputFilePath in case it does not yet exist
def createFilePath_recursively(filePath):
    filePath_items = filePath.split('/')
    currentFilePath = "/"
    for filePath_item in filePath_items:
        currentFilePath = os.path.join(currentFilePath, filePath_item)
        if len(currentFilePath) <= 1:
            continue
        if not os.path.exists(currentFilePath):
            os.mkdir(currentFilePath)


if outputFilePath.find("/castor") != -1:
    try:
        castor.rfstat(outputFilePath)
    except RuntimeError:
        print "outputFilePath does not yet exist, creating it."
        os.system("rfmkdir %s" % outputFilePath)
        os.system("rfchmod 777 %s" % outputFilePath)
elif outputFilePath.find("/store") != -1:
    try:
        eos.lsl(outputFilePath)
    except IOError:
        print "outputFilePath does not yet exist, creating it."
        eos.mkdir(outputFilePath)
else:
    hostname = socket.gethostname()
    numChars = min(len(hostname), len(outputFileMachine))
    if outputFileMachine[:numChars] != hostname[:numChars]:
        raise ValueError(
    currentFilePath = "/"
    for filePath_item in filePath_items:
        currentFilePath = os.path.join(currentFilePath, filePath_item)
        if len(currentFilePath) <= 1:
            continue
        if not os.path.exists(currentFilePath):
            #sys.stdout.write("creating directory %s\n" % currentFilePath)
            os.mkdir(currentFilePath)

hostname = socket.gethostname()
print "hostname = %s" % hostname
if hostname == 'ucdavis.cern.ch':
    print "Running on %s" % hostname

try:
    castor.rfstat(harvestingFilePath)
except RuntimeError:
    # harvestingFilePath does not yet exist, create it
    print "harvestingFilePath does not yet exist, creating it."
    os.system("rfmkdir %s" % harvestingFilePath)
    os.system("rfchmod 777 %s" % harvestingFilePath)
    
outputFilePath = os.path.join(outputFilePath, version)
print "outputFilePath = %s" % outputFilePath
createFilePath_recursively(outputFilePath)
for channelToAnalyze in channelsToAnalyze:
    outputFilePath_channel = os.path.join(outputFilePath, channelToAnalyze)
    createFilePath_recursively(outputFilePath_channel)

configFilePath = os.path.join(os.getcwd(), "lxbatch")
print "configFilePath = %s" % configFilePath