#!/bin/ksh93

## \ProvidesFile{adhocfilelist.sh}
##              [2012/11/19 v0.71 command line -> \string\listfiles 
##                                                      \space(UL)]

typeset -r FPROG=${.sh.file}
typeset -r PROG=${FPROG##*/}
SDIR=${FPROG%/*}

function showUsage {
	[[ -z $1 ]] && X='--man' || X='-?'
	getopts -a ${PROG} "${ print ${USAGE} ; }" OPT $X
}

## reporting detected options:
function opt_log {
    print '+ adhocfilelist +' "$@" '+'
}

adhoc_head='\\RequirePackage{myfilist}[2012/10/11]\\EmptyFileList'
adhoc_find_std='-L -maxdepth 1 ( -name \*.tex'			## -L 2012-11-19
for X in sty cfg cls dtx def fd ; do
	adhoc_find_std+=" -o -name \*.$X"
done
adhoc_find_std+=' )'

## standard action (v0.7):
adhoc_fmt_std=',%f'
adhoc_fmt=${adhoc_fmt_std}
adhoc_latex='pdftex'

function doMain {
	if [[ -n ${adhoc_find} ]]; then
		## as `find' input
		if [[ -n ${adhoc_find_file} ]]; then
			adhoc_file_find=$(< $adhoc_find_file)   ## bad
			adhoc_file_find="${adhoc_file_find//$'\n'/ }"
		else
			adhoc_file_find="$adhoc_find_std"
		fi
		#echo adhoc_file_find = $adhoc_file_find; ## rm. 2012/10/15
		if [[ -n ${adhoc_day} ]]; then
			#adhoc_file_find="$adhoc_file_find    ## bad before $@
			adhoc_day="-daystart -mtime ${adhoc_day}"
		fi
		print "find criteria: ${adhoc_file_find} $@ ${adhoc_day}" ## in 2012/10/15
		adhoc_files="${ find ${adhoc_file_find} $@ ${adhoc_day} \
                       -printf "${adhoc_fmt}" ; }"
	else    ## as list of file names
		#adhoc_files="$@" ## works too
		adhoc_files="$*"
	fi
	## run latex with generated list of files, unless empty:
	#if [ $adhoc_files ]; then ## bad with $@/$*
	if [[ -n ${adhoc_files} ]]; then
		print "Files: ${adhoc_files}"
		if [[ ${adhoc_fmt} == "${adhoc_fmt_std}" ]]; then 
			adhoc_list="\\ReadListFileInfos$adhoc_target{$adhoc_files}"
		else
			adhoc_list='\\RequirePackage{filedate}\\EmptyFileList\\ModDates
'"${adhoc_files} \\ListInfos${adhoc_target}\\\\relax"
		fi
		#echo TeX Code:
		#echo $adhoc_head $adhoc_find \\NoBottomLines $adhoc_list
		#echo $adhoc_head $adhoc_find \\NoBottomLines $adhoc_list > adhoclsi.tex
		#latex adhoclsi.tex
		${adhoc_latex} "${adhoc_head}" "${adhoc_find}" '\\NoBottomLines' \
			"${adhoc_list}"
	else
		showUsage 1
	fi
}

USAGE='[-?0.71 ]
[-copyright?Copyright (c) 2012 Uwe Lück.]
[-copyright?Copyright (c) 2015 Jens Elkner (ksh93 port).]
[-license?LaTeX Project Public License]
[+NAME?'"${PROG}"' - produce a stand-alone list of \\Provides...]
[+DESCRIPTION?'"${PROG}"' varies LaTeX'"'"'s \\listfiles to produce a stand-alone list of \\Provides... contents for files typed explicitly or found by the \bfind\b(1) utility.]
[+?Without \bfind\b(1) \aFILES\a is a comma-separated list of file names. Without -l or -n, \bmyfilist.sty\b is used. For more information (e.g. the mentioned *.sty files) see latexfileinfo_pkgs.htm from package http://ctan.org/pkg/latexfileinfo-pkgs .]
[h:help?Print this help and exit.]
[F:functions?Print a list of all functions available.]
[T:trace]:[fnList?A comma separated list of functions of this script to trace (convinience for troubleshooting).]
[+?]
[0?Files modified today, in addition to -g .]
[a]:[num?Files modified \anum\a days ago (see also -0).]
[c?Compare info with modification dates (filedate.sty).]
[f?Read \aFILES\a as \bfind\b(1) options and tests (*.tex).]
[F]:[file?Read (first) \bfind\b(1) options/tests from \afile\a (*.tex).]
[g?Add \aFILES\a to standard \bfind\b(1) restrictions (see also -f).]
[i]:[file?Input TeX \afile\a (maybe for more "nicefilelist" settings, see also \bmyfilist\b v0.5 documentation).]
[l]:[num?Maximum (base) filename length for \blongnamefilelist.sty\b.]
[n]:[file?Longest (base) \afile\aname for \bnicefilelist.sty\b (or ".").]
[o]:[file?Write list to \afile\a (plain text).]
[x?Use \bxetex\b(1) instead of \bpdftex\b(1).]
\n\n[\aFILES\a]
'
X="${ print ${USAGE} ; }"
while getopts "${X}" OPT ; do
	case ${OPT} in
		h) showUsage ; exit 0 ;;
		T)	if [[ ${OPTARG} == 'ALL' ]]; then
				typeset -ft ${ typeset +f ; }
			else
				typeset -ft ${OPTARG//,/ }
			fi
			;;
		F) typeset +f && exit 0 ;;
		0)										## was -2 in v0.5
			adhoc_day='0' 
			adhoc_find='\UseFindUtility'
			opt_log 'looking for files modified today, using standard settings'
			## <- plural missing 2012-10-17 
			;;
		a)										## was -d in v0.4
			adhoc_day="${OPTARG}" 
			adhoc_find='\UseFindUtility'
			opt_log "looking for files modified ${adhoc_day} days ago," \
				'using standard settings'
			;;
		c)										## v0.7
			adhoc_find='\UseFindUtility'
			adhoc_fmt='\\ReadCheckDateOf{%f}{%TY/%Tm/%Td}'
			;;
		f)
			adhoc_find='\UseFindUtility'
			adhoc_find_std=''
			opt_log 'expecting find utility, adding nothing'
			;;
		F)
			adhoc_find='\UseFindUtility'
			adhoc_find_file="${OPTARG}"
			opt_log "reading find criteria from ${adhoc_find_file}"
			;;
		g)										##v0.5
			adhoc_find='\UseFindUtility'
			opt_log 'expecting find utility, using standard settings'
			;;
		i)										## was -m) before v0.5
			adhoc_head="\\input{${OPTARG}}"		## \input allows -l/-n
			opt_log "formatting by ${adhoc_head}"
			;;
		l)
			adhoc_head='\\RequirePackage{longnamefilelist}
\listfiles['"${OPTARG}"']
'"${adhoc_head}"								## default or -i
			## v0.6 not using \MaxLengthEmptyList thinking of -i ...
			opt_log 'formatting with "longnamefilelist",' \
				"max. name length ${OPTARG}"
			;;
		n)
			[[ ${OPTARG} == '.' ]] && OPTARG='abcdefgh'	## v0.5
			adhoc_head='\\RequirePackage{nicefilelist}
\\MFfieldtemplate{f-base}{'"${OPTARG}"'}
'"${adhoc_head}"
			## v0.6 not using \MaxBaseEmptyList thinking of -i ...
			opt_log 'formatting with "nicefilelist", template ' "'${OPTARG}'"
			;;
		o)
			adhoc_target="[${OPTARG}]"
			adhoc_head="${adhoc_head}"'
\\def\\ListGenerator{^^J by adhocfilelist.sh^^J}'
			opt_log "writing list to ${OPTARG}"	## still was $OPTARG before v0.7
			;;
		x)										## v0.7
			adhoc_latex='xetex'
			opt_log "using xetex"
			;;
	esac
done
X=$((OPTIND-1))
shift $X && OPTIND=1
unset X

doMain "$@"
