#!/bin/bash

### Localization ###
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=ft10-templates

# Source variables and templates
FT10_LIBS=/usr/local/lib/ft10
. "$FT10_LIBS/ft10-variables.sh"
. "$FT10_LIBS/templates/jgmenu.template"
. "$FT10_LIBS/templates/tint2rc.template"

# Create tint2 folder if missing
TINT2_FOLDER="$HOME/.config/tint2"
[ ! -d "$TINT2_FOLDER" ] && mkdir -p "$TINT2_FOLDER"
# Make backup of old tint2 configuration
TINT2RC_FILE="$TINT2_FOLDER/tint2rc"
[ -r "$TINT2RC_FILE" ] && cp "$TINT2RC_FILE" "$TINT2_FOLDER/tint2rc.$(date +%+4Y-%m-%d-%H:%M:%S)"
# Create new tint2rc from template
tint2_menu_template


# Create jgmenu folder if it doesn't exist
JGMENU_FOLDER="$HOME/.config/jgmenu"
[ ! -d "$JGMENU_FOLDER" ] && mkdir -p "$JGMENU_FOLDER"
# Make backup of old jgmenu configuration
if [ -f "$JGMENU_FOLDER/jgmenurc" ] || [ -f "$JGMENU_FOLDER/prepend.csv" ] || \
 [ -f "$JGMENU_FOLDER/append.csv" ] || [ -f "$JGMENU_FOLDER/buttons.csv" ]; then
    BACKUP_FOLDER="$JGMENU_FOLDER/backup/$(date +%4Y%m%d%H%M%S)"
    mkdir -p "$BACKUP_FOLDER"
    [ -f "$JGMENU_FOLDER/jgmenurc" ]    && cp "$JGMENU_FOLDER/jgmenurc"    "$BACKUP_FOLDER/"
    [ -f "$JGMENU_FOLDER/prepend.csv" ] && cp "$JGMENU_FOLDER/prepend.csv" "$BACKUP_FOLDER/"
    [ -f "$JGMENU_FOLDER/append.csv" ]  && cp "$JGMENU_FOLDER/append.csv"  "$BACKUP_FOLDER/"
    [ -f "$JGMENU_FOLDER/buttons.csv" ] && cp "$JGMENU_FOLDER/buttons.csv" "$BACKUP_FOLDER/"
    [ -f "$JGMENU_FOLDER/tiles.csv" ]   && cp "$JGMENU_FOLDER/tiles.csv"   "$BACKUP_FOLDER/"
fi

# Create jgmenu files from templates
CATEGORY_LAYOUT="$FT10_LIBS/icons/category-layout.svg"
TILED_LAYOUT="$FT10_LIBS/icons/tiled-layout.svg"
CUSTOM_YAD_DIALOG=(
    --title="FT10 menus" --window-icon="/usr/share/icons/papirus-antix/24x24/actions/application-menu.png" --center
    --button="!$CATEGORY_LAYOUT!":0
    --button="!$TILED_LAYOUT!":2
    )

yad "${CUSTOM_YAD_DIALOG[@]}"
EXIT_CODE=$?

# Category layout selected
if [ $EXIT_CODE -eq 0 ]; then
    echo "Processing Category template"
    # Needed jgmenu configuration files
    jgmenu_category_template
    #~ # Optional (do not overwrite) <- May need to separate optional cases for the future
    #~ [ ! -f "$JGMENU_FOLDER/append.csv" ] && jgmenu_category_append
# Tiled layout selected
elif [ $EXIT_CODE -eq 2 ]; then
    echo "Processing TILED template"
    # Needed jgmenu configuration files
    jgmenu_tiled_template
    #~ # Optional (do not overwrite) <- May need to separate optional cases for the future
    #~ [ ! -f "$JGMENU_FOLDER/append.csv" ] && jgmenu_tiled_append
    #~ [ ! -f "$JGMENU_FOLDER/tiles.csv" ] && jgmenu_tiled_tiles
fi

#Automatic configure antiX FT10 Transformation Pack
#Free GPL license, by PCC, 25-11-2021
window_manager=$(wmctrl -m| sed -n '1p' | cut -d ' ' -f2)

#Allow for addicional antiX configuration (installing some apps and disable volumeicon across all WM
choices=$(yad  --window-icon="/usr/share/icons/papirus-antix/24x24/apps/tint2.png" --center --height=300 --width=700 --button="OK" --title="FT10" --list  --checklist --column "" --column "" TRUE $"Configure zzzfm File Manager (and install it, if needed)" FALSE $"Install Compton compositor" FALSE $"Install Dunst - to allow system notifications" FALSE $"Install Kronometer - a GUI stopwatch" FALSE $"Install Krename - a GUI batch file renamer" TRUE $"Disable default antiX volumeicon, affects all Window Managers" TRUE $"Auto-Updates" TRUE $"Disable Conky, affects current session only")

install_apps=0;a="";b="";c="";d=""; e=""

if [[ "$choices" =~ "zzz" ]]; then f10-fm-config.sh && echo done configuring zzzfm
fi

if [[ "$choices" =~ "Compton" ]]; then b="compton"; install_apps=1
fi

if [[ "$choices" =~ "Dunst" ]]; then c="dunst"; install_apps=1
fi

if [[ "$choices" =~ "Kronometer" ]]; then d="kronometer"; install_apps=1
fi

if [[ "$choices" =~ "Krename" ]]; then e="krename"; install_apps=1
fi

if [[ "$install_apps" -eq 1 ]]; then x-terminal-emulator -T $"FT10" -e /bin/bash -c "gksu 'apt -y install $b $c $d $e' && yad --center --title=FT10 --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png' --fixed --width=300 --height=100 --image=/usr/share/icons/numix-square-antix/32x32/actions/gtk-ok.png  --text='Package(s) installed! ' --button='x'|| yad --center --title=FT10 --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png'  --fixed --width=300 --height=100 --image=/usr/share/icons/papirus-antix/32x32/emblems/emblem-rabbitvcs-modified.png --text=' Error installing the package! \n  Please read the log on the terminal window below  ' --button='x'"
fi

###update-reminder
 if [[ "$choices" =~ "Updates" ]]; then  echo "trying to ENABLE	 update reminder"
   sed -i 's/^#execp_command = ft10-update-reminder.sh/execp_command = ft10-update-reminder.sh/' $HOME/.config/tint2/tint2rc
   
    else
    echo "trying to DISABLE	 update reminder"
    sed -i 's/^execp_command = ft10-update-reminder.sh/#execp_command = ft10-update-reminder.sh/' $HOME/.config/tint2/tint2rc
 fi

###volume-icon
if [[ "$choices" =~ "volume" ]]; then echo disabling default volumeicon
 pkill volumeicon
	if grep -Po '^volumeicon' ~/.desktop-session/startup; then
		echo volumeicon found in startup, removing it
		sed -i 's/volumeicon &//g'  ~/.desktop-session/startup
	fi 

 else
  echo enabling default volumeicon
  pkill volumeicon ; volumeicon &
  	if grep -Po '^volumeicon' ~/.desktop-session/startup; then
		echo volumeicon already in startup
	else
		echo volumeicon not found in startup, inserting it
		echo "volumeicon &" >> ~/.desktop-session/startup
	fi 

  
fi

###conky
if [[ "$choices" =~ "Conky" ]]; then echo disabling Conky
 pkill conky
 else
 echo enabling Conky
 conky &
fi

######################
#configuring FT10... #
######################
cp  ~/.desktop-session/startup ~/.config/tint2/desktop-session-startup-backup
##########
#Automatically configure IceWM to use ft10 toolbar by default:
##########
if [ $window_manager = "IceWM" ] ; then
#Back up config files, if no back up exists:
	if [ ! -f ~/.icewm/preferences-ft10-backup ]; then
		cp ~/.icewm/preferences  ~/.icewm/preferences-ft10-backup
	fi
	if [ ! -f ~/.icewm/startup-ft10-backup ]; then
		cp ~/.icewm/startup  ~/.icewm/startup-ft10-backup
		
    fi
#Get ShowTaskbar variable from icewm's preferences file:
default_icewm_toolbar=$(egrep "^ShowTaskBar=" ~/.icewm/preferences | cut -d'=' -f2 | cut -d' ' -f1 |  cut -d'%' -f1)
#if no option for showing Taskbar exists in icewm's preferences file, then create it at set to hide the default taksbar (also disables it's systray)
if [[ -z "$default_icewm_toolbar" ]]; then
echo ShowTaskBar=0 >> ~/.icewm/preferences
fi
#Make sure that the default iceWM taskbar is disabled:
sed -i 's/ShowTaskBar=.*/ShowTaskBar=0/' ~/.icewm/preferences
#Make sure that ft10's toolbar is launched at system start:
if ! grep -q ^ft10-start ~/.icewm/startup; then
echo "ft-10 is not set to auto start in IceWM, setting it up..."
echo ft10-start >> ~/.icewm/startup
else
echo "ft-10 is already set up to autostart in IceWM!"
fi
#Restart IceWM
killall -SIGHUP icewm
fi

##########
#Automatically configure Fluxbox to use ft10 toolbar by default:
##########
if [ $window_manager = "Fluxbox" ] ; then
echo Trying to configure FT10 in Fluxbox
#Back up config files, if no back up exists:
if [ ! -f ~/.fluxbox/init-ft10-backup ]; then
cp ~/.fluxbox/init  ~/.fluxbox/init-ft10-backup
cp $HOME/.fluxbox/startup  $HOME/.fluxbox/startup-ft10-backup
fi
#Make sure that ft10's toolbar is launched at system start:
if ! grep -q ^ft10-start ~/.fluxbox/startup; then
	echo "ft-10 is not set to auto start in Fluxbox, setting it up..."
# Perform conf file changes
echo "session.screen0.allowRemoteActions: true" >> ~/.fluxbox/init
echo "session.screen0.toolbar.tools: rootmenu, iconbar, clock"  >> ~/.fluxbox/init
echo "session.screen0.toolbar.visible: false" >> ~/.fluxbox/init
echo "[app] (name=tint2)" >> ~/.fluxbox/apps
echo "[Layer] {6}" >> ~/.fluxbox/apps
echo "[end]" >> ~/.fluxbox/apps
#awk '/!\/bin\/sh/ { print; print "ft10-start &"; next }1' ~/.fluxbox/startup > /tmp/t2instal && \cp /tmp/t2instal ~/.fluxbox/startup
sed -i '1 s/^/ft10-start \&\n/' ~/.fluxbox/startup
  else
	echo "ft-10 is already set up to autostart in Fluxbox!"
	#try to restart fluxbox:
/usr/local/lib/desktop-session/desktop-session-restart
fi
#try to restart fluxbox:
/usr/local/lib/desktop-session/desktop-session-restart
fi

##########
#Automatically configure JWM to use ft10 toolbar by default:
##########

if [ $window_manager = "JWM" ] ; then

#Back up config files, if no back up exists:
if [ ! -f ~/.jwmrc-ft10-backup ]; then
cp ~/.jwmrc  ~/.jwmrc-ft10-backup
fi
if [ ! -f ~/.jwm/startup-ft10-backup ]; then
cp ~/.jwm/startup  ~/.jwm/startup-ft10-backup
fi
# Perform conf file changes to disable JWM's tray
sed -i '/$HOME\/.jwm\/tray/c\<!--<Include>$HOME\/.jwm\/tray<\/Include>-->' ~/.jwmrc
#Make sure that ft10's toolbar is launched at system start:
if ! grep -q ^pkill ~/.jwm/startup; then
echo "ft-10 is not set to auto start in JWM, setting it up..."
awk '/<StartupCommand>/ { print; print "pkill tint2; ft10-start &"; next }1' ~/.jwm/startup > /tmp/t2instal
cp /tmp/t2instal ~/.jwm/startup
else
echo "ft-10 is already set up to autostart in JWM"
/usr/local/lib/desktop-session/desktop-session-restart
fi

fi

#yad --center --title="FT10 Transformation Pack" --width=350 --text="		Finished!		" --button=" x "
###close the initial if loop, if user clicked "ok" in the initial window:


# Create this file for future checks
touch $HOME/.config/tint2/ft10.conf
