#!/cygdrive/c/python34/python
import pdb
import subprocess
from bird_utils import ls_cmd

# Move the raw files from the specified directory (from) to the specified directory (to)
# if the 'to' .jpg file has a .cr2 file in 'from'
import os.path

base = 'c:/Users/Nancy/Pictures/'
raw_file = 'data/move_raw'


        # first need the directory where the raw files are
        # do I have one saved
#pdb.set_trace()
if not os.path.isfile(raw_file):
    rawd = input('raw Directory to process:')
    rawd = base+rawd
    if not os.path.isdir(rawd):
        print ("Raw directory not found: ", rawd)
        exit
    outf = open(raw_file, 'w')
    outf.write(rawd)
else:
    rawd = open(raw_file).read()
    rawd = rawd.rstrip('\n')
    ans = input(rawd+ " (y) or new directory? ")
    ans = ans.rstrip('\n')
    if ans != 'y':
        rawd = base + ans
        outf = open(raw_file, 'w')
        outf.write(rawd)
        outf.close()

    # Not find out where the 'to' directory is or where the raw files will go
jdir = input('.jpg directory(s) to process:')
jdir = jdir.rstrip('\n')
jdir = base + jdir
if not os.path.isdir(jdir):
    print ("Not a valid directory: ", jdir)
    exit
files = ls_cmd("-Rc1", jdir)

pdb.set_trace()
    # first find all the subdirectories and create a list of them

for afile in files:
    afile = afile.rstrip("\n")
    if ":" in afile:
        ldir = afile
        ldir = ldir.replace(":", "")
        ldir = ldir.replace("c/", "c:/")
    if '.JPG' in afile or '.jpg' in afile:
        #pdb.set_trace()
        afile = afile.replace('JPG', 'CR2')
        afile = afile.replace('jpg', 'CR2')
        from_file = rawd + '/' + afile
        to_file = ldir + '/' + afile
        if os.path.isfile(from_file):
            os.rename(from_file, to_file)
        else:
            print ('mv of file ' + afile + 'failed: .Cr2 file not found')
