#!/bin/bash
#If Tiles are not enabled (i.e the file "tiles.csv" does not exist), just exit

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=jgmenu-editor

if [ ! -f ~/.config/jgmenu/tiles.csv ]; then exit 0 
fi

#Get number of tile that will be edited
choice=$(yad --title="FT10 - Tiles"  --window-icon="/usr/share/icons/papirus-antix/24x24/categories/app-launcher.png" --center --form --field="Tile::"CB "1!2!3!4!5!6!7!8!9!10!11!12!13!14!15!16!17!18!19!20!21")

#Exit if User presses Cancel
 foo=$?
 [[ $foo -eq 1 ]] && exit 0

tile=$(echo $choice| cut -d\| -f1)

#get line numbers where that tile is:
 declare -i edited_tile
 edited_tile=$(grep -nr "#Tile$tile#" ~/.config/jgmenu/tiles.csv | cut -d: -f1)
 edited_tile+=1
 echo $edited_tile
 edited_tile_line2=$((edited_tile+1))
 echo $edited_tile_line2
 
#get the Application name, icon and eventually name that is displayed
contents_of_first_line=$(sed "${edited_tile}q;d" ~/.config/jgmenu/tiles.csv)
contents_of_second_line=$(sed "${edited_tile_line2}q;d" ~/.config/jgmenu/tiles.csv)
sed "${edited_tile}q;d" ~/.config/jgmenu/tiles.csv > /tmp/line_contents1
original_line_one=$(cat /tmp/line_contents1)

sed "${edited_tile_line2}q;d" ~/.config/jgmenu/tiles.csv > /tmp/line_contents2
original_line_two=$(sed "${edited_tile2}q;d" ~/.config/jgmenu/tiles.csv)
echo $contents_of_second_line

tile_exec=$(echo $contents_of_first_line| cut -d, -f2)
tile_icon=$(echo $contents_of_first_line| cut -d, -f12)
tile_tile=$(echo $contents_of_second_line| cut -d, -f12)

 yad --title="FT10 - Tiles"  --window-icon="/usr/share/icons/papirus-antix/24x24/categories/app-launcher.png" --center --image="$tile_icon" --image-on-top --text="$tile_tile"
 
#Exit if User presses Cancel 
 foo=$?
 [[ $foo -eq 1 ]] && exit 0
 

#get the info of the Application the user wants to install
#use app-select to get application to be added to the menu
add_icon=$(app-select --s)
#if no choice was made, exit
[ -z "$add_icon" ] && exit

#if a choice was made, process app-select's output
new_Exec=$(echo $add_icon| cut -d '|' -f3)
new_Icon=$(echo $add_icon| cut -d '|' -f6)
new_Name=$(echo $add_icon| cut -d '|' -f2)

#edit the Application name, icon and eventually name that is displayed
sed -i "s#$tile_exec#$new_Exec#g" /tmp/line_contents1
sed -i "s#$tile_icon#$new_Icon#g" /tmp/line_contents1

new_line_one=$(cat /tmp/line_contents1)

#Process first line related to the Tile:
# Process Escape backslash, forward slash and ampersand in variable used in the sed command:
new_line_escaped=$( echo "$new_line_one" | sed -e 's/[\/&]/\\&/g' )
sed -i "${edited_tile}s/.*/$new_line_escaped/"  ~/.config/jgmenu/tiles.csv

#Process the optional second related to the Tile (that displays the Tile name):
#Make sure that the Tile description has 9 or less letters, so it fits in the intended spaced
selected_title=$(yad --title="FT10 - Tiles"  --window-icon="/usr/share/icons/papirus-antix/24x24/categories/app-launcher.png" --center --image="$new_Icon" --image-on-top --text=$"Optional Tile name -max. 9 characters" --entry --entry-text="$new_Name")
new_title=${selected_title:0:9}
# Process Escape backslash, forward slash and ampersand in variable used in the sed command:
new_title_escaped=$( echo "$new_title" | sed -e 's/[\/&]/\\&/g' )
sed -i "s|$tile_tile|${new_title_escaped}|" ~/.config/jgmenu/tiles.csv

#Display menu, so user can see changes:
sleep 0.1 && jgmenu_run
