#!/bin/bash
set -e

usage()
{
cat << EOF
usage: $0 options

This script will collect info from a cisco sccp phone and save it to a file.

OPTIONS:
   -h      Show this message
   -i	   Ip-Address of the phone to pull the screenshot from
   -u      Phone Username	(also requires password)
   -p      Phone Password
EOF
}

IPADDRESS=
OUTPUTFILE=info
USERNAME=
PASSWORD=
while getopts “h:i:u:p:?” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         i)
             IPADDRESS=$OPTARG
             ;;
         u)
             USERNAME=$OPTARG
             ;;
         p)
             PASSWORD=$OPTARG
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

if [[ -z $IPADDRESS ]]
then
     usage
     exit 1
fi


USERPASS=""
echo "Retrieving Info from phone at $IPADDRESS..."
if [[ ! -z $USERNAME ]]; then
	if [[ ! -z $PASSWORD ]]; then
		USERPASS="--user $USERNAME:$PASSWORD"
	else 
		usage
		exit 1
	fi
fi
deviceinfo_tmp=$(mktemp)
curl -q --basic $USERPASS --url http://$IPADDRESS/DeviceInformationX -O $deviceinfo_tmp 2>/dev/null
modelnumber=`grep modelNumber $deviceinfo_tmp`
[ -f $deviceinfo_tmp ] && rm $deviceinfo_tmp
if [[ -z "`grep -e 7960 -e 7940 DeviceInformationX`" ]]; then
        curl -q --basic $USERPASS --url http://$IPADDRESS/CGI/LineInfo     -o LineInfo 2>/dev/null
        curl -q --basic $USERPASS --url http://$IPADDRESS/CGI/ModeInfo     -o ModeInfo 2>/dev/null
        curl -q --basic $USERPASS --url http://$IPADDRESS/CGI/SettingsInfo -o SetttingsInfo 2>/dev/null
        curl -q --basic $USERPASS --url http://$IPADDRESS/CGI/CallInfo     -o CallInfo.tmp 2>/dev/null
        if [ -f CallInfo.tmp ] && [ $? == 0 ] && [ -z "`grep -e Error CallInfo.tmp`" ]; then 
                cat CallInfo.tmp |xmllint --pretty 1 - > CallInfo; rm CallInfo.tmp
        else
                rm CallInfo.tmp
        fi
fi
