#!/bin/bash
#

#DEBUG="ON"

CONFIG_FILE_LOC=/etc/nyquist
CONFIG_FILENAME=SIP.config
OLD_CONFIG_FILENAME="$CONFIG_FILENAME-orig"
DIFF_FILE=$CONFIG_FILE_LOC/config_diff
TEMP_DIFF=$CONFIG_FILE_LOC/diff_temp

echo () {
  [[ "$DEBUG" ]] && builtin echo $@
}

check_intercom () {
  INTERCOM_VOL=$(awk '/< INTERCOM_VOLUME/' $TEMP_DIFF)
  echo "INTERCOM_VOL: $INTERCOM_VOL"
  if [ "$INTERCOM_VOL" != "" ]
  then
    NEW_INTERCOM_VOL=$(builtin echo "$INTERCOM_VOL" | sed 's/^..//')
    echo "NEW_INTERCOM_VOL: $NEW_INTERCOM_VOL"
    NEW_INTERCOM_VOL2=$(builtin echo "$INTERCOM_VOL" | sed 's/^..//' | sed 's/INTERCOM_VOLUME*/; INTERCOM_VOLUME/g' | sed -zE 's/[[:space:]]([;])/\1/g' | sed -zE 's/;//')
    echo "NEW_INTERCOM_VOL2: $NEW_INTERCOM_VOL2"
    
    CHANGED_VARS+="$NEW_INTERCOM_VOL2; "
  fi
}

check_emergency_all_call () {
  E_ALL_CALL_MULTICAST_STRING=MULTICAST_E_ALL_CALL

  NEW_EAC_MULTICAST_VAL=$(awk "/< $E_ALL_CALL_MULTICAST_STRING/" $TEMP_DIFF)
  OLD_EAC_MULTICAST_VAL=$(awk "/> $E_ALL_CALL_MULTICAST_STRING/" $TEMP_DIFF)

  if [ "$OLD_EAC_MULTICAST_VAL" == "" ]
  then
    return 0;
  fi

  echo "OLD:  $OLD_EAC_MULTICAST_VAL     NEW: $NEW_EAC_MULTICAST_VAL"

  OLD_EAC_MC_AP=$( builtin echo "$OLD_EAC_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')
  NEW_EAC_MC_AP=$( builtin echo "$NEW_EAC_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')

  echo "OLD EAC MC AP:   $OLD_EAC_MC_AP      NEW EAC MC AP:   $NEW_EAC_MC_AP"

  if [ "$OLD_EAC_MC_AP" != "$NEW_EAC_MC_AP" ]
  then
    echo "Old and new multicast Emergency All-Call port/addr/stations do not match, we have to restart the application"
    builtin echo "0"
    exit 0
  fi

  echo "Old and new multicast Emergency All-Call port/addr/stations match, get new volume"

  NEW_EAC_MC_VOLUME=$(grep -oP '\[\K[^\]]*' <<< "$NEW_EAC_MULTICAST_VAL")

  echo "$NEW_EAC_MC_VOLUME"

  CHANGED_VARS+="$E_ALL_CALL_MULTICAST_STRING = $NEW_EAC_MC_VOLUME; "
}

check_all_call () {
  ALL_CALL_MULTICAST_STRING=MULTICAST_ALL_CALL

  NEW_AC_MULTICAST_VAL=$(awk "/< $ALL_CALL_MULTICAST_STRING/" $TEMP_DIFF)
  OLD_AC_MULTICAST_VAL=$(awk "/> $ALL_CALL_MULTICAST_STRING/" $TEMP_DIFF)
  
  if [ "$OLD_AC_MULTICAST_VAL" == "" ]
  then
    return 0;
  fi

  echo "OLD ALL CALL:  $OLD_AC_MULTICAST_VAL     NEW ALL CALL: $NEW_AC_MULTICAST_VAL"

  OLD_AC_MC_AP=$( builtin echo "$OLD_AC_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')
  NEW_AC_MC_AP=$( builtin echo "$NEW_AC_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')

  echo "OLD AC MC AP:   $OLD_AC_MC_AP      NEW AC MC AP:   $NEW_AC_MC_AP"

  if [ "$OLD_AC_MC_AP" != "$NEW_AC_MC_AP" ]
  then
    echo "Old and new multicast All-Call port/addr/stations do not match, we have to restart the application"
    builtin echo "0"
    exit 0
  fi

  echo "Old and new multicast All-Call port/addr/stations match, get new volume"

  NEW_AC_MC_VOLUME=$(grep -oP '\[\K[^\]]*' <<< "$NEW_AC_MULTICAST_VAL")

  echo "$NEW_AC_MC_VOLUME"

  CHANGED_VARS+="$ALL_CALL_MULTICAST_STRING = $NEW_AC_MC_VOLUME; "
}


check_audio_dist () {
  AD_MULTICAST_STRING=MULTICAST_AUDIO_DIST

  NEW_AD_MULTICAST_VAL=$(awk "/< $AD_MULTICAST_STRING/" $TEMP_DIFF)
  OLD_AD_MULTICAST_VAL=$(awk "/> $AD_MULTICAST_STRING/" $TEMP_DIFF)
  
  if [ "$OLD_AD_MULTICAST_VAL" == "" ]
  then
    return 0;
  fi

  echo "OLD AUDIO DIST:  $OLD_AD_MULTICAST_VAL     NEW AUDIO DIST: $NEW_AD_MULTICAST_VAL"

  OLD_AD_MC_AP=$( builtin echo "$OLD_AD_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')
  NEW_AD_MC_AP=$( builtin echo "$NEW_AD_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')

  echo "OLD AD MC AP:   $OLD_AD_MC_AP      NEW AD MC AP:   $NEW_AD_MC_AP"

  if [ "$OLD_AD_MC_AP" != "$NEW_AD_MC_AP" ]
  then
    echo "Old and new multicast audio distribution port/addr/stations do not match, we have to restart the application"
    builtin echo "0"
    exit 0
  fi

  echo "Old and new multicast audio distribution port/addr/stations match, get new volume"

  NEW_AD_MC_VOLUME=$(grep -oP '\[\K[^\]]*' <<< "$NEW_AD_MULTICAST_VAL")

  echo "$NEW_AD_MC_VOLUME"

  CHANGED_VARS+="$AD_MULTICAST_STRING = $NEW_AD_MC_VOLUME; "
}


check_multicast_zones () {
  MULTICAST_ZONE_STRING=MULTICAST_LIST

  NEW_MULTICAST_VAL=$(awk "/< $MULTICAST_ZONE_STRING/" $TEMP_DIFF)
  OLD_MULTICAST_VAL=$(awk "/> $MULTICAST_ZONE_STRING/" $TEMP_DIFF)
  
  if [ "$OLD_MULTICAST_VAL" == "" ]
  then
    return 0;
  fi

  echo "OLD MULTICAST ZONE:  $OLD_MULTICAST_VAL     NEW MULTICAST ZONE: $NEW_MULTICAST_VAL"



  OLD_MC_AP=$( builtin echo "$OLD_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')
  NEW_MC_AP=$( builtin echo "$NEW_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')

  echo "OLD MC AP:   $OLD_MC_AP      NEW MC AP:   $NEW_MC_AP"


  if [ "$OLD_MC_AP" != "$NEW_MC_AP" ]
  then
    echo "Old and new multicast zone port/addr/stations do not match, we have to restart the application"
    builtin echo "0"
    exit 0
  fi

  IFS=',' read -r -a array <<< "$NEW_MULTICAST_VAL"
  echo "${#array[@]}"

  for index in "${!array[@]}"
  do
    echo "$index ${array[index]}"
  done

  VOLUMES_ONLY=$(builtin echo -n "$NEW_MULTICAST_VAL" | grep -Po "(?<=\[)[^\]]*(?=\])")
  #VOLUMES_ONLY=$(builtin echo "$NEW_MULTICAST_VAL" | sed 's/.*\[\([^]]*\)\].*//g')
  echo "VOLUMES_ONLY = $VOLUMES_ONLY"

  echo "Old and new multicast zone port/addr/stations match, get new volume"
  #NEW_MC_LIST_VOL=$(builtin echo -n "$NEW_MULTICAST_VAL" | sed 's/^..//' | sed 's/{[^}]*}//g' | sed 's/=[^\[]*\[/= [/' | sed 's/[][]//g' | sed 's/;/; /')
  #echo "NEW_MC_LIST_VOL: $NEW_MC_LIST_VOL"
  NEW_VOLUMES=$(builtin echo -n "$VOLUMES_ONLY" | sed ':a;N;$!ba;s/\n/,/g')
  echo "NEW_VOLUMES = $NEW_VOLUMES"
  #NEW_MC_LIST_VOL=$(builtin echo -n "" | sed 's/^..//' | sed 's/{[^}]*}//g' | sed 's/=[^\[]*\[/= [/' | sed 's/[][]//g' | sed 's/;/; /')
  CHANGED_VARS+="MULTICAST_LIST = $NEW_VOLUMES;"
}


check_listed_multicast_zones() {
  MULTICAST_ZONE_STRING=MULTICAST_LIST_

  NEW_MULTICAST_VAL=$(awk "/< $MULTICAST_ZONE_STRING/" $TEMP_DIFF)
  OLD_MULTICAST_VAL=$(awk "/> $MULTICAST_ZONE_STRING/" $TEMP_DIFF)
  
  if [ "$OLD_MULTICAST_VAL" == "" ]
  then
    return 0;
  fi

  echo "OLD MULTICAST ZONE:  $OLD_MULTICAST_VAL     NEW MULTICAST ZONE: $NEW_MULTICAST_VAL"

  OLD_MC_AP=$( builtin echo "$OLD_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')
  NEW_MC_AP=$( builtin echo "$NEW_MULTICAST_VAL" | sed -e 's/\[[^][]*\]//g' | sed 's/^.//')

  echo "OLD MC AP:   $OLD_MC_AP      NEW MC AP:   $NEW_MC_AP"


  if [ "$OLD_MC_AP" != "$NEW_MC_AP" ]
  then
    echo "Old and new multicast zone port/addr/stations do not match, we have to restart the application"
    builtin echo "0"
    exit 0
  fi
 
  MULTICAST_VALS=$(builtin echo "$NEW_MULTICAST_VAL" | grep -Po "(?<=\<).*?(?=\=)")

  #IFS=',' read -r -a array <<< "$NEW_MULTICAST_VAL"
  
  echo "MULTICAST_VALS = $MULTICAST_VALS"
  arr1=($MULTICAST_VALS) 

  for index in "${!arr1[@]}"
  do
    echo "$index ${arr1[index]}"
  done

  VOLUMES_ONLY=$(builtin echo -n "$NEW_MULTICAST_VAL" | grep -Po "(?<=\[)[^\]]*(?=\])")
  #VOLUMES_ONLY=$(builtin echo "$NEW_MULTICAST_VAL" | sed 's/.*\[\([^]]*\)\].*//g')
  echo "VOLUMES_ONLY = $VOLUMES_ONLY"

  arr2=($VOLUMES_ONLY)
  for index in "${!arr2[@]}"
  do
    echo "$index ${arr2[index]}"
  done

  echo "Old and new multicast zone port/addr/stations match, get new volume"
  #NEW_MC_LIST_VOL=$(builtin echo -n "$NEW_MULTICAST_VAL" | sed 's/^..//' | sed 's/{[^}]*}//g' | sed 's/=[^\[]*\[/= [/' | sed 's/[][]//g' | sed 's/;/; /')
  #echo "NEW_MC_LIST_VOL: $NEW_MC_LIST_VOL"
  #NEW_VOLUMES=$(builtin echo -n "$VOLUMES_ONLY" | sed ':a;N;$!ba;s/\n/,/g')
  #echo "NEW_VOLUMES = $NEW_VOLUMES"
  #NEW_MC_LIST_VOL=$(builtin echo -n "" | sed 's/^..//' | sed 's/{[^}]*}//g' | sed 's/=[^\[]*\[/= [/' | sed 's/[][]//g' | sed 's/;/; /')

  for index in "${!arr1[@]}"
  do
    NEW_VOLUMES+=$(builtin echo "${arr1[index]}=${arr2[index]};")
  done

  UPDATED_VOLUMES=$(builtin echo "$NEW_VOLUMES" | sed 's/\(.*\);/\1/')
  
  FINAL_VOL=$(builtin echo "$UPDATED_VOLUMES" | sed 's/=/ = /g' | sed 's/;/; /g')

  CHANGED_VARS+="$FINAL_VOL;"

}



check_opus () {
  OPUS_STRING=OPUS_VOLUME

  NEW_OPUS_VAL=$(awk "/< $OPUS_STRING/" $TEMP_DIFF)
  OLD_OPUS_VAL=$(awk "/> $OPUS_STRING/" $TEMP_DIFF)
  
  if [ "$OLD_OPUS_VAL" == "" ]
  then
    return 0;
  fi

  echo "OLD OPUS:  $OLD_OPUS_VAL     NEW OPUS: $NEW_OPUS_VAL"

  IFS=',' read -r -a array <<< "$NEW_OPUS_VAL"
  echo "${#array[@]}"

  for index in "${!array[@]}"
  do
    echo "$index ${array[index]}"
  done

  VOLUMES_ONLY=$(builtin echo -n "$NEW_OPUS_VAL" | sed 's/.*=//' | sed 's/;//g')
  echo "VOLUMES_ONLY = $VOLUMES_ONLY"

  NEW_VOLUMES=$(builtin echo -n "$VOLUMES_ONLY" | sed ':a;N;$!ba;s/\n/,/g')
  echo "NEW_VOLUMES = $NEW_VOLUMES"
  CHANGED_VARS+="OPUS_VOLUME =$NEW_VOLUMES;"
}


#echo "Comparing $CONFIG_FILENAME with $OLD_CONFIG_FILENAME to look for changes"

builtin echo "$(diff $CONFIG_FILE_LOC/$CONFIG_FILENAME $CONFIG_FILE_LOC/$OLD_CONFIG_FILENAME)" > $DIFF_FILE

#cat $DIFF_FILE

builtin echo "$(sed -n -e '/^[><] /p' $DIFF_FILE)" > $TEMP_DIFF

#Check to see if any of the changed parameters are something other than a potential volume
#Here, we check to see if the changed values either start with MULTICAST_, INTERCOM_VOLUME, or OPUS_VOLUME
#  if anything else is found, then we have to restart the appliance application

NUM_NEG_MATCHES=$(awk '!/MULTICAST_/ && !/INTERCOM_VOLUME/ && !/OPUS_VOLUME/ && !/DISPLAY_/ { count++ } END { print count }' $TEMP_DIFF)

#echo "NUM_NEG_MATCHES = $NUM_NEG_MATCHES"


if [ "$TEMP_DIFF" == "" ]
then
  #There were no changes to the config file, simply exit with an UNCHANGED code to notify the application
  builtin echo "UNCHANGED"
  exit 0
fi

if [ "$NUM_NEG_MATCHES" != "" ]
then
  #This change requires a restart
  builtin echo "0"
  exit 0
fi

DEVICE_TYPE=$(cat /sys/devices/platform/bone_capemgr/slot-0/part-number)
echo "Device Type: $DEVICE_TYPE"

# At this point, we need to check if the change made is strictly for a volume and nothing else. If so, then we need to tell the application
# what volume to adjust and what the new value will be. If something other than the volume has changed (a multicast address or port number), then
# we can exit and report 0 which tells the application to restart and re-read the SIP.config file.

# First check Intercom Volume (if it exists)
check_intercom

# Emergency All-Call Changes
check_emergency_all_call

# All-Call Changes
check_all_call

# Audio Distribution
check_audio_dist

# Multicast Zones (ASBs do this a little differently)
if [ "$DEVICE_TYPE" == "nq-asb" ] || [ "$DEVICE_TYPE" == "nq-a2xxx" ] || [ "$DEVICE_TYPE" == "nq-a2xxxg2" ] || [ "$DEVICE_TYPE" == "nq-paxxx" ] || [ "$DEVICE_TYPE" == "nq-a4xxx" ] || [ "$DEVICE_TYPE" == "nq-a4xxxg2" ]
then
  check_listed_multicast_zones
else
  check_multicast_zones
fi

# OPUS Volumes
check_opus

RESULT=$(builtin echo -n "$CHANGED_VARS" | sed ':a;N;$!ba;s/\n//g')

builtin echo "$RESULT"

#rm $DIFF_FILE
#rm $TEMP_DIFF


