#!/usr/bin/env python

import MySQLdb
import sys

if __name__ == '__main__':
    if len(sys.argv) == 1:
        usage()
        sys.exit(1)
    node = sys.argv[1]
    filesMonitoring = MySQLdb.connect( 'ippc11.ipp.ifa.hawaii.edu',
                                       'ippuser', 'ipp',
                                       'FilesMonitoring' )
    letters = '0123456789abcdef'
    last_table_with_entries = ''
    zero_seen = False
    for letter1 in letters:
        for letter2 in letters:
            query = 'SELECT COUNT(*) FROM Files_%s_%s%s;' % ( node,
                                                              letter1,
                                                              letter2 )
            cursor = filesMonitoring.cursor()
            cursor.execute(query)
            rows = cursor.fetchall()
            cursor.close()
            if rows[0][0] != 0:
                last_table_with_entries = '%s%s' % ( letter1,
                                                     letter2 )
                if zero_seen:
                    sys.stderr.write('Entries in %s%s while last_table_with_entries is %s\n' % ( letter1,
                                                                                                 letter2,
                                                                                                 last_table_with_entries) )
            else:
                zero_seen = True

    sys.stderr.write('Last table with an entry: Files_%s_%s\n' % (node,
                                                                  last_table_with_entries) )
    if last_table_with_entries != 'ff':
        print 'DELETE FROM Files_%s_%s;'  % (node, last_table_with_entries)
