#! /bin/bash
#
#  Autor: Hans Baier, idowapro
#  Historie:
#   2019-03-27 -- Init
#
#  Beschreibung:
#   Aufruf von 'webcam_update' -> 'php $PHP_SCRIPT ( akt/webcam_neukirchen/webcamSync.php )'
#   cronjob erlaubt nur einen Zyklus von 1h
#   aus diesem Grund diese while schleife, dadurch wird 4 mal eine Aktualisierung gemacht
#
#
#

SCRIPT_NAME=$(basename $0)
SCRIPT_DIR=$(dirname $0)
SCRIPT_DIR_ABS=$( cd $SCRIPT_DIR; pwd -L )

DAY=$(date +%d)

LOGFILE=${SCRIPT_DIR_ABS}/../log/${DAY}_${SCRIPT_NAME}.txt
PHP_SCRIPT=${SCRIPT_DIR_ABS}/../akt/webcam_neukirchen/webcamSync.php

function remove_old_logfiles () {
   LOGPATH=${SCRIPT_DIR_ABS}/../log
   echo "$(date) :: find $LOGPATH -ctime +30 -type f -ls -delete "
   find $LOGPATH -ctime +30 -type f -ls -delete 
}

function webcam_update () {
  echo "Start: ${SCRIPT_NAME}  $SCRIPT_DIR_ABS  $(date)"  >> $LOGFILE
  /bin/php ${PHP_SCRIPT}                                  >> $LOGFILE  2>&1
  echo "ENDE: ${SCRIPT_NAME}   $(date)"                   >> $LOGFILE
}

export -f  webcam_update  remove_old_logfiles

# ------------------- main --------------------
echo "START OF BASH SCRIPT :  ${SCRIPT_NAME}  $(date) "  >> $LOGFILE

echo "remove old logfiles "       >> $LOGFILE
remove_old_logfiles               >> $LOGFILE  2>&1

# 4 mal die while schleife = 40 Min aktualisierung
let i=1
while [ $i -lt 4 ]
do 
  echo "$(date) -- i($i)"         >> $LOGFILE
  webcam_update
  let i=i+1
  sleep 900
done

echo "END OF BASH SCRIPT :  ${SCRIPT_NAME}  $(date) "  >> $LOGFILE
