#!/bin/sh
# UI
STYLE=false
DECO=false
MENU=false
STARTER=false
SESSION=false
SIDEBAR=false
quit=true

options=$(kdialog --title "Baghira component loader" --checklist "<qt>You're about to activate the several components of the baghira package<br>\"Bab\" is loaded automatically<br><b>Check the items you want to have</qt>" _style "The STYLE (application appereance)" on _deco "The DECORATION (window titlebar)" on _menu "An OS X like MENUBAR (Menu on top of screen /w app name)" on _starter "A STARTMENU REPLACEMENT (search driven ALI, skinnable button)" on _session "A SESSIONMANAGER applet (recommanded with the ALI)" on _sidebar "A Finder like SIDEBAR for Konqueror" on)

for tag in $options; do
case $tag in
   "\"_style\"")
   STYLE=true
   quit=false
   ;;
   "\"_deco\"")
   DECO=true
   quit=false
   ;;
   "\"_menu\"")
   MENU=true
   quit=false
   ;;
   "\"_starter\"")
   STARTER=true
   quit=false
   ;;
   "\"_session\"")
   SESSION=true
   quit=false
   ;;
   "\"_sidebar\"")
   SIDEBAR=true
   quit=false
   ;;
esac
done
if $quit; then exit 0; fi

# GENERAL
# create baghira folders (if necessary)
mkdir $HOME/.baghira 2>/dev/null
mkdir $HOME/.baghira/.bab 2>/dev/null
# start bab
if ! dcop bab > /dev/null 2>/dev/null; then
   echo -e "Starting bab...\n"
   bab&
else
   echo -e "Restarting bab...\n"
   dcop bab MainApplication-Interface quit
   bab&
fi
sleep 2 # time for bab to startup - we're gonna need it ;)
if $STYLE; then
   echo -n "Activating the Baghira style..."
   ##### STYLE #######
   # create backup of qtrc
   cp $HOME/.qt/qtrc $HOME/.qt/qtrc.bbu
   cp $HOME/.kde/share/config/kdeglobals $HOME/.kde/share/config/kdeglobals.bbu
   cp $HOME/.kde/share/config/kdesktoprc $HOME/.kde/share/config/kdesktoprc.bbu
   # modify qtrc (from backup file)
   sedString=""
   if $MENU; then sedString="s/macStyle=.*$/macStyle=true/"; fi
   sed -e "s/style=.*$/style=baghira/" $HOME/.qt/qtrc.bbu > $HOME/.qt/qtrc
   sed -e "s/widgetStyle=.*$/widgetStyle=baghira/" -e "$sedString" $HOME/.kde/share/config/kdeglobals.bbu > $HOME/.kde/share/config/kdeglobals
   sed -e "s/ShowMenubar=.*$/ShowMenubar=false/" -e $sedString $HOME/.kde/share/config/kdesktoprc.bbu > $HOME/.kde/share/config/kdesktoprc
   dcop bab default emitStyleChanged
   echo "done"
fi

if $DECO; then
   echo -n "Activating the Baghira Window decoration..."
   ##### DECO ########
   # create backup of kwinrc
   if [ -f $HOME/.kde/share/config/kwinrc ]; then
      cp $HOME/.kde/share/config/kwinrc $HOME/.kde/share/config/kwinrc.bbu
   else
      cp $(kde-config --prefix)/share/config/kwinrc $HOME/.kde/share/config/kwinrc.bbu
   fi
   # modify kwinrc (from backup file)
   if grep ButtonsOnLeft $HOME/.kde/share/config/kwinrc.bbu > /dev/null; then
      sedString1="s/ButtonsOnLeft=.*$/ButtonsOnLeft=XIA/"
   else
      sedString1="/[Style]$/{x;s/$/ButtonsOnLeft=XIA/;G;}"
   fi
   if grep ButtonsOnRight $HOME/.kde/share/config/kwinrc.bbu > /dev/null; then
      sedString2="s/ButtonsOnRight=.*$/ButtonsOnRight=HM/"
   else
      sedString2="/[Style]$/{x;s/$/ButtonsOnRight=HM/;G;}"
   fi
   if grep CustomButtonPositions $HOME/.kde/share/config/kwinrc.bbu > /dev/null; then
      sedString3="s/CustomButtonPositions=.*$/CustomButtonPositions=true/"
   else
      sedString3="/[Style]$/{x;s/$/CustomButtonPositions=true/;G;}"
   fi
   sed -e "s/PluginLib=.*$/PluginLib=kwin3_baghira/" -e "$sedString1" -e "$sedString2" -e "$sedString3" $HOME/.kde/share/config/kwinrc.bbu > $HOME/.kde/share/config/kwinrc
   # reconfigure kwin
   dcop kwin default reconfigure
   echo "done"
fi

##### KICKER ######
if $MENU; then
   # create backup of menuapplet.desktop
   if [ -f $HOME/.kde/share/apps/kicker/applets/menuapplet.desktop ]; then
      cp $HOME/.kde/share/apps/kicker/applets/menuapplet.desktop $HOME/.kde/share/apps/kicker/applets/menuapplet.desktop.bbu
   else
      mkdir $HOME/.kde/share/apps/kicker 2>/dev/null
      mkdir $HOME/.kde/share/apps/kicker/applets 2>/dev/null
      cp $(kde-config --prefix)/share/apps/kicker/applets/menuapplet.desktop $HOME/.kde/share/apps/kicker/applets/menuapplet.desktop.bbu
   fi
   # modify menuapplet.desktop (from backup file)
   sed -e "s/X-KDE-Library=.*$/X-KDE-Library=b_menu_panelapplet/" $HOME/.kde/share/apps/kicker/applets/menuapplet.desktop.bbu > $HOME/.kde/share/apps/kicker/applets/menuapplet.desktop
   
   # create backup of kicker_menubarpanelrc
   cp $HOME/.kde/share/config/kicker_menubarpanelrc $HOME/.kde/share/config/kicker_menubarpanelrc.bbu
   # write osx alike kicker_menubarpanelrc
   echo -e \
"[Applet_1]
ConfigFile=b_menu_panelappletrc
DesktopFile=menuapplet.desktop
FreeSpace2=0
WidthForHeightHint=568

[Applet_2]
ConfigFile=systemtray_panelappletrc
DesktopFile=systemtrayapplet.desktop
FreeSpace2=1
WidthForHeightHint=244

[Applet_3]
ConfigFile=clock_panelapplet_liqgak5bnrdv0p4zggjd_rc
DesktopFile=clockapplet.desktop
FreeSpace2=1
WidthForHeightHint=82
" > $HOME/.kde/share/config/kicker_menubarpanelrc
   if $STARTER; then
   echo -e \
"[Applet_4]
ConfigFile=libbaghirastarterrc
DesktopFile=starter.desktop
FreeSpace2=0
WidthForHeightHint=55
" >> $HOME/.kde/share/config/kicker_menubarpanelrc
   fi
   if $SESSION; then
   echo -e \
"[Applet_5]
ConfigFile=usermanager_panelappletrc
DesktopFile=usermanager.desktop
FreeSpace2=1
WidthForHeightHint=110
" >> $HOME/.kde/share/config/kicker_menubarpanelrc
   fi

   APPLETSTRING=""
   if $STARTER; then APPLETSTRING="Applet_4,"; fi
   APPLETSTRING=$APPLETSTRING"Applet_1,Applet_2,Applet_3"
   if $SESSION; then APPLETSTRING=$APPLETSTRING",Applet_5"; fi
   APPLETSTRING=$APPLETSTRING",ExtensionButton_1"

   echo -e \
"[ExtensionButton_1]
DesktopFile=find.desktop
FreeSpace2=1

[General]
Alignment=0
Applets2=$APPLETSTRING
AutoHideDelay=3
AutoHidePanel=false
AutoHideSwitch=false
BackgroundHide=false
CustomSize=24
ExpandSize=true
HideAnimation=true
HideAnimationSpeed=40
HideButtonSize=14
IExist=true
Position=2
ShowLeftHideButton=false
ShowRightHideButton=false
Size=2
SizePercentage=100
UnhideLocation=6
XineramaScreen=-2
" >> $HOME/.kde/share/config/kicker_menubarpanelrc
fi
#restart kicker
if $MENU; then
   dcop kicker default restart
fi

if $SIDEBAR; then
   echo -n "Activating the Baghira Konqueror sidebar..."
   ##### SIDEBAR ######
   # create backup of menuapplet.desktop
   if [ -f $HOME/.kde/share/config/konqsidebartng.rc ]; then
      cp $HOME/.kde/share/config/konqsidebartng.rc $HOME/.kde/share/config/konqsidebartng.rc.bbu
   else
      cp $(kde-config --prefix)/share/config/konqsidebartng.rc $HOME/.kde/share/config/konqsidebartng.rc.bbu
   fi
   # modify menuapplet.desktop (from backup file)
   sed -e "s/OpenViews=.*$/OpenViews=baghirasidebar.desktop/" $HOME/.kde/share/config/konqsidebartng.rc.bbu > $HOME/.kde/share/config/konqsidebartng.rc
   # create a desktop entry (we could copy, but hey - this is more safe ;)
   mkdir $HOME/.kde/share/apps/konqsidebartng 2>/dev/null
   mkdir $HOME/.kde/share/apps/konqsidebartng/entries 2>/dev/null
   echo -e \
"[Desktop Entry]
Encoding=UTF-8
Type=Link
URL=
Icon=bab_brushed
Comment=Baghira Sidebar
Name=baghirasidebar
Name[xx]=xxbaghirasidebarxx

Open=false
X-KDE-KonqSidebarModule=konqsidebar_baghirasidebar" > $HOME/.kde/share/apps/konqsidebartng/entries/baghirasidebar.desktop
echo "done (start a new konqueror instance)"
fi

echo -e "=================================================\nDone.\n\nThe new icon in your System Tray is from \"bab\".\n\nRightclick: configure Baghira\n-----------\nLeftclick: instantly switch between the different visual generations\nJaguar (blue), Panther (black), Brushed Metal (grey) and Tiger (yellow)\n------------------------"
