#!/bin/bash

# Script for the 'desktop-manager' subproject of Cairo-Dock
#
# Copyright : (C) see the 'copyright' file.
# E-mail    : see the 'copyright' file.
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/licenses.html#GPL

# This script removes Unity Compiz plugin and launches Cairo-Dock with a short delay

if [ "$1" = "--replace" ]; then
	UNITY_NAME="unityshell"
	COMPIZ_FLAT_FILE="$HOME/.config/compiz-1/compizconfig/Default.ini"
	COMPIZ_GCONF="/apps/compiz-1/general/screen0/options/active_plugins"
	if test -d "$HOME/.config/compiz-1"; then # compiz >= 0.9
		# plug-ins in double are NO LONGER filtered by Compiz in this version... (and if plugins are in double or wrong, compiz crashes :) )
		# flat file
		if test -f "$COMPIZ_FLAT_FILE"; then
			pluginsFlat=`grep "s0_active_plugins" $COMPIZ_FLAT_FILE`
			if test `echo $pluginsFlat | grep -c $UNITY_NAME` -gt 0; then
				pluginsFlatWithoutUnity=`echo $pluginsFlat | sed -e "s/$UNITY_NAME;//g"`
				sed -i "/s0_active_plugins/ s/$pluginsFlat/$pluginsFlatWithoutUnity/g" $COMPIZ_FLAT_FILE
				killall unity-panel-service
			fi
		fi
		# gconf
		plugins=`gconftool-2 -g $COMPIZ_GCONF`
		if test `echo $plugins | grep -c $UNITY_NAME` -gt 0; then
			pluginsWithoutUnity=`echo $plugins | sed -e "s/$UNITY_NAME,//g"`
			gconftool-2 -s $COMPIZ_GCONF --type=list --list-type=string "$pluginsWithoutUnity"
			killall unity-panel-service
		fi
	fi
fi

if test `ps -U $USER -u $USER u | grep -c " [c]airo-dock"` -eq 0; then # cairo-dock not launched
	cairo-dock -w 3
fi
