#!/bin/bash

readonly RC=${RC:=${HOME}/.piedockrc}
readonly ICON_PATH=${ICON_PATH:=${HOME}/.piedock/icons}

# abort if there's already a config file
[ -f "$RC" ] && exit

# check for compositing
COMPOSITING="#compositing 1"
which pgrep &>/dev/null && {
	for M in compiz xcompmgr
	do
		pgrep $M &>/dev/null || continue

		COMPOSITING=${COMPOSITING:1}
		break
	done
}

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

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

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

# search for applications that may have good icons
ICONS=""
{
	APPS="terminal \
		konsole \
		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"

	SVGS=/usr/share/icons/hicolor/scalable/apps/
	[ -d "$SVGS" ] &&
		which convert &>/dev/null &&
		HAVE_CONVERT=1

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

	for A in $APPS
	do
		which $A &>/dev/null || continue

		FOUND=0
		for P in "/usr/share/icons/default.kde4/128x128/apps" "/usr/share/icons/hicolor/128x128/apps" "/usr/share/icons/gnome/256x256/apps"
		do
			stat $P/$A &>/dev/null || continue

			FOUND=1
			break
		done
		(( FOUND )) && continue

		(( HAVE_CONVERT )) && {
			for F in ${SVGS}${A}*.svg
			do
				stat "$F" &>/dev/null && {
					convert -density 128 -background transparent "$F" "${ICON_PATH}/${A}.png" &>/dev/null
					break;
				}
			done
		}

		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
# have grabbed that key or button already.
trigger button Mod4 1
trigger button Mod4 4
trigger button Mod4 5

# 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
button 4 SpinNext
button 5 SpinPrevious

# 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 "$ICON_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 title "* OpenOffice.org Writer" oowriter
alias title "* OpenOffice.org Calc" oocalc
alias title "* OpenOffice.org Draw" oodraw
alias title "* OpenOffice.org Impress" ooimpress
alias class "Firefox" firefox
alias class "Thunderbird" thunderbird
alias class "Chrome" chromium
alias class "NEdit" nedit
alias class "Xpdf" xpdf
alias class "MPlayer" mplayer
alias class "Gimp" gimp
alias "aterm" terminal
alias "xterm" terminal
alias "xfce4-terminal" terminal
alias "gedit" text-editor
alias "Konsole" terminal
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

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