#!/bin/sh -ef
#
# The hsh-rebuild utility for the hasher project.
#
# Copyright (C) 2003-2022  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2006  Alexey Gladkov <legion@altlinux.org>
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

. hsh-sh-functions
. hsh-sh-rebuild-functions

show_help()
{
	cat <<EOF
hsh-rebuild - rebuild source package in chroot prepared by hsh-initroot.

Usage: $PROG [options] [<path-to-workdir>] <source-package>
  or:  $PROG [options] --workdir=DIR <source-package>

<path-to-workdir> must be valid writable directory.

Options:
  --args=ARGS                       a deprecated synonym for --rpmbuild-args;
  --build-srpm-only                 do not build binary packages, produce src.rpm only;
  --excludedocs                     do not install documentation files;
  --hasher-priv-dir=DIR             hasher-priv directory;
  --install-only                    do not build, stop after installing build dependencies;
  --mountpoints=LIST                comma-separated list of known mount points;
  --repo-mountpoints=LIST           comma-separated list of mountpoints
                                    containing a repo used for image building;
  --nodeps                          ignore package dependencies (dangerous);
  --no-sisyphus-check-in[=LIST]     do not run sisyphus_check input tests [specified in LIST];
  --no-sisyphus-check[=LIST]        do not run sisyphus_check tests [specified in LIST];
  --no-sisyphus-check-out[=LIST]    do not run sisyphus_check output tests [specified in LIST];
  --number=NUMBER                   subconfig identifier;
  --query-repackage                 repackage the source before query for requirements (default);
  --no-query-repackage              do not repackage the source before query for requirements;
  --query-req-prog=FILE             program to run to query for requirements instead of autogenerated script;
  --rebuild-prog=FILE               program to run for rebuild instead of autogenerated script;
  --repo=DIR                        repository directory;
  --repo-bin=DIR                    binary packages destination directory,
                                    overriding --repo option for binary packages;
  --repo-src=DIR                    source packages destination directory,
                                    overriding --repo option for source packages;
  --images=DIR                      image output directory;
  --rpmbuild-args=ARGS              extra arguments for rpmbuild;
  --source-only                     do not build, stop after installing source package;
  --target=ARCH                     target architecture;
  --wait-lock                       wait for workdir and hasher-priv locks;
  --no-wait-lock                    do not wait for workdir and hasher-priv locks;
  --img-apt-config                  path to a custom apt.conf file used for image building;
  --without-stuff                   do not generate apt index files;
  --with-stuff                      generate apt index files;
  --workdir=DIR                     path to workdir to use;
  -q, --quiet                       try to be more quiet;
  -v, --verbose                     print a message for each action;
  -V, --version                     print program version and exit;
  -h, --help                        show this text and exit.

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

EOF
	exit
}

TEMP=`getopt -n $PROG -o $getopt_common_opts -l args:,build-srpm-only,excludedocs,hasher-priv-dir:,install-only,mountpoints:,repo-mountpoints:,nodeps,no-lock,no-sisyphus-check::,no-sisyphus-check-in::,no-sisyphus-check-out::,no-stuff,number:,query-repackage,no-query-repackage,query-req-prog:,rebuild-prog:,repo:,repo-bin:,repo-src:,images:,rpmbuild-args:,save-fakeroot,source-only,target:,wait-lock,no-wait-lock,img-apt-config:,without-stuff,with-stuff,workdir:,$getopt_common_longopts -- "$@"` ||
	show_usage
prepare_cgroup "$TEMP" "$0" "$@"
eval set -- "$TEMP"

workdir_from_opt=
nodeps=
install_only=
source_only=
build_srpm_only=
query_repackage_option=
while :; do
	case "$1" in
		--build-srpm-only) build_srpm_only=1
			;;
		--excludedocs) exclude_docs=--excludedocs
			;;
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--install-only) install_only=1
			;;
		--mountpoints) shift; known_mountpoints="$1"
			;;
		--repo-mountpoints) shift; repo_mountpoints="$1"
			;;
		--nodeps) nodeps=--nodeps
			;;
		--no-lock) no_lock=1
			;;
		--no-sisyphus-check) shift
			[ -n "$1" ] && no_sisyphus_check="$1" || no_sisyphus_check=all
			;;
		--no-sisyphus-check-in) shift
			[ -n "$1" ] && no_sisyphus_check_in="$1" || no_sisyphus_check_in=all
			;;
		--no-sisyphus-check-out) shift
			[ -n "$1" ] && no_sisyphus_check_out="$1" || no_sisyphus_check_out=all
			;;
		--number)
			number="$(opt_check_number_ge_0 "$1" "$2")"
			shift
			;;
		--query-repackage)
			query_repackage=1
			query_repackage_option="$1"
			;;
		--no-query-repackage)
			query_repackage=
			query_repackage_option="$1"
			;;
		--query-req-prog)
			prog_query_req="$(opt_check_read "$1" "$2")"
			shift
			;;
		--rebuild-prog)
			prog_rebuild="$(opt_check_read "$1" "$2")"
			shift
			;;
		--repo) shift; repo="$1"
			;;
		--repo-bin) shift; repo_bin="$1"
			;;
		--repo-src) shift; repo_src="$1"
			;;
		--images) shift; images="$1"
			;;
		--rpmbuild-args|--args) shift; rpmargs="$1"
			;;
		--source-only) source_only=1
			;;
		--save-fakeroot) message "Warning: obsolete option $1 ignored."
			;;
		--target) shift; target="$1"
			[ -z "${target##[A-Za-z]*}" ] ||
				fatal "--target: $target: invalid architecture."
			;;
		--wait-lock) lock_nowait=
			;;
		--no-wait-lock) lock_nowait=1
			;;
		--img-apt-config) shift; img_apt_config="$1"
			;;
		--without-stuff|--no-stuff) no_stuff=1
			;;
		--with-stuff) no_stuff=
			;;
		--workdir) shift; workdir="$1"
			workdir_from_opt=1
			;;
		--) shift; break
			;;
		*) parse_common_option "$1"
			;;
	esac
	shift
done

if [ -n "$prog_query_req" ] && [ -n "$query_repackage_option" ]; then
	show_usage "--query-req-prog and $query_repackage_option are mutually exclusive options."
fi

if [ -n "$build_srpm_only" ] && [ -n "$prog_rebuild" ]; then
	show_usage '--rebuild-prog does not make sense with --build-srpm-only.'
fi

if [ -z "$workdir_from_opt" ]; then
	if [ -z "$workdir" ] || [ "$#" -ge 2 ]; then
		# One more argument for workdir.
		[ "$#" -ge 2 ] || show_usage 'Insufficient arguments.'
		workdir="$1"
		shift
	fi
fi

# Exactly one argument.
[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
[ "$#" -le 1 ] || show_usage 'Too many arguments.'

set_workdir

source="$1"
[ -z "${source##/*}" ] || source="$saved_cwd/$source"

sname="${source##*/}"
shift

lock_workdir
[ -d "$chroot" ] || fatal "$chroot: cannot find chroot."
deduce_lock_hasher_priv

install_source_package

case "$source_package_type" in
	img|oci) [ -z "$build_srpm_only" ] ||
			fatal "--build-srpm-only is not supported for $source_package_type source packages"
		;;
esac

# Execute sisyphus_check for input.
case "$source_package_type" in
	pkg.tar|src.rpm)
		run_sisyphus_check '$HOME/in/srpm' "$no_sisyphus_check_in" ;;
esac

[ -z "$source_only" ] || exit 0

# Calculate, check and install build dependencies.
if [ -z "$nodeps" ]; then
	case "$source_package_type" in
		pkg.tar|src.rpm)
			install_deps_pkg
			;;
		img)
			install_deps_img "$prog_query_req"
			;;
	esac
fi

[ -z "$install_only" ] || exit 0

case "$source_package_type" in
	pkg.tar|src.rpm)
		build_pkg
		;;
	img|oci)
		copy_apt_conf
		build_img
		;;
esac

case "$source_package_type" in
	pkg.tar|src.rpm)
		copy_results_pkg
		;;
	img|oci)
		copy_results_img
		;;
esac
