#! /bin/sh
# quick-mod-req: Process the request, leaving the outgoing files in
#	the subdirectory 'outgoing', and the file 'address' with
#	where to send the files
#
PATH=:.:/bin:/usr/bin:/ucb:${PATH}
export PATH

prog=$0
case $# in
    3)
	processdir=$1
	request=$2
	address=$3
	;;
    *)
	echo "Usage: `basename ${prog}` dir file" 1>&2
	exit 1
	;;
esac

if [ ! -d ${processdir} ]
then
    echo "'${processdir}' is not a directory"
    exit 1
fi

true="[ 1 = 1 ]"
false="[ 0 = 1 ]"

#
# system dependant macros
#
archive=ARCHDIR		# where the sources archive lives
destdir=${processdir}/out-${address}	# change this for your system
MOD=${processdir}/quicklist	# file which has the posted TOC from mod.sources
mailprog=${processdir}/do-mail	# back end which does mailing of outgoing srcs

if [ ! -d ${destdir} ]
then
    if [ ${request} = "Mail" ]
    then
	echo "'${destdir}' missing for 'Mail' request" 1>&2
	exit 1
    else
	mkdir ${destdir}
	chmod 777 ${destdir}
    fi
elif [ ${request} != "Mail" ]
then
    echo "'${destdir}' exists!" 1>&2
    echo -n "Delete and continue (y/n) [y] "
    read ans
    case ${ans} in
	N*|n*)
	    exit 99 # let the driver know we should continue
	    ;;
	*)
	    /bin/rm -rf ${destdir}
	    mkdir ${destdir}
	    chmod 777 ${destdir}
	    ;;
    esac
fi

#
# boolean parameters
#
verbose=${false}	# talk to us
veryverbose=${false}	# LOTS
showexec=${false}	# don't show execution (turn on -x)
nooptimize=${true}	# don't optimize the archive extraction
nodebug=${true}		# we are not 'debugging'

if ${showexec}
then
    set -x
fi

#
# list of temp files
#
tmp=${processdir}/tmp.$$
tmp1=${processdir}/tmp1.$$
list=${processdir}/list.$$

#
# validate that important things are here
#
if [ ! -f ${mailprog} ]
then
    echo "${mailprog} is missing" 1>&2
    exit 1
fi
if [ ! -d ${archive} ]
then
    echo "'${archive}' is not a directory or is missing" 1>&2
    exit 1
fi
if [ ! -d ${destdir} ]
then
    echo "'${destdir}' is not a directory or is missing" 1>&2
    exit 1
fi
if [ ! -r ${MOD} ]
then
    echo "'${MOD} is not readable or is missing" 1>&2
    exit 1
fi

if [ ${request} = "Mail" ]
then
    ${mailprog} ${request} ${address} ${processdir} ${prog} &
    exit 0
fi

if ${veryverbose}; then verbose=${true}; fi

if ${verbose}; then echo -n "Setup, "; fi

if [ ! -r ${request} ]
then
    echo "Can't find ${request}" 1>&2
    exit 1
fi

if [ -f ${request}.Work ]
then
    echo "Work file exists for ${request}" 1>&2
    exit 1
fi

if ${nodebug}
then
    trap "echo 'cleaning up'; /bin/rm -f $list $tmp $tmp1 ${request}.Work ; exit 1" 1 2 3 9 15
fi

if ${veryverbose}; then echo -n "Create address"; fi

cp ${request} ${request}.Work

# construct the address to send to
#
ed - ${request}.Work <<'lasted' 2>/dev/null
/^END$/
+1,w address.tmp
1,/^BEGIN$/d
/^END$/,$d
w
q
lasted

mv address.tmp ${address}

sed -e 's/ /_/g' < ${request}.Work > ${request}.tmp
mv ${request}.tmp ${request}.Work

/bin/rm -f ${tmp}

if ${verbose}; then echo -n "Lookup from quick TOC, "; fi

# lookup the line in the posted table of contents.
while read line
do
    if ${veryverbose}; then echo -n "${line}, "; fi
    grep "^${line} " ${MOD} >> ${tmp}
done < ${request}.Work

sort -u ${tmp} -o ${tmp}

if ${verbose}; then echo "done"; echo -n "Build extract list, "; fi
ext=1
#
# now we can extract the files and stuff them into 'destdir'.
# Foreach line from the table of contents, process it into
# something which can be 'extracted' from the archive.
#
while read line
do
    if ${veryverbose}; then echo -n "${line}, "; fi
    leng=`expr length "${line}"`
    if [ ${leng} -lt 12 ]
    then
	echo "Line '${line}' too short, skipped" 1>&2
    else
	set $line
	shift		# skip mod.sources name
	dir=$1; shift
	file=$1; shift
	name=$1
	shift
	while [ $# -gt 0 ]
	do
	    name="${name}_${1}"
	    shift
	done
	xname=`echo $name |
	    tr '[\"\.\<\>\,\|\\\[\]{}]' '_' |
	    tr '[\`\!\@\#\$\%\^\&\*\(\)]' '_' |
	    tr "[\-\+\=\~\'\;\:\/\?\.]" "_" |
	    sed -e 's/^_//' -e 's/__/_/g' -e 's/_$//'`
	yname=`echo $xname | sed -e 's/__/_/g'`
	if ${nooptimize}
	then
	    echo -n "./get mod " >> ${tmp1}
	fi
	echo "${dir} ${file} ${destdir}/${yname}.${ext}" >> ${tmp1}
	ext=`expr ${ext} + 1`
    fi
done < ${tmp}

cp ${tmp1} ${tmp}

if ${verbose}; then echo "done"; fi

#
# if not optimizing the extraction, then
# move the new temp file to the final destination
# otherwise, process for cleaner extraction
#
if ${nooptimize}
then
    cp ${tmp} ${tmp1}
else
    sort ${tmp} > ${tmp1}
    /bin/rm -f ${tmp}
    if ${veryverbose}; then echo "Optimize extraction commands"; fi
    curmonth="FOO"
    curline="X"
    while true
    do
	if read line
	then
	    set ${line}
	    if [ $1 = ${curmonth} ]
	    then
		curline=`echo -n "${curline}$2 $3 "`
	    else
		if [ `expr length "${curline}"` -gt 1 ]
		then
		    echo ${curline} >> ${tmp}
		fi
		curmonth=$1
		curline="./get mod ${curmonth} $2 $3 "
	    fi
	else
	    echo ${curline} >> ${tmp}
	    break
	fi
    done < ${tmp1}
    sort -u ${tmp} > ${tmp1}
fi # no optimize

#
# do the extraction from the archive
#
while read line
do
    if ${veryverbose}; then echo "${line}"; fi
    if ${nodebug}
    then
	(cd ${archive}; ${line})
    else
	echo "(cd ${archive}; ${line})"
    fi
done < ${tmp1}

echo "Requested Modules were:"
pr -t -l1 -5 ${request}.Work

if ${nodebug}
then
    /bin/rm -f $list $tmp $tmp1 ${request}.Work
fi
${mailprog} ${request} ${address} ${processdir} ${prog} &
exit 0
