#!/bin/sh

application=`basename 0`
appdir="/usr/share/3desktopman"
profilepath="$appdir"
privdir="$HOME/.config/compiz/compizconfig"

title="3Desktop Manager"
msg="3D Effects Manager \n\nSee bellow availabel actions. \nPlease select a action from list \nand press [ OK ] button \n"
sel="Sel."
now="Now set"
numers="No."
action="Actions list"
addaprofile=" - Add a new profile."
addprofilemsg="Add new profile? Please select a profile file to install as privat profile"
inprivdir="###### Profiles in MyDir ######"
insysdir="###### Profiles in SysDir ######"
detector="Run DRI Detector Manager"
manager="Effects Profil Manager"
disable="Disable 3Detect Service"
enable="Enable 3Detect Service"
profiledialog="Available Profiles."
applyactual="Now used new selected profile!"
emptytext="Please select a item from list and press [ OK ] button"
conferrmsg="The config file corrupt, now try fixating. The correct value in config file this: \"profile = 3Desktopman\". If not applied selected profile, please check in privat config file here: "
existmsg="Already exist a this name ini file in home config directory! Here: \n"
profilexistmsg="Already exist a this name ini file in home config directory! Renamed now. New name: \n"
existmsg2=" \nPlease rename file or select another profile!"
nonemsg="* not available profiles here *"

case $LANGUAGE in
	hu* )	

	    title="3Desktop Manager"
	    msg="3D Effect Manager \n\nItt lthatja az elrhet mveleteket. \nKrem vlasszon egyet majd nyomja meg az \n[ OK ] gombot.. \n"
	    sel="Sel."
	    now="Most beállított"
	    numers="No."
	    action="Mveletek listja"
	    addaprofile=" - Adjon hozza egy profilt"
	    addprofilemsg="j profil hozzadsa? Vlasszon ki egy profil fjlt, hogy telepthesse mint privt profil.."
	    inprivdir="##### Profilok a saját mappaban ######"
	    insysdir="###### Profilok a sys mappában #####"
	    detector="DRI Detector futtatása"
	    manager="Effekt Profilok kezelése"
	    disable="3Detect kikapcsolása"
	    enable="3Detect bekapcsolása"
	    profiledialog="Elérhető profilok."
	    applyactual="Most az j kivlasztott profilt hasznlja!"
	    emptytext="Krem vlasszon egy elemet a listbl s kattintson az [ OK ] gombra"
	    conferrmsg="A konfigurcis fjl rvnytelen, megprblom javtani. A helyes rtk a fjlban ez: \"profile = 3Desktopman\". Ha nem sikerl alkalmazni a profilt, krem ellenrize a sajt konfig fjlt itt: "
	    existmsg="Ilyen nven mr ltezik egy ini fjl a home konfigurcis knyvtrban! Itt: \n"
	    existmsg2=" \nNevezze t a fjlt vagy vlasszon msik profilt!"
	    nonemsg="--- nincsenek elerheto profilok itt ---"
esac

suapp=`which gksu 2>/dev/null || which gsu 2>/dev/null || which kdesu 2>/dev/null || echo "Error1"`

if [ "$suapp" = Error1 ]
then 
    zenity --error --text "SU APPLICATION NOT FOUND IN SYSTEM! PLEASE INSTALL!"
    echo "[Warning! The SU manager not found. Please check your installation]"
    exit 
fi

mainq() {
ekson=`zenity --title "$title" --list --radiolist --text "$msg" --column "$sel" --column "$numers" --column "$action" \
FALSE "1" "$detector" \
FALSE "2" "$manager" \
FALSE "3" "$disable" \
FALSE "4" "$enable" --width 350 --height 300 2>/dev/null`
ret=$?
echo ""
test "$ret" -ne 0 && exit

if [ "$ekson" = "" ];then
    emptymsg
fi
command
}

command() {
if [ "$ekson" = "1" ];then
    3detect
elif [ "$ekson" = "2" ];then
    profilist
elif [ "$ekson" = "3" ];then
    $suapp "3detect --disable"
elif [ "$ekson" = "4" ];then
    $suapp "3detect --enable"
fi

}

emptymsg() {
zenity --error --text "$emptytext"
mainq
}

addprofile() {
zenity --info --text "$addprofilemsg"
selectini=`zenity --file-selection`
cd "$privdir"
inichk=`ls "$selectini"`
if [ -n "$inichk" ];then
    newname="3D_$RANDOM.ini"
    zenity --error --text "$profilexistmsg [$newname]"
    cp $selectini $privdir/$newname
    profilist
else
    if [ "$selectini" = "" ];then
	profilist
    fi
    cp $selectini $privdir/

fi

}
profilist() {
cd $privdir/
profile=`find . -type 'f' -name '*.ini' | sed 's|.ini||g' | sed 's|./||' | sed 's|^|\[# |g'| sort` #| zenity --list --column "$profiledialog"
profile2=`ls $profilepath/ | sed 's|.ini||g' | sed 's|^|\]# |g'| sort` #| zenity --list --column "$profiledialog"`
actual=`cat $privdir/config | grep profile | head -n 1 | awk -F= '{ print $2 }'`

if [ ! -n "$profile" ];then
    profile="$nonemsg"
fi

echo "[$inprivdir]" > /tmp/list
echo "$profile" >> /tmp/list
echo "$addaprofile" >> /tmp/list
echo "[$insysdir]" >> /tmp/list
echo "$profile2" >> /tmp/list
echo ""
list=`cat /tmp/list | zenity --title Profiles --list --column " $now:[$actual ] $profiledialog" --width 350 --height 300`
ret=$?
test "$ret" -ne 0 && mainq

if [ "$list" = "" ];then
    emptymsg
    profilist
 else
    if [ -n $(echo $list| grep '####') ];then
	echo ".."
	else
	profilist
	exit
    fi
fi

if [ "$list" = "$nonemsg" -o "$list" = "$addaprofile" ];then
    addprofile
    exit
fi
if [ -n `echo $list | grep ^]` ];then
    linkpath="$privdir"
    profinal=`echo $list | sed 's|\[# ||g'`
    confedit
 else
    linkpath="$profilepath"
    profinal=`echo $list | sed 's|\]# ||g'`
    existchk
fi
#echo "debug:$ret"
profilist
#zenity --error "Any error found, sorry"

}

confchk() {
conf=`cat $privdir/config | grep profile | head -n 1 | grep 3Desk`
if [ ! -n "$conf" ];then
    zenity --error --text "$conferrmsg \n$privdir/config"
    linking
    #kwin --replace &
    #killall compiz -KILL
    #killall emerald -KILL
    sleep 2
    cat $privdir/config | sed 's|profile =\(.*\).*|profile = 3Desktopman|' > $privdir/config
    sleep 2
    compiz-fusion --replace &
fi
#linking
}

existchk() {
exist=`find $privdir/ -type f -name $profinal.ini`

if [ -n "$exist" ];then
    zenity --error --text "$existmsg [$privdir] $existmsg2"
    profilist
    #exit
else
    linking
fi

}

linking() {
ln -sf $linkpath/$profinal.ini $privdir/$profinal.ini
confedit
}

confedit() {
cat << EOF > $privdir/config
[kde_session]
backend = ini
profile = $profinal
plugin_list_autosort = true

[gnome_session]
backend = ini
profile = $profinal
plugin_list_autosort = true

[general]
backend = ini
profile = $profinal
integration = true
EOF

}

ended() {
zenity --info --text "$applyactual ($profinal)"
mainq

}

mainq
ret=$?
echo "debug:$ret"
ended
