#! /bin/sh

#================================================================
# rbbsping
# Ping sender to blog update ping server
#================================================================


# set variables
LANG=C ; export LANG
LC_ALL=C ; export LC_ALL
pathtmp="$PATH"
PATH="$HOME/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
PATH="$PATH:/usr/ccs/bin:/usr/ucb:$pathtmp"
LIBRARY_PATH="$HOME/lib:/usr/local/lib:$LIBRARY_PATH"
LD_LIBRARY_PATH="$HOME/lib:/usr/local/lib:$LD_LIBRARY_PATH"
progname="rbbsping"
#proxy="localhost:8080"


# check arguments
if [ $# != 3 ]
then
  printf '%s: usage: %s server_url target_url target_title\n' "$progname" "$progname" 1>&2
  exit 1
fi


# function to send the ping message
sendping(){
  opt=""
  if [ -n "$proxy" ]
  then
    opt="--proxy $proxy"
  elif [ -n "$RBBSPINGPROXY" ]
  then
    opt="--proxy $RBBSPINGPROXY"
  fi
  curl $opt --data-binary "@-" -H "Content-Type: text/xml" "$1"
}


# output the data and send it
awk -v url="$2" -v title="$3" '
BEGIN {
  gsub(/&/, "\\&amp;", title)
  gsub(/</, "\\&lt;", title)
  gsub(/>/, "\\&gt;", title)
  printf("<?xml version=\"1.0\"?>\n");
  printf("<methodCall>\n");
  printf("<methodName>weblogUpdates.ping</methodName>\n");
  printf("<params>\n");
  printf("<param>\n");
  printf("<value>%s</value>\n", title);
  printf("</param>\n");
  printf("<param>\n");
  printf("<value>%s</value>\n", url);
  printf("</param>\n");
  printf("</params>\n");
  printf("</methodCall>\n");
}
' | sendping "$1"


# exit normally
exit 0


# END OF FILE
