#!/bin/bash

CONFIG_DIR=/etc/nyquist
SIP_FILE=$CONFIG_DIR/SIP.config
DEFAULTS_DIR=$CONFIG_DIR/defaults
DEVICE_TYPE_FILE=$CONFIG_DIR/device_type

NYQ_SPEAKER=Speaker.config
NYQ_PAGING_AMP=PagingAmp.config
NYQ_ASB=ASB.config

#Do we have a device_type file?
if [ -e "$DEVICE_TYPE_FILE" ]
then
  echo "$DEVICE_TYPE_FILE exists"
  #What is our device type?
  DEVICE_ID=$(cut -d "=" -f 2 <<< "$(cat $DEVICE_TYPE_FILE)")

  echo "Device ID: $DEVICE_ID"
  
  #Is the device type valid?
  if ! [[ "$DEVICE_ID" =~ ^(10|11|12|13|14|15)$ ]]; then rm $SIP_FILE ; fi

  if [ "$DEVICE_ID" == "10" ]
  then 
    echo "Restoring $NYQ_SPEAKER"
    cp $DEFAULTS_DIR/$NYQ_SPEAKER $SIP_FILE
  elif [ "$DEVICE_ID" == "12" ] 
  then
    echo "Restoring $NYQ_PAGING_AMP"
    cp $DEFAULTS_DIR/$NYQ_PAGING_AMP $SIP_FILE
  elif [ "$DEVICE_ID" == "14" ] 
  then
    echo "Restoring $NYQ_ASB"
    cp $DEFAULTS_DIR/$NYQ_ASB $SIP_FILE
  fi

else
  echo "$DEVICE_TYPE_FILE does not exist"
  rm $SIP_FILE
fi



