#!/bin/bash
#
# Script for automated launch o fuss-client on fuss 12 client
#
# Copyright (C) 2005-2023 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)"

if [ -e /root/reboot ]; then

	rm /root/reboot
	if [ ! -e /root/standalone ] ; then
            if [ -e /root/cluster ] ; 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

        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)"
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/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
	# Following dhclient was necessary inside a virtual(box) environment. Not sure if needed anymore. In case it can be removed or commented.
	dhclient $myNic
	# 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

        rm /etc/ssh/ssh_host_* 
        dpkg-reconfigure openssh-server

        screen -d -m -S XY
        screen -S XY -X stuff "exec 2>&1 > /dev/tty7\n"
        if [ -e /root/join ]; then
            screen -S XY -X stuff "apt update --allow-releaseinfo-change ; apt install -y fuss-client ; fuss-client -H  $HOSTNAME $clusterJoin $light $locale $keyboard $wifi_fuss_client  || true && rm -f /etc/machine-id /var/lib/dbus/machine-id ;  dbus-uuidgen --ensure=/etc/machine-id ; dbus-uuidgen --ensure;  rm /root/join ; reboot\n"
        else
            screen -S XY -X stuff "apt update --allow-releaseinfo-change ; apt install -y fuss-client ; fuss-client -S $light $locale $keyboard || true && rm /root/standalone ; reboot\n"
        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

fi
