1#!/bin/bash
2wspace_re='[ \t]+$'
3
4if [ $# -ge 1 ]
5then
6    if [ "$1" = "-n" ]
7    then
8        trailingcommand=(-print)
9        shift
10    else
11        trailingcommand=(-print -exec perl -i -pe "s/$wspace_re//;" '{}' ';')
12    fi
13fi
14
15if [ $# -ne 1 ]
16then
17    echo "Usage:" >& 2
18    echo "  $0 [-n] directory-to-purge" >& 2
19    echo >& 2
20    echo "  -n for \"dry-run\", to just print out files with trailing whitespace"
21    exit 1
22fi
23
24find "$1" \( -name '*.sml' -o -name '*.sig' -o -name '*.tex' -o -name '*.ML' -o -name '*.lem' -o -name '*.bib' -o -name '*.doc' \) \
25          \! -name '*ML.sml' \! -name '*Theory.sml' \! -name '*Theory.sig' \
26          -exec perl -e "while (<>) { if (/$wspace_re/) { exit 0; }}; exit 1" \{\} \; \
27          "${trailingcommand[@]}"
28