#! /usr/bin/python2.7

# RKN 08/01/2016  Rev 3.0
# Removes duplicate entries in sharestoget file.
# reorder data by date recorded
# reports missing Friday info
# removes repeated Friday info
# updates index in sharestoget file 

import calendar
from datetime import date
import shutil



def copyFile(srcf, destf):
    try:
        shutil.copy(srcf, destf)
    except shutil.Error as e:
        print ('Error: %s' %e)
    except IOError as e:
        print('Error: %s' %e.strerror)


base = "SharesData/"
#base = "TestSharesData/"



# check sharestoget file for duplicate records

filename = base + "sharestoget.txt"
tempsharestoget = base + "tempsharestoget.txt"


savedshareslist = open(filename, 'r')

tempsharestogetlist = open(tempsharestoget, 'w')

tempsharestogetlist.close()

textfound = 0

for textline in savedshareslist:
    datafortemp = textline
    commapos = textline.find(",")
    sharename = textline[0:commapos]
    #print sharename
    tempsharestogetlist = open(tempsharestoget, 'r')
    for lineoftxt in tempsharestogetlist:
        if lineoftxt.find(str(sharename)) != -1:
            textfound = 1
            
    if textfound != 1:
        #print "TEXT NOT FOUND"
        tempsharestogetlist.close()
        tempsharestogetlist = open(tempsharestoget, 'a')
        tempsharestogetlist.write(datafortemp)
        tempsharestogetlist.close()
        textfound = 0
    else:
        print "duplicated record found in sharestoget file" , datafortemp
        tempsharestogetlist.close()
        textfound= 0

savedshareslist.close()


copyFile(srcf =str(tempsharestoget),destf= str(filename))
print "sharestoget file checks complete"
print " "




# next section


filename = base + "sharestoget.txt"
tempdate = base + "tempdate.txt"
tempreorder = base + "tempreorder.txt"

savedshareslist = open(filename, 'r')


    
tempsharestogetlist = open(tempreorder, 'w')

tempsharestogetlist.close()



for line in savedshareslist:

    yearold = 0

    newsharestogetline = line    

    #print "raw line", str(line)
    lengthofline = len(line)
    # print "lengthofline" , lengthofline
    commapos = line.find(",")

    line = line[0:(commapos-1)]
        
    #print "modified string" , line

    savefilename = base + line + ".txt"

    print "Checking the data file " , savefilename
        
    result = open(savefilename, 'r')



    maxdateval =0
    mindateval = 20501231



    for graphdata in result:

        
        commapos = graphdata.find(",")
        secondcommapos = graphdata.find(",", commapos+1)
        thirdcommapos = graphdata.find(",", secondcommapos+1)
        fourthcommapos = graphdata.find(",", thirdcommapos+1)
        fifthcommapos = graphdata.find(",", fourthcommapos+1)

        yearfound = graphdata[fifthcommapos+2: fifthcommapos+6]
        monthfound = graphdata[fifthcommapos+7: fifthcommapos+9]
        dayfound = graphdata[fifthcommapos+10: fifthcommapos+12]

        #print "x" + yearfound + monthfound+ dayfound + "x"

        dateval = int(yearfound + monthfound+ dayfound)
        
        if dateval < mindateval:
            mindateval = dateval

        if dateval > maxdateval:
            maxdateval = dateval

        #print mindateval, maxdateval
            
        

        graphdata = graphdata[0:secondcommapos]
    
        #print "graphdata" , graphdata
        
    result.close()


    startyear = int(str(mindateval)[0:4])
    startmonth = int(str(mindateval)[4:6])
    startday = int(str(mindateval)[6:8])
    
    print "Earliest Record Found " + str(mindateval)[0:4] + "-" + str(mindateval)[4:6] + "-" + str(mindateval)[6:8]

    endyear = int(str(maxdateval)[0:4])
    endmonth = int(str(maxdateval)[4:6])
    endday = int(str(maxdateval)[6:8])
    
    print "Most Recent Record Found " + str(maxdateval)[0:4] + "-" + str(maxdateval)[4:6] + "-" + str(maxdateval)[6:8]


    tempdat = open(tempdate, 'w')

    for yearNum in range(startyear, endyear + 1):

        if yearNum == startyear:
            startmonthrange = startmonth
        else:
            startmonthrange = 1

        if yearNum == endyear:
            endmonthrange = endmonth + 1
        else:
            endmonthrange = 13
            

        for monthNum in range(startmonthrange,endmonthrange):


            if monthNum == 4 or monthNum == 6 or monthNum == 9 or monthNum == 11:
                days = 31
            else:
                days = 32


            if calendar.isleap(yearNum):
                #print "Leap Year"
                if monthNum == 2:
                    days = 30
            else:
                #print "Not Leap Year"
                if monthNum ==2:
                    days = 29

                
        

            if yearNum == startyear and monthNum == startmonth:
                startdayrange = startday
            else:
                startdayrange = 1

            
            if yearNum == endyear and monthNum == endmonth:
                enddayrange = endday + 1
            else:
                enddayrange = days


            for dayNum in range(startdayrange,enddayrange):

                    
                if date(yearNum, monthNum, dayNum).weekday() == 4:
                    
                    if len(str(monthNum)) == 1:
                        monthNumber  = "0" + str(monthNum)
                    else:
                        monthNumber = str(monthNum)


                    if len(str(dayNum)) == 1:
                        dayNumber  = "0" + str(dayNum)
                    else:
                        dayNumber = str(dayNum)

                    datetowrite = str(yearNum)+ "-" + monthNumber + "-" + dayNumber + "\n"

                    tempdat.write(datetowrite)

    tempdat.close()
            
    #print "Date file created"


    tempdat = open(tempdate, 'r')
    
    tempresult = base + "tempresult.txt"
    tempres = open(tempresult, 'w')
    recordnumb = 0
    

    for datetofind in tempdat:
        compdate = 0
        missingdata = 0
        result = open(savefilename, 'r') 
        lengthofdatetofind = len(datetofind)
        datetofind = str(datetofind[0:lengthofdatetofind-1])
        #print "datetofind", datetofind
        for datetocheck in result:
            if datetocheck.find(datetofind) != -1:
                #if compdate > 0:
                    #print " "
                    #print "REPEATED DATA  " , datetocheck
                if compdate == 0:
                    
                    recordnumb = recordnumb + 1
                    commapos = datetocheck.find(",")
                    datetocheck = datetocheck[commapos+1:]
                    #print "datetocheckmissing index" , datetocheck
                    datetocheck = str(recordnumb) + "," + datetocheck
                    #print "datetocheck", datetocheck
                    tempres.write(datetocheck)
                    compdate = compdate + 1
                    missingdata = missingdata + 1
                    #print "date match" ,
                    #print datetofind
                    #print datetocheck
        if missingdata ==0:
            print " "
            print "MISSING DATA FOR " + savefilename + " @ ", datetofind
            raw_input('Please press return to continue...\n')
        result.close()
    tempres.close()

    
    copyFile(srcf =str(tempresult),destf= str(savefilename))
    
    tempsharestogetlist = open(tempreorder, 'a')
    
    

    commapos = newsharestogetline.find(",")
    secondcommapos = newsharestogetline.find(",", commapos+1)

    #recordnumb = recordnumb - 1

    sharesindextowrite = newsharestogetline[0:secondcommapos+1] + str(recordnumb) + "\n"


    
    print ""
    #print sharesindextowrite
    #print ""

    tempsharestogetlist.write(sharesindextowrite)

    tempsharestogetlist.close()

    
    #raw_input('Please press return to continue...\n')



copyFile(srcf =str(tempreorder),destf= str(filename))

print "Process Complete "

### END OF LISTING ###
