#!/bin/sh
#(c) Copyright Barry Kauler March 2012, bkhome.org
#License GPL3 (refer /usr/share/doc/legal).
#this script is a replacement for 'xmessage'.
#two reasons: 1: better appearance than gxmessage, 2: non-English text.
#120323 first version. only a partial replacement for xmessage.

if [ ! $1 ];then
 echo "Partial replacement for xmessage.
Can use only -bg -fg -timeout -title -center (default)
ex: pupmessage -bg '$ff8080' -title 'Title of popup' 'Body text here'
Note: use 'pupkill $!' to kill a pupmessage window."
 exit
fi

#note: use 'pupkill $!' to kill a pupmessage window.

VALUEFLAG='no'; TITLETEXT=''; SEPPARAMS=''
for AFIELD in "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" "${8}" "${9}" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" TOOLONG
do
 [ "$AFIELD" = "" ] && break
 PREVFIELD="$AFIELD"
 [ "$AFIELD" = "TOOLONG" ] && exit 255
 case $VALUEFLAG in
  yes)
   SEPPARAMS="${SEPPARAMS} ${AFIELD}"
   VALUEFLAG='no'
   continue
  ;;
  title) #value can have multiple words.
   TITLETEXT="$AFIELD"
   VALUEFLAG='no'
   continue
  ;;
 esac
 ADDME='no'
 case "$AFIELD" in
  -title) VALUEFLAG='title'  ;;
  -bg|-fg|-buttons|-default|-file|-timeout) VALUEFLAG='yes'; ADDME='yes'  ;;
  -print|-center|-nearmouse) ADDME='yes' ;;
 esac
 [ "$ADDME" = "yes" ] && SEPPARAMS="${SEPPARAMS}
${AFIELD}"
done
SEPMSG="$PREVFIELD" #message to be displayed.

#if certain options, use original xmessage (or gxmessage)...
if [ "`echo "$SEPPARAMS" | grep -E '^-buttons|^-file|^-print|^-nearmouse|^-default'`" != "" ];then
 xSEPPARAMS=$(echo "$SEPPARAMS" | tr '\n' ' ')
 if [ "$TITLETEXT" ];then
  exec xmessage $xSEPPARAMS -title "$TITLETEXT" "$SEPMSG"
 else
  exec xmessage $xSEPPARAMS "$SEPMSG"
 fi
fi

xSEPPARAMS=$(echo "$SEPPARAMS" | sed -e 's%^-bg%--background%' -e 's%^-fg%--foreground%' -e 's%^-timeout%--timeout%' | sed '/^-center/d' | tr '\n' ' ')

#echo "xSEPPARAMS=${xSEPPARAMS}" #TEST

if [ "$TITLETEXT" ];then
 exec pupdialog $xSEPPARAMS --backtitle "$TITLETEXT" --msgbox "$SEPMSG" 0 0
else
 exec pupdialog $xSEPPARAMS --msgbox "$SEPMSG" 0 0
fi

###END###
