# !/bin/bash
#
#  write_nodename v0.5
#
#  Doug, WA3DSP 8/18/2015
#  Portions by Marshall Dias
#
# Script to write Calls to /var/lib/asterisk/sounds/rpt/nodenames
# which will then be used in place of node numbers. This script
# will NOT overwrite exisitng files unless you use the -o option.
# If you want to start clean and delete all nodename files do
#    rm -rf /var/lib/asterisk/sounds/rpt/nodenames/*
# or delete just the files you want to replace.
#
# A single node number can be specified on the command line with
# the -n node -ex. -n 40000
#
# A -v option gives a little more info   

# See all comments below
 
# Set the source directory to the Allstar database on your system.
# 'locate astdb.php'  This file will exist if you are running any of the
# images from hamvoip.org or you have installed allmon or lsnodes on
# your system. For the BBB and RPi2 it will generally be /var/log/asterisk
 
SRCDIR=/var/log/asterisk

# Set the destination directory. 
# Use a tmp directory for testing
# The final destination for Allstar would be 
# /var/lib/asterisk/sounds/rpt/nodenames
# Use the -d path option to overide
 
DESTDIR=/var/lib/asterisk/sounds/rpt/nodenames 
# Test destination directory - must exist
#DESTDIR=/tmp/nodes

# Definitions of sound file directories

RPTSOUNDS=/var/lib/asterisk/sounds/rpt
NODENAMES=/var/lib/asterisk/sounds/rpt/nodenames
LETTERS=/var/lib/asterisk/sounds/letters
NUMBERS=/var/lib/asterisk/sounds/digits
STRING=""


usage()
{
cat << EOF

$0 v0.5
Usage: $0 options

Writes voice files of call signs from node numbers

OPTIONS:

   -h        Show this message
   -a        Process all nodes
   -i        Include node number with call
   -o        Overwrite existing file(s)
   -n node   Process single node (overrides -a)
   -d path   Specify path to output files
             Default: /var/lib/asterisk/sounds/rpt/nodenames
   -v        Verbose

Write callsign for node 40000, no overwrite, all nodes -
    $0 -an 40000
Write callsign for node 40000, overwrite, all nodes -
    $0 -aon 40000
Write all callsigns, no overwrite -
    $0 -a
Write all callsigns, overwrite
    $0 -ao
Write single node to specific directory -
    $0 -n 40000 -d /etc/asterisk/local
Write single node including both call and node number, overwrite
    $0 -ion 40000

EOF
}

OVERWRITE=""
VERBOSE=""
INCNODE=""

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

while getopts "haiovn:d:" OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
	 a)  ;;
	 d)
	     DESTDIR=$OPTARG
	     ;;
	 i)
	     INCNODE=1
	     ;;
         o)  
	     OVERWRITE=1
             ;;
         n)
             node=$OPTARG
             ;;
         v)
             VERBOSE=1
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

# Form filenames from callsign
make_call ()
{
 foo=${1,,}
 for (( i=0; i<${#foo}; i++ )); do
  char=${foo:$i:1}

                case ${foo:$i:1} in
                [0-9]*) FILENAME=$NUMBERS/$char.gsm ;;
                "/") FILENAME=$LETTERS/slash.gsm ;;
                "-") FILENAME=$LETTERS/dash.gsm ;;
                [a-z]*|[A-Z]*) FILENAME=$LETTERS/$char.gsm ;;
                esac
STRING="$STRING $FILENAME"
done
}

# Concatenate filenames into final audio file
write_call ()
{
if [ ! $OVERWRITE ]
 then
   if [ -f "$DESTDIR/$f1.gsm" ]
     then
        echo "$DESTDIR/$f1.gsm exists - not overwriting"
	return
   fi
fi
echo "Writing $DESTDIR/$f1.gsm"
cat $STRING > "$DESTDIR/$f1.gsm"
}

# If no node given process all nodes
if [ -z "$node" ]
then
  while IFS='|' read f1 f2 f3 f4
  do      
    if [ -z $f1 ]; then continue ; fi
    if [ $VERBOSE ]; then echo "Processing Node Number: $f1 - Callsign: $f2" ; fi
    make_call $f2
# Tes for including node number 
   if [ $INCNODE ]
    then
     STRING=$STRING" $RPTSOUNDS/node.gsm "
     make_call $f1
    fi
    write_call
  done < $SRCDIR/astdb.txt
else
# process just given node (-n node)
  CALL=`grep -m 1 $node $SRCDIR/astdb.txt`
  if [ -z "$CALL" ]
    then
      echo "No Call found for node - $1"
      exit
    else
      f2=`echo $CALL | awk -F"|" '{print $2}'`
  fi
  f1=$node
  if [ $VERBOSE ]; then echo "Processing Node Number: $node - Callsign: $f2" ; fi
  STRING=""
  make_call $f2
# Test for including node number  
  if [ $INCNODE ]
  then
    STRING=$STRING" $RPTSOUNDS/node.gsm "
    make_call $f1
  fi
  write_call
fi


