#!/usr/bin/python

import os
import time
import datetime
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw


f = open("/home/pi/events/meteo.txt","r")


Testo1 = f.read()
today = str(datetime.date.today().strftime("%d/%m"))
tomorrow = str((datetime.date.today() + datetime.timedelta(days=1)).strftime("%d/%m"))

print Testo1 
text = (("                     ",(255,0,0)),(Testo1, (255, 214, 0)))

font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 16)
all_text = ""
for text_color_pair in text:
    t = text_color_pair[0]
    all_text = all_text + t

#print(all_text)
width, ignore = font.getsize(all_text)
tempo = ((width) + 30)/32 * 2
print(width)


im = Image.new("RGB", (width + 30, 16), "black")
draw = ImageDraw.Draw(im)

x = 0;
for text_color_pair in text:
    t = text_color_pair[0]
    c = text_color_pair[1]
    print("t=" + t + " " + str(c) + " " + str(x))
    draw.text((x, 0), t, c, font=font)
    x = x + font.getsize(t)[0]


im.save("/home/pi/ppmfile/meteo.ppm")

os.system("while pgrep -u root led-matrix > /dev/null; do sleep 1; done")
os.system("mpg123 -m --gain 17 /home/pi/suoni/meteo.mp3 &")
os.system("sudo /home/pi/stilltext/rpi-rgb-led-matrix/led-matrix -r 16 -c 4 -t 5 -D 1 /home/pi/ppmfile/meteogadg.ppm ; sudo /home/pi/scrollingtext/rpi-rgb-led-matrix/led-matrix -t " + str(tempo) + " -D 1 /home/pi/ppmfile/meteo.ppm")
