#!/bin/sh

#
# message_to_rssacint_combined
#
# Copyright (C) 2023-2024 University of Southern California.
# All rights reserved.                                            
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#

usage () {
    cat 1>&2 <<END
usage:
        message_to_rssacint_combined filename.message_question.fsdb.xz

$@

convert one file to rssacint.  Input is auto-unxz'ed file the input
filename ends in .xz.

By default, output goes to the derived filename in the current directory.

OPTIONS:

    -o OUTPUT    write output to OUTPUT (perhaps /dev/stdout)

END
    exit 1
}

die () {
	echo "$@" 1>&2
	exit 1
}

DEBUG=false
while getopts do: ch
do
        case $ch in
                d) DEBUG=true;;
                o) OUT="$OPTARG";;
                *) usage;;
        esac
done
shift `expr $OPTIND - 1`

IN=$1
OUT=${OUT:-`basename $IN .message_question.fsdb.xz`.rssacint.fsdb}
CODEDIR=`dirname $0`

# already handled
test -s "$OUT"  &&  exit 0

CATTER=cat
case "x$IN" in
        *.xz) CATTER=xzcat;;
esac

$CATTER $IN |
  $CODEDIR/message_to_rssacint --file-seqno=comment |
  LC_COLLATE=C sort -k 1,1 |
  $CODEDIR/rssacint_reduce >$OUT

exit $?
