#!/usr/bin/python


import sys
import mitsfs


__release__ = '0'

program = 'barcoder'


def main(args):
    mitsfs.banner(program, __release__)

    dex = mitsfs.DexDB()

    while True:
        book = mitsfs.specify_book(dex)

        if not book:
            break

        print 'Adding barcode to:'
        print book

        while True:
            text = mitsfs.read('New Barcode: ')
            code = mitsfs.valifrob(text)

            # SKABETTI
            if code:
                other = dex.barcode(code)
                if other:
                    print "No, that's", other
                    continue
                if book.addbarcode(code):
                    print 'Now:'
                    print book
                else:
                    print 'Error assigning code?'
                break
            else:
                if text:
                    print 'Invalid Code'
                else:
                    break


if __name__ == '__main__':
    main(sys.argv)
