#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  80 [FILENAME...]
		  80 -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -h, --help    show this message

		  FILENAME      the name of the file to work on (default:
				the list of file in the VCS repository)


		List the lines in the files that contain more than 80 columns.


		Examples:

		  Check for all the files in the current repository:

		    $ 80


		  Detect on the given two files:

		    $ 80 f1 f2
	EOF
}


for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

len() {
	awk -vNAME="$1" '
	length > 80 {
		printf "%s:%s:%s\n", NAME, FNR, $0
	}
	'
}

if [ $# = 0 ]; then
	vcs ls | while read -r f; do
		expand "$f" | len "$f"
	done
else
	for f in "$@"; do
		expand "$f" | len "$f"
	done
fi
