#!/bin/bash

#$1 is the command to be run.
#$2 is the title.
commands="$1"
title="$2"

#set DEBUG variable to 1 to display which terminal is being used
[ -z "$DEBUG" ] && DEBUG=0

function error {
  echo -e "\e[91m$1\e[39m"
  exit 1
}

#reset the GTK theme so terminals follow the system GTK theme
export GTK_THEME=''

#add a line to the terminal's command-to-run that saves the terminal's PID to a known filename
temp_pid_file="$(mktemp -u)"
commands="echo "\$""\$" > $temp_pid_file
$commands"

#add a line to the terminal's command-to-run that sets the title. This method allows it to be changed later, while specifying the title using a flag does not.
commands="echo -ne '\e]0;${title}\a'
$commands"

terminals='lxterminal
xfce4-terminal
mate-terminal
lxterm
uxterm
xterm
konsole
terminator
gnome-terminal
gnome-terminal.wrapper
qterminal'

#Try to honor the preference of update-alternatives x-terminal-emulator
terminal="$(basename "$(readlink -f "$(command -v x-terminal-emulator)")")"

[ $DEBUG == 1 ] && echo "Default x-terminal-emulator: $terminal"

#if x-terminal-emulator is found and it matches an option in $choices, then we can use it.
choice=''
if [ ! -z "$terminal" ] && echo "$terminals" | grep -qFx "$terminal" ;then
  #choose the same terminal that x-terminal-emulator uses
  choice="$terminal"
  [ $DEBUG == 1 ] && echo "Using $terminal"
else
  #otherwise we have to run through each supported terminal, hoping that one is installed on the system
  IFS=$'\n'
  for terminal in $terminals ;do
    if command -v "$terminal" >/dev/null ;then
      choice="$terminal"
      [ $DEBUG == 1 ] && echo "Using $terminal"
      break
    fi
  done
fi

if [ -z "$choice" ];then
  error "Failed to locate any terminal emulators!!!"
elif [ "$choice" == lxterminal ];then
  lxterminal -e bash -c "$commands" &
  
elif [ "$choice" == xfce4-terminal ];then
  xfce4-terminal -x bash -c "$commands" &
  
elif [ "$choice" == mate-terminal ];then
  mate-terminal -x bash -c "$commands" &
  
elif [ "$choice" == lxterm ];then
  lxterm -e bash -c "$commands" &
  
elif [ "$choice" == uxterm ];then
  uxterm -e bash -c "$commands" &
  
elif [ "$choice" == xterm ];then
  xterm -e bash -c "$commands" &
  
elif [ "$choice" == konsole ];then
  konsole -e bash <(echo "$commands") &
  
elif [ "$choice" == terminator ];then
  terminator -x bash -c "$commands" &
  
elif [ "$choice" == gnome-terminal ];then
  gnome-terminal -x bash -c "$commands" &
  
elif [ "$choice" == gnome-terminal.wrapper ];then
  gnome-terminal -x bash -c "$commands" &
  
elif [ "$choice" == qterminal ];then
  qterminal -e bash <(echo "$commands") &
    
else
  error "Failed to locate any terminal emulators!!!"
fi

#A terminal should now be launching and in a few seconds the pid file should appear
if [ ! -z "$temp_pid_file" ];then
  #echo "Waiting for terminal to close..."
  #wait until pid file exists and give up in 5 seconds
  attempts=0
  while [ ! -f "$temp_pid_file" ];do
    
    if [ "$attempts" -gt 10 ];then
      error "terminal-run: No terminal detected as it never created the pid file within 5 seconds."
    fi
    sleep 1
    attempts=$((attempts + 1))
  done #pid file now exists
  terminalrun_pid="$(cat "$temp_pid_file")"
  
  #now, wait until the pid stops (terminal closes)
  while [ -d /proc/${terminalrun_pid} ];do
    sleep 1
  done
  rm -f "$temp_pid_file"
  
fi

true
