#!/bin/bash
# Export PIN numbers from Asterisk voicemail configuration file
# use -sp if submitting to www.pinpop.com
# Andrew Horton

conf="/etc/asterisk/voicemail.conf"

function usage() {
 echo "Usage: $0 [-s] [-p]"
 echo "Export PINs from Asterisk voicemail, /etc/asterisk/voicemail.conf"
 echo -e "\n-s\tSupress PINs the same as their extensions."
 echo -e "-p\tPrivacy option. Only display PINs."
 exit
}

supress=0
privacy=0
while getopts " s p h" o ; do  
	case $o in  
	s ) supress=1;;  
	p ) privacy=1;;
	h ) usage ;;
	esac  
done  

if [ ! -r $conf ]; then
	echo "Cannot read $conf"
	exit 1
fi

# turn the voicemail.conf file into a CSV of extensions & PINs 
RESULTS=`cat $conf |
tr -d ' \t' |egrep "^[0-9]+(=>)[0-9]+" | cut -d , -f 1 |sed 's/=>/,/g'`

if [ $supress = 1 ]; then
 RESULTS=`echo "$RESULTS" | awk -F , '{if ($1!=$2)  print $1","$2 }'`
fi

if [ $privacy = 1 ]; then
# Print out PINs but only if they differ from the phone extension 
 echo "$RESULTS" | cut -d , -f 2
else
 echo "$RESULTS"
fi

