#!/usr/bin/env python3
'''
HOW THIS WORKS:
    - Install and set up ticker, the linux cli tool to track stock: https://github.com/achannarasappa/ticker
    - ticker config needs to contain lots, you can just fake the quantity and price...
    - create a cron job that runs this command:
ticker --config=./.ticker.yaml print --format=csv | awk -vFPAT='([^,]*)|("[^"]+")' -vOFS=, '{print $2 " " $3}' | tail -n +2 > /tmp/stock_ticker
    - the frequency of the cron schedule is how up to date your stock prices will be. I run it once a minute.
    - make sure that file exists, then run this script..
'''
import os

STOCK_TICKER_POSITION_PATH = '/tmp/stock_ticker_position'
STOCK_TICKER_PATH = '/tmp/stock_ticker'
STOCK_TICKER_POSITION = 0
STOCK_TICKER_RESET = False
STOCK_TICKER_LINE_COUNT = 0

if not os.path.exists(STOCK_TICKER_POSITION_PATH):
    with open(STOCK_TICKER_POSITION_PATH, 'w+') as f:
        STOCK_TICKER_RESET = True
        STOCK_TICKER_POSITION = 0
        f.write('0')

if not STOCK_TICKER_RESET:
    with open(STOCK_TICKER_POSITION_PATH, 'r') as f:
        STOCK_TICKER_POSITION = int(f.readline())

with open(STOCK_TICKER_PATH, 'r') as f:
    data = f.readlines()
    STOCK_TICKER_LINE_COUNT = len(data) - 2

    print(data[STOCK_TICKER_POSITION])

with open(STOCK_TICKER_POSITION_PATH, 'w+') as f:
    if STOCK_TICKER_POSITION != None:
        if STOCK_TICKER_POSITION + 1 <= STOCK_TICKER_LINE_COUNT:
            f.write(str(STOCK_TICKER_POSITION + 1))
        else:
            f.write(str(0))
    else:
        f.write(str(0))
