#!/bin/ash

#Initially written by mave, june 2010 as replacement for yaf-splash - under gpl license.
#Changed to a stand-alone script - Argolance 181011

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="******************************************************************************\n\t\t\t\tgtk-splash\n******************************************************************************\n
usage: gtk-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), OR\n
	-icon <full path> (full path to an icon, ex: /usr/share/doc/puppylogo48.png)\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=\$!)"
	
params=$@
	
if [ ${#params} -eq 0 ]; then	# no options, no text, so print info/help...
	echo -e $helptext
	exit
fi

BasePIDFile="/tmp/GTK-SPLASH"
dummy=SECONDS
text=""
timeout=0
bg="grey"	
fg="black"
icon=""
fontsize="medium" # small, medium, large, x-large
pid=""
placement='center'
deco='false'
title='gtk-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
		if [ "`echo -n "$arg" | grep '^/'`" = "" ];then #111016
		 icon="<pixmap><input file stock=\"$arg\"></input></pixmap>"
        else
		 icon="<pixmap><input file>${arg}</input></pixmap>"
        fi
		optarg=$dummy
	elif [ $optarg == "KILL" ]
	then
		pid=$arg
		if [ ${#pid} -eq -1 ]
		then
			dlgPID=`ps|grep -w 'GTK_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
	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"
			;;
		-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

if [ ! -f /tmp/gtk-splash_gtkrc_${bg}_${fg} ];then
 gtkrc="style \"gtk-splash\"
{
	bg[NORMAL]		= \"$bg\"
	fg[NORMAL]		= \"$fg\"
}
class \"*\" style \"gtk-splash\""

 echo "$gtkrc" > /tmp/gtk-splash_gtkrc_${bg}_${fg}
fi

export GTK2_RC_FILES=/tmp/gtk-splash_gtkrc_${bg}_${fg}:/root/.gtkrc-2.0

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

case $WINTYPE in
 2) #mouse
  [ ${timeout} -eq 0 ] && exec gtkdialog --class="gtk-splash" --program=GTK_SPLASH #to keep same pid.
  gtkdialog --class="gtk-splash" --program=GTK_SPLASH &
  ;;
 10)
  [ ${timeout} -eq 0 ] && exec gtkdialog --class="gtk-splash" --program=GTK_SPLASH --geometry=+"${X}"+"${Y}" #to keep same pid.
  gtkdialog --class="gtk-splash" --program=GTK_SPLASH --geometry=+"${X}"+"${Y}" &
 ;;
 *) #center
  [ ${timeout} -eq 0 ] && exec gtkdialog --class="gtk-splash" --program=GTK_SPLASH --center #to keep same pid.
  gtkdialog --class="gtk-splash" --program=GTK_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###
