#!/bin/bash 
# 
# This script add a HBLink DMO/DUP port in the DMR_Hosts.txt file of Pi-star distribution only 
# for HB_FR_BELGIQUE server. 
# It may be placed in crontab with redirection to dev/null to avoid getting mail from crontab process 
# 73, Benjamin - ON5BGO - Ver 1.0 - 01.11.2020 
# 
# Sample line: 
# HB_FR_BELGIQUE_DMO68            0000    51.75.249.21   PASSWORD   55568 
# 
########################################################################################################### 

DMRHOST=/usr/local/etc/DMR_Hosts.txt 
if [ ! -e "$DMRHOST" ]; then 
   echo "DMR host file doesn't exist ! Endind..." 
   exit 
fi 
if [ $# -lt 3 ]; then 
   echo "USAGE : $0 <DUPxx|DMOxx> <PASSWORD> <Port>" 
else 
   NEWPORT=$1 
   PASSW=$2 
   UDPPORT=$3 
   zz=$(grep HB_FR_BELGIQUE $DMRHOST | grep $NEWPORT) 
   if [ -n "$zz" ]; then 
       echo 
       echo "The port DMO/DUP already exists. No more action. Bye..." 
       echo 
       exit 
   fi 
   awk -v newport=$NEWPORT -v passw=$PASSW -v udpport=$UDPPORT ' 
   BEGIN { ctrBE=0; ctrOT=0 } 
   $1 ~ /HB_FR_BELGIQUE/ { 
      delete hb_array; 
      split($1, hb_array,"_"); 
      hb_array[4]=newport; 
      newlinedmo=hb_array[1]"_"hb_array[2]"_"hb_array[3]"_"hb_array[4]"\t\t"$2"\t"$3"\t\t\t"passw"\t"udpport; 
      print $0; 
      ctrBE=ctrBE+1; 
      ctrOT=0; 
   } 

   $1 !~ /HB_FR_BELGIQUE/ && ctrBE==0 { 
      print $0; 
      ctrOT=ctrOT+1; 
   } 

   $1 !~ /HB_FR_BELGIQUE/ && ctrOT==0 && ctrBE > 0 { 
      print newlinedmo; 
      ctrOT=ctrOT+1; 
      ctrBE=0; 
   } 
   ' $DMRHOST 
fi 




























