#!/bin/bash

# returns a file's age in seconds
function file_age() {
  local filename=$1
  local changed=`stat -c %Y "$filename"`
  local now=`date +%s`
  local elapsed

  let elapsed=now-changed
  echo $elapsed
}

#
# call stuff using the two minutes timer from UI
#

STATION_NUMBER=$1

CONFIG="/opt/pos/etc/config.ini"
STATION=`cat $CONFIG | grep "STATION_NUMBER=" | cut -f2 -d=`
SUGESTIONS=`cat $CONFIG | grep "PRODUCTS_SUGESTION=" | cut -f2 -d=`
INTERNET_SEND_DAILY_Z=`cat $CONFIG | grep "INTERNET_SEND_DAILY_Z=" | cut -f2 -d=`
INTERNET_SEND_MONTHLY_Z=`cat $CONFIG | grep "INTERNET_SEND_MONTHLY_Z=" | cut -f2 -d=`
INTERNET_SEND_MONTHLY_SAFT=`cat $CONFIG | grep "INTERNET_SEND_MONTHLY_SAFT=" | cut -f2 -d=`
INTERNET_SEND_DAILY_SAFT=`cat $CONFIG | grep "INTERNET_SEND_DAILY_SAFT=" | cut -f2 -d=`
SAFT_GENERATE_LOCALY=`cat $CONFIG | grep "SAFT_GENERATE_LOCALY=" | cut -f2 -d=`
SAFT_MONTHLY_GENERATE_LOCALY=`cat $CONFIG | grep "SAFT_MONTHLY_GENERATE_LOCALY=" | cut -f2 -d=`
USE_NETBO=`cat $CONFIG | grep "USE_NETBO=" | cut -f2 -d=`
POINTS_SYNC_TOKEN=`cat $CONFIG | grep "POINTS_SYNC_TOKEN=" | cut -f2 -d=`
BUSINESS_DATE=""

if [ -e "/opt/pos/.business_date" ]; then
  BUSINESS_DATE=`cat /opt/pos/.business_date`
else
  BUSINESS_DATE=`date +%Y-%m-%d`
fi

# prevent hdd from filling
if [ -e "/home/pos/.xsession-errors" ]; then
  >/home/pos/.xsession-errors
fi

if [ -e "/root/.xsession-errors" ]; then
  >/root/.xsession-errors
fi

if [ "$STATION" = "0" ]; then

  # kills idle and slow queries
  /opt/pos/server/apps/kill_slow_queries

  CUR_DATE=`date +%Y%m%d`
  CUR_YEAR=`date +%Y`
  CUR_DAY=`date +%d`
  CUR_MONTH=`date +%m`
  CUR_HOUR=`date +%H`

  # keep nomachine sessions cleaned
  if [ -d "/var/lib/nxserver/db/running" ]; then
    find /var/lib/nxserver/db/running -type f -daystart -mtime +1 | xargs rm -f
  fi

  # cleanup orders archive
  if [ -d "/opt/pos/server/data/order_archive" ]; then
    find "/opt/pos/server/data/order_archive" -mmin +59 -type f -name "*.txt" -exec rm -f {} \;
  fi

  # cleanup docs print archive
  if [ -d "/opt/pos/server/data/print_checks_archive" ]; then
    find "/opt/pos/server/data/print_checks_archive" -mmin +59 -type f -name "*.txt" -exec rm -f {} \;
  fi

  # netbo
  if [ "$USE_NETBO" = "1" ]; then
    TMP_DATE=`date +%Y%m%d_%H%M%S`
    mkdir -p /opt/pos/common/data/netbo 2> /dev/null
    echo 1 > /opt/pos/common/data/netbo/run.$TMP_DATE.txt
    find /opt/pos/common/data/netbo -type f -mmin +60 -delete
  fi

  # handles emails
  if [ "$INTERNET_SEND_DAILY_Z" != "" ] || [ "$INTERNET_SEND_MONTHLY_Z" != "" ]; then
    if [ "$CUR_HOUR" = "07" ] || \
       [ "$CUR_HOUR" = "10" ] || \
       [ "$CUR_HOUR" = "16" ] || \
       [ "$CUR_HOUR" = "19" ] || \
       [ "$CUR_HOUR" = "23" ]
    then
      /opt/pos/common/bin/emails_send cron
    fi
  fi

  # handles monthly SAF-T sending
  if [ "$INTERNET_SEND_MONTHLY_SAFT" != "" ] || [ "$SAFT_MONTHLY_GENERATE_LOCALY" != "" ]; then
    if [ "$CUR_DAY" != "1" ]; then
      if [ "$CUR_HOUR" = "07" ] || \
         [ "$CUR_HOUR" = "10" ] || \
         [ "$CUR_HOUR" = "16" ] || \
         [ "$CUR_HOUR" = "19" ] || \
         [ "$CUR_HOUR" = "23" ]
      then
        /opt/pos/common/bin/saft_monthly "" "" cron
      fi
    fi
  fi

  # handles daily SAF-T sending
  if [ "$INTERNET_SEND_DAILY_SAFT" != "" ] || [ "$SAFT_GENERATE_LOCALY" != "" ]; then
    if [ "$CUR_HOUR" = "07" ] || \
       [ "$CUR_HOUR" = "10" ] || \
       [ "$CUR_HOUR" = "16" ] || \
       [ "$CUR_HOUR" = "19" ] || \
       [ "$CUR_HOUR" = "23" ]
    then
      /opt/pos/common/bin/saft_daily cron
    fi
  fi
fi

if [ "$SUGESTIONS" = "1" ]; then
  /opt/pos/server/apps/suggestions_generate.pl
fi

# ping server
if [ ! -e "/tmp/.ping_server_$BUSINESS_DATE" ]; then
  HAS_NETWORK=`timeout 5s /opt/pos/common/bin/has_network`

  if [ "$HAS_NETWORK" = "YES" ]; then
    RES=`/opt/pos/common/bin/ping_server.sh`

    if [ "$RES" = "OK" ]; then
      echo 1 > "/tmp/.ping_server_$BUSINESS_DATE"
    fi
  fi
fi

# check system log for hardrive errors
IO_ERROR=`dmesg | grep "I\/O error" | grep sda`

if [ "$IO_ERROR" != "" ]; then
  CUR_DATE=`date +%Y%m%d%H%M%S`
  echo "ERRO DE DISCO<BR><BR>POR FAVOR, CONTACTE UM TÉCNICO" > "/opt/pos/common/tmp/msg_to_ui#${STATION_NUMBER}#0#${CUR_DATE}.txt"
  echo "$CUR_DATE> $IO_ERROR" >> /root/hdd_errors.txt
fi

# get weather for screensaver
#/opt/pos/common/bin/get_weather.sh &



