#!/bin/bash

MODIFY_SYSTEM=$1

if [ "$MODIFY_SYSTEM" = "1" ]; then
  HAS_NETWORK=`/opt/pos/common/bin/has_network`

  # if we've network, net's try to find out if current time zone is the
  # same of the network one

  if [ "$HAS_NETWORK" = "YES" ]; then
    # configure timezone based on the country where the machine is installed
    CUR_TIMEZONE=`cat /etc/timezone`
    TIMEZONE=`wget -o /dev/null -qO- http://ip-api.com/csv | cut -d, -f10`

    if [ "$TIMEZONE" != "" ]; then
      if [ "$TIMEZONE" != "$CUR_TIMEZONE" ]; then
        echo $TIMEZONE > /etc/timezone
        dpkg-reconfigure --frontend noninteractive tzdata
      fi
    fi

    # set the clock from ntp
    ntpdate -t 10 pool.ntp.org
  fi
else
  # set the clock from ntp
  timeout 60s ntpdate -t 10 pool.ntp.org
fi

