#!/bin/bash
# from https://gist.github.com/jschaub30/c67cf9e214d83accd4db - kudos
# adapted by Dominic to allow piping, any separator, left-align headings
VERSION="0.9.2 [13 Apr 2026]"
THIS=$(basename "$0")
COLUMNS="$(stty size 2>/dev/null||echo 30000)"; COLUMNS="${COLUMNS##* }"
TIMEOUT=0.1

while getopts ":dfhlnt:w" optname; do
    case "$optname" in
		"d")	DEBUG="-d";;
		"f")	FORCE="y";;
		"h")	HELP="y";;
		"l")	CHANGELOG="y";;
		"n")	NOHEADINGS="y";;
		"t")	TIMEOUT=$OPTARG;;
		"w")	COLUMNS=30000;;
		"?")	echo "Unknown option $OPTARG">&2; exit 1;;
		":")	echo "No argument value for option $OPTARG">&2; exit 1;;
		*)	# Should not occur
			echo "Unknown error while processing options">&2; exit 1;;
    esac
done
shift $((OPTIND-1))

TMPF=$(mktemp -u)
if [[ -n $1 && $1 != "-" ]]; then
	[[ -f $1 ]] || { echo "Unable to locate file '$1', aborting" >&2; exit 1; }
	SOURCEF="$1"
elif [[ -z $CHANGELOG ]]; then
	timeout "$TIMEOUT" grep . >"${TMPF}1" # put piped data into ${TMPF}1 or create null file if pipe missing or empty
	if [[ -s ${TMPF}1 || -n $FORCE ]]; then
		SOURCEF="${TMPF}1"
	else
		HELP=y
	fi
fi
[[ -n $HELP$CHANGELOG ]] && echo -e "\n$THIS v$VERSION - by Dominic (-h for help)\n${THIS//?/=}\n"
if [[ -n $HELP ]]; then
	echo -e "Converts tabular data to html <table> format for viewing with a browser or \
for use in emails with content type 'text/html'. Adapted from \
https://gist.github.com/jschaub30/c67cf9e214d83accd4db - kudos.

Usage  : $THIS [options] [csv_file,or blank/dash for stdin] [source field separator(default is comma, tilde is not allowed)] [is-a-table-line regex identifier(default '.')] [not-a-table-line regex identifier]

Example:
    echo -e \"A B C\\\n1 Fred_Bloggs 123_Acacia_Ave\"|$THIS -f - \" \"

Options: -f do not display help on null input (recommended: use this)
         -h show this help
         -l show changelog
         -n do not treat first line of each table as a header line
         -t TIME add a timeout for any piped input, specify TIME as for command 'timeout' (default: 0.1 (seconds))

Notes  : 1. If required, you can specify tab separator on command line with \$'\\\t'
         2. A regex separator should work but may need escaping, but tilde ('~') is not allowed

License: Copyright © 2026 Dominic Raferd. Licensed under the Apache License, \
Version 2.0 (the \"License\"); you may not use this file except in compliance \
with the License. You may obtain a copy of the License at \
https://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable \
law or agreed to in writing, software distributed under the License is \
distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY \
KIND, either express or implied. See the License for the specific language \
governing permissions and limitations under the License.
"|fold -sw"$COLUMNS"
fi
if [[ -n $CHANGELOG ]]; then
	[[ -n $HELP ]] && echo "Changelog:"
	echo "\
0.9 [31 Dec 2025] - add -f option
0.8 [17 Dec 2025] - show help if no param1 nor piped input (before timeout), add -t option for longer timeout if required
0.7 [29 Jan 2025] - bug fix for heading html, point out in help that tilde separator is not allowed
0.6 [16 Jan 2024] - add 4th param
0.5 [12 Jul 2021] - bugfix (replace awk script that sequences number lines)
0.4 [18 May 2021] - a couple of bugfixes
0.3 [06 May 2021] - add 3rd param, can tablify multiple tables and only those tables in a file
0.2 [18 Dec 2019] - add changelog, better help
0.1 [28 Sep 2019] - initial version
"|fold -sw"$COLUMNS"
fi
[[ -n $HELP$CHANGELOG ]] && exit 0

[[ -z $2 ]] && SEPARATOR="," || SEPARATOR="$2"
[[ -z $3 ]] && LINE_ID="." || LINE_ID="$3"
[[ -z $4 ]] && LINE_EXCL="asdfajmkdfkljasdf" || LINE_EXCL="$4"
[[ -n $DEBUG ]] && echo -e "\$SEPARATOR: '$SEPARATOR'\n\$LINE_ID: '$LINE_ID'\n\${TMPF}2: '${TMPF}2'"
sed -e "\
# sed script start
#s/\r//g
/${LINE_ID}/{/${LINE_EXCL}/!{
	s~^~<tr><td>~
	s~$SEPARATOR~</td><td>~g
	/\r$/!{s~$~</td></tr>~}
	/\r$/{s~\r$~</td></tr>\r~}
}}
# sed script end
" "$SOURCEF" >"${TMPF}2"
#" "${1:-/dev/stdin}" >"${TMPF}2"

[[ -n $DEBUG ]] && ls -l "${TMPF}2"
# now add the table(s) begin and end
if [[ -z $NOHEADINGS ]]; then
	ADDHEADINGS='s~<td>~<th align=\"left\">~g;'
fi
while IFS=- read -r TABLE_LINE_START TABLE_LINE_END; do
	[[ -n $DEBUG ]] && echo -e "\$TABLE_LINE_START: '$TABLE_LINE_START'\n\$TABLE_LINE_END: '$TABLE_LINE_END'"
	sed -i -e "\
${TABLE_LINE_END}{
  /\r/!{s~$~\n</table>~}
  /\r/{s~\r$~\n</table>\r~}
}
${TABLE_LINE_START}{${ADDHEADINGS}s~^~<table>\n~}" "${TMPF}2"
[[ -n $DEBUG ]] && ls -l "${TMPF}2"
# the source sequence here is to produce dash-delimited line sequences of table output e.g. 2-2\n5-33
done < <(grep -nE '^<tr>' "${TMPF}2"|cut -d: -f1|awk '
    function output() { print START"-"PREV }
    NR == 1 {START = PREV = $1; next}
    $1 > PREV+1 {output(); START = $1}
    {PREV = $1}
    END {output()}'|tac)
cat "${TMPF}2"
if [[ -z $DEBUG ]]; then
	rm -- "${TMPF}"[12] 2>/dev/null
else
	echo "Retained:"; ls -l "${TMPF}"[12] 2>/dev/null|sed 's/^/  /'
fi
exit 0
