#!/bin/bash

#this script is executed whenever you click on an entry from the logviewer script. This script handles opening a text editor to view your log file.

DIRECTORY="$(readlink -f "$(dirname "$(dirname "$0")")")"

file="$6"
if [ -z "$file" ];then
  file="$1"
fi
echo "$file" > "$DIRECTORY/data/current-viewed-logfile"

if command -v mousepad ;then
  mousepad "$file" &
  pid=$!
elif command -v leafpad ;then
  leafpad "$file" &
  pid=$!
else
  #for text_editor function
  source "${DIRECTORY}/api"
  text_editor "$file" &
  pid=$!
fi

#while the user-selection stays the same, AND the text editor is still running, wait.
while [ "$(cat "$DIRECTORY/data/current-viewed-logfile")" == "$file" ] && kill -0 $pid;do
  sleep 1
done
kill $pid 2>/dev/null


