#!/bin/bash
# AQ
# 20210312

logger -t cmcsys "Start" 

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
LOG_DIR="/home/terminal/atm/log/add" #define path for logs location
PLATFORM_DIR="$BASE_DIR/Platform" #export needed qt and qml paths

cd $BASE_DIR #set current working directory
chmod -R 777 $BASE_DIR #give rights recursively inside current working directory

echo 'groups:' $(groups) >> $LOG_DIR/cofffee.log
echo "`date +%H:%M:%S`  INFO starting cmcsys from start.sh" >> $LOG_DIR/cofffee.log
echo "`date +%H:%M:%S`  INFO base path is" $BASE_DIR >> $LOG_DIR/cofffee.log
echo "`date +%H:%M:%S`  INFO log path is" $LOG_DIR >> $LOG_DIR/cofffee.log
echo "`date +%H:%M:%S`  INFO qt path is" $PLATFORM_DIR >> $LOG_DIR/cofffee.log

export QTDIR=$PLATFORM_DIR
export LD_LIBRARY_PATH=$PLATFORM_DIR/lib/:$PLATFORM_DIR:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=$PLATFORM_DIR/plugins/:QT_PLUGIN_PATH
export QT_QPA_PLATFORM_PLUGIN_PATH=$PLATFORM_DIR/plugins/platforms:QT_QPA_PLATFORM_PLUGIN_PATH

#start application
while true; do

  if [ -f $BASE_DIR/cmconfig.cfg ] || [ -f $BASE_DIR/recipes.cfg ];
    then
      echo "`date +%H:%M:%S`  INFO starting cmcsys exe" >> $LOG_DIR/cofffee.log
      $BASE_DIR/CMCSys -c:$BASE_DIR -d:$LOG_DIR "$@"
    else
      zenity --error --text="cmconfig.cfg or recipes.cfg is missing from Data. CMCSys won't start" --title="Warning"
      echo "`date +%H:%M:%S`  ERROR cmconfig.cfg or recipes.cfg is missing from Data. CMCSys won't start" >> $LOG_DIR/cofffee.log
  fi

done
