#!/bin/bash
#
# Script for automated launch o fuss-client on fuss 12 client
#
# Copyright (C) 2005-2026 FUSS Project <info@fuss.bz.it>
#
# Authors:
#   Donato Florio <>
#   Marco Marinello <mmarinello@fuss.bz.it
#   Paolo Dongilli <dongilli@fuss.bz.it>
#   Elena Grandi <elena@truelite.it>
#   Simone Piccardi <piccardi@truelite.it>
#   Claudio Cavalli <ccavalli@fuss.bz.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; 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

myNic="$(ls /sys/class/net/ -1 | grep -v lo | grep -v wlan)"

LOGFILE=/var/log/fuss-fucc.log

if [ -e /root/reboot ]; then

	rm /root/reboot
	if [ ! -e /root/standalone ] ; then
            if [ -e /root/cluster ] && [ ! -e /root/cloud ]; then 
	        touch /root/join
	    #else
		#if dialog --title "FUSS FUCC" --yesno "WARNING!!! \nAn invalid cluster name was selected! \nThe computer will not be joined. \nChoose Ok if you want to proceed anyway, otherwise the machine will be turned off. \nCorrect the computerList and repeat!  " 13 70; then
                    #echo "The machine will be installed without join"
	        #else
		    #shutdown -h now
		#fi
	    fi
        fi

        # temporarily disable unattended upgrades
        # see https://wiki.debian.org/UnattendedUpgrades
        cat << EOT > /etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::Update-Package-Lists "0";
APT::Periodic::Unattended-Upgrade "0";
EOT
	> $LOGFILE
	echo "Fist step, change SSH keys and machine-id" >> $LOGFILE
        rm /etc/ssh/ssh_host_* >> $LOGFILE
        ssh-keygen -A >> $LOGFILE
	echo SSH Keys regen completed >> $LOGFILE
	echo Old machine ID is $(cat /etc/machine-id) >> $LOGFILE
	rm -f /etc/machine-id /var/lib/dbus/machine-id >> $LOGFILE
	dbus-uuidgen --ensure=/etc/machine-id >> $LOGFILE
	dbus-uuidgen --ensure >> $LOGFILE
	echo New machine ID is $(cat /etc/machine-id) >> $LOGFILE
	echo First step at first reboot completed >> $LOGFILE
	
        systemctl reboot
	exit 0
fi

if [ -e /root/cluster ]; then
	clusterJoin="$(cat /root/cluster)"
fi

if [ -e /root/light ]; then
	light="$(cat /root/light)"
fi

if [ -e /root/locale ]; then
	locale="$(cat /root/locale)"
else
        # If you want a different default, change the locale.
        locale="--locale it_IT.UTF-8"
fi

if [ -e /root/keyboard ]; then
	keyboard="$(cat /root/keyboard)"
fi

if [ -e /root/wifi ]; then
        wifi="$(cat /root/wifi)"
	wifi=$(echo "$wifi" | sed 's/\$/\\\$/g')
        wifi_fuss_client="&& fuss-client -U $wifi"
fi

if [ -e /root/join ] || [ -e /root/cloud ] || [ -e /root/standalone ]; then
	# Stop the display manager to output on tty7
	systemctl stop lightdm
	# Change root password
	if [ -e /root/new_root_pw ] ; then
		echo "root:$(cat /root/new_root_pw)" | chpasswd -e
	fi
	
	# Wait while the server is discovered
	while ! ping -c 1 proxy; do
		echo "No ping to proxy... wait 2 more seconds"
		sleep 2
	done

        # Set proxy for apt through apt-cacher-ng before fuss-client
        cat << EOT > /etc/apt/apt.conf.d/90aptcacher
# using server apt-cacher-ng proxy
Acquire::http::Proxy "http://proxy:3142";
Acquire::https::Proxy "http://proxy:3142";
EOT
	MESSAGE="
	\n
	\n
	Updating packages and installing fuss-client, it will take some time.\n
	Operations will be logged in $LOGFILE.\n
	You can follow them directly logging in another console or via SSH \n
	with the command screen -x. \n
	\n
	"
	echo -e $MESSAGE
	
        if [ -e /root/join ]; then
	    [ -d /var/log/fuss-client ] || mkdir /var/log/fuss-client
	    ln -sf /dev/tty7 /var/log/fuss-client/ansible.log
	    echo "Second step, install and run fuss-client -H"  >> $LOGFILE
	    screen -d -m -L  -Logfile $LOGFILE  bash -c "
	    apt update --allow-releaseinfo-change ; apt install -y fuss-client ; fuss-client -H  $HOSTNAME $clusterJoin $light $locale $keyboard $wifi_fuss_client  || true && rm /root/join ; rm /var/log/fuss-client/ansible.log ; reboot
	    "
        elif [ -e /root/cloud ]; then
	    [ -d /var/log/fuss-client ] || mkdir /var/log/fuss-client
	    ln -sf /dev/tty7 /var/log/fuss-client/ansible.log
	    echo "Install and run fuss-client --cloud"  >> $LOGFILE
            screen -d -m -L  -Logfile $LOGFILE  bash -c "
            apt update --allow-releaseinfo-change ; apt install -y fuss-client ; fuss-client --cloud --non-interactive $clusterJoin $light $locale $keyboard $wifi || true && rm /root/cloud ; reboot
            "
        else
	    [ -d /var/log/fuss-client ] || mkdir /var/log/fuss-client
	    echo "Updating packages, it will take around 30s" > /dev/tty7
	    ln -sf /dev/tty7 /var/log/fuss-client/ansible.log
	    echo "Second step, install and run fuss-client -S"  >> $LOGFILE
	    screen -d -m -L -Logfile $LOGFILE  bash -c "
	    apt update --allow-releaseinfo-change ; apt install -y fuss-client ; fuss-client -S $light $locale $keyboard || true && rm /root/standalone ; rm /var/log/fuss-client/ansible.log ; reboot
	    "
        fi
else
        rm /etc/rc.local
	rm /root/clientScript
	rm -fr /root/.ssh/id_rsa*
	rm /root/.ssh/known_hosts
	sed -i '/clonezilla/d' /root/.ssh/authorized_keys
        rm /root/cluster /root/light /root/locale /root/keyboard /root/wifi || true

        # re-enable unattended upgrades
        # see https://wiki.debian.org/UnattendedUpgrades
        cat << EOT > /etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOT

systemctl enable fuss_client.service
systemctl enable fuss_client.timer

fi
