#!/bin/sh
#
# Quick configurator for controlling IceWM preferences
# Written for Vector Linux Light 7.0 by nightflier

# initial values
mprefs="$HOME/.icewm/preferences"
tprefs="$HOME/.icewm/toolbar"
startf="$HOME/.icewm/startup"
if [ "$(grep ShowWorkspaces=0 $mprefs)" ];then wkspnm=1;else wkspnm="$(grep WorkspaceNames $mprefs | grep [0-9] -o | wc -l)";fi
# show tips if set to do so
if [ "$(grep ^QuickConfigShowTips=1 $mprefs)" ];then
 yad --text-info --title=QuickTips --filename=/usr/share/icewm/quicktips.txt --geometry=600x350+100+50 --button="gtk-ok:0"
fi

# display gui using yad
OPTS=$(
yad --title="IceQuickConfig" --text="  Set IceWM preferences. \n  Log out and back in \n  to activate changes." --form \
--field="Desktop Icons and Wallpaper:CHK" \
--field="Show Desktop Button:CHK" \
--field="Window List:CHK" \
--field="Terminal:CHK" \
--field="File Manager:CHK" \
--field="Browser:CHK" \
--field="Volume Control:CHK" \
--field="Network Manager:CHK" \
--field="Network Monitor:CHK" \
--field="CPU Monitor:CHK" \
--field="Clock:CHK" \
--field="Show tips:CHK" \
--field="Number of Workspaces:NUM" \
$(if [ "$(grep ^pcmanfm $startf)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^TaskBarShowShowDesktopButton=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^TaskBarShowWindowListMenu=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi)  \
$(if [ "$(grep "^prog Terminal" $tprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep "^prog \"File Manager\"" $tprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep "^prog Browser" $tprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^obmixer $startf)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^wicd-client $startf)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^TaskBarShowNetStatus=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^TaskBarShowCPUStatus=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^TaskBarShowClock=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^QuickConfigShowTips=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$wkspnm!1..4!1 \
--button="gtk-cancel:1" --button="gtk-ok:0"
)

# Bail out if cancel
if [ "$OPTS" = "" ];then echo Cancel;exit;fi

# Set selected options
if [ "$(echo "$OPTS" | cut -d "|" -f1)" = TRUE ];then sed -i "s/^#pcmanfm/pcmanfm/" $startf;else sed -i "s/^pcmanfm/#pcmanfm/" $startf;fi
if [ "$(echo "$OPTS" | cut -d "|" -f2)" = TRUE ];then sed -i 's/ShowDesktopButton=0/ShowDesktopButton=1/' $mprefs;else sed -i 's/ShowDesktopButton=1/ShowDesktopButton=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f3)" = TRUE ];then sed -i 's/ShowWindowListMenu=0/ShowWindowListMenu=1/' $mprefs;else sed -i 's/ShowWindowListMenu=1/ShowWindowListMenu=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f4)" = TRUE ];then sed -i "s/^#prog Terminal/prog Terminal/" $tprefs;else  sed -i "s/^prog Terminal/#prog Terminal/" $tprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f5)" = TRUE ];then sed -i "s/^#prog \"File Manager\"/prog \"File Manager\"/" $tprefs;else  sed -i "s/^prog \"File Manager\"/#prog \"File Manager\"/" $tprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f6)" = TRUE ];then sed -i "s/^#prog Browser/prog Browser/" $tprefs;else  sed -i "s/^prog Browser/#prog Browser/" $tprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f7)" = TRUE ];then sed -i "s/^#obmixer/obmixer/" $startf;else sed -i "s/^obmixer/#obmixer/" $startf;fi
if [ "$(echo "$OPTS" | cut -d "|" -f8)" = TRUE ];then sed -i "s/^#wicd-client/wicd-client/" $startf;else sed -i "s/^wicd-client/#wicd-client/" $startf;fi
if [ "$(echo "$OPTS" | cut -d "|" -f9)" = TRUE ];then sed -i 's/ShowNetStatus=0/ShowNetStatus=1/' $mprefs;else sed -i 's/ShowNetStatus=1/ShowNetStatus=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f10)" = TRUE ];then sed -i 's/ShowCPUStatus=0/ShowCPUStatus=1/' $mprefs;else sed -i 's/ShowCPUStatus=1/ShowCPUStatus=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f11)" = TRUE ];then sed -i 's/ShowClock=0/ShowClock=1/' $mprefs;else sed -i 's/ShowClock=1/ShowClock=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f12)" = TRUE ];then sed -i 's/QuickConfigShowTips=0/QuickConfigShowTips=1/' $mprefs;else sed -i 's/QuickConfigShowTips=1/QuickConfigShowTips=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f13)" = "1.000000" ];then 
  sed -i 's/ShowWorkspaces=1/ShowWorkspaces=0/' $mprefs
  else 
    sed -i 's/ShowWorkspaces=0/ShowWorkspaces=1/' $mprefs
    wkspnm="$(echo "$OPTS" | cut -d "|" -f13 | sed 's/.000000//')"
    WOLD="$(grep WorkspaceNames= $mprefs)"
    if [ "$wkspnm" = "2" ];then WNEW="WorkspaceNames=\" 1 \", \" 2 \"";fi
    if [ "$wkspnm" = "3" ];then WNEW="WorkspaceNames=\" 1 \", \" 2 \", \" 3 \"";fi
    if [ "$wkspnm" = "4" ];then WNEW="WorkspaceNames=\" 1 \", \" 2 \", \" 3 \", \" 4 \"";fi
    sed -i "s/$WOLD/$WNEW/" $mprefs
fi
