#! /bin/bash
#set -v
#
# manca check che "fixed" e' = "static"
# 

CONFIG=/home/st/configured_cfg_net.json
CONNMANDIR=/var/lib/connman
ERROR=0

function log() {
  echo $*
}

function logg() {
  # echo $*
  if [ "$DEBUG" != "NONE" ]
  then
    echo $* 
  fi
}



function getConfigured () {
  WIFI_ENABLED=`jsonpar.py -f $CONFIG -p wifi_enabled`
  ETH_ENABLED=`jsonpar.py -f $CONFIG -p eth_enabled`
  case $ETH_ENABLED in
    true)
      export CONF_CONN_TYPE="ethernet"
      logg "Conf Service : $CONF_CONN_TYPE "

      export CONF_CONN_MODE=`jsonpar.py -f $CONFIG -p eth_mode` 
      logg "Conf Service mode : $CONF_CONN_MODE"      

      if [ "$CONF_CONN_MODE" = "static" ]
      then
        # logg "checking $SERVICE_MODE"

        CONF_IP=`jsonpar.py -f $CONFIG -p eth_ip`
        logg "CONF Service IP : " $CONF_IP

        CONF_DNS1=`jsonpar.py -f $CONFIG -p eth_dns1`
        CONF_DNS2=`jsonpar.py -f $CONFIG -p eth_dns2`
        logg "CONF Service DNS : " $CONF_DNS1 , $CONF_DNS2
        logg $CONF_DNS1 $CONF_DNS2
      else
        logg "CONF Service DNS : " "(( see RUNNING ))"
      fi
      ;;
    false)
      # logg "ETH DISABLED"
      case $WIFI_ENABLED in
        true)
          export CONF_CONN_TYPE="wifi"
          logg "Conf Service : $CONF_CONN_TYPE "

          export CONF_CONN_MODE=`jsonpar.py -f $CONFIG -p wifi_mode` 
          logg "Conf Service mode : $CONF_CONN_MODE"      

          #if [ "$RUN_CONN_MODE" = "static" ]
          if [ "$CONF_CONN_MODE" = "static" ]
          then
            # logg "checking $SERVICE_MODE"
            CONF_DNS1=`jsonpar.py -f $CONFIG -p wifi_dns1`
            CONF_DNS2=`jsonpar.py -f $CONFIG -p wifi_dns2`
            logg "Conf Service DNS : " $CONF_DNS1 , $CONF_DNS2
            logg $CONF_DNS1 $CONF_DNS2
          else
            logg "CONF Service DNS : " "(( see RUNNING ))"
          fi
          ;;
        false)
          logg "Conf : ERROR : ETH and WIFI DISABLED"
          ;;
      esac
  esac
}



function getRunning () {
  ### OLD prec 17/5 ## export SERVICE=`connmanctl services 2>/tmp/$$ | grep "\*A" | awk '{ print $3 }'`
  export SERVICE=`connmanctl services 2>/tmp/$$ | grep "\*A" | awk '{ print $NF }'`
  ls -lat /tmp/$$ >>/tmp/$$
  ERR_CONNMANCTL=${PIPESTATUS[0]}
  logg "error in connmanctl : $ERR_CONNMANCTL"
  if [ ${PIPESTATUS[0]} -ne 0 ]
  then
      ERR_CONNMANCTL=${PIPESTATUS[0]}
      log "error in connmanctl : $ERR_CONNMANCTL"
      log "RESTART CONNMAN ??" 
      log "RESTART CONNMAN ??" 
  fi

  logg "Runn Service : " $SERVICE

  export RUN_CONN_TYPE=`echo $SERVICE | awk -F "_" '{ print $1 }' `
  logg "Runn Service type :" $RUN_CONN_TYPE 

  export RUN_CONN_MODE=`connmanctl services $SERVICE| grep "IPv4.Configuration" | awk '{ print $4  }' | awk -F '=' '{ print $2 }' | awk -F ',' '{ print $1 }' `
  logg "Runn Service mode :" $RUN_CONN_MODE 

  export RUN_CONN_IP=`connmanctl services $SERVICE| grep "IPv4" | grep -v "IPv4.Configuration" | awk '{ print $5 }' | awk -F '=' '{ print $2 }' | awk -F ',' '{ print $1 }'`
  logg "Runn Service IP :" $RUN_CONN_IP 

  RUN_DNS2=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $5  }' | awk -F ',' '{ print $1 '}`
  RUN_DNS1=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $4  }' | awk -F ',' '{ print $1 '}`

  if [ "$RUN_DNS2" = "]" ]
  then
       logg "Runn Service IP (DNS2 null corrected ):" $RUN_DNS1
       RUN_DNS2="$RUN_DNS1"
  fi   
  
  logg "Runn Service DNS : " $RUN_DNS1 , $RUN_DNS2
}





function getdnsvalues () {
  SERVICE_TYPE=$1
  SERVICE_MODE=$2
  logg "Service type :" $SERVICE_TYPE "Service mode : " $SERVICE_MODE
  if [ "$SERVICE_MODE" = "dhcp" ]
  then
    SERVICE=`connmanctl services | grep "\*A" | awk '{ print $NF }'`
    logg "Servizio in modo dhcp : " $SERVICE
    DNS1=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $4  }' | awk -F ',' '{ print $1 }'`
    DNS2=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $5  }' | awk -F ',' '{ print $1 '}`
    log $DNS1 $DNS2
  else
    # logg "checking $SERVICE_MODE"
    SERVICE=`connmanctl services | grep "\*A" | awk '{ print $NF }'`
    logg "Servizio in modo static : " $SERVICE
    DNS1=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $4  }' | awk -F ',' '{ print $1 }'`
    DNS2=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $5  }' | awk -F ',' '{ print $1 '}`
    #set -x
    CONF_DNS1=`jsonpar.py -f $CONFIG -p eth_dns1`
    CONF_DNS2=`jsonpar.py -f $CONFIG -p eth_dns2`
    logg "configurated dns1 : " $CONF_DNS1 " - " $CONF_DNS2

	if [ "$DNS2" = "]" ]
	then
	       logg "Runn Service IP (DNS2 null corrected): $DNS1"
	       DNS2="$DNS1"
	fi  

    log $DNS1 $DNS2
  fi
}

function getdns () {
  ##log $RUN_DNS1 $RUN_DNS2
  ## return
  WIFI_ENABLED=`jsonpar.py -f $CONFIG -p wifi_enabled`
  ETH_ENABLED=`jsonpar.py -f $CONFIG -p eth_enabled`
  case $ETH_ENABLED in
    true)
      logg "Net configured : ETH enabled"
      CONN_MODE=`jsonpar.py -f $CONFIG -p eth_mode` 
      getdnsvalues eth $CONN_MODE
      ;;
    false)
      # logg "ETH DISABLED"
      case $WIFI_ENABLED in
        true)
          logg "Net configured : WIFI enabled"
          CONN_MODE=`jsonpar.py -f $CONFIG -p wifi_mode` 
          getdnsvalues wifi $CONN_MODE
          ;;
        false)
          logg "ERROR : ETH and WIFI DISABLED"
          ;;
    esac
  esac
}


function checkRunning() {
  export ERROR=0
  if [ "$RUN_CONN_TYPE" != "$CONF_CONN_TYPE" ]
  then
    logg "ERROR Check CONN_TYPE : configured : $CONF_CONN_TYPE ,running : $RUN_CONN_TYPE"
    ERROR=1
  fi

  if [ "$RUN_CONN_MODE" != "$CONF_CONN_MODE" ]
  then
    if [ "$RUN_CONN_MODE" == "fixed" ]
    then
      if [ "$CONF_CONN_MODE" != "static" ]
      then
        logg "ERROR Check (fixed) CONN_MODE : configured : $CONF_CONN_MODE ,running : $RUN_CONN_MODE"   
        ERROR=`expr $ERROR + 2`    
      else
          if [ "$RUN_DNS1" != "$CONF_DNS1" ]      
          then
            logg "ERROR Check (fixed) CONN_MODE : configured DNS1 : $CONF_DNS1 ,running : $RUN_DNS1"   
            ERROR=`expr $ERROR + 4`    
          fi
          if [ "$RUN_DNS2" != "$CONF_DNS2" ]      
          then
            logg "ERROR Check (fixed) CONN_MODE : configured DNS2 : $CONF_DNS2 ,running : $RUN_DNS2"   
            ERROR=`expr $ERROR + 8`    
          fi
      fi
    else
      logg "ERROR Check (not fixed) CONN_MODE : configured : $CONF_CONN_MODE ,running : $RUN_CONN_MODE"
      ERROR=`expr $ERROR + 2`
    fi
  fi
  
  RUN_DNS2=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $5  }' | awk -F ',' '{ print $1 '}`
  RUN_DNS1=`connmanctl services $SERVICE| grep "Nameservers" | grep -v "Nameservers.Configuration" | awk '{ print $4  }' | awk -F ',' '{ print $1 '}`

  if [ "$RUN_DNS2" = "]" ]
  then
       logg "Runn Service IP (DNS2 null corrected): $RUN_DNS1"
       RUN_DNS2="$RUN_DNS1"
  fi  

  logg "Runn Service DNS (static) : $RUN_DNS1 , $RUN_DNS2"

}



############# START 
############# START 
############# START 

if [ "$1" == "-d" -a "$2" != "" ]
then
  case $2 in
  INFO)
      export DEBUG=INFO  
      ;;
  TRACE)
      export DEBUG=TRACE  
      ;;
  ERROR)
      export DEBUG=ERROR
      ;;
  *)
      export DEBUG=INFO
      ;;
  esac 
  log "DEBUG set to $DEBUG "
else
  export DEBUG=NONE
fi

getConfigured

getRunning

checkRunning

getdns 

if [ $ERROR -gt 0 ]
then
    logg "ERROR $ERROR, exit"
    exit $ERROR
fi

