#!/bin/bash
# Desc: Writes stdin to disk every 5 seconds
yell() { echo "$0: $*" >&2; }      #o Yell, Die, Try Three-Fingered Claw technique
die() { yell "$*"; exit 111; }     #o Ref/Attrib: https://stackoverflow.com/a/25515370
try() { "$@" || die "cannot $*"; } #o
declare -a buffer # Initialize buffer array
scriptTTL="15";
bufferTTL="5";
magicWriteBuffer() {
    printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output1.txt;
    printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output2.txt;
    printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output3.txt;
}

bufferRound=0;
# Run until script TTL seconds pass
while [[ $SECONDS -lt "scriptTTL" ]]; do
    bufferTOD="$((SECONDS + $bufferTTL))";
    lineCount=0;
    # Consume stdin to fill buffer until buffer time-of-death (TOD) arrives
    while read -r -t "$bufferTTL" line && [[ $SECONDS -lt "$bufferTOD" ]]; do
	# Append line to buffer array
	buffer+=("$line");
	echo "DEBUG:Processing line:$lineCount";
	echo "DEBUG:Current line   :$line";
	echo "DEBUG:buf elem count :${#buffer[@]}";
	((lineCount++));
    done;
    # Export buffer to asynchronous processing.
    magicWriteBuffer &
    unset buffer; # Clear buffer array for next bufferRound
    # Increment buffer round
    ((bufferRound++));
done;

# Author: Steven Baltakatei Sandoval
# License: GPLv3+