#! /bin/bash

# Step script for Debian major version upgrade for fuss
# 
# Copyright (C) 2025 FUSS Project <info@fuss.bz.it>
# Authors: Simone Piccardi <piccardi@truelite.it>
#
# 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; version 3 of the License.
#
# 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 or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307   USA


. $SCRIPTS_DIR/functions.sh

# write a log
echo "Step $STEP: checking missing run of latest Debian 12 fuss-client version"
echo "===================================================================="
echo 

exec_dpkg_configure
check_already_debian13

#
# check if fuss-client -U (to be parametrized) is needed
#

# Get last available version of fuss-client package
LAST_VERSION=$(apt list fuss-client -q 2>/dev/null | cut -d " " -f2 | grep -v Elencazione )
# Get the date of the last  “fuss-server upgrade”
USED_VERSION=$(jq -r '.version' /etc/ansible/facts.d/fuss_client.fact)

echo "# Most recent fuss-client $LAST_VERSION"
echo "# Used fuss-client $USED_VERSION"

if [ $LAST_VERSION != $USED_VERSION ]; then
    fuss-client -U
    if [ $? != 0 ]; then
	echo "#   Failed fuss-client -U"
	echo FAILED > $STEPRES
	exit 1
    else
	echo "#   Successful fuss-client -U"
	echo PASSED > $STEPRES
	exit 0
    fi
else
    echo "#   Last version used, no need to relaunch fuss-client"
    echo PASSED > $STEPRES
    exit 0
fi
