#!/bin/bash
#
# Copyright (C) 2007-2009  Stanislav Ievlev <inger@altlinux.org>
# Copyright (C) 2011  Andrey Cherepanov <cas@altlinux.org>
# Copyright (C) 2011-2016  Anton V. Boyarshinov <boyarsh@altlinux.org>
# Copyright (C) 2022-2025  Anton Midyukov <antohami@altlinux.org>
# Copyright (C) 2024  Ajrat Makhmutov <rauty@altlinux.org>
# Copyright (C) 2025  Ivan A. Melnikov <iv@altlinux.org>
# Copyright (C) 2025  Mikhail Efremov <sem@altlinux.org>
#
# This program 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 3 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 <https://www.gnu.org/licenses/>.

apt_install="/usr/bin/apt-get --simple-output install"

templist=

alterator_api_version=1

. alterator-sh-functions
. alterator-pkg-functions

export LANG=C


message_quote()
{
    echo "$1"|string_quote
}

pkg_size_to_human_readable()
{
    numfmt --to=iec "$1" 2>/dev/null | sed 's/K$/k/'
}

stdout_handler()
{
	while read -r n;do
		if [[ "$n" == "apt-get:status:disk-size:"* ]]; then
			if [ -s /tmp/live-size ]; then
				local pkg_size="${n#apt-get:status:disk-size:}"
				local live_size="$(head -1 /tmp/live-size)"
				pkg_size="$(pkg_size_to_bytes "$pkg_size")"
				if [ -n "$pkg_size" ] && [ -n "$live_size" ]; then
					local tmp="$(pkg_size_to_human_readable "$(($pkg_size + $live_size))")"
					[ -z "$tmp" ] || n="apt-get:status:disk-size:$tmp"
				fi
			fi
			pkg_size_to_file "$n"
		fi
		alterator-mailbox-send "message \"$(message_quote "$n")\""
	done
}

trap exit_handler EXIT HUP INT QUIT TERM

make_manifest()
{
    rm -f "$templist"
    templist="$(mktemp -t pkg-size.XXXXXX)"
	local base_listname=".base"
	if [ -r "$lists_dir/$edition_id/base" ]; then
		read_use_edition_conf && base_listname="$edition_id/base"
	fi
	cd "$lists_dir"
	    paste -d\\n "$@" "${base_listname}" |
		sed -r \
		    -e '/^[[:space:]]*$/ d'\
		    -e '/-[[:space:]]*$/ d'\
		    -e '/^[[:space:]]*#/ d' >"$templist"
	cd - >/dev/null

    echo "$templist"
}

make_apt()
{
	local rc=0

	echo "pkg-size:start"
	echo "$in_lists" > "$pkg_list_file"
	local tmpfile="$(make_manifest ${in_lists//;/ })"
	if [ -s "$tmpfile" ]; then
	    echo "n"|apt-get --simple-output install --manifest "$tmpfile" 2>> /tmp/install2.log || rc=$?
	else
	    echo "apt-get:status:disk-size:0k"
	fi
	rm -f "$tmpfile"

	# ignore the no free space error before partitioning the disk
	local no_space_err_msg="E: You don't have enough free space in "
	if [[ ! -e /tmp/fstab && "$(tail -n 1 /tmp/install2.log)" == "$no_space_err_msg"* && "$rc" == "100" ]]; then
		rc=1
	fi
	echo "pkg-size:finish:$rc"
}

make_pipe()
{
    make_apt|stdout_handler
}

stop_pipe()
{
    killall -9 apt-get >/dev/null 2>/dev/null ||:
}

start_pipe()
{
    stop_pipe
    make_pipe&
}

on_message()
{
	case "$in_action" in
		write)
			start_pipe
			;;
	esac
}

message_loop
