#!/usr/bin/python

import sys

import mitsfs

d=mitsfs.dexdb(dsn=mitsfs.database_dsn)

if len(sys.argv) == 1:
    ((inventory_code, inventory_id, inventory_desc),) = \
        d.cursor.execute('select inventory_code, inventory_id, inventory_desc'
                         ' from inventory order by inventory_stamp desc limit 1')
else:
    _, inventory_code = sys.argv
    ((inventory_id, inventory_desc),) = \
        d.cursor.execute('select inventory_id, inventory_desc'
                         ' from inventory where inventory_code=%s',
                         (inventory_code,))

print '%s (%s)' % (inventory_desc, inventory_code)

while True:
    shelfcode = mitsfs.read('shelfcode: ', callback=mitsfs.codes.iterkeys).upper().strip()
    if not shelfcode:
        break
    try:
        (shelfcode_id,) = d.cursor.execute('select shelfcode_id from shelfcode where shelfcode=%s', (shelfcode,))
    except ValueError:
        print 'No such shelfcode'
        continue

    print shelfcode, mitsfs.codes[shelfcode].get('name', '')
    print
    while True:
        title = mitsfs.specify(d)
        if not title:
            break
        print title.title_id, title
        comment = mitsfs.read('comment? ')
        d.cursor.execute('insert into shelf_divisions(title_id, inventory_id, shelfcode_id, division_comment)'
                         ' values (%s,%s,%s,%s)',
                         (title.title_id, inventory_id, shelfcode_id, comment))
        d.db.commit()

        print '(%s)' % shelfcode

