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

# initial values
mprefs="$HOME/.icewm/preferences"
vprefs="$HOME/.icewm/vl-mods"
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

# display gui using yad
OPTS=$(
yad --title="IceQuickConfig" --form \
--field="Advanced desktop:CHK" \
--field="Auto menu:CHK" \
--field="Desktop button:CHK" \
--field="Window list:CHK" \
--field="Terminal:CHK" \
--field="File manager:CHK" \
--field="Browser:CHK" \
--field="Clipboard:CHK" \
--field="Volume control:CHK" \
--field="Network manager:CHK" \
--field="Network monitor:CHK" \
--field="CPU monitor:CHK" \
--field="Clock:CHK" \
--field="24 hour clock:CHK" \
--field="Settings and Help:CHK" \
--field="Auto-hide panel:CHK" \
--field="Use gksudo in menu:CHK" \
--field="Use logout dialog:CHK" \
--field="Number of workspaces:NUM" \
$(if [ "$(grep ^spacefm $startf)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^automenu=1 $vprefs)" ];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 ^parcellite $startf)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^pnmixer $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 'TimeFormat="%R"' $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^ShowSettingsMenu=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^TaskBarAutoHide=1 $mprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^gksudo=1 $vprefs)" ];then echo TRUE;else echo FALSE;fi) \
$(if [ "$(grep ^LogoutCommand= $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/^#spacefm/spacefm/" $startf;else sed -i "s/^spacefm/#spacefm/" $startf;fi
if [ "$(echo "$OPTS" | cut -d "|" -f2)" = TRUE ];then sed -i "s|^automenu=0|automenu=1|" $vprefs;else sed -i "s|^automenu=1|automenu=0|" $vprefs;/usr/share/icewm/menu-gen quiet;fi
if [ "$(echo "$OPTS" | cut -d "|" -f3)" = 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 "|" -f4)" = 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 "|" -f5)" = 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 "|" -f6)" = 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 "|" -f7)" = 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 "|" -f8)" = TRUE ];then sed -i "s/^#parcellite/parcellite/" $startf;else sed -i "s/^parcellite/#parcellite/" $startf;fi
if [ "$(echo "$OPTS" | cut -d "|" -f9)" = TRUE ];then sed -i "s/^#pnmixer/pnmixer/" $startf;else sed -i "s/^pnmixer/#pnmixer/" $startf;fi
if [ "$(echo "$OPTS" | cut -d "|" -f10)" = 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 "|" -f11)" = 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 "|" -f12)" = 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 "|" -f15)" = TRUE ];then sed -i 's/ShowSettingsMenu=0/ShowSettingsMenu=1/' $mprefs; sed -i 's/ShowHelp=0/ShowHelp=1/' $mprefs; else sed -i 's/ShowSettingsMenu=1/ShowSettingsMenu=0/' $mprefs; sed -i 's/ShowHelp=1/ShowHelp=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f14)" = TRUE ];then sed -i 's/TimeFormat="%_I:%M %p"/TimeFormat="%R"/' $mprefs;else sed -i 's/TimeFormat="%R"/TimeFormat="%_I:%M %p"/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f15)" = TRUE ];then sed -i 's/ShowSettingsMenu=0/ShowSettingsMenu=1/' $mprefs;else sed -i 's/ShowSettingsMenu=1/ShowSettingsMenu=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f16)" = TRUE ];then sed -i 's/TaskBarAutoHide=0/TaskBarAutoHide=1/' $mprefs;else sed -i 's/TaskBarAutoHide=1/TaskBarAutoHide=0/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f17)" = TRUE ];then sed -i 's/gksudo=0/gksudo=1/' $vprefs;/usr/share/icewm/menu-gen quiet; else sed -i 's/gksudo=1/gksudo=0/' $vprefs;/usr/share/icewm/menu-gen quiet;fi
if [ "$(echo "$OPTS" | cut -d "|" -f18)" = TRUE ];then sed -i 's/#LogoutCommand=/LogoutCommand=/' $mprefs;else sed -i 's/LogoutCommand=/#LogoutCommand=/' $mprefs;fi
if [ "$(echo "$OPTS" | cut -d "|" -f19)" = "1.000000" ];then 
  sed -i 's/TaskBarShowWorkspaces=1/TaskBarShowWorkspaces=0/' $mprefs
  else 
    sed -i 's/TaskBarShowWorkspaces=0/TaskBarShowWorkspaces=1/' $mprefs
    wkspnm="$(echo "$OPTS" | cut -d "|" -f19 | 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
