<commit_before>#!/bin/bash
# Copyright 2019 Digital Aggregates Corporation, Colorado, USA
# Licensed under the terms in LICENSE.txt
# <NAME> <<EMAIL>>
# https://github.com/coverclock/com-diag-diminuto
#
# ABSTRACT
#
# Uses the inotify tools to watch a log file produced by something
# like Hazer's  gpstool running in "headless" (-H FILE) mode in which
# the latest full screen update is written to a file using the Diminuto
# Observation feature. See the Hazer/bin/base.sh and Hazer/bin/rover.sh
# for examples of this. So you might use the command
#
# headless out/host/log/base
#
# to watch the full screen updates performed by the base script. Multiple
# headless instantiations for the same output file can be run at one time.
# Exiting the script directly or indirectly (e.g. by logging off or exiting
# a terminal emulator) should have no effect on the gpstool instance running
# in the background.
#
# The optional LIMIT parameter is to deal with the vagaries of various
# terminal emulators; I'm lookin' at you, Chromebook's Beagle Term.
#
# This script traps SIGHUP and creates a timestamped copy of the next
# headless file being watched in the same directory. A HUP (hangup)
# signal can be easily sent from the bash command line by suspending the
# script (
#
# DEPENDENCIES
#
# sudo apt-get install inotify-tools
#

PROGRAM=$(basename ${0})
HEADLESS=${1:-"/dev/null"}
LIMIT=${2:-$(($(stty size | cut -d ' ' -f 1) - 2))}

CANONICAL=$(readlink -f ${HEADLESS})
DIRECTORY=$(dirname ${CANONICAL})
FILE=$(basename ${CANONICAL})
TARGET="${DIRECTORY}/ MOVED_TO ${FILE}"
CHECKPOINT=N

test -d ${DIRECTORY} || exit 1

trap "CHECKPOINT=Y" SIGHUP

clear
while MOVED=$(inotifywait -e moved_to ${DIRECTORY} 2> /dev/null); do
  if [[ "${MOVED}" == "${TARGET}" ]]; then
    if [[ "${CHECKPOINT}" == "Y" ]]; then
	TEMPORARY=$(mktemp ${CANONICAL}-XXXXXXXXXX)
	cp ${CANONICAL} ${TEMPORARY}
	mv ${TEMPORARY} ${CANONICAL}-$(date -u '+%Y%m%dT%H%M%SZ')
	CHECKPOINT=N
    fi
    clear
    awk '
      begin   { inp="INP [   ]"; out="OUT [   ]"; arm=1; }
      /^INP / { inp=substr($0,0,79); arm=1; next; }
      /^OUT / { out=substr($0,0,79); arm=1; next; }
              { if (arm!=0) { print inp; print out; arm=0; } print $0; next; }
      end     { if (arm!=0) { print inp; print out; arm=0; } }
    ' ${CANONICAL} | head -n ${LIMIT}
  fi
done

exit 0<commit_msg>[1] headless now creates PID file in current directory for SIGHUPping. [2] TESTED.<commit_after>#!/bin/bash
# Copyright 2019 Digital Aggregates Corporation, Colorado, USA
# Licensed under the terms in LICENSE.txt
# <NAME> <<EMAIL>>
# https://github.com/coverclock/com-diag-diminuto
#
# ABSTRACT
#
# Uses the inotify tools to watch a log file produced by something
# like Hazer's  gpstool running in "headless" (-H FILE) mode in which
# the latest full screen update is written to a file using the Diminuto
# Observation feature. See the Hazer/bin/base.sh and Hazer/bin/rover.sh
# for examples of this. So you might use the command
#
# headless out/host/log/base
#
# to watch the full screen updates performed by the base script. Multiple
# headless instantiations for the same output file can be run at one time.
# Exiting the script directly or indirectly (e.g. by logging off or exiting
# a terminal emulator) should have no effect on the gpstool instance running
# in the background.
#
# The optional LIMIT parameter is to deal with the vagaries of various
# terminal emulators; I'm lookin' at you, Chromebook's Beagle Term.
#
# This script traps SIGHUP and creates a timestamped copy of the next
# headless file being watched in the same directory. A HUP (hangup)
# signal can be easily sent from the bash command line by suspending the
# script (
#
# DEPENDENCIES
#
# sudo apt-get install inotify-tools
#

PROGRAM=$(basename ${0})
HEADLESS=${1:-"/dev/null"}
LIMIT=${2:-$(($(stty size | cut -d ' ' -f 1) - 2))}

CANONICAL=$(readlink -f ${HEADLESS})
DIRECTORY=$(dirname ${CANONICAL})
FILE=$(basename ${CANONICAL})
TARGET="${DIRECTORY}/ MOVED_TO ${FILE}"
CHECKPOINT=N
SELF=$$

test -d ${DIRECTORY} || exit 2

echo ${HEADLESS} > ${PROGRAM}.${SELF}
trap "CHECKPOINT=Y" SIGHUP
trap "rm -f ${PROGRAM}.${SELF}" EXIT

clear
while MOVED=$(inotifywait -e moved_to ${DIRECTORY} 2> /dev/null); do
  if [[ "${MOVED}" == "${TARGET}" ]]; then
    if [[ "${CHECKPOINT}" == "Y" ]]; then
	TEMPORARY=$(mktemp ${CANONICAL}-XXXXXXXXXX)
	cp ${CANONICAL} ${TEMPORARY}
	mv ${TEMPORARY} ${CANONICAL}-$(date -u '+%Y%m%dT%H%M%SZ')
	CHECKPOINT=N
    fi
    clear
    awk '
      begin   { inp="INP [   ]"; out="OUT [   ]"; arm=1; }
      /^INP / { inp=substr($0,0,79); arm=1; next; }
      /^OUT / { out=substr($0,0,79); arm=1; next; }
              { if (arm!=0) { print inp; print out; arm=0; } print $0; next; }
      end     { if (arm!=0) { print inp; print out; arm=0; } }
    ' ${CANONICAL} | head -n ${LIMIT}
  fi
done

exit 1
{'fraction_non_alphanumeric': 0.14303329223181258, 'fraction_numerical': 0.011919441019317715, 'mean_word_length': 3.847609561752988, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1039046', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1664, 'n_words': 592}