#! /usr/bin/python2.7

# RKN 08/01/2016  Rev 2.0
# RKN 06/11/2015  Rev 1.0
# Sets the Watch/Bought Sold price from date entered

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/"


sharetosetprice = raw_input('Enter the share name to be amended eg, CAL.L ...\n')

datetochangeprice = raw_input('Enter the start date e.g 2015-06-30 ...\n')
datetochangeprice = int(datetochangeprice[0:4]+datetochangeprice[5:7]+datetochangeprice[8:10])
#print datetochangeprice

newprice = raw_input('Enter the new Watch/Bought/Sold price..e.g 102.56....\n')


filename = base + sharetosetprice + ".txt"
tempreorder = base + "tempreorder.txt"

savedshareslist = open(filename, 'r')

tempsharestogetlist = open(tempreorder, 'w')
tempsharestogetlist.close()

tempsharestogetlist = open(tempreorder, 'a')
        

for line in savedshareslist:

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

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

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

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

    dateval = int(yearfound + monthfound+ dayfound)

    
    if dateval < datetochangeprice:
        shareindextowrite = line
        tempsharestogetlist.write(shareindextowrite)
        
    if dateval > datetochangeprice:
        shareindextowrite = line[0:secondcommapos+1] + " " + newprice + line[thirdcommapos:lengthofline]
        tempsharestogetlist.write(shareindextowrite)
 
tempsharestogetlist.close()

savedshareslist.close()
    
copyFile(srcf =str(tempreorder),destf= str(filename))

print "Process Complete "

### END OF LISTING ###
