#!/bin/sh
# 
# Sample script to send an SMS email notifcation to SMS2Email's HTTP gateways

# Username and password associated with SMS2Email account
# Modify these values to match your account credentials if you don't want to
# specify them as command line arguments.
username=
password=

number=
message="Test message"



# Show usage if necessary
if [ $# -eq 0 ]; then
    echo "Usage: $0 -n [number] -m [message] -u [username] -p [password]";
    echo "";
    echo "[number]   = SMS number to send message to";
    echo "[message]  = Text of message you want to send";
    echo "[username] = Username assocated with SMS2Email account";
    echo "[password] = Password assocated with SMS2Email account";
    echo "               Both the username and password options are optional and";
    echo "               override the account credentials defined in this script.";
    echo "";
    exit 1;
fi


# Get command line arguments
while [ "$1" != "" ] ; do
    case $1
    in
	-n)
	    # Get the SMS number that we should send message to
	    number=$2;
	    shift 2;
	    ;;
	-m)
	    # Get the message we should send
	    message=$2;
	    shift 2;
	    ;;
	-u)
	    # Get the username
	    username=$2;
	    shift 2;
	    ;;
	-p)
	    # Get the password
	    password=$2;
	    shift 2;
	    ;;
	*)
	    echo "Unknown option: $1"
	    exit 1;
	    ;;
    esac
done


# We haven't sent the message yet
message_sent_ok=0;

# Try to send an HTTP POST message (try all servers until successful)
for server in gw1 gw11 gw2 gw22; do

    RESPONSE=`curl -s -d username=$username -d password=$password -d to_num=$number -d message="$message" http://$server.sms2email.com/sms/postmsg.php`

    # Curl was able to post okay...
    if [ "$?" -eq "0" ]; then

	# Test the response from the SMS2Email server
	case $RESPONSE
	in
	    AQSMS-OK)
		# Message was queued ok
		mesage_sent_ok=1;
		echo "Message posted OK to HTTP gateway $server"
		exit 0;
		;;
	    AQSMS*)
		# Some kind of fatal error occurred
		echo "Fatal error received from HTTP gateway $server: $RESPONSE"
		exit 1;
		;;
	    *)
		# No response or invalid response
		;;
	esac

    fi

done




