#!/bin/bash

if [ -e "/.resized_partition" ]; then
  RESIZED_PARTITION=`cat /.resized_partition`
  rm -rf /.resized_partition

  if [ "$RESIZED_PARTITION" != "" ]; then
    resize2fs $RESIZED_PARTITION
    reboot
    exit
  fi
fi

if [ -e "/opt/bin/xrandr.sh" ]; then
  /opt/bin/xrandr.sh
fi

# cpu optimized for performance
echo "performance" > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

POSTGRES_RUNNUNG=`ps xa | grep postgresql | grep postgresql.conf | grep -v grep`

if [ "$POSTGRES_RUNNUNG" = "" ]; then
  /etc/init.d/postgresql restart
fi

CONFIG="/opt/pos/etc/config.ini"
MYLANG=`cat $CONFIG | grep LANG= | cut -f2 -d=`
HAS_X64=`cat $CONFIG | grep X64_ACTIVE | grep -v "#X64" | grep -v grep | grep 1`
HAS_MUSIC=`cat $CONFIG | grep MUSIC_PLAY | grep -v grep | grep 1`
STATION_NUMBER=`cat $CONFIG | grep STATION_NUMBER | cut -f2 -d=`
PRODUCTS_SUGESTION=`cat $CONFIG | grep PRODUCTS_SUGESTION | grep -v AUTO | cut -f2 -d=`
USE_NETBO=`cat $CONFIG | grep USE_NETBO | grep -v AUTO | cut -f2 -d=`
PRINTERS_INIT_ON_BOOT=`cat $CONFIG | grep PRINTERS_INIT_ON_BOOT | cut -f2 -d=`
EXTERNAL_INVOICE_TOPRINTER=`cat $CONFIG | grep EXTERNAL_INVOICE_TOPRINTER | grep -v grep | grep 1`
#HAS_MOBILE_DEVICES=`cat $CONFIG | grep MOBILE_DEVICES | grep -v grep | grep 1`
TEAMVIEWER_ALWAYS_RUNNING=`cat $CONFIG | grep TEAMVIEWER_ALWAYS_RUNNING | cut -f2 -d=`
ANYDESK_ALWAYS_RUNNING=`cat $CONFIG | grep ANYDESK_ALWAYS_RUNNING | cut -f2 -d=`
BACKGROUND_WEB_BROWSER=`cat $CONFIG | grep BACKGROUND_WEB_BROWSER | cut -f2 -d=`
INTERNET_NTP_DATE_UPDATE=`cat $CONFIG | grep INTERNET_NTP_DATE_UPDATE | cut -f2 -d=`

xhost +

# hide mouse cursor
# unclutter -idle 2 &

IS_XFCE4=`ps xa | grep xfce4-session | grep -v grep`
IS_LUBUNTU=`ps xa | grep lubuntu-rc | grep -v grep`

# define background image
if [ "$IS_XFCE4" != "" ]; then
  rm -rf /usr/share/backgrounds/xfce/* 2> /dev/null

  if [ -e "/opt/pos/common/skins/$MYLANG/custom/login.png" ]; then
    cp /opt/pos/common/skins/$MYLANG/custom/login.png /usr/share/backgrounds/xfce
  else
    cp /opt/pos/common/skins/$MYLANG/login.png /usr/share/backgrounds/xfce
  fi
elif [ "$IS_LUBUNTU" ]; then
  if [ -e "/opt/pos/common/skins/$MYLANG/custom/login.png" ]; then
    cp /opt/pos/common/skins/$MYLANG/custom/login.png /usr/share/lubuntu/wallpapers
  else
    cp /opt/pos/common/skins/$MYLANG/login.png /usr/share/lubuntu/wallpapers
  fi

  # lubuntu related
  if [ -e "/home/pos/.config/openbox/lubuntu-rc.xml" ]; then
    AUTOHIDE=`cat /home/pos/.config/lxpanel/Lubuntu/panels/panel | grep autohide=1`

    if [ "$AUTOHIDE" = "" ]; then
      perl -pi -e "s/autohide=\d/autohide=1/" /home/pos/.config/lxpanel/Lubuntu/panels/panel
      lxpanelctl restart
    fi
  fi

else
  sleep 2

  # define background image
  if [ -e "/opt/pos/common/skins/$MYLANG/login.png" ]; then
    if [ -e "/usr/bin/feh" ]; then
      feh --bg-scale /opt/pos/common/skins/$MYLANG/login.png
    else
      fbsetbg /opt/pos/common/skins/$MYLANG/login.png
    fi
  fi
fi

if [ -e "/home/pos/.fluxbox/init.17.hidden" ]; then
  TASKBAR_VISIBLE=`cat /home/pos/.fluxbox/init | grep session.screen0.toolbar.visible | grep true`

  # taskbar is visible, hide it
  if [ "$TASKBAR_VISIBLE" != "" ]; then
    cp /home/pos/.fluxbox/init.17.hidden /home/pos/.fluxbox/init
    ps xa | grep fluxbox | grep -v grep | awk '{ print $1 }' | xargs kill -SIGUSR2 2> /dev/null
  fi

fi

# egalax is installed
if [ -e "/etc/init.d/eGTouch.sh" ]; then
  EGALAX_RUNNING=`ps xa | grep eGTouchD | grep -v grep`

  if [ "$EGALAX_RUNNING" = "" ]; then
    /etc/init.d/eGTouch.sh &
  fi
fi

if [ -e "/home/pos/.fluxbox/lastwallpaper" ]; then
  rm -rf /home/pos/.fluxbox/lastwallpaper
fi

# logs cleanup
> /root/.xsession-errors
> /var/log/syslog ; chown syslog:adm /var/log/syslog
> /var/log/kern.log ; chown syslog:adm /var/log/kern.log
> /var/log/auth.log ; chown syslog:adm /var/log/auth.log
> /var/log/lastlog ; chown root:utmp /var/log/lastlog
#> /var/log/Xorg.0.log
find /var/log | grep \.*.gz | xargs rm 2> /dev/null
find /var/log/ | egrep '\.log\.' | xargs rm 2> /dev/null

# psql safeness
rm -rf /tmp/pg_stat_tmp ; mkdir /tmp/pg_stat_tmp ; chmod 777 /tmp/pg_stat_tmp 2> /dev/null
rm -rf /var/log/postgresql/*

# port mappings
/opt/pos/common/bin/com_port_mappings.sh

# apply global tweaks, if needed
if [ -e "/opt/bin/startup.sh" ]; then
  /opt/bin/startup.sh &
fi

# apply customer display tweaks, if needed
if [ -e "/opt/bin/customer_display.sh" ]; then
  /opt/bin/customer_display.sh &
fi

# apply calibration, if needed
if [ -e "/opt/bin/calibrate.sh" ]; then
  /opt/bin/calibrate.sh &
fi

# cpu monitor
ps xa | grep cpu_monitor | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
/opt/pos/common/bin/cpu_monitor.sh &

ps xa | grep errors_check | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
/opt/pos/common/bin/errors_check.sh &

# custom scales
/opt/pos/server/apps/custom_scale.sh &

# init printers on boot
if [ "$PRINTERS_INIT_ON_BOOT" = "1" ]; then
  /opt/pos/common/bin/printers_init &
fi

# start music play
if [ "$HAS_MUSIC" != "" ]; then
  /opt/pos/common/bin/music.sh &
fi

# external printing with cups
if [ "$EXTERNAL_INVOICE_TOPRINTER" != "" ]; then
  service cups start
#  PRINTER=`lpstat -p | cut -f2 -d" "`
#
#  if [ "$PRINTER" != "" ]; then
#    lpadmin -p $PRINTER -E
#  fi
else
  service cups stop
fi

# mobile devices
#if [ "$HAS_MOBILE_DEVICES" != "" ]; then
#  /opt/pos/common/bin/mobile_devices.sh &
#else
#  service nginx stop &
#fi

# background web browser
if [ "$BACKGROUND_WEB_BROWSER" != "" ]; then
  CHROME_RUNNING=`ps xa | grep chromium-browser | grep -v grep`

  if [ "$CHROME_RUNNING" = "" ]; then
    sudo -H -u pos bash -c 'export DISPLAY=:0.0 ; /usr/bin/chromium-browser --user-data-dir=/home/pos/.chrome' &
  fi
fi

# start a vnc server
ps xa | grep vnc_server | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
/opt/pos/common/bin/vnc_server.sh &

# remote maintenance
if [ "$TEAMVIEWER_ALWAYS_RUNNING" = "1" ]; then
  /opt/pos/common/bin/teamviewer.sh 1 &
else
  if [ -e "/etc/systemd/system/multi-user.target.wants/teamviewerd.service" ]; then
    systemctl disable teamviewerd
    systemctl stop teamviewerd
  fi
fi

if [ "$ANYDESK_ALWAYS_RUNNING" = "1" ]; then
  /opt/pos/common/bin/any_desk.sh 1 &
  sleep 5
fi

# sign server
IS_64_BITS=`uname -a | grep x86_64`

if [ "$IS_64_BITS" != "" ]; then
  /opt/pos/common/skins/$MYLANG/sign_server.src &
else
  /opt/pos/common/skins/$MYLANG/sign_server &
fi

# usblp printers init
/opt/pos/common/bin/usb_lp_printers &

HAS_NTPDATE_CRON=`crontab -l | grep ntp_set_date`

if [ -e "/etc/cron.d/ntpdate" ]; then
  rm -f /etc/cron.d/ntpdate
fi

# ntp date update every 3 hours
if [ "$INTERNET_NTP_DATE_UPDATE" = "1" ]; then
  if [ "$HAS_NTPDATE_CRON" = "" ]; then
    (crontab -l ; echo "0 */3 * * * /opt/pos/common/bin/ntp_set_date.sh") | crontab
  fi

  /opt/pos/common/bin/ntp_set_date.sh &
else
  if [ "$HAS_NTPDATE_CRON" != "" ]; then
    (crontab -l | grep -v ntp_set_date) | crontab
  fi
fi

#
# server machine
#

if [ "$STATION_NUMBER" = "0" ]; then
  # cleanup registered stations and printers
  rm -rf /opt/pos/server/data/registered_stations/*
  rm -rf /opt/pos/server/data/registered_printers/*

  # 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

  # cleanup done files
  find /opt/pos/server/data/areas/ | grep "\.done" | xargs rm 2> /dev/null

  ONLINE_ORDERS_TOKEN=`cat $CONFIG | grep "ONLINE_ORDERS_TOKEN=" | cut -f2 -d=`
  ONLINE_ORDERS_API_PASSWD=`cat $CONFIG | grep "ONLINE_ORDERS_API_PASSWD=" | cut -f2 -d=`

  if [ "$ONLINE_ORDERS_TOKEN" != "" ] && [ "$ONLINE_ORDERS_API_PASSWD" != "" ]; then
    /opt/pos/server/apps/sync_online_orders.sh &
  fi

  INTERNET_SYNC_TOKEN=`cat $CONFIG | grep "INTERNET_SYNC_TOKEN=" | cut -f2 -d=`

  if [ "$INTERNET_SYNC_TOKEN" != "" ]; then
    /opt/pos/server/apps/db_sync.sh &
  fi

  RFID_CLOUD_SYNC=`cat $CONFIG | grep "RFID_CLOUD_SYNC=" | cut -f2 -d=`
  INTERNET_SMS_SYNC_TOKEN=`cat $CONFIG | grep "INTERNET_SMS_SYNC_TOKEN=" | cut -f2 -d=`
  POINTS_SYNC_TOKEN=`cat $CONFIG | grep "POINTS_SYNC_TOKEN=" | cut -f2 -d=`
  VOUCHERS_SYNC_TOKEN=`cat $CONFIG | grep "VOUCHERS_SYNC_TOKEN=" | cut -f2 -d=`
  INTERNET_DASHBOARD=`cat $CONFIG | grep "INTERNET_DASHBOARD=" | cut -f2 -d=`
  QUIZ_QUESTIONS=`cat $CONFIG | grep "QUIZ_QUESTIONS=" | cut -f2 -d=`

  INTERNET_DYN_DNS=`cat $CONFIG | grep INTERNET_DYN_DNS | cut -f2 -d=`

  # start online api feeder
  if [ "$POINTS_SYNC_TOKEN" != "" ]       || \
     [ "$VOUCHERS_SYNC_TOKEN" != "" ]     || \
     [ "$INTERNET_SMS_SYNC_TOKEN" != "" ] || \
     [ "$QUIZ_QUESTIONS" != "" ]          || \
     [ "$RFID_CLOUD_SYNC" != "" ]
  then
    /opt/pos/server/apps/run_online_api_feeder.sh &
  fi

  # wont use /etc cron anymore
  if [ -e "/etc/cron.d/ptkeys" ]; then
    rm -f /etc/cron.d/ptkeys
  fi

  if [ -e "/etc/cron.d/dyndns" ]; then
    rm -f /etc/cron.d/dyndns
  fi

  HAS_DYNDNS_CRON=`crontab -l | grep dyndns`
  HAS_DASHBOARD_CRON=`crontab -l | grep ptkeys`

  # dyndns
  if [ "$INTERNET_DYN_DNS" = "1" ]; then
    if [ "$HAS_DYNDNS_CRON" = "" ]; then
      (crontab -l ; echo "*/15 * * * * /opt/pos/server/apps/dyndns.pl") | crontab
    fi

    /opt/pos/server/apps/dyndns.pl &
  else
    if [ "$HAS_DYNDNS_CRON" != "" ]; then
      (crontab -l | grep -v dyndns) | crontab
    fi
  fi

  # internet dashboard
  if [ "$INTERNET_DASHBOARD" = "1" ]; then
    if [ "$HAS_DASHBOARD_CRON" = "" ]; then
      (crontab -l ; echo "*/10 * * * * /opt/pos/server/apps/ptkeys.pl") | crontab
    fi

    # make it run in daemon mode for online updating
    /opt/pos/server/apps/ptkeys.pl 1 &
  else
    if [ "$HAS_DASHBOARD_CRON" != "" ]; then
      (crontab -l | grep -v ptkeys) | crontab
    fi
  fi

  # netbo
  if [ "$USE_NETBO" = "1" ]; then
    IS_RUN=`ps xa | grep tray_application | grep -v grep`

    if [ "$IS_RUN" = "" ]; then
      /root/weckOFFICE/bin/tray_application
    fi

    /opt/pos/common/bin/previous_date_export &
  fi

  # feed suggestions
  /opt/pos/server/apps/run_suggestions.sh &

  # keep counters only for some days
  /opt/pos/server/apps/counters_cleanup.pl &

#
# station
#

else
  touch /tmp/server_error.txt
  /opt/pos/common/bin/fast_mount.sh
fi

# cpu monitor
ps xa | grep cpu_monitor | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
/opt/pos/common/bin/cpu_monitor.sh &

/opt/pos/common/bin/run_software.sh &


