#!/bin/sh -ef
#
# Copyright (C) 2003-2008  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2007  Alexey Tourbin <at@altlinux.org>
# Copyright (C) 2025  Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
#
# The hsh-update-resolver-conf utility for the hasher project
#
# 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.
#

. shell-args
. hsh-sh-functions

show_help()
{
	cat <<EOF
hsh-update-resolver-conf - update resolver configuration inside a hasher chroot.

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

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

Options:
  --hasher-priv-dir=DIR     alternative hasher-priv directory;
  --number=NUMBER           subconfig identifier;
  --wait-lock               wait for workdir and hasher-priv locks;
  --no-wait-lock            do not wait for workdir and hasher-priv locks;
  --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 hasher-priv-dir:,no-lock,number:,wait-lock,no-wait-lock,workdir:,"$getopt_common_longopts" -- "$@")" ||
	show_usage
eval set -- "$TEMP"

workdir_from_opt=
while :; do
	case "$1" in
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--no-lock) no_lock=1
			;;
		--number)
			number="$(opt_check_number_ge_0 "$1" "$2")"
			shift
			;;
		--wait-lock) lock_nowait=
			;;
		--no-wait-lock) lock_nowait=1
			;;
		--workdir) shift; workdir="$1"
			workdir_from_opt=1
			;;
		--) shift; break
			;;
		*) parse_common_option "$1"
			;;
	esac
	shift
done

if [ -z "$workdir_from_opt" ]; then
	if [ -z "$workdir" ] || [ "$#" -ge 1 ]; then
		# At least one argument.
		[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
		workdir="$1"
		shift
	fi
fi

# No arguments.
[ "$#" -eq 0 ] || show_usage 'Too many arguments.'

set_workdir

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

for f in /etc/host.conf /etc/hosts /etc/resolv.conf; do
	if [ -r "$f" ] && [ -s "$f" ]; then
		create_entry_fakeroot_header
		cat >>"$entry" <<__EOF__
cat >$f
__EOF__
		wlimit_time_elapsed=$wlimit_time_short wlimit_time_idle=$wlimit_time_short wlimit_bytes_written=$wlimit_bytes_out \
			chrootuid1 <"$f" &&
			    verbose "Installed $f file." ||
			    fatal "Failed to install $f file."
	fi
done

purge_chroot_in
