#!/bin/sh

usage() {
	printf 'usage: %s [-f field] [-h] [-l label] [-p point] [-t term] file..\n' "${0##*/}" 1>&2
}

field="2"
label="shear velocity [m/s]"
term="pdf"
point="-1"
while getopts 'f:hl:p:t:' arg; do
	case "${arg}" in
		f) field="${OPTARG}" ;;
		h) usage; exit 0 ;;
		l) label="${OPTARG}" ;;
		p) point="${OPTARG}" ;;
		t) term="${OPTARG}" ;;
		*) usage; exit 1 ;;
	esac
done
shift $((OPTIND - 1))

if test "$point" -lt 1; then
	rowsel="tail -n "$(printf '%d * -1\n' "$point" | bc)""
	trim="head -n 1"
else
	rowsel="head -n ${point}"
	trim="tail -n 1"
fi

for f in "$@"; do
	$rowsel "$f" | $trim
done | \
gnuplot -e "set term ${term};\
            set xlabel 'Output file [-]';\
            set ylabel '${label}';\
            plot '-' u 0:${field} with lines title ''"
