#!/usr/bin/python
import mitsfs
d=mitsfs.dexdb()

((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')

print '%s (%s)' % (inventory_desc, inventory_code)
last = None
last_title = None
while True:
    scan = mitsfs.read('Scan shelfdex code: ').strip().strip('Aa')
    if not scan:
        break
    if scan=='-':
        if not last:
            print 'huh?'
            continue
        d.cursor.execute('delete from inventory_missing where inventory_entry_id=%s', (inventory_entry_id,))
        print 'deleted',mitsfs.title(d, title_id)
        last = None
    try:
        (inventory_entry_id,title_id) = d.cursor.execute('select inventory_entry_id where entry_barcode=%s and inventory_id=%s', (scan, inventory_id))
    except ValueError:
        print 'Bad scan, try again, then tell libcomm'
    	continue

    d.cursor.execute('insert into inventory_missing(inventory_entry_id) values (%s)', (inventory_entry_id,))
    d.db.commit()
    print inventory_entry_id,mitsfs.title(d, title_id)
    last=inventory_entry_id
    last_title=title_id
                         
                         

    
