#!/bin/bash

export DISPLAY=:0.0

SERVER_IP=`cat /opt/pos/etc/config.ini | grep "NETWORK_SERVER_IP" | cut -f2 -d=`

if [ -e "/tmp/.did_fast_mount.txt" ]; then
  rm -f /tmp/.did_fast_mount.txt
fi

if [ ! -d "/shares/pos" ]; then
  mkdir -p /shares/pos
fi

COUNT=0

while [ true ]; do
  ping -c1 $SERVER_IP > /dev/null

  if [ $? -eq 0 ]; then
    break
  fi

  COUNT=`expr $COUNT + 1`

  if [ $COUNT -gt 5 ]; then
    break
  fi

  sleep 1
done

timeout 5s /sbin/mount.cifs //$SERVER_IP/pos /shares/pos -o guest,rw,noperm,noserverino,nobrl

if [ $? -eq 0 ]; then
  echo "Mount ok..."

  if [ -e "/tmp/server_error.txt" ]; then
    rm -rf /tmp/server_error.txt
  fi

  echo 1 > /tmp/.did_fast_mount.txt
  /opt/pos/common/bin/restart_hardware_handler.sh

else
  echo "Mount error..."
  touch /tmp/server_error.txt
fi

