#! /usr/bin/python2.7

# RKN 23/01/2016  Rev 3.0 fixed decimal place issue.
# RKN 08/01/2016  Rev 2.0
# RKN 03/12/2015  Rev 1.0
# Adjusts share price between stop & start date
# Fixed adjustment

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


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


newfactor = raw_input('Enter the adjustment factor..e.g 0.1 for divide by 10....\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 "Old line ", str(line)
    lengthofline = len(line)
    
    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]

    dateval = int(yearfound + monthfound+ dayfound)

    
    if dateval < datetochangeprice:
        shareindextowrite = line
        tempsharestogetlist.write(shareindextowrite)
        
    if dateval >= datetochangeprice and dateval <= datetochangepricestop:
        decimal = line[commapos+2:secondcommapos]
        #print "Old value  " +  decimal
        lengthofdecimal = len(decimal)
        firstdecimal = decimal.find(".")
        #print "x"+ decimal[0:firstdecimal]+ "x"
        fronthalf = int(decimal[0:firstdecimal])
        lengthofbackhalf = len(decimal[firstdecimal+1:lengthofdecimal])
        backhalf = int(decimal[firstdecimal+1:lengthofdecimal])
        #print "front half string =", fronthalf
        #print "backhalf half string =", backhalf
        
        
        backhalf = float(backhalf)/(10**float(lengthofbackhalf))
        #print backhalf
        
        newsharevalue = (float(fronthalf) + backhalf)* float(newfactor)
        
        newsharevaluestring = str(newsharevalue)                 
        
        shareindextowrite = line[0:commapos+2] + newsharevaluestring + line[secondcommapos:lengthofline]
        print "New line ", shareindextowrite
        tempsharestogetlist.write(shareindextowrite)

        print " "
        print " "
        
    if dateval > datetochangepricestop:
        shareindextowrite = line
        tempsharestogetlist.write(shareindextowrite)


 
tempsharestogetlist.close()

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

print "Process Complete "

### END Of LISTING  ###
