#!/bin/bash --norc
set -o pipefail
set -e
# note: bash-dependent, see checkbashisms

export LC_ALL=C
verbose=
OUTGITS=OUT.gits
OUTSRPMS=OUT.SRPMS
OUTREBUILD=OUT.rebuild
rebuild_if_not_found=
ignore_failure=
commit=
names=

for config_file in /etc/girar-nmu/default ${XDG_CONFIG_HOME:-$HOME/.config}/girar-nmu/default; do
    ! [ -e "$config_file" ] || . "$config_file"
done
# --- autodetection in absence of config file ---
if [ -z "$gitery_default_host" ]; then
    if grep -E '^Host[[:space:]]+gitery[[:space:]]*$' ~/.ssh/config >/dev/null; then
	gitery_default_host=gitery
    elif grep -E '^Host[[:space:]]+git.alt[[:space:]]*$' ~/.ssh/config >/dev/null; then
	gitery_default_host=git.alt
    else
	gitery_default_host=gitery
    fi
fi
if [ -z "$GIRAR_HOST" ]; then
    if grep -E '^Host[[:space:]]+girar[[:space:]]*$' ~/.ssh/config >/dev/null; then
	GIRAR_HOST=girar
    elif grep -E '^Host[[:space:]]+gyle[[:space:]]*$' ~/.ssh/config >/dev/null; then
	GIRAR_HOST=gyle
    elif grep -E '^Host[[:space:]]+gyle.alt[[:space:]]*$' ~/.ssh/config >/dev/null; then
	GIRAR_HOST=gyle.alt
    elif grep -E '^Host[[:space:]]+sborochnitsa[[:space:]]*$' ~/.ssh/config >/dev/null; then
	GIRAR_HOST=sborochnitsa
    else
	GIRAR_HOST=girar
    fi
fi
# --- end autodetection ---------------------------


# improved_getopt does not fit due to <opts,args> -- <hsh opts,args>
#OPTIONS=ZS:G:''cf
#LONGOPTIONS=,rebuild-not-found,GITS:,SRPMS:,REBUILD:,--out.gits:,--out.srpms:,--out.rebuild:'',commit,ignore-failure
#4_call_improved_getopt

while [ "$1" != '' ]; do
    case "$1" in
	-c|--commit)
	    commit='--commit'
	    readonly commit
            shift ;;
	-f|--ignore-failure)
	    ignore_failure=1
	    readonly ignore_failure
            shift ;;
	-S|--SRPMS|--out.srpms) OUTSRPMS="$2"
            shift 2 ;;
	-G|--GITS|--out.gits) OUTGITS="$2"
            shift 2 ;;
	--REBUILD|--out.rebuild) OUTREBUILD="$2"
            shift 2 ;;
	-Z|--rebuild-not-found) rebuild_if_not_found=1
            shift ;;
	-P|--profile)
	    local profile_file="$2"
	    for config_file in /etc/girar-nmu/"$profile_file" ${XDG_CONFIG_HOME:-$HOME/.config}/girar-nmu/"$profile_file"; do
		! [ -e "$config_file" ] || . "$config_file"
	    done
            shift 2 ;;
        -q|--quiet)
            verbose=
            shift ;;
        -V|--version)
            echo "2.015.3"
	    exit 0
            shift ;;
        -v|--verbose)
            verbose=$(($verbose+1))
            shift ;;
	-h|--help)
	    pod2usage --exit=0 "$0";
	    exit 0 ;;
        --)
            shift
            break
            ;;
        -*)
	    echo "Internal error: Unexpected option: $1 $2" >&2
	    #pod2usage --exit=3 "$0"
            exit 3
            ;;
        *)
	    names="$names $1"
            shift
            ;;
    esac
done

hshargs=
[ $# -gt 0 ] || hshargs="hsh --with-stuff  --mountpoints=/proc,/dev/pts"

if [ -n "$verbose" ] && [ "$verbose" -gt 1 ]; then
    echo "DEBUG: names=<$names>"
    echo "DEBUG: hsh call =$hshargs $@"
fi

get_local_rebuild_srpm()
{
    local_rebuild_srpm=`altlinux-repolist-src-names-to-src-files $ALTLINUX_REPOLIST_UTILS_OPTIONS "$1"`
    if [ -z "$local_rebuild_srpm" ]; then
	echo Command failed: altlinux-repolist-src-names-to-src-files $ALTLINUX_REPOLIST_UTILS_OPTIONS "$1"
	echo export ALTLINUX_REPOLIST_UTILS_OPTIONS to pass options to altlinux-repolist-src-names-to-src-files.
	exit 3
    fi
}

rebuild()
{
    local name="$1"
    shift
    local_rebuild_srpm=
    if [ -d "$OUTGITS/$name.git" ]; then
	pushd "$OUTGITS/$name.git"
	echo gear --hasher -- $hshargs "$@"
	gear $commit --hasher -- $hshargs "$@"
	RETVAL=$?
	popd >/dev/null
	return $RETVAL
    elif [ -d "$OUTSRPMS/$name" ]; then
	if [ -e "$OUTSRPMS/$name/"*.src.rpm ]; then
	    echo $hshargs "$@" "$OUTSRPMS/$name/"*.src.rpm
	    $hshargs "$@" "$OUTSRPMS/$name/"*.src.rpm
	    return $?
	elif [ -e "$OUTSRPMS/$name/"*.tar ]; then
	    echo $hshargs "$@" "$OUTSRPMS/$name/"*.tar
	    $hshargs "$@" "$OUTSRPMS/$name/"*.tar
	    return $?
	else
	    echo "ERROR: strange: neither src.rpm nor tar is not found in $OUTSRPMS/$name/. maybe error during girar-nmu-prepare?."
	    exit 3;
	fi
    elif [ -e "$OUTREBUILD/$name" ] || [ -n "$rebuild_if_not_found" ]; then
	get_local_rebuild_srpm "$name"
	echo $hshargs "$@" "$local_rebuild_srpm"
	$hshargs "$@" "$local_rebuild_srpm"
	return $?
    else
	echo "ERROR: NMU package not found for $name. run girar-nmu-prepare first."
	exit 3;
    fi
}

[ 0"$verbose" -ge 3 ] && set -x ||:

for name in $names; do
    if ! rebuild $name "$@" && [ -z "$ignore_failure" ] ; then
	echo "ERROR: build failed for $name"
	exit 4;
    fi
done

: <<'__EOF__'

=head1	NAME

girar-nmu-local-build - rebuild generated packages in prescribed order.

=head1	SYNOPSIS

B<girar-nmu-local-build>
[B<-c>]
[B<-f>]
[B<-G|--GITS|--out.gits> I<path/to/OUT.gits dir>]
[B<-S|--SRPMS|--out.srpms> I<path/to/OUT.SRPMS dir>]
[B<--REBUILD|--out.rebuild> I<path/to/OUT.rebuild dir>]
[B<-Z>]
[B<-P|--profile> I<profile>]
[B<-h|--help>]
[B<-v|--verbose>]
[B<-q|--quiet>]
I<name ...>
[B<--> hsh <hasher arguments...>]

=head1	DESCRIPTION

B<girar-nmu-local-build>
rebuild packages generated by B<girar-nmu-prepare> in prescribed order.
The utility invokes 

Example:
girar-nmu-local-build `cat names` -- hsh --mountpoints=/proc /tmp/hasher

=head1	OPTIONS

=over

=head1	OPTIONS

=over

=item	B<-c>

Add --commit to gear options.

=item	B<-f>

Continue building packages even if some builds fail.

=item	B<-G|--GITS|--out.gits> I<dir>

name of output directory with Git repositories.
Default is "OUT.gits".

=item	B<-S|--SRPMS|--out.srpms> I<dir>

name of output directory with src.rpms.
Default is "OUT.SRPMS".

=item	B<--REBUILD|--out.rebuild> I<dir>

name of output directory with names to rebuild.
Default is "OUT.rebuild".

=item	B<-Z|--rebuild-not-found>

Generate girar task add rebuild <name> commands if prepared packages
are not found.

=item	B<-P|--profile> I<profile>

Name of the configuration profile. The profile is loaded as
/etc/girar-nmu/I<profile> and $HOME/.config/girar-nmu/I<profile>.
Note that default configuration is stored in
/etc/girar-nmu/default and $HOME/.config/girar-nmu/default.

=item	B<-V|--version>

Print version and exit.

=item	B<-v|--verbose>

Verbose. Prints extra information. Multiple -v accumulate.
-v -v -v set debug mode (set -x).

=item	B<-q|--quiet>

Quiet. Print no warnings.

=item	B<-h|--help>

Display this help and exit.

=back

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	COPYING

Copyright (c) 2010-2023 Igor Vlasenko, ALT Linux Team.

This 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.

=cut

__EOF__
