#!/bin/sh

########################################################################
###                       select theme from file                     ###
########################################################################
if [ $1 = "themeselect" ]; then
themename=$(yad --width=600 --height=600 --file-selection --filename=/usr/share/themes/)
/usr/bin/theme-engine $themename
exit 0
fi


########################################################################
###                         input theme file                         ###
########################################################################
### input user theme variables ###
source /home/$USER/.config/alteratio/theme.s7t
### input new theme file ###
source $1
### fix for old theme files, old theme files should be updated ###
winman_theme="$system_theme_xfwm"
### input new theme file ###
source $1


########################################################################
###               change theme file variables if needed              ###
########################################################################
panel_position="bottom"

if [ $panel_background = "default" ]
then
panel_background="/usr/share/themes/$theme_dir/alteratio/panel/background.png"
fi

if [ $panel_desktop_icon = "default" ]
then
panel_desktop_icon="/usr/share/themes/$theme_dir/alteratio/panel/show-desktop.png"
fi

if [ $menu_icon = "default" ]
then
menu_icon="/usr/share/themes/$theme_dir/alteratio/panel/menu.png"
fi

if [ $system_icon = "default" ]
then
system_icon="$theme_dir"
fi

if [ $system_widget = "default" ]
then
system_widget="$theme_dir"
fi

if [ $system_cursor = "default" ]
then
system_cursor="$theme_dir"
fi

if [ $winman_theme = "default" ]
then
winman_theme="$theme_dir"
fi


########################################################################
###               make needed directories if not found               ###
########################################################################
### make directory if not found ###
if [ -d /home/$USER/.kde4 ]
then
echo "found"
else
mkdir /home/$USER/.kde4
fi
### make directory if not found ###
if [ -d /home/$USER/.kde4/share ]
then
echo "found"
else
mkdir /home/$USER/.kde4/share
fi
### make directory if not found ###
if [ -d /home/$USER/.kde4/share/config ]
then
echo "found"
else
mkdir /home/$USER/.kde4/share/config
fi
### make directory if not found ###
if [ -d /home/$USER/.config/alteratio ]
then
echo "found"
else
mkdir /home/$USER/.config/alteratio
fi
### make directory if not found ###
if [ -d /home/$USER/.config/lxpanel ]
then
echo "found"
else
mkdir /home/$USER/.config/lxpanel
fi
### make directory if not found ###
if [ -d /home/$USER/.config/lxpanel/Alteratio ]
then
echo "found"
else
mkdir /home/$USER/.config/lxpanel/Alteratio
fi
### make directory if not found ###
if [ -d /home/$USER/.config/lxpanel/Alteratio/panels ]
then
echo "found"
else
mkdir /home/$USER/.config/lxpanel/Alteratio/panels
fi


########################################################################
###                        change xsettings.xml                      ###
########################################################################
xfconf-query -c xsettings -p /Gtk/CursorThemeName -s "$system_cursor"
xfconf-query -c xsettings -p /Net/IconThemeName -s "$system_icon"
xfconf-query -c xsettings -p /Net/ThemeName -s "$system_widget"


########################################################################
###                       set xfce4-panel size                       ###
########################################################################
panel_xfce_height=$((panel_height-1))
xfconf-query -c xfce4-panel -p /panels/panel-1/size -s $panel_xfce_height


########################################################################
###                write Alteratio theme config files                ###
########################################################################
### the actual theme file ###
cat > /home/$USER/.config/alteratio/theme.s7t << EOF
panel_background="$panel_background"
panel_opacity="$panel_opacity"
panel_tint="$panel_tint"
panel_height="$panel_height"
panel_font_color="$panel_font_color"
panel_icon_size="$panel_icon_size"
panel_desktop_icon="$panel_desktop_icon"
panel_taskbar_flat="$panel_taskbar_flat"
panel_taskbar_iconsonly="$panel_taskbar_iconsonly"
panel_taskbar_group="$panel_taskbar_group"
panel_layout="$panel_layout"
panel_type="$panel_type"
menu_icon="$menu_icon"
system_icon="$system_icon"
system_widget="$system_widget"
system_cursor="$system_cursor"
system_background="$system_background"
winman_theme="$winman_theme"
default_browser="$default_browser"
EOF


########################################################################
###                          set xfwm4 theme                         ###
########################################################################
xfconf-query -c xfwm4 -p /general/theme -s "$winman_theme"


########################################################################
###                           write panel                            ###
########################################################################
### keep whisker menu the default menu ###
menu_type="whisker"
### writes a temp file used in deciding if whisker or lxde menu ###
if [ $menu_type = "lxde" ] 
then
cat > /home/$USER/.config/alteratio/menu-temp << EOF
Plugin {
  type=menu
  Config {
    system {
    }
    separator {
    }
    item {
      command=run
    }
    separator {
    }
    item {
      command=logout
      image=gnome-logout
    }
    image=$menu_icon
  }
}
Plugin {
  type=launchbar
  Config {
    Button {
      id=/usr/share/applications/pcmanfm.desktop
    }
    Button {
      id=/usr/share/applications/$default_browser.desktop
    }
  }
}
EOF
elif [ $menu_type = "whisker" ] 
then
cat > /home/$USER/.config/alteratio/start.desktop << EOF
[Desktop Entry]
GenericName=Start
Name=Start
Comment=Start
Icon=$menu_icon
Exec=xfce4-popup-whiskermenu
Terminal=false
Type=Application
Categories=GTK;Settings;HardwareSettings;
NoDisplay=true
EOF
cat > /home/$USER/.config/alteratio/menu-temp << EOF
Plugin {
  type=launchbar
  Config {
    Button {
      id=/home/$USER/.config/alteratio/start.desktop
    }
    Button {
      id=/usr/share/applications/pcmanfm.desktop
    }
    Button {
      id=/usr/share/applications/$default_browser.desktop
    }
  }
}
EOF
fi
menufile=$(cat /home/$USER/.config/alteratio/menu-temp)


### decide panel look type ###
if [ $panel_type = "transparent" ]
then
panel_type_transparent="1"
panel_type_image="0"
elif [ $panel_type = "image" ]
then
panel_type_transparent="0"
panel_type_image="1"
elif [ $panel_type = "gtk" ]
then
panel_type_transparent="0"
panel_type_image="0"
fi

###panel flat/grouped/type icons###
if [ $panel_taskbar_flat = "true" ]
then
panel_taskbar_a="1"
elif [ $panel_taskbar_flat = "false" ]
then
panel_taskbar_a="0"
fi

if [ $panel_taskbar_iconsonly = "true" ]
then
panel_taskbar_b="1"
elif [ $panel_taskbar_iconsonly = "false" ]
then
panel_taskbar_b="0"
fi

if [ $panel_taskbar_group = "true" ]
then
panel_taskbar_c="1"
elif [ $panel_taskbar_group = "false" ]
then
panel_taskbar_c="0"
fi


if [ $panel_layout = "1" ] 
then
cat > /home/$USER/.config/lxpanel/Alteratio/panels/panel << EOF
# lxpanel <profile> config file. Manually editing is not recommended.
# Use preference dialog in lxpanel to adjust config when you can.

Global {
  edge=$panel_position
  allign=left
  margin=0
  widthtype=percent
  width=100
  height=$panel_height
  transparent=$panel_type_transparent
  tintcolor=#$panel_tint
  alpha=$panel_opacity
  setdocktype=1
  setpartialstrut=1
  usefontcolor=1
  fontcolor=#$panel_font_color
  background=$panel_type_image
  backgroundfile=$panel_background
  iconsize=$panel_icon_size
}
Plugin {
  type=space
  Config {
    Size=2
  }
}
$menufile
Plugin {
  type=space
  Config {
    Size=4
  }
}
Plugin {
  type=taskbar
  expand=1
  Config {
    tooltips=1
    IconsOnly=$panel_taskbar_b
    AcceptSkipPager=1
    ShowIconified=1
    ShowMapped=1
    ShowAllDesks=1
    UseMouseWheel=1
    UseUrgencyHint=1
    FlatButton=$panel_taskbar_a
    MaxTaskWidth=150
    spacing=1
    GroupedTasks=$panel_taskbar_c
  }
}
Plugin {
  type=tray
  Config {
  }
}
Plugin {
  type=volumealsa
  Config {
  }
}
Plugin {
  type=dclock
  Config {
    ClockFmt=%I:%M
    TooltipFmt=%A %x
    BoldFont=0
    IconOnly=0
    CenterText=0
  }
}
Plugin {
  type=wincmd
  Config {
    image=$panel_desktop_icon
    Button1=iconify
    Button2=shade
    Toggle=1
  }
}

EOF
elif [ $panel_layout = "2" ] 
then
cat > /home/$USER/.config/lxpanel/Alteratio/panels/panel << EOF
# lxpanel <profile> config file. Manually editing is not recommended.
# Use preference dialog in lxpanel to adjust config when you can.

Global {
  edge=$panel_position
  allign=left
  margin=0
  widthtype=percent
  width=100
  height=$panel_height
  transparent=$panel_type_transparent
  tintcolor=#$panel_tint
  alpha=$panel_opacity
  setdocktype=1
  setpartialstrut=1
  usefontcolor=1
  fontcolor=#$panel_font_color
  background=$panel_type_image
  backgroundfile=$panel_background
  iconsize=$panel_icon_size
}
Plugin {
  type=space
  Config {
    Size=2
  }
}
$menufile
Plugin {
  type=space
  Config {
    Size=2
  }
}
Plugin {
  type=wincmd
  Config {
    image=$panel_desktop_icon
    Button1=iconify
    Button2=shade
    Toggle=1
  }
}
Plugin {
  type=space
  Config {
    Size=4
  }
}
Plugin {
  type=taskbar
  expand=1
  Config {
    tooltips=1
    IconsOnly=$panel_taskbar_b
    AcceptSkipPager=1
    ShowIconified=1
    ShowMapped=1
    ShowAllDesks=1
    UseMouseWheel=1
    UseUrgencyHint=1
    FlatButton=$panel_taskbar_a
    MaxTaskWidth=150
    spacing=1
    GroupedTasks=$panel_taskbar_c
  }
}
Plugin {
  type=tray
  Config {
  }
}
Plugin {
  type=volumealsa
  Config {
  }
}
Plugin {
  type=dclock
  Config {
    ClockFmt=%I:%M
    TooltipFmt=%A %x
    BoldFont=0
    IconOnly=0
    CenterText=0
  }
}
Plugin {
  type=space
  Config {
    Size=4
  }
}

EOF
elif [ $panel_layout = "2b" ] 
then
cat > /home/$USER/.config/lxpanel/Alteratio/panels/panel << EOF
# lxpanel <profile> config file. Manually editing is not recommended.
# Use preference dialog in lxpanel to adjust config when you can.

Global {
  edge=$panel_position
  allign=left
  margin=0
  widthtype=percent
  width=100
  height=$panel_height
  transparent=$panel_type_transparent
  tintcolor=#$panel_tint
  alpha=$panel_opacity
  setdocktype=1
  setpartialstrut=1
  usefontcolor=1
  fontcolor=#$panel_font_color
  background=$panel_type_image
  backgroundfile=$panel_background
  iconsize=$panel_icon_size
}
$menufile
Plugin {
  type=space
  Config {
    Size=2
  }
}
Plugin {
  type=wincmd
  Config {
    image=$panel_desktop_icon
    Button1=iconify
    Button2=shade
    Toggle=1
  }
}
Plugin {
  type=space
  Config {
    Size=4
  }
}
Plugin {
  type=taskbar
  expand=1
  Config {
    tooltips=1
    IconsOnly=$panel_taskbar_b
    AcceptSkipPager=1
    ShowIconified=1
    ShowMapped=1
    ShowAllDesks=1
    UseMouseWheel=1
    UseUrgencyHint=1
    FlatButton=$panel_taskbar_a
    MaxTaskWidth=150
    spacing=1
    GroupedTasks=$panel_taskbar_c
  }
}
Plugin {
  type=tray
  Config {
  }
}
Plugin {
  type=volumealsa
  Config {
  }
}
Plugin {
  type=dclock
  Config {
    ClockFmt=%I:%M
    TooltipFmt=%A %x
    BoldFont=0
    IconOnly=0
    CenterText=0
  }
}
Plugin {
  type=space
  Config {
    Size=4
  }
}

EOF
fi


########################################################################
###                          set KDE icons                           ###
########################################################################
configfile=$(cat /usr/share/alteratio/conf/kdeglobals2)
cat > /home/$USER/.kde4/share/config/kdeglobals << EOF
$configfile
EOF
sed -i s/ICONTHEMENAME/"$system_icon"/g /home/$USER/.kde4/share/config/kdeglobals


########################################################################
###                          set wallpapers                          ###
########################################################################
pcmanfm --set-wallpaper "$system_background" --wallpaper-mode stretch


########################################################################
###                       reload panel and exit                      ###
########################################################################
lxpanelctl restart & exit 0