#!/bin/sh
#
# smarc was written by Omar Polo <op@openbsd.org> and is placed in the
# public domain.  The author hereby disclaims copyright to this source
# code.

progname="$(basename "$0")"
usage() {
	u="usage: $progname [-c cachedir] [-j n]"
	u="$u [-m maildir] [-o outdir] [-t tmpldir]"
	echo "$u">&2
	exit 1
}

# changed at install-time
libexec=/usr/local/libexec/smarc
mblaze=/usr/local/share/smarc/mblaze
tmpldir=/etc/smarc

# fix perl include path for in-repo hacking
if [ "$libexec" = . ]; then
	export PERL5LIB="$PWD${PERL5LIB:+:}$PERL5LIB"
fi

cachedir=$HOME/.cache/smarc
mdir=$HOME/Mail/smarc
outdir=/var/www/smarc

while getopts c:j:m:o:t: flag; do
	case $flag in
	c) cachedir="$OPTARG" ;;
	j) MAKE_JOBS="$OPTARG" ;;
	m) mdir="$OPTARG" ;;
	o) outdir="$OPTARG" ;;
	t) tmpldir="$OPTARG" ;;
	?) usage ;;
	esac
done

# set up the env
export CSUMDIR="$cachedir/threadsum"
export MAKE_JOBS="${MAKE_JOBS:-1}"
export MBLAZE="$mblaze"
export MBLAZE_PAGER=cat
export MDIR="$mdir"
export OUTDIR="${outdir:-/var/www/smarc}"
export TMPLDIR="$tmpldir"
export TZ=UTC

# make sure the directories are there
set -e
mkdir -p "$CSUMDIR"
mkdir -p "$OUTDIR/mail"
mkdir -p "$OUTDIR/parts"
mkdir -p "$OUTDIR/text"
mkdir -p "$OUTDIR/thread"
set +e

export PATH="$libexec:$PATH"

fmt='%i-%R %16D<%64f>%128S'
mlist "${MDIR}" | mthread -r | mscan -f "$fmt" | pe | mkindex
