#!/usr/bin/env python

import subprocess
import sys
import os

bin_directory = "%s/%s/bin" % (os.environ['PSCONFDIR'], os.environ['PSCONFIG'])
share_directory = "%s/%s/share/filemon/" % (os.environ['PSCONFDIR'], os.environ['PSCONFIG'])
node_directory = '/export/%s.0/ipp_filemon/'

def populate(node, directory):
    root_dir = "%s/populate" % node_directory
    try:
        os.makedirs(('%s/sql' % root_dir) % node)
    except OSError:
        pass
    output = open(('%s/%s' % (root_dir, 'sql/populate_%s_%s.sql')) % (node, node, directory),
                  'w')
    ignored = open(('%s/%s' % (root_dir, 'log/ignored_%s_%s')) % (node, node, directory),
                   'w')
    stdout = open(('%s/%s' % (root_dir, 'log/stdout_%s_%s')) % (node, node, directory),
                  'w')
    index_filename = ('%s/%s' % (node_directory,'/mlocate/%s.%s.mlocate_db')) % (node, node, directory)
    p = subprocess.Popen(('%s/locate -d %s .' 
                          % bin_directory,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('%s/sql/insert_template.sql' % share_directory)
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)

