#!/bin/sh -efu
#
# Copyright (C) 2012-2016  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2014-2018  Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# 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, see <http://www.gnu.org/licenses/>.
#

PROG_VERSION=0.1
. shell-getopt
. shell-error

show_help()
{
	cat <<-EOF
	Usage: $PROG [options] lib <compat package> <native package>
	or:    $PROG [options] prog <compat package>

	Options:
	-a, --arch                arepo target architecture (i586 by default);
	-w, --workdir             hasher workdir (~/hasher by default);
	-v, --verbose             print a message for each action;
	-V, --version             print program version and exit;
	-h, --help                show this text and exit.

	EOF
	exit
}

print_version()
{
	cat <<-EOF
	$PROG version $PROG_VERSION
	Written by Dmitry V. Levin <ldv@altlinux.org>
	Transformed into standalone script by
	Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>

	Copyright (C) 2012-2016  Dmitry V. Levin <ldv@altlinux.org>
	Copyright (C) 2014-2018  Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
	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
	exit
}

TEMP=`getopt -n $PROG -o "a:,d,w:,v,V,h" -l "arch:,debug,workdir:,verbose,version,help" -- "$@"` ||
	show_usage
eval set -- "$TEMP"

debug=
AREPO_ARCH=i586
hasher_workdir=$HOME/hasher

while :; do
	case "$1" in
		--) shift; break
			;;
		-a|--arch) AREPO_ARCH="$1"; shift
			;;
		-d|--debug) debug=x
			;;
		-w|--workdir) hasher_workdir="$1"; shift
			;;
		-h|--help) show_help
			;;
		-V|--version) print_version
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
	shift
done

[ $# -ge 2 ] ||
	show_help

[ -z "$debug" ] ||
	set -$debug

AREPO_MODE="$1"; shift
case "$AREPO_MODE" in
	lib) [ $# -eq 2 ] || show_help
		;;
	prog) [ $# -eq 1 ] || show_help
		;;
	*)
		message "unknown mode: $AREPO_MODE"
		message "expected \`lib' or \`prog'"
		show_help
		;;
esac

AREPO_COMPAT="$1"; shift
AREPO_NATIVE=
if [ "$AREPO_MODE" = lib ]; then
	AREPO_NATIVE="$1"; shift
fi

cat > "$hasher_workdir"/chroot/.host/arepoize << EOF
#!/bin/sh -efu$debug

[ -x /usr/bin/rpmrebuild ]
[ -s /usr/lib/rpmrebuild/plugins/arepo.plug ]

AREPO_PKGLIST=\$HOME/pkg.list
[ -f "\$AREPO_PKGLIST" ] || touch "\$AREPO_PKGLIST"
AREPO_ARCH=$AREPO_ARCH
target=\$1; shift
AREPO_MODE=\$1; shift
AREPO_COMPAT=\$1; shift
AREPO_NATIVE=\$1; shift
export AREPO_PKGLIST AREPO_ARCH AREPO_MODE AREPO_COMPAT AREPO_NATIVE
time rpmrebuild \
	--include arepo.plug -np \
	--define '__find_debuginfo_files %nil' \
	--define '_spec_line_buffer_size 262144' \
	--define '_allow_deps_with_beginning_dot 1' \
	\$AREPO_COMPAT
EOF
chmod +x "$hasher_workdir"/chroot/.host/arepoize

cp "$AREPO_COMPAT" $hasher_workdir/chroot/.in/
AREPO_COMPAT=/.in/"`basename $AREPO_COMPAT`"
if [ -n "$AREPO_NATIVE" ]; then
	cp "$AREPO_NATIVE" $hasher_workdir/chroot/.in/
	AREPO_NATIVE=/.in/"`basename $AREPO_NATIVE`"
fi

target="$AREPO_ARCH-`basename $AREPO_COMPAT`"
hsh-run /.host/arepoize "$target" "$AREPO_MODE" "$AREPO_COMPAT" "$AREPO_NATIVE"
hsh-run cp /usr/src/RPM/RPMS/$AREPO_ARCH/$target /.out/

ls "$hasher_workdir"/chroot/.out/$target

exit 0
