#!/bin/bash
#
# Copyright (C) 2007-2009  Stanislav Ievlev <inger@altlinux.org>
# Copyright (C) 2010-2013  Andrey Cherepanov <cas@altlinux.org>
# Copyright (C) 2016  Michael Shigorin <mike@altlinux.org>
# Copyright (C) 2016  Sergey V Turchin <zerg@altlinux.org>
# Copyright (C) 2022-2025  Anton Midyukov <antohami@altlinux.org>
# Copyright (C) 2023-2025  Dmitrii Fomchenkov <sirius@altlinux.org>
# Copyright (C) 2024-2025  Dmitriy Terekhin <jqt4@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/>.

alterator_api_version=1

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

print_notfound()
{
    if [ "$1" = "notfound" ];then
	echo ""
    else
	echo "$1"
    fi
}

# Print group list
print_groups()
{
    local IFS='	'
    while read name required lists conflicts label parent depends; do
	name="${name##$groups_dir/}"
	name="${name%%.directory}"
	write_table_item \
	     name "$name" \
	     parent "$parent" \
	     label "$label" \
	     required "$required" \
	     depends "$depends" \
	     lists "$(print_notfound "$lists")" \
	     conflicts "$(print_notfound "$conflicts")"
    done
}

# Raw group list
list_groups()
{
    local edition_dir=''
    read_use_edition_conf && edition_dir="/$edition_id"
    [ ! -d "$groups_dir" ] ||
	find "$groups_dir$edition_dir" -type f -name "*.directory"| LC_ALL=POSIX sort |
	    xargs alterator-dump-desktop \
		-v lang="$in_language" \
		-v out="Filename;X-Alterator-Required;X-Alterator-PackageList;X-Alterator-Conflicts;Name;X-Alterator-Parent;X-Alterator-Depends" \
		-v def="notfound;no;notfound;notfound;;;" |
	    print_groups
}

# Print profile list
print_profiles()
{
    local IFS='	'
    while read name label groups; do
        name="${name##*/}"
        name="${name%%.directory}"
        write_table_item \
             name "$name" \
             label "$label" \
             groups "$groups"
    done
}

# Raw profile list
list_profiles()
{
    local edition_dir=''
    read_use_edition_conf && edition_dir="/$edition_id"
    [ ! -d "$profiles_dir" ] ||
        find "$profiles_dir$edition_dir" -type f -name "*.directory"| LC_ALL=POSIX sort |
            xargs alterator-dump-desktop \
                -v lang="$in_language" \
                -v out="Filename;Name;X-Alterator-Groups" \
                -v def="notfound;;notfound" |
            print_profiles
}

# Return group content
read_group_content()
{
    gfile="$groups_dir/$1.directory"

    [ -e "$gfile" ] || return 0

    comment="$(alterator-dump-desktop -v lang="$in_language" -v out="Comment" -v def="" "$gfile")"
    pkg_lists="$(alterator-dump-desktop -v lang="$in_language" -v out="X-Alterator-PackageList" -v def="" "$gfile")"
    pkg_lists="${pkg_lists%;}"
    pkg_lists="$lists_dir/${pkg_lists//;/ $lists_dir\/}"

    if [ -n "$comment" ] ; then
        # Show package list with comments if it exists
        write_string_param content "$comment$(echo -e '\n'; cat $pkg_lists | sort -u | grep '^[[:alnum:]]')"
    else
        # Show package list without comments and empty lines
        write_string_param content "$(cat $pkg_lists | sort -u | grep '^[[:alnum:]]')"
    fi

}

### pkg-groups configuration
read_pkg_groups_conf()
{
    . shell-config
    . shell-var

    local expand_description_state="$(shell_config_get "$pkg_groups_conf" expand-description)"
    if [ -n "$expand_description_state" ] && shell_var_is_no "$expand_description_state"; then
        expand_description_state='false'
    else
        expand_description_state='true'
    fi

    write_bool_param expand_description "$expand_description_state"
}

### Notification about transferring the selection of profiles and groups to the installed system.
read_notifi_transfer_select_grp_conf()
{
    . shell-config
    . shell-var

    local show_message_state="$(shell_config_get "$pkg_groups_conf" show_message)"
    if [ -n "$show_message_state" ] && shell_var_is_yes "$show_message_state"; then
        show_message_state='true'
    else
        show_message_state='false'
    fi

    write_bool_param show_message "$show_message_state"
}

on_message()
{
	case "$in_action" in
	    list)
		case "${in_objects}" in
                    profiles)
                        list_profiles
                        ;;
                    *)
                        list_groups
                        ;;
                esac
                ;;
	    read)
            case "${in_objects}" in
                pkg_groups-config)
                    read_pkg_groups_conf
                    ;;
                notifi_transfer_select_grp)
                    read_notifi_transfer_select_grp_conf
                    ;;
                *)
                    [ -z "$in_group" ] || read_group_content "$in_group"
                    ;;
            esac
	esac
}

message_loop
