#!/usr/bin/env bash
#
# Script for checking the syntax of Pms Gateway csv config file
#
# Installation and usage:
# 1 - Copy this file into oxe ( > scp check-pmsgw-config mtcl@<oxe-host>:. )
# 2 - Set executable attributes ( > chmod +x check-pmsgw-config )
# 3 - Run the file ( > ./check-pmsgw-config <csv-file> )
# 4 - View results
#
# Bjarne Ingelsson, SourceTech AB, 2025
#
# Version history
# 1.0
# 	Initial version
# 1.1
# 	Added NEC PMS
# 1.2
# 	Added FIAS options

# Constants
VERSION=1.2
CLEAR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
LIGHTGRAY='\033[0;37m'
RE='^[^,\n]*,("[^"]*"|[^,]*)$'

# Echo wrappers
info(){
  echo -e ${CLEAR}$@
}

ok(){
  echo -e ${GREEN}[ OK ]${CLEAR} $@
}

ko(){
  echo -e ${RED}[ KO ]${CLEAR} $@
}

warn(){
  echo -e ${YELLOW}[ -- ]${LIGHTGRAY} $@${CLEAR}
}

# Validates input file
validate_csv(){
  if [ ! -e $1 ]; then
    ko $1 ' not found.'
    exit 0
  fi
}

# Parse file content
parse_csv(){
  while read row; do
   validate_row $row
   if [ $rowstat == 1 ]; then
     parse_env $row
   fi
  done < $1
}

# Validate row
validate_row(){
  if echo $1 | grep -E $RE > /dev/null; then
    rowstat=1
  else
    ko $1
    rowstat=0
  fi
}

# Parse environment
parse_env(){
  case $1 in
    GEN_* ) parse_gen $1 ;;
    *_OXE_* ) parse_oxe $1 ;;
    *_FIAS_* ) parse_fias $1 ;;
    *_MEWS_* ) parse_mews $1 ;;
    *_OHIP_* ) parse_ohip $1 ;;
    *_MITEL_* ) parse_mitel $1 ;;
    *_AHL_*) parse_ahl $1 ;;
    *_NEC_*) parse_nec $1 ;;
    *_PROXY* ) ok $1 ;;
    TZ* ) ok $1 ;;
    * ) warn $1 ;;
  esac
}

parse_gen(){
  if [[ $1 =~ (_ROUTES|_LOGTOFILE|_LOGTOSYSLOG|_SYSLOGSERVER|_DNS) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_oxe(){
  if [[ $1 =~ (_HOST|_PORT|_GUESTMODE|_MINIBAR|_FRAMETIMEOUT) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_fias(){
  if [[ $1 =~ (_HOST|_PORT|_DESCR|_WOUPREQ|_CODEPAGE|_VOICEMAIL|_GPINOFFSET|_LANG|_AUTH|_PINCODE|_PINASROOM|_CALLTYPENAT|_CALLTYPEINT|_KALS) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_mews(){
  if [[ $1 =~ (_ACCESSTOKEN|_DESCR|_ROOMSTATUSREASON|_PHONESERVICENAME|_VOICEMAIL|_LANG) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_ohip(){
  if [[ $1 =~ (_DESCR|_VOICEMAIL|_LANG|_APPKEY|_CLIENTID|_CLIENTSECRET|_HOTELID|_CHAINCODE|_ENTERPRISEID|_GATEWAYURL|_SCOPE|_CASHIERID|_TRANSACTIONCODE) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_mitel(){
  if [[ $1 =~ (_PORT|_DESCR|_FIVEDIGIT|_RSTR) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_ahl(){
  if [[ $1 =~ (_PORT|_DESCR|_FIVEDIGIT) ]]; then
    ok $1
  else
    warn $1
  fi
}

parse_nec(){
  if [[ $1 =~ (_DESCR|_SMDR|_TWODIGITSEQNR|_LANG|_RSTR|_MAID|_VOICEMAIL|_GPINOFFSET) ]]; then
    ok $1
  else
    warn $1
  fi
}

usage(){
  info usage: ${0} '<mysettings-file.csv>'
}

# Legends
legends(){
  info '+++++++++++++++++++++++++++++++++++++++ Legends ++++++++++++++++++++++++++++++++++++++++'
  ok ' This is an ok line'
  ko ' This line contains syntax errors'
  warn ' This line contains unknown info'
  info '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
}

# Lets go
clear

# Validate input
if [ $# -eq 0 ]; then
  usage
  exit 0
fi

info  ------------------------------- PmsGw Syntax Check v${VERSION} --------------------------------
validate_csv $1
info - Checking $1
legends
parse_csv $1
info '----------------------------------------------------------------------------------------'
