#!/usr/bin/python

# Copyright (C) 2006 Peter Poeml.  All rights reserved.
# This program is free software; it may be used, copied, modified
# and distributed under the terms of the GNU General Public Licence,
# either version 2, or (at your option) any later version.


import sys, os, MySQLdb

import ConfigParser

conffile = '/etc/mirrorbrain.conf'

cp = ConfigParser.SafeConfigParser()
cp.read(conffile)
cfg = dict(cp.items('general'))

try:
    conn = MySQLdb.connect(host=cfg['dbhost'], port=int(cfg['dbport']), 
                          user=cfg['dbuser'], passwd=cfg['dbpass'], 
                          db=cfg['dbname'])
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

cursor = conn.cursor()

# cursor.execute('insert into file_server set fileid=13, serverid=1333, path_md5=%s', ['no md5, just a test']);
# print 'file_server inserted:         %10d' % cursor.rowcount

# cursor.execute('select count(*) from file_server')
# cursor.execute('select count(*) from file_server')
# cursor.execute('select count(*) from file_server')
# cursor.execute('select count(*) from file_server')
# print 'file_server total:            %10d' % cursor.fetchone()[0]
# conn.commit()


# file_server table
cursor.execute('select count(*) from file_server as fs left outer join server as s on fs.serverid = s.id where isnull(s.id)')
print 'file_server stale:            %10d' % cursor.fetchone()[0]

cursor.execute('select count(*) from file_server as fs left outer join server as s on fs.serverid = s.id where isnull(s.id) or s.enabled = 0')
print 'file_server stale/disabled:   %10d' % cursor.fetchone()[0]

print 


# file table
cursor.execute('select count(*) from file')
print 'file total:                   %10d' % cursor.fetchone()[0]

cursor.execute('select count(*) from file as f left outer join file_server as fs on f.id = fs.fileid where isnull(fs.fileid)')
print 'file stale:                   %10d' % cursor.fetchone()[0]

print 


#
# now, delete...
#

cursor.execute('delete fs from file_server fs left outer join server s on fs.serverid = s.id where isnull(s.id) or s.enabled = 0')
print 'file_server rows to delete:   %10d' % cursor.rowcount
cursor.execute('select count(*) from file_server')
print 'file_server remaining:        %10d' % cursor.fetchone()[0]

print 

cursor.execute('delete f from file as f left outer join file_server as fs on f.id = fs.fileid where isnull(fs.fileid)')
print 'file rows to delete:          %10d' % cursor.rowcount
cursor.execute('select count(*) from file_server')
print 'file remaining:               %10d' % cursor.fetchone()[0]

print 


cursor.close()

print 'committing...'
conn.commit()
print 'done.'
conn.close()
