#!/usr/bin/env python

import subprocess
import sys
import os

def populate(node, directory):
    try:
        os.makedirs('/export/%s.0/schastel/populate/sql' % node)
    except OSError:
        pass
    output = open('/export/%s.0/schastel/populate/sql/populate_%s_%s.sql' % (node, node, directory),
                  'w')
    ignored = open('/export/%s.0/schastel/populate/ignored_%s_%s' % (node, node, directory),
                   'w')
    stdout = open('/export/%s.0/schastel/populate/stdout_%s_%s' % (node, node, directory),
                  'w')
    index_filename = '/export/%s.0/schastel/mlocate/%s.%s.mlocate_db' % (node, node, directory)
    p = subprocess.Popen(('/home/panstarrs/schastel/local/bin/locate -d %s .' 
                          % index_filename).split(' '),
                         stdout = subprocess.PIPE)
    p.poll()
    to_remove = '/export/%s.0/nebulous/%s' % (node, directory)
    count = 1
    for line in p.stdout:
        if count % 10000 == 0:
            stdout.write('%s %s %s\n' % (node, directory, count))
        line = line[:-1].replace(to_remove, '')
        fields = line.split('/')
        if len(fields) == 3:
            count += 1
            subdirectory = fields[1]
            filename = fields[2]
            elements = filename.split('.')
            neb_key = '.'.join(elements[1:]).replace(':', '/')
            output.write(insertTemplate % (node, directory, filename, elements[0], subdirectory, neb_key))
        else:
            ignored.write('Ignored: %s %s %s\n' % (node, directory, line))
    output.close()

letters = '0123456789abcdef'
insertTemplate=""
sqlFile = open('/home/panstarrs/schastel/dev/FilesMonitoring/sql/insert_template.sql')
for line in sqlFile:
    insertTemplate = "%s%s" % (insertTemplate, line)
sqlFile.close()

node = sys.argv[1]
for letter1 in letters:
    for letter2 in letters:
        directory = '%s%s' % (letter1, letter2)
        populate(node, directory)

