#!/usr/bin/env python3 
#import random

from commands import folder
from ezemail import sendEmail
import codecs

def getIndex():
	fil = open(folder()+'cats/index','r')
	out = int(fil.read())
	fil.close()
	return  out

def setIndex(imput):
	fil = open(folder()+'cats/index','w')
	fil.write(imput)
	fil.close()

def getFact():
	index = getIndex()
	f = codecs.open(folder()+'cats/catfacts','r','utf-8')
	out = f.read().splitlines()[index]
	f.close
	setIndex(str(index+1))
	return out

def getClients():
	fil = open(folder()+'cats/clients')
	out = fil.read().splitlines()
	fil.close()
	return out

clients = getClients()
fact = getFact()
print(clients)
print(fact)
[sendEmail(client,'Cat facts',fact) for client in clients]
