#!/bin/bash
parseConfig(){
	link=$(readlink -f "$0")
	scriptpath=$(dirname "$link")
	if [[ -r "$scriptpath/config" ]]; then
			while IFS='' read -r line || [[ -n "$line" ]]; do
									 if [[ "$line" != "#"* ]]; then
											 IFS='=' read -r -a array <<< "$line"
											 case ${array[0]} in
															 username)
																			USERNAME=${array[1]}
															 ;;
															 password)
																			PASSWORD=${array[1]}
															 ;;
															 host)
																			HOST=${array[1]}
															 ;;
															 mode)
																			MODE=${array[1]}
															 ;;
															 proxy)
																			PROXY=${array[1]}
															 ;;
															 insecuressl)
																			INSECURESSL=${array[1]}
															 ;;
											 esac
									fi       
			done < "$scriptpath/config"
	fi
}
parseConfig
if [[ "$#" -gt 1 ]]
then
 TO=$1
 TEXT=$2
 if [[ -n "$3" ]]
	then
	USERNAME=$3
 fi
 if [[ -n "$4" ]]
	 then
	 PASSWORD=$4
 fi
 if [[ -n "$5" ]]
	 then
	 HOST=$5
 fi
 if [[ -n "$6" ]]
	 then
	 MODE=$6
 fi 
 if [[ -n "$7" ]]
    then
    PROXY=$7
 fi 
 if [[ -n "$8" ]]
    then
    INSECURESSL=$8
 fi
PARAMETERINSECSSL=""
 PARAMETERPROXY=""
 if [[ -n "$INSECURESSL" ]]
    then
    PARAMETERINSECSSL="-k"
 fi
if (( -z "$PROXY" ))
   then
   PARAMETERPROXY="-x $PROXY"
 fi
curl -s "$PARAMETERINSECSSL" "$PARAMETERPROXY" -G "http://${HOST}/api.php" --data-urlencode "mode=${MODE}" --data-urlencode "to=${TO}" --data-urlencode "text=${TEXT}" --data-urlencode "username=${USERNAME}" --data-urlencode "password=${PASSWORD}"
else
 echo "
usage: $0 [to] \"[text]\"
Optional Parameters:
$0 [to] \"[text]\" <[Username]> <[Password]> <[Host]> <[Mode]> <[Proxy]> <[Insecuressl]>
Options in config file \"config\" in $(pwd):
 username=myUsername
 password=myPassword
 host=myHost
 insecuressl=true or commented out
 proxy=[protocol://][user:password@]proxyhost[:port]
 mode=number/group/user/telnumber/telgroup/teluser
mode:
 number to = Recipient’s telephone number
 group to = ID (recommended) or name of a user group
 user to = ID (recommended) or name of a contact in the address book
 telnumber to = Recipient’s telephone number
 telgroup to = ID (recommended) or name of a user group
 teluser to = ID (recommended) or name of a contact in the address book
insecuressl true = allow insecure SSL Certificates 
proxy <[protocol://][user:password@]proxyhost[:port]>
"
fi