#!/bin/bash

HAS_NET=`/opt/pos/common/bin/has_network`

if [ "$HAS_NET" = "NO" ]; then
  #echo "No network... aborting..."
  exit
fi

POS_HARDLOCK=""
MAC_ADDR=`/opt/pos/common/bin/machine_id 1`
VPN_IP=`/opt/pos/common/bin/vpn_ip_address.sh`
IP=`ip route get 1 | awk '{print $NF;exit}'`
GATEWAY=`ip route | awk '/default/ { print $3 }'`
STATION_NUMBER=`cat /opt/pos/etc/config.ini | grep STATION_NUMBER | cut -f2 -d=`
COMPANY_NAME=`cat /opt/pos/etc/config.ini | grep SAFT_CompanyName= | cut -f2 -d=`
BIZ_NAME=`cat /opt/pos/etc/config.ini | grep SAFT_BusinessName= | cut -f2 -d=`
ADDRESS=`cat /opt/pos/etc/config.ini | grep SAFT_AddressDetail= | cut -f2 -d=`
POSTAL_CODE=`cat /opt/pos/etc/config.ini | grep SAFT_PostalCode= | cut -f2 -d=`
CITY=`cat /opt/pos/etc/config.ini | grep SAFT_City= | cut -f2 -d=`
NIF=`cat /opt/pos/etc/config.ini | grep SAFT_TaxRegistratioNumber= | cut -f2 -d=`
TIB_VERSION=`cat /tib_version 2> /dev/null`
HAS_CLOUD_BACKUPS="0"
VERSION=`cat /opt/pos/version | cut -d"#" -f1`
RELEASE=`cat /opt/pos/version | cut -d"#" -f2`
HAS_CLOUD_BACKUPS_TMP=`cat /opt/pos/etc/config.ini | grep "INTERNET_BACKUP=1"`

if [ "$HAS_CLOUD_BACKUPS_TMP" != "" ]; then
  HAS_CLOUD_BACKUPS="1"
fi

HDD_HARDLOCK=""
PEN_HARDLOCK=""

if [ -e "/opt/pos/hardlock" ]; then
  HDD_HARDLOCK=`/opt/pos/common/bin/machine_id 1`
fi

if [ -e "/opt/pos/.machine_id" ]; then
  PEN_HARDLOCK=`cat "/opt/pos/.machine_id"`
fi

if [ "$HDD_HARDLOCK" = "" ] && [ "$PEN_HARDLOCK" = "" ]; then
  for dir in `ls /media/`; do
    if [ -d "/media/$dir" ] && [ -d "/media/$dir/pos" ] && [ -e "/media/$dir/pos/hardlock" ]; then
      TMP=`/opt/pos/common/bin/device_id "/media/$dir" 2> /dev/null`

      if [ "$TMP" != "" ]; then
        POS_HARDLOCK=$TMP
      fi
    fi
  done
else
  if [ "$PEN_HARDLOCK" != "" ]; then
    POS_HARDLOCK=$PEN_HARDLOCK
  else
    POS_HARDLOCK=$HDD_HARDLOCK
  fi
fi

if [ "$POS_HARDLOCK" = "" ]; then
  echo "No hardlock on the machine, aborting..."
  exit
fi

curl --connect-timeout 3 --max-time 3 \
-F "hardlock=$POS_HARDLOCK" \
-F "skin=$SKIN" \
-F "station=$STATION_NUMBER" \
-F "mac_addr=$MAC_ADDR" \
-F "company_name=$COMPANY_NAME" \
-F "biz_name=$BIZ_NAME" \
-F "nif=$NIF" \
-F "address=$ADDRESS" \
-F "postal_code=$POSTAL_CODE" \
-F "city=$CITY" \
-F "ip=$IP" \
-F "gateway=$GATEWAY" \
-F "tib_version=$TIB_VERSION" \
-F "version=$VERSION" \
-F "release=$RELEASE" \
-F "has_cloud_backups=$HAS_CLOUD_BACKUPS" \
-F "vpn_ip=$VPN_IP" http://www.ptcert.com/vpn_register 2> /dev/null



