#!/bin/bash

if [ "`which ansifilter`" == "" ] ; then
  echo ""
  echo "You should install ansifilter to make the recall buffer more usable:"
  echo ""
  echo "wget 'https://gitlab.com/saalen/ansifilter/-/archive/master/ansifilter-master.zip?ref_type=heads'"
  echo "mv \"ansifilter-master.zip?ref_type=heads\" ansifilter-master.zip"
  echo "unzip ansifilter-master.zip"
  echo "mv ansifilter-master.zip  ansifilter-master/"
  echo "cd ansifilter-master/"
  echo "make"
  echo "mv ./src/ansifilter ~/bin/"
  echo ""
  exit 1
fi

# my ~/.screenrc contains these lines:  (don't use "~")
# logfile ${HOME}/.screenlog.%n
# deflog on

# Get the actual log file name using the current window number from the $WINDOW environment variable set by screen:
# stop recording while in 'recall' command (same as was done on EMAS):
screen -X log off ; screen -X logfile flush 0

ansifilter < "`echo ${HOME}/.screenlog.${WINDOW}`" > /tmp/screen-$$.log

# alternative if no ansifilter:
# tr -d '\r' < "$LOG_FILE" | col -b > /tmp/screen-$$.log

# use TERM to suppress colourization, and -f end-of-buffer to start edit at end of file:
TERM=vt100 emacs -nw /tmp/screen-$$.log -f end-of-buffer && rm -f /tmp/screen-$$.log || rm -f /tmp/screen-$$.log

echo "There will be a short delay as your shell resumes, or press return to dismiss this message immediately:"
screen -X log on
