#!/bin/bash

if [ ! -e "/usr/bin/cutycapt" ]; then
  exit
fi

HAS_NETWORK=`/opt/pos/common/bin/has_network`

if [ "$HAS_NETWORK" != "YES" ]; then
  exit
fi

export DISPLAY=:0.0

W_FILE="/tmp/weather_tmp.png"
W_FILE_FINAL="/tmp/weather.png"

if [ -e "$W_FILE_FINAL" ]; then
  rm -f $W_FILE_FINAL
fi

TR=`cat /opt/pos/etc/config.ini | grep LANG_TRANSLATION | cut -f2 -d=`
SAFT_CITY=`cat /opt/pos/etc/config.ini | grep SAFT_City | cut -f2 -d=`
SAFT_CITY="Penafiel"

LINK="https://www.msn.com/pt-pt/meteorologia"

if [ "$TR" = "ES" ] || [ "$TR" = "ESC" ] || [ "$TR" = "ES2" ]; then
  LINK="https://www.msn.com/es-es/eltiempo"
elif [ "$TR" = "EN" ]; then
  LINK="https://www.msn.com/en-gb/weather"
fi

LINK="https://www.msn.com/pt-pt/meteorologia/hoje/$SAFT_CITY,Portugal/we-city"

timeout 15s cutycapt --url=$LINK --out=$W_FILE

if [ $? -ne 0 ]; then
  exit
fi

if [ ! -e "$W_FILE" ]; then
  exit
fi

CUR_DATE=`date +"  %Y-%m-%d %H:%M:%S"`

convert $W_FILE -crop 800x350+0+175 $W_FILE_FINAL
convert $W_FILE_FINAL -border 1 -bordercolor white -resize 650x285 -weight 100 -pointsize 11 -draw "fill yellow text 540,155 '$CUR_DATE' " /tmp/$$.png
mv /tmp/$$.png $W_FILE_FINAL
#cp $W_FILE_FINAL /media/sf_MiguelManso/Downloads

if [ -e "$W_FILE" ]; then
  rm -f $W_FILE
fi

