#!/bin/sh -efu
#
# Copyright (C) 2026  BaseALT /basealt.ru/
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

PROG="${0##*/}"
PROG_VERSION=0.2.0
PROG_YEAR=2026

SCRIPTLIB="${SCRIPTLIB:-/usr/libexec/clickhouse-audit-export}"

DBHOST=
DBNAME=
DBUSER=

show_help()
{
	cat <<EOF
Usage: $PROG [options] -a ARCH LIST_OF_SRPM...

$PROG is a tool to export build log audit data from a Clickhouse
database.

Options:

  -a ARCH, --arch=ARCH     package build architecture (the option is
                           mandatory);

  -h HOST, --host=HOST       host name or IP to connect to Clickhouse
                           database with audit data (default is
                           localhost);

  -u USER, --user=USER       user name to authenticate with;

  -d DB, --db=DB             database name to use (default is empty);

  -O OUTDIR, --outdir=OUTDIR    directory where to write the files;

  -q, --quiet                don't print any info messages;

  -V,--version               print program version and exit;

  --help                     show this text and exit.


Report bugs to https://bugzilla.altlinux.org/.

EOF
}

print_version()
{
	cat <<EOF
$PROG version $PROG_VERSION
Written by: see the source for author info.

Copyright (C) $PROG_YEAR BaseALT /basealt.ru/
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
EOF
}

show_usage()
{
	cat <<EOF
Usage: $PROG [options] -a ARCH LIST_OF_SRPM...

Run $PROG --help to see the help page.
EOF
    return 1
}

OPTS="$(getopt -n "$PROG" -o a:h:u:d:O:qV \
             -l arch:,host:,user:,db:,outdir:,quiet,version,help -- "$@")" || show_usage
eval set -- "$OPTS"

arch=
outdir=
quiet=0
while :; do
    case "$1" in
	-a|--arch)
            shift
            arch="$1"
            ;;
	-h|--host)
            shift
            DBHOST="$1"
            ;;
	-u|--user)
            shift
            DBUSER="$1"
            ;;
	-d|--db)
            shift
            DBNAME="$1"
            ;;
	-O|--outdir)
            shift
            outdir="$1"
            ;;
	--quiet)
	    quiet=1
	    ;;
	-V|--version)
            print_version
            exit 0
            ;;
	--help)
            show_help
            exit 0
            ;;
        --)
            shift
            break
            ;;
	*)
            print_error "ERROR: Unrecognized option: %s" "$1"
            exit 1
            ;;
    esac
    shift
done

if [ -z "$arch" ]; then
    echo "Please, specify the architecture for which to export the build logs" >&2
    exit 1
fi

export DBHOST
export DBNAME
export DBUSER

print_info() {
    local fmt="$1"; shift
    # shellcheck disable=SC2059
    [ "$quiet" -ne 0 ] || printf "$fmt\\n" "$@" >&2
}

(
    if [ -n "$outdir" ]; then
	cd "$outdir"
    fi

    print_info "Extracting build index for $arch to $(pwd)/$arch-builds.index..."
    cat "$@" | "$SCRIPTLIB/extract_pkg_builds.sh" "$arch" >"$arch"-builds.index
    print_info "...done extracting build index for $arch."
    print_info "See $(pwd)/$arch-builds.index for details."

    print_info "Extracting the build logs..."
    "$SCRIPTLIB/extract_build_logs.sh" <"$arch"-builds.index
    print_info "...done extracting the build logs."
)
