#!/cygdrive/c/python32/python
import pdb
import subprocess

im = {}
full = {}
im_sm = {}
inv = {}
breed = {}
birds = {}
places = {}
class_birds = {}
class_places = {}

# First get a list of the files, what is returned has the directory sturcture
# returned on seperate lines and the files, followed by the list of files,
# each on a separate line.
ls_dir = str(subprocess.check_output(['ls', '-R', '../2*']), encoding='utf8')
files = ls_dir.split('\n')
for i in files:
                # looking for the three subdirectories that contain the files
    if 'Images' in i or 'Full_res' in i or 'Images_sm' in i:
        fields = i.split('/')
        if len(fields) > 2:
            date = fields[1] + fields[2]
        if 'Images:' in i:
            sub = 'Images:'
        elif 'Full_res:' in i:
            sub = 'Full_res:'
        elif 'Images_sm:' in i:
            sub = 'Images_sm:'
                # looking for the .jpg files
    elif '.jpg' in i.lower():
        i = i[0:-4]
        ii = date + ':' + i
        if 'Images:' in sub:
            im[ii] = 1
        elif 'Full_res:' in sub:
            full[ii] = 1
        elif 'Images_sm:' in sub:
            im_sm[ii] = 1
                # following lines are for debug purposes, print out the files
                # found in each checked subdirectory
imkeys = list(im.keys())
imkeys.sort()
fout = open('out-image', 'w')
for k in imkeys:
    fout.writelines(k + '\n')
fout.close()

flkeys = list(full.keys())
flkeys.sort()
fout = open('out-full', 'w')
for k in flkeys:
    fout.writelines(k + '\n')
fout.close()

smkeys = list(im_sm.keys())
smkeys.sort()
fout = open('out-small', 'w')
for k in im_sm:
    fout.writelines(k + '\n')
fout.close()

            # read in the inventory database and make sure each entry has a
            # corresponding .jpg file in Images, Images_sm, and Full_res.
print ('Start checking the inventory database against the .jpg files:')
filein = open('inventory.txt')
for line in filein:
    fields = line.split('\t')
            # while we are here, lets checks some of the fields in the inventory
            # database
    if len(fields) < 6:
        print ('Not enough fields in line: ' + line)
            # Check the second field is a number (priority)
    if not fields[1].isdigit():
        print ('Priority field is not an integer: ' + line)
            # Check the third field is a number (breeding).  Also must be between 1-4
    if not fields[2].isdigit():
        print ('Breeding field is not an integer: ' + line)
    else:
        if (int(fields[2]) < 0 or int(fields[2]) > 4):
            print ('Breeding field is not between 1-4: ' + line)
            # only want the year and month of the date field which is
            # concantenated with the picture name (minus .jpg)
    value = fields[0][0:6] + ':' + fields[3]
    if not value in im:
        print (fields[0] + fields[3] + ' not in Images')
    if not value in full:
        print (fields[0] + fields[3] + ' not in Full_res')
    if not value in im_sm:
        print (fields[0] + fields[3] + ' not in Images_sm')
            # store inventory date+file and breeding dates, first making sure
            # there is not already an image with the same date (duplicate 
            # and errorneous database error)
    if (value in inv):
        print ('----Duplicate inventory database entry for: ' + value)
    fields[4] = fields[4].lstrip()
    fields[4] = fields[4].rstrip()
    inv[value] = fields[4] + ':' + fields[2]
            # save the bird name for later checking agains bird_class
    birds[fields[4]] = 1
filein.close()

#pdb.set_trace()
            # read in the place database, do some simple error checking
print ('Start checking the place database against the .jpg files:')
filein = open('place.txt')
for line in filein:
    fields = line.split('\t')
            # while we are here, lets checks some of the fields in the inventory
            # database
    if len(fields) < 6:
        print ('----Not enough fields in line: ' + line)
            # Check the second field is a number (priority)
    value = fields[0][0:6] + ':' + fields[3]
    if not value in im:
        print (----fields[0] + fields[3] + ' not in Images')
    if not value in full:
        print (----fields[0] + fields[3] + ' not in Full_res')
    if not value in im_sm:
        print (----fields[0] + fields[3] + ' not in Images_sm')
            # store inventory date+file and breeding dates making sure
            # there is not already an image with the same date (duplicate 
            # and errorneous database error)
    if (value in inv):
        print ('----Duplicate property database entry for: ' + value)
            # store the birds name and breeding status, for
            # later checking with bird_class
    fields[4] = fields[4].lstrip()
    fields[4] = fields[4].rstrip()
    inv[value] = fields[4] + ':' + fields[2]
            # save the place name for later checking agains place_class
    places[fields[4]] = 1
filein.close()


            # now check the files against the database making sure that
            # each file has an entry in the database
print ('Start checking the .jpg files against the database:')
print ('  Checking files in Image subdirectory')
for k in imkeys:
    if not k in inv:
        #pdb.set_trace()
        print ('----Not in inventory database: ' + k )
print ('  Checking files in Full-res subdirectory')
for k in flkeys:
    if not k in inv:
        print ('----Not in inventory database: ' + k )
print ('  Checking files in Image-sm subdirectory')
for k in smkeys:
    if not k in inv:
        print ('----Not in inventory database: ' + k )


        # now read in and check the class databases (bird and place) against the
        # inventory database
print ('Checking bird_class database')
filein = open('bird_class')
for line in filein:
    line = line.rstrip('\n')
        # only look at bird lines
    if 'bird:' in line[0:5]:
        fields = line.split(':')
                # fields[2] hold the picture path info
        ff = fields[2].split ('/')
                # gets year, month and pic name (-1)
        pic = ff[0]+ff[1]+':'+ff[-1]
                # get rid of .jpg extension
        pic = pic[0:-4]
                # get rid of spaces
        pic = pic.replace(' ','')
                # check for it in inventory database
        if not pic in inv:
            print ('----Following bird picture not in inventory database: ' + line)
                # check on breeding status, entry in fields[4]
        fields[4] = fields[4].replace('\s','')
        fields[1] = fields[1].lstrip()
        fields[1] = fields[1].rstrip()
        if (fields[4]):
            breed[fields[1]] = 1
                # save the names for later checking against birds in inventory
        class_birds[fields[1]] = 1
filein.close()

        # now run through the inventory and make sure that the breeding dates are correct
keys = list(inv.keys())
for k in keys:
    fields = inv[k].split(':')
    if fields[1] == '2' or fields[1] == '3':
        if not fields[0] in breed:
            #pdb.set_trace()
            print ('----Incorrect breeding status: ' + inv[k])
    elif fields[1] == '4':
        if fields[0] in breed:
            print ('----Incorrect breeding status: ' + inv[k])

        # Now check the place class database against the inventory database
print ('Checking place database')
filein = open('place_class')
for line in filein:
    line = line.rstrip('\n')
    if 'place:' in line[0:6]: 
        fields = line.split(':')
                # fields[2] hold the picture path info
        ff = fields[2].split ('/')
                # gets year, month and pic name (-1)
        pic = ff[0]+ff[1]+':'+ff[-1]
                # get rid of .jpg extension
        pic = pic[0:-4]
                # get rid of spaces
        pic = pic.replace(' ','')
                # check for it in inventory database
        if not pic in inv:
            print ('----Following place picture not in inventory database: ' + line)
                # save the names for later checking against places in place_class
        fields[1] = fields[1].lstrip()
        fields[1] = fields[1].rstrip()
        class_places[fields[1]] = 1
filein.close()


        # Now I need to make sure all the birds and places in the inventory
        # and place database have an entry in the in_class database
print ('Checking to make sure all bird and place in class databases')
keys = list(birds.keys())
for k in keys:
    if not k in class_birds:
        print ('----Birds not in birds_class database: ' + k)
keys = list(places.keys())
for k in keys:
    if not k in class_places:
        pdb.set_trace()
        print ('----Place not in place_class database: ' + k)
