#!/bin/sh

doit=yes
show=no
temp=$(mktemp)
trap "rm -f $temp" 0 1 2 3 15

usage() {
  cat <<EOF
usage: ${0##*/} [options] [html-files]

options:
  -n   no-op
  -v   verbose, showing diff
EOF
  exit 1
}

set -- $(getopt 'nv' $*)
if test $? != 0 || test $# = 1; then
  usage
fi

for name in $*; do
  case "$name" in
    -n)
          doit=no
          ;;
    -v)
          show=yes
          ;;
    --)
          ;;
    *)
        case $(file $name) in
        *perl*|*Perl*)
          ;;
        *)
          case "$name" in
          *.pm|*.pl)
                  ;;
          *)
                  echo "...skipping $name (not a perl script)"
                  continue
                  ;;
          esac
          ;;
        esac
        perltidy "${name}" 2>/dev/null -o "${temp}"
        if cmp -s "${name}" "${temp}"
        then
          test "${show}" = yes && echo "... unchanged $name"
        else
          test ${show} = yes && diff -u $name $temp
          test ${doit} = yes && copy -v $temp $name
        fi
        rm -f "${temp}"
        ;;
  esac
done
