#!/bin/ash
#brilliant replacement for yaf-splash, written by mave, june 2010.
#gpl license see: /usr/share/doc/legal/gpl-2.0.txt
#BK added '-placement', '-close'.
#100604 mave: added '-deco', shows windows decoration, default none. BK: added title.
#100604 BK: proper handling if killed before timeout.
#100604 name /tmp/yaf-splash same as original, as some scripts have 'pidof yaf-splash' or 'killall yaf-splash'.
#100917 if old yaf-splash installed, and '-font' param, execute old yaf-splash.
#100929 my god, this is clumsy: above did not work, fixed.
# 4jan11 shinobar: interprete '-font' option as like '-fontsize', never use old yaf-splash.

#ignore scripts that do 'killall gtkdialog3'...
GTKDIALOG3="`which gtkdialog3`"
[ ! "$GTKDIALOG3" ] && GTKDIALOG3="`which gtkdialog`"
[ ! "$GTKDIALOG3" ] && exit
[ ! -f /tmp/yaf-splash ] && ln -s $GTKDIALOG3 /tmp/yaf-splash

ROOTGEOM="`xwininfo -root | grep -o ' \\-geometry .*' | cut -f 3 -d ' '`" #ex: 1280x800+0+0
ROOTX=`echo -n "$ROOTGEOM" | cut -f 1 -d 'x'`
ROOTY=`echo -n "$ROOTGEOM" | cut -f 1 -d '+' | cut -f 2 -d 'x'`
CENTERX=`expr $ROOTX \/ 2`

helptext="\ngtkdialog-splash\n
\n
an easy replacement for yaf-splash in Puppy (NOT Puppy <=4.x)\n
mave 30.05.2010\n
no warranty\n
Thanks for Puppy!\n
\n
usage: gtkdialog-splash [OPTIONS] [-timeout SEC] -text TEXT | -kill PID \n
\n
	-text EXPRESSION\n
	-timeout COUNT (in seconds)\n
	-icon GTK-XXX (for example: gtk-info all gtk-stock-symbols, default: none)\n
	-bg COLOR (background color red, blue, yellow..., default: grey)\n
	-fg COLOR (font color, default: black)\n
    -placement center|mouse|top|bottom|top-left|bottom-left (default center)\n
    -close never|mouseover|box (default is mouseover)\n
	-deco TITLE (shows windows decorations, with title)\n
	-fontsize SIZE (small, medium, large, x-large, default: medium)\n\n
	-kill PID (process ID called before: \tPID=0 auto-search fo the last one,\n
	  \t\t\t\t\tPID=xxxxx kill the last one read by PID=\$!)
	\n"
	
	
params=$@
	
if [ ${#params} -eq 0 ]; then	# no options, no text, so print info/help...
	echo -e $helptext
	exit
fi

BasePIDFile="/tmp/GTKDIALOG-SPLASH"
dummy=SECONDS
text=""
timeout=0
bg="grey"	
fg="black"
icon=""
fontsize="medium" # small, medium, large, x-large
pid=""
placement='center'
deco='false'
title='gtkdialog-splash'

param=""
optarg=$dummy

for arg in "$@";
 do
	if [ $optarg == "TEXT" ]		# getting optional argument of a parameter
	then
		text=$arg
		optarg=$dummy
	elif [ $optarg == "PLACEMENT" ]
	then
        placement=$arg
		optarg=$dummy
	elif [ $optarg == "CLOSE" ]
	then
        close=$arg
		optarg=$dummy
	elif [ $optarg == "TITLE" ]
	then
        title=$arg
		optarg=$dummy
	elif [ $optarg == "TIMEOUT" ]
	then
		timeout=$arg
		optarg=$dummy
	elif [ $optarg == "BG" ]
	then
		bg=$arg
		optarg=$dummy
	elif [ $optarg == "FG" ]
	then
		fg=$arg
		optarg=$dummy
	elif [ $optarg == "ICON" ]
	then
		icon="<pixmap>
          <input file stock=\"$arg\"></input>
        </pixmap>"
		optarg=$dummy
	elif [ $optarg == "KILL" ]
	then
		pid=$arg
		if [ ${#pid} -eq -1 ]
		then
			dlgPID=`ps|grep -w 'GTKDIALOG_SPLASH'`
			kill $dlgPID 2>/dev/null
		else
			TmpFile=$BasePIDFile${pid}
			dlgPID="`cat $TmpFile`"
			kill $dlgPID 2>/dev/null
			rm -f $TmpFile
			exit
		fi
	elif [ $optarg == "FONTSIZE" ]
	then
		fontsize=$arg
		optarg=$dummy
	elif [ $optarg == "FONTSPEC" ]
	then
		N=$(echo $arg | sed -e 's/^.[^0-9]*//'| cut -b1-2| tr -dc '0-9')
		[ "$N" != "" ] || N=0
		fontsize="medium"
		[ $N -gt 12 ] && fontsize="large"
		[ $N -gt 14 ] && fontsize="x-large"
		optarg=$dummy
	else
		case $arg in
		-kill)
			optarg="KILL"
			;;
		-text)
			optarg="TEXT"
			;;
		-timeout)
			optarg="TIMEOUT"
			;;
		-bg)
			optarg="BG"
			;;
		-fg)
			optarg="FG"
			;;
		-icon)
			optarg="ICON"
			;;
		-fontsize)
			optarg="FONTSIZE"
			;;
		-font)
			optarg="FONTSPEC"
			;;
		-placement)
			optarg="PLACEMENT"
			;;
		-close)
			optarg="CLOSE"
			;;
		-deco)
			deco="true"
            [ ! "$close" ] && close="never"
            optarg="TITLE"
			;;
		-help | --help)
			echo -e $helptext
			exit
			;;
		esac
	fi
 done

[ ! "$close" ] && close='mouseover'

#process placement, default is center...
WINPLACE=''
WINTYPE=1
case $placement in
 mouse)
  GEOMPARAM=''
  WINPLACE='window-position="2"'
  WINTYPE=2
 ;;
 top|top-right)
  X=`expr $CENTERX - 200` #not accurate
  Y=0
  WINTYPE=10
 ;;
 top-left)
  X=0
  Y=0
  WINTYPE=10
 ;;
 bottom|bottom-right)
  X=`expr $CENTERX - 200` #not accurate
  Y=`expr $ROOTY - 70`
  WINTYPE=10
 ;;
 bottom-left)
  X=0
  Y=`expr $ROOTY - 70`
  WINTYPE=10
 ;;
esac

#process close of window, default is mouseover...
CLOSETAG1=''
CLOSETAG2='<action signal="enter-notify-event" type="exit">Exit on mouse-over</action>'
case $close in
 never)
  CLOSETAG2=''
 ;;
 box)
  CLOSETAG2=''
  CLOSETAG1='<vbox><button><input file icon="mini-cross"></input><action type="exit">Exit on click close-box</action></button></vbox>'
 ;;
esac

gtkrc="style \"gtkdialog-splash\"
{
	bg[NORMAL]		= \"$bg\"
	fg[NORMAL]		= \"$fg\"
}
class \"*\" style \"gtkdialog-splash\""

echo $gtkrc > /tmp/gtkdialog-splash_gtkrc

export GTK2_RC_FILES=/tmp/gtkdialog-splash_gtkrc:/root/.gtkrc-2.0

#      <action signal=\"button-press-event\" type=\"exit\">Exit on mouse click</action>

export GTKDIALOG_SPLASH="	
<window title=\"${title}\" icon-name=\"gtk-preferences\" resizable=\"false\" decorated=\"${deco}\" ${WINPLACE}>
      <hbox>
        ${icon}
        <text use-markup=\"true\">
          <label>\"<b><span size='${fontsize}'>${text}</span></b>\"</label>
        </text>
        ${CLOSETAG1}
      </hbox>
      ${CLOSETAG2}
</window>"

case $WINTYPE in
 2) #mouse
  [ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH #to keep same pid.
  /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH &
  ;;
 10)
  [ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH --geometry=+"${X}"+"${Y}" #to keep same pid.
  /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH --geometry=+"${X}"+"${Y}" &
 ;;
 *) #center
  [ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH --center #to keep same pid.
  /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH --center &
 ;;
esac
dlgPID=$!

pidPATTERN="^${dlgPID} "
while [ $timeout -ne 0 ];do #100604
 sleep 1
 ALLPS="`ps`"
 timeout=`expr $timeout - 1`
 [ "`echo "$ALLPS" | sed -e 's%^ *%%' | grep "$pidPATTERN"`" == "" ] && exit #already killed.
done
kill $dlgPID
echo 'EXIT="Exit on timeout"'

###END###
