#!/bin/sh

#
# unique_html_id.sh
#
# Print those values of the HTML attibute "id"
# that are repeated. In case SHOWALL is set, all
# existing values are printed instead.
#
# With unset SHOWALL, the presence of some repeated
# label gives non-zero exit status. Otherwise, exit
# status is always naught.
#
# Version: 0.2
#
# Author: Mats Erik Andersson, 2013 - 2016.
#
# License: ISC, i.e., two-clause BSD.

set -eu

FLAG=

test -n "${SHOWALL+yes}" || FLAG=-d

SED=${SED:-sed}

sed_text='
### Insert embedded newlines to get separted id="...".
s/\(id="[^"]*"\)[ 	]*/\1\n/g

### Extract the identity value from the first embedded line.
:again
s/[^\n]*id="\([^"]*\)"/\1/
t print

### There was none.
b

### Display the first label, then check remaining pattern space.
:print
P
D
b again
'

# Temporary file used for status code.
result=`mktemp`

# Perform the planned examination, saving a copy
# of printout for additional examination. The exit
# code of uniq(1) cannot be relied upon.
$SED -n "$sed_text" "$@" | sort | uniq $FLAG | tee $result

# Evaluate outcome. An empty file makes also COUNT empty.
count=`$SED -n '$=' $result`

unlink $result

# In case of unset SHOWALL and some duplicate label was found,
# the exit status is set to no-zero.
if test -z "${SHOWALL+yes}" && test -n "$count"
then
    exit 1
fi
