#!/bin/bash
####GUI script to manage antiX startup (add/remove applications from the start of the OS)

        TEXTDOMAINDIR=/usr/share/locale
        TEXTDOMAIN=edit_antix_startup_file

 ice_startup(){
	 startupfile=$HOME/.desktop-session/startup

startup_title=$"antiX-startup GUI"	 
add_remove_text=$"Startup ("
add_startup_appselect=$"Add application from a list"
add_startup_command=$"Add a (manual) command"
remove_startup=$"Remove an application"
add_startup_confirmation=$"The line was added to $startupfile. It will start automatically the next time you start  antiX"
add_startup_command_title=$"Enter command to be added to the startup file"
add_startup_command_text=$"Enter command you want to run at antiX's startup. Note: an ampersand/& will automatically be appended to the end of the command"
remove_startup_text="Select command to be removed from the startup file. Note: all entries with the selected command will be deleted!"
remove_button=$"Remove"

	local add_remove_main_button=$add_remove_main_button
yad --title="$startup_title" --window-icon=/usr/share/pixmaps/numix-square/icewm.png --height=400 --width=550 --center --text=$"\n $add_remove_text $startupfile):" --button="$add_startup_appselect":1 --button="$add_startup_command":2 --button="$remove_startup":3 --text-info <   $startupfile --show-uri --fontname="ubuntu 12"
### wait for a button to be pressed then perform the selected function
main_selection=$?

if [[ $main_selection -eq 1 ]]; then
 selected=$(app-select --select| cut -d '|' -f 5| tail -1)
 #If a selection was not done, exit
	if [ -z "$selected" ]; then exit
	fi

  if echo "$selected" | grep 'Overall:'; then  exit
	fi

  echo "$selected  &" >> $startupfile
 #call main window again, to show the result
 ice_startup
fi

if [[ $main_selection -eq 2 ]]; then
 selected=$(yad --window-icon=/usr/share/pixmaps/numix-square/icewm.png --title="$add_startup_command_title" --center --width=500 --text="$add_startup_command_text" --form --field ':CE') 
 
# Remove trailing | from the end of the selection:
 selected=${selected::-1}
 
#If a selection was not done, exit
	if [ -z "$selected" ]; then exit
	fi
 
#Check if command already includes "&" in the end, if not, add it
 end_of_content=$(echo $selected| awk 'NF>1{print $NF}')
  if [ $end_of_content = "&" ]; then
   result="command already ends in an &, doing nothing"
   else
   #command did not end in an &, adding it
   selected=$(echo $selected "&")
  fi
 
 echo "$selected" >>  $startupfile
#call main window again, to show the resultice_startup
fi


if [[ $main_selection -eq 3 ]]; then
  #### remove an  entry to startup file:
 #List contents of startup file, removing first line (with /bin/bash) and also any commented line
 cat $startupfile| grep -v "#\!/bin/bash" | grep -o '^[^#]*' > /tmp/startuplist
 #clean up temporary file
 echo "" > /tmp/good
 #allow file to be truncated (i.e. append to the end of the line, instead of into a new line)
 truncate -s-1 /tmp/good
 #cicle trough the temporary file's contents and create /temp/good with everything into the same line, separed by "!", so it ca be used by yad drop box
 while read -r line; do
    echo -n ${line}\!  >> /tmp/good
 done < /tmp/startuplist
 #Show yad selection window, so user select what entry will be removed
 selection=$(yad --center --title="$remove_startup"  --window-icon=/usr/share/pixmaps/numix-square/icewm.png --text="${remove_startup_text}\n" --form --field="${remove_button}:CB" < /tmp/good)

 # Remove trailing | from the end of the selection:
 selection=${selection::-1}
 
 #process only untiul the first "&"
 b=${selection%\&*}
 line_number=$(cat $startupfile | fgrep -n "$b"| cut -d ':' -f 1 | head -n 1)

#If a selection was not done, exit
	if [ -z "$line_number" ]; then exit
	fi
	
	if echo "$selected" | grep 'Overall:'; then  exit
	fi

 #Remove all entries with this contents from the startup file (it's missing the -i flag)
 sed -i "${line_number}d"  $startupfile
 #call main window again, to show the result
  ice_startup
fi
}

#Run main function
 ice_startup
