#!/usr/bin/env bash

readonly RC=${RC:=${HOME}/.piedockrc}
readonly APP_ICONS_PATH=${APP_ICONS_PATH:=${HOME}/.piedock/applications}
readonly ACTIONS_ICONS_PATH=${ACTIONS_ICONS_PATH:=${HOME}/.piedock/actions}

[ -f "$RC" ] && {
	echo 'setup skipped, there is already a configuration file' >&2
	exit
}

# check if compositing is available
COMPOSITING='#compositing 1'
{
	AVAILABLE=0

	# query configuration
	for C in \
		'qdbus org.kde.kwin /KWin compositingActive' \
		"gconftool-2 -g '/apps/metacity/general/compositing_manager'" \
		'xfconf-query --channel=xfwm4 --property=/general/use_compositing'
	do
		which ${C%% *} || continue

		read <<EOF
`$C`
EOF

		[ "$REPLY" == 'true' ] && {
			AVAILABLE=1
			break
		}
	done

	# check processes
	(( AVAILABLE )) || {
		which pgrep &&
			for P in compiz xcompmgr
			do
				pgrep $P || continue

				AVAILABLE=1
				break
			done
	}

	(( AVAILABLE )) && COMPOSITING=${COMPOSITING:1}
} &>/dev/null

# search for proper active indicator icon
INDICATOR=
[ -d /usr/share/icons/ ] && {
	CANDIDATE=

	for I in 'emblem-new.png'
	do
		for R in 48x48 32x32 16x16
		do
			CANDIDATE=`find /usr/share/icons/ -name $I | grep -m 1 $R`
			[ "$CANDIDATE" ] && break
		done
	done

	[ "$CANDIDATE" ] &&
		INDICATOR="active-indicator \"$CANDIDATE\" right bottom"
}

# copy action icons if available
{
	ACTIONS_SRC_PATH=${0%sh/setup}'res/window-control'

	[ -d "$ACTIONS_SRC_PATH" ] &&
		mkdir -p "$ACTIONS_ICONS_PATH" &&
		cp "$ACTIONS_SRC_PATH"/*.png "$ACTIONS_ICONS_PATH"
}

# search for some popular applications
ICONS=
{
	SVGS=/usr/share/icons/hicolor/scalable/apps/
	[ -d "$SVGS" ] &&
		which convert &>/dev/null &&
		HAVE_CONVERT=1

	[ -d "$APP_ICONS_PATH" ] ||
		mkdir -p "$APP_ICONS_PATH" ||
		HAVE_CONVERT=0

	for A in terminal \
		konsole \
		gnome-terminal \
		chromium \
		opera \
		firefox \
		thunderbird \
		evolution \
		gedit \
		gimp \
		inkscape \
		blender \
		nautilus \
		thunar \
		rekonq \
		dolphin \
		amarok \
		kopete \
		libreoffice \
		ooffice \
		abiword \
		gnumeric \
		exaile \
		skype \
		empathy \
		rhythmbox \
		banshee \
		anjuta \
		eclipse
	do
		which $A &>/dev/null || continue

		HAVE_ICON=0

		# check for SVG icon
		(( HAVE_CONVERT )) && {
			for F in ${SVGS}${A}*.svg
			do
				[ -r "$F" ] || continue

				convert -density 128 \
					-background transparent \
					"$F" \
					"${APP_ICONS_PATH}/${A}.png" &>/dev/null

				HAVE_ICON=1
				break
			done
		}

		# check for PNG icon
		(( HAVE_ICON )) ||
			for P in /usr/share/icons/default.kde4/128x128/apps \
				/usr/share/icons/hicolor/128x128/apps \
				/usr/share/icons/gnome/256x256/apps
			do
				[ -f "${P}/${A}.png" ] || continue

				HAVE_ICON=1
				break
			done

		(( HAVE_ICON )) || continue

		ICONS="${ICONS}	icon ${A}
"
	done
}

# now write the configuration file
cat <<EOF > $RC
# window size in pixels
# if you're running weak hardware, try to keep this low
# usage: size PIXELS
size 320

# observe Fitts' Law and make each pie-slice extend to the edge of the screen?
# see http://en.wikipedia.org/wiki/Fitts%27_law
# usage: fitts (0|1)
fitts 1

# use compositing manager for true transparency?
# usage: compositing (0|1)
$COMPOSITING

# do you want to have an icon title in the middle of the pie?
# usage: title (0|1)
title 1

# what font do you want to use?
# usage: font FAMILY SIZE [COLOR]
#font Sans 9.0 ffffff

# customize font cartouche
# usage: cartouche CORNER [ALPHA] [COLOR]
# CORNER 0..255 - corner radius in pixels
# ALPHA 0..255 - lower values mean higher transparency, higher values mean
# higher opacity
# COLOR - hex code of color
#cartouche 6 192 000000

# display workspace position for windows that are on another workspace
# usage: show-workspace no|WORKSPACE [WINDOW] [(horizontal|vertical|square)]
# WORKSPACE - hex code of ARGB color for the workspaces
# WINDOW - hex code of ARGB color for the window
#show-workspace 88323232 88888888 square

# zoom icons on mouse-over?
# usage: zoom VALUE
# VALUE can be any float from 0.0 (no zoom) - 1.0 (full zoom)
#zoom 1.0

# should clicking in the middle of the menu have any effect?
# usage: centre (Ignore|NearestIcon|Disappear)
#centre Disappear

# should triggers still work if some mask (like Num_Lock) is enabled?
# usage: ignore-mask KEY
ignore-mask Num_Lock

# activation keys and (pointer) buttons
# usage: trigger [menu NAME] (button[-up]|button-down|key) [MODIFIER] (KEY|BUTTON)
#
# If you get "X Error of failed request:  BadAccess", some other program
# has grabbed that key or button already.
trigger button Shift 4
trigger button Shift 5
trigger menu window-control button-down Shift 3

# button configuration or what the mouse-buttons do while the menu is open
# usage: button [BUTTON] [ACTION]
# available actions are:
#   Launch       - start the program identified by that icon
#   ShowNext     - show next window of icon, or launch if not running
#   ShowPrevious - show previous of icon,    or launch if not running
#   ShowWindows  - show menu of all windows, or launch if not running
#   Hide         - hide next window of icon  (minimize / iconify)
#   Close        - close next window of icon (might lose data!)
#   SpinUp       - spin menu up              (rotate)
#   SpinDown     - spin menu down            (rotate widdershins)
#   SpinNext     - spin to next icon         (rotate)
#   SpinPrevious - spin to previous icon     (rotate widdershins)
#   Disappear    - hide pie menu
button 1 ShowNext
button 2 ShowPrevious
button 3 Launch

# key configuration or what the keys on the keyboard do while the menu is open
# usage: key KEYSYM ACTION
# available actions: same as in button configuration
key Escape Disappear
key Return ShowNext
key Up SpinNext
key Down SpinPrevious

# spin-step: how far we'll spin the menu on SpinUp/SpinDown
# usage: spin-step [-0.75 ... 0.75]
# default: 0.5 negative values reverse direction widdershins/deasil
#spin-step -0.5

# focused and unfocused alpha transparency
# usage: (focused|unfocused|launchers) VALUE
# VALUE 0..255 - lower values mean higher transparency, higher values mean
# higher opacity
#focused 255
#unfocused 64

# start radius of appearing animation
# if you're running weak hardware, you might want to disable this (set to 1.0)
# usage: start-radius VALUE
# VALUE can be any float from 0.0 (start form centre) - 1.0 (no start animation)
#start-radius .9

# should the be a minumum number of icons in a menu?
# usage: minimum-number VALUE
# VALUE can be any integer greater than 1
#minimum-number 6

# active indicator or what should mark an icon as having windows
# usage: active-indicator FILE [(left|center|right)] [(top|center|bottom)]
#active-indicator "path/to/active-indicator.png" right bottom
$INDICATOR

# what icon should be used for unassociated menu items/windows
# usage: missing-icon FILE
#missing-icon "path/to/missing-icon.png"

# what icon should be used to fill menu up to minimum-number (see above)
# usage: filler-icon FILE
#filler-icon "path/to/filler-icon.png"

# path to PNG files, the files must be named like the application
# usage: path PATH...
path "$APP_ICONS_PATH"
path "$ACTIONS_ICONS_PATH"
path "/usr/share/icons/default.kde4/128x128/apps"
path "/usr/share/icons/default.kde4/32x32/apps"
path "/usr/share/icons/hicolor/128x128/apps"
path "/usr/share/icons/hicolor/32x32/apps"
path "/usr/share/icons/gnome/256x256/apps"
path "/usr/share/icons/gnome/32x32/apps"
path "/usr/share/pixmaps"

# you may specify windows to ignore, those windows will never show up in
# the window list, find the window name with "utils/piedockutils -l"
# usage: ignore-window NAME
#ignore-window "gnome-panel"

# aliases
# tweak windows having weird class names to get the right icon
# usage: alias [(name|class|title)] VALUE APPLICATION
alias class "Opera" opera
alias class "Firefox" firefox
alias class "Thunderbird" thunderbird
alias class "Chrome" chromium
alias class "NEdit" nedit
alias class "Eclipse" eclipse
alias class "Xpdf" xpdf
alias class "MPlayer" mplayer
alias class "Gimp" gimp
alias class "VirtualBox" virtualbox
alias "aterm" terminal
alias "xterm" terminal
alias "xfce4-terminal" terminal
alias "gedit" text-editor
alias "Konsole" terminal
alias "Blender" blender
alias "Blender:Render" blender
alias "googleearth-bin" googleearth

# menu of sticky icons
# the asterisk (*) will expand the menu with unassigned windows
# usage:
# menu [NAME]
#   icon APPLICATION [COMMAND]
#   menu ICONNAME
#     icon APPLICATION [COMMAND]
#     ...
#   end
#   ...
#   *
# end
menu
$ICONS
	*
end

menu window-control
	icon close "piedockutils -c $WID"
	icon iconify "piedockutils -i $WID"
	icon fullscreen "piedockutils -f $WID"
	icon lower "piedockutils -o $WID"
	icon stick "piedockutils -s $WID"
	icon shade "piedockutils -d $WID"
end

# preload icons
# usage: preload [(menus|all|none)]
# NOTE: you really shouldn't choose "all" when using /usr/share/* icon paths!
preload menus
EOF
