#!/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.
#111023 01micko, add user icon to ICON arg
#120510 zigbert: added '-border', '-bg_gradient' and '-margin'
#130212 zigbert: Fix placement: top-right, bottoms, center. Added -font, -align and -icon_width
#130623 zigbert: Fix text color

#ignore scripts that do 'killall gtkdialog3'...
GTKDIALOG4="`which gtkdialog4`"
[ ! "$GTKDIALOG4" ] && GTKDIALOG4="`which gtkdialog`"
[ ! "$GTKDIALOG4" ] && exit
[ ! -f /tmp/yaf-splash ] && ln -s $GTKDIALOG4 /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'`

MAX_CHAR_WIDTH=40 #Maximum number of chars in one line
MARGIN_DESKTOP=40 #WM bars could conflict if pushing splashes all into the corner

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
	OR path/to/pixmap.png|gif|jpeg \n
	-icon_width WIDTH in pixels\n
	-bg COLOR (background color red, blue, yellow..., default: grey)\n
	-bg_gradient true|false (default: true)\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
	-font NAME\n
	-fontsize SIZE\n
	-align ALIGNMENT left, right or center
	-margin SIZE (default: 10)\n
	-border true|false (default: true)\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"
bg_gradient=true
fg="black"
icon=""
font="DejaVu bold"
fontsize="10"
margin=10
border=true
pid=""
placement='center'
deco='false'
title='gtkdialog-splash'
ICON_WIDTH=32
ALIGN=0 #left

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 == "BG_GRADIENT" ]
	then
		bg_gradient=$arg
		optarg=$dummy
	elif [ $optarg == "FG" ]
	then
		fg=$arg
		optarg=$dummy
	elif [ $optarg == "ICON" ]
	then
		icon="<pixmap width-request=\"$ICON_WIDTH\" space-expand=\"false\" space-fill=\"false\">
          <width>$ICON_WIDTH</width>
          <input file stock=\"$arg\"></input>
        </pixmap>"
        [ "`echo $arg | grep '^gtk'`" = "" ]&& icon="<pixmap width-request=\"$ICON_WIDTH\" space-expand=\"false\" space-fill=\"false\"> 
          <width>$ICON_WIDTH</width>
          <input file>\"$arg\"</input>
        </pixmap>" #111023
		optarg=$dummy
	elif [ $optarg == "ICON_WIDTH" ]
	then
		ICON_WIDTH=$arg
		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
		[ $arg = small ] && fontsize="8"
		[ $arg = medium ] && fontsize="10"
		[ $arg = large ] && fontsize="12"
		[ $arg = x-large ] && fontsize="14"
		optarg=$dummy
	elif [ $optarg == "ALIGN" ]
	then
		case $arg in
			right) ALIGN=1 ;;
			center) ALIGN=2 ;;
			*) ALIGN=0 ;; #left
		esac
		optarg=$dummy
	elif [ $optarg == "MARGIN" ]
	then
		margin=$arg
		optarg=$dummy
	elif [ $optarg == "BORDER" ]
	then
		border=$arg
		[ $border = false ] && notebook_border='show-border="false"'
		optarg=$dummy
	elif [ $optarg == "FONTSPEC" ]
	then
		N=$(echo $arg | sed -e 's/^.[^0-9]*//'| cut -b1-2| tr -dc '0-9')
		[ "$N" ] && fontsize=''
		font="$arg"
		optarg=$dummy
	else
		case $arg in
		-kill)
			optarg="KILL"
			;;
		-text)
			optarg="TEXT"
			;;
		-timeout)
			optarg="TIMEOUT"
			;;
		-bg)
			optarg="BG"
			;;
		-bg_gradient)
			optarg="BG_GRADIENT"
			;;
		-fg)
			optarg="FG"
			;;
		-icon)
			optarg="ICON"
			;;
		-icon_width)
			optarg="ICON_WIDTH"
			;;
		-fontsize)
			optarg="FONTSIZE"
			;;
		-margin)
			optarg="MARGIN"
			;;
		-border)
			optarg="BORDER"
			;;
		-font)
			optarg="FONTSPEC"
			;;
		-align)
			optarg="ALIGN"
			;;
		-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'
#define height and width of the window 
WIDTH=$(($WIDTH+$margin))
HEIGHT=$(($HEIGHT+$margin))
if [ "$border" = "true" ]; then
	WIDTH=$(($WIDTH+10))
	HEIGHT=$(($HEIGHT+10))
fi
NR_CHARS="`echo "$text" | wc -c`"
NR_LINES=`awk -v VAR1=$NR_CHARS -v VAR2=$MAX_CHAR_WIDTH 'BEGIN{print int((VAR1/VAR2)+1)}'`
if [ $NR_CHARS -gt $MAX_CHAR_WIDTH ]; then
	CHAR_WIDTH=$MAX_CHAR_WIDTH
else
	CHAR_WIDTH=$NR_CHARS
fi
TMP=`awk -v VAR1=$CHAR_WIDTH -v VAR2=$fontsize 'BEGIN{print int(VAR1*(VAR2*1.0))}'`
WIDTH=$(($WIDTH+$TMP))
TMP=`awk -v VAR1=$NR_LINES -v VAR2=$fontsize 'BEGIN{print int(VAR1*(VAR2*2.0))}'`
HEIGHT=$(($HEIGHT+$TMP))
[ $NR_LINES -gt 1 ] && HEIGHT=$(($HEIGHT+$fontsize)) #gtkdialog <text> widgets does not has such accurate wrapping as we calculate. It wraps words, not chars
if [ "$icon" ]; then
	WIDTH=$(($WIDTH+$ICON_WIDTH+10))
	[ $HEIGHT -lt 62 ] && HEIGHT=62
fi

#process placement, default is center...
WINPLACE=''
WINTYPE=1
case $placement in
 mouse)
  GEOMPARAM=''
  WINPLACE='window-position="2"'
  WINTYPE=2
 ;;
 top)
  X=$((($ROOTX/2)-($WIDTH/2)))
  Y=$MARGIN_DESKTOP
  WINTYPE=10
 ;;
 top-right)
  X=$(($ROOTX-$WIDTH-$MARGIN_DESKTOP))
  Y=$MARGIN_DESKTOP
  WINTYPE=10
 ;;
 top-left)
  X=$MARGIN_DESKTOP
  Y=$MARGIN_DESKTOP
  WINTYPE=10
 ;;
 bottom)
  X=$((($ROOTX/2)-($WIDTH/2)))
  Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP))
  WINTYPE=10
 ;;
 bottom-right)
  X=$(($ROOTX-$WIDTH-$MARGIN_DESKTOP))
  Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP))
  WINTYPE=10
 ;;
 bottom-left)
  X=$MARGIN_DESKTOP
  Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP))
  WINTYPE=10
 ;;
esac

#set backgound
if [ $bg_gradient = true ]; then
	#build svg-background image
	echo '
	<svg version="1.1" width="1000" height="1000" id="svg1">
	  <defs
		id="defs4">
		<linearGradient id="LGD_01">
		  <stop style="stop-color:'$bg';stop-opacity:1" offset="0" />
		  <stop style="stop-color:'$bg';stop-opacity:0.1" offset="1" />
		</linearGradient>
		<linearGradient id="LG_01"
		  x1="7" y1="80" x2="0" y2="0"	  
		  xlink:href="#LGD_01"
		  gradientUnits="userSpaceOnUse" />
	  </defs>
	  <rect
		style="fill:url(#LG_01);fill-opacity:1;stroke-width:0"
		width="1000" height="1000"/>
	</svg>' > /tmp/gtkdialog_splash_bg.svg #full opacity
	echo '
	<svg version="1.1" width="1000" height="1000" id="svg1">
	  <defs
		id="defs4">
		<linearGradient id="LGD_01">
		  <stop style="stop-color:'$bg';stop-opacity:1" offset="0" />
		  <stop style="stop-color:'$bg';stop-opacity:0.1" offset="1" />
		</linearGradient>
		<linearGradient id="LG_01"
		  x1="7" y1="80" x2="0" y2="0"	  
		  xlink:href="#LGD_01"
		  gradientUnits="userSpaceOnUse" />
	  </defs>
	  <rect
		style="fill:url(#LG_01);fill-opacity:0.5;stroke-width:0"
		width="1000" height="1000"/>
	</svg>' > /tmp/gtkdialog_splash_border.svg #a bit transparent
	if [ $border = false ]; then
		gtk_border='bg_pixmap[NORMAL] = "gtkdialog_splash_bg.svg"'
	else
		gtk_border='bg_pixmap[NORMAL] = "gtkdialog_splash_border.svg"'
		gtk_bg='bg_pixmap[NORMAL] = "gtkdialog_splash_bg.svg"'
	fi
else
	gtk_bg="bg[NORMAL] = \"$bg\""
	gtk_border="bg[NORMAL] = \"$bg\""
fi

#gtk-theme
gtkrc='
pixmap_path "/tmp"
style "gtkdialog-splash"
{
	'"$gtk_border"'
}
class "GtkWindow" style "gtkdialog-splash"

style "notebook"
{
	'"$gtk_bg"'
	xthickness	= '"$(($margin+5))"'
	ythickness	= '$margin'
}
class "GtkNotebook" style "notebook"

style "font"
{
	font_name="'"$font"' '"$fontsize"'"
	text[NORMAL]="'$fg'"
	fg[NORMAL]="'$fg'"
}
class "GtkLabel" style "font"'
echo $gtkrc > /tmp/gtkdialog-splash_gtkrc

#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

export GTK2_RC_FILES=/tmp/gtkdialog-splash_gtkrc

#      <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}'>
 <vbox>
  <notebook show-tabs="false" '"$notebook_border"' space-expand="true" space-fill="true">
   <hbox>
    <vbox>
     '${icon}'
     <text space-expand="true" space-fill="true"><label>""</label></text>
    </vbox> 
    <text justify="'${ALIGN}'" width-chars="'${CHAR_WIDTH}'" space-expand="true" space-fill="true">
 	 <label>"'${text}'"</label>
    </text>
    '${CLOSETAG1}'
    </hbox>
   </notebook>
  </vbox>
 '${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="$WIDTH"x"$HEIGHT"+"${X}"+"${Y}" #to keep same pid.
  /tmp/yaf-splash --class="gtkdialog-splash" --program=GTKDIALOG_SPLASH --geometry="$WIDTH"x"$HEIGHT"+"${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###
