#!/bin/sh
# Instead of running the application directly, we run it from this script.
# Running from this script preserves the ulimit settings that were set
# in /etc/init.d/b2qt-settings-sh which allows a core dump to be generated if a crash occurs.
# b2qt-settings.sh will exit after being run which takes the ulimit settings with it
# When we run the app from this script, it does not exit which preserves the ulimit settings
# Documentation of ulimit says: "The ulimit utility shall set or  report  the  file-size
# writing limit imposed on files written by the shell and its child processes"


#######################################################################
#  Set the Qt Library path, this is thought to be needed if we attempt to
#  run a Qt5.05 application on the 5.12 target.  The library path will
#  point to the correct location of Qt libraries
#######################################################################
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/qt5_12_3/lib

psplash --no-console-switch &
until [ -p /var/tmp/psplash_fifo ]; do msleep 1; done
psplash-write "PROGRESS 99"

grep '^#cron' /etc/syslog.conf &> /dev/null
if [ $? -eq 0 ]; then
  sed -i 's/#\(.*cron.*\)/\1/' /etc/syslog.conf
  /etc/init.d/syslog restart
fi

if [ ! -f /etc/cron.hourly/bunn ]; then
  chown root /etc/crontab
  sed -i 's/#\(.*hourly.*\)/\1/' /etc/crontab
  ln -s /opt/Bunn/application/linuxSystemFiles/cron.hourly.sh /etc/cron.hourly/bunn
  /etc/init.d/crond restart
fi

if [ ! -f /etc/cron.daily/bunn ]; then
  chown root /etc/crontab
  sed -i 's/#\(.*daily.*\)/\1/' /etc/crontab
  ln -s /opt/Bunn/application/linuxSystemFiles/cron.daily.sh /etc/cron.daily/bunn
  /etc/init.d/crond restart
fi

# unexpected signals (ie. SIGSEGV) to the application can cause broken pipes, terminating logger instances with an unhandled SIGPIPE
# logger instances are launched below in forever loops, this prevents the application relaunch from hanging
#
# mkfifo(3)
# Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.

if [ ! -p /var/run/bunn-stdout ]; then
  mkfifo /var/run/bunn-stdout
  /bin/sh -c "while true; do logger --tag bunn-stdout < /var/run/bunn-stdout; done" &
fi

if [ ! -p /var/run/bunn-stderr ]; then
  mkfifo /var/run/bunn-stderr
  /bin/sh -c "while true; do logger --tag bunn-stderr < /var/run/bunn-stderr; done" &
fi

if [ ! -f /etc/init.d/bunn ]; then
  ln -s /opt/Bunn/application/bin/initd.service /etc/init.d/bunn
fi

if [ ! -f /etc/init.d/bunn-main ]; then
  ln -s /opt/Bunn/application/bin/main-initd.service /etc/init.d/bunn-main
fi


# prevent pr_info "alloc_contig_range: [%lx, %lx) PFNs busy" spew on the console
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/page_alloc.c
# KERN_INFO = 6
echo 6 > /proc/sys/kernel/printk

ulimit -c unlimited
cd /opt/Bunn/application/bin
while true; do
  ./FastCup $1 1>/var/run/bunn-stdout 2>/var/run/bunn-stderr
  code=$?
  if [ $code -ne 143 ] && [ $code -ne 137 ]; then # SIGTERM && SIGKILL
    logger --tag bunn-unexpected-exit $code
    if [ $code -eq 139 ] || [ $code -eq 134 ]; then # SIGSEGV || SIGABRT
      killall -9 WatchDawg.sh
      /opt/Bunn/application/bin/progress-bar-loop.sh &
      pid=$!
      gdb -se FastCup -c core -batch -ex "bt" | logger --tag bunn-backtrace
      kill $pid
    fi
  fi

  pgrep WatchDawg.sh
  if [ $? -eq 0 ]; then
    exit 0 # let WatchDawg restart application
  fi
done
