# run a program from a terminal and destroy the terminal
# think "using the terminal as a dmenu replacement"

# store process list to iterate over below
# this lets us avoid calling ps(1) per loop iteration times
ps=$(ps -f -o pid -o ppid -o comm)

while [ -z "${term}" ]; do
  # iterate over the ps(1) list and grab the parent process ID's information
  # heredocs are used to avoid subshell usage with restricts variable scope
  # shellcheck disable=SC2249
  while read -r pid _ppid comm; do
    case ${pid} in
      "${ppid:-$$}")
        ppid=${_ppid}
        break
        ;;
    esac
  done <<EOF
${ps}
EOF

  # abort the loop once we pass through the blacklist of process names
  case ${comm} in
    "${0##*/}" | "${SHELL##*/}" | init) ;;
    *) term=${comm} ;;
  esac
done

# run the program in a new session and then kill the found PPID
# (which will be the terminal)
setsid "$@" >/dev/null &
kill "${pid}"
