#!/bin/bash
Version=0.03
Myname=${0##*/}

doc=$( cat \
<<'DOC'
= cltex - remove TeX auxiliary/intermediate files

= Synopsis
cltex [options]	[directory]	

= Options
-h,--help	print short help and exit
-H,--Help	print full documentation and exit
-V,--version	print version and exit
-v,--verbose	run verbosely
-a,--all	include pdf, dvi and ps files
-d,--dryrun	do a dry run: show what would be removed
-r,--recursive	remove files recursively

= Description
cltex removes tex auxiliary and intermediate files for all files with
|.tex|, |.dtx| and |.ltx| extensions in the current directory. With the
|--all| option, pdf, ps and dvi files are also removed. Files are removed
from the given directory, the current directory by default. With the
|--recursive| option, files are removed from subdirectories, too.

= Author
[Wybo Dekker](wybodekker@me.com)

= Copyright
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html)
DOC
)
# --- colors, help from embedded doc, die and warn ---
[ -t 1 ] && { Red=$'\e[1;31m' Mag=$'\e[1;35m' Nor=$'\e[0m'; } || Red='' Mag='' Nor=''
nv="-$Myname-${Version/./\\.} (press h for help, q to quit)"
helpsrt() { sed -n '/^= Synopsis/,/^= Desc/{//d;p}' <<<"$doc"; exit; }
helpall() { less -P"$nv" <<<"$doc"; exit; }
    die() { printf "%s$Nor\n" "${@/#/$Myname: $Red}" >&2; exit 1; }
   Warn() { printf "%s$Nor\n" "${@/#/$Myname: $Mag}" >&2; }
   warn() { $verbose && Warn "$@"; }

cleanext='{4ct,4dx,4ix,4tc,aux,bbl,blg,chk,ent,fls,glg,glo,gls,hd,idv,idx,\
ilg,ind,ist,fdb_latexmk,latexmk,lg,lof,log,lot,out,soc,synctex.gz,t2d,tmp,\
toc,tuc,tui,tuo,xdv,xdy,xref}'

options=$(getopt \
      -n "$Myname" \
      -o hHVIvdra \
      -l help,Help,version,verbose,dryrun,recursive,all \
      -- "$@"
   ) || exit 1
eval set -- "$options"
verbose=false dryrun=false OPT=(-maxdepth 1)

while true; do
   case $1 in
   (-h|--help)	# print short help and exit
         helpsrt
         ;;
   (-H|--Help)	# print full documentation and exit
         helpall
         ;;
   (-V|--version)	# print version and exit
         echo $Version
         exit
         ;;
   (-v|--verbose)	# run verbosely
         verbose=true
         shift
         ;;
   (-a|--all)	# include pdf, dvi and ps files
         cleanext=${cleanext/\}/,pdf,ps,dvi\}}
         shift
         ;;
   (-d|--dryrun)	# do a dry run: show what would be removed
         dryrun=true
         shift
         ;;
   (-r|--recursive)	# remove files recursively
         OPT=()
         shift
         ;;
   (-I)  instscript "$0" ||
            die 'the -I option is for developers only'
         exit
         ;;
   (--)  shift
         break
         ;;
   (*)   break
         ;;
   esac
done

dir=.
case $# in
(1) dir="$1"
    cd "$1" 2>/dev/null ||
	 die "Could not find or enter directory $1"
    ;;
(0) ;;
(*) die "I expect at most 1 directory, not  $1"
esac

# tex sources to be cleaned:
mapfile -t files < <(find . "${OPT[@]}" -name '*.tex' -o -name '*.dtx' -o -name '*.ltx')
set -- "${files[@]}"

if $dryrun; then
   Warn "These files will be deleted without the --dryrun option:"
   for i in "$@"; do
      eval "ls -1 \"${i%???}\"$cleanext" 2>/dev/null
   done |sed "s=^\.=$dir="
else
   for i in "$@"; do
      eval "rm -rf \"${i%???}\"$cleanext"
   done
fi

