#!/bin/bash
#
# Script for automated cloning and renaming of Fuss 9 client computers
# using mac-address as unique identifier.
# Donato Florio 11 April 2019
#
# Last modified 02 May 2019 --> added modification to support cloning on nvme* devices (HP G450)
# Last modified 07 May 2019 --> added various stuff for experimental autojoin to a domain
# Last modified 21 June 2019 --> added various stuff for experimental join to a cluster
# Last modified 01 July 2019 --> added ssh to script and changed disk detection method
#
# PRT 1 ---------------------------

service ssh start

getNetStuff(){
    if [ $1 -eq 1 ];then
	ls /sys/class/net/ -1 | grep -v lo | grep -v wlan
    else
	cat /sys/class/net/$(getNetStuff 1)/address
    fi
}

getDisk(){
    for i in $(lsblk -d -n -o NAME | grep -Ev '(loop|sr|dvd|cd)')
    do
        if [ $(cat /sys/block/$i/removable) -eq 0 ];then
            myDevice="${myDevice} $i"
        fi
    done

    if [ $(echo $myDevice | wc -w) -gt 1 ];then
        echo "ATTENZIONE! Questo computer sembra provvisto di più di un disco!"
        echo ""
        echo "La procedura automatica non prevede l'installazione su sistemi"
        echo "multidisco. Contattare il tecnico informatico."
        echo ""
        echo "Per sicurezza il computer verrà spento tra 10 secondi."
	sleep 10
	shutdown -h now
    else
        diskVar="$(echo $myDevice | tr -d [:blank:])"
    fi

    # SSD of HP g450 laptops
    if [ $diskVar == "nvme0n1" ];then
	rootPartition="p$rootPartition"
    fi
    # FINE g450
}

rootPartition=2
getDisk
rootDisk="/dev/$diskVar"
mountPoint="/mnt"
computerList="/home/partimag/computerList.txt"
macAddress=$(getNetStuff 2)

if [ "$1" = "check" ]; then
  myLine=$(grep $macAddress $computerList | tail -1)
  hostname=$(echo $myLine | awk '{print $1}')
  img=$(echo $myLine | awk '{print $3}')
  join=$(echo $myLine | awk '{print $4}')
  cluster=$(echo $myLine | awk '{print $5}')
  if ! dialog --title "FUSS FUCC" --yesno "Hostname: $hostname \nMAC: $macAddress \nImage: $img \nJoin: $join \nCluster: $cluster \nContinue?" 13 70; then
    grep -v $macAddress $computerList > ${computerList}.new
    cp ${computerList}.new ${computerList}
    rm ${computerList}.new
  fi
fi

if ! grep $macAddress $computerList; then
	dialog --title "FUSS FUCC" --msgbox "Hi,\nunfortunatley, there is no valid configuration to setup this computer. Please go through the next few steps to configure how to install this PC.\nPlease make sure that the file /home/clonezilla/computerList.txt is owned and writable by clonezilla, otherwise we'll not be able to save the informations you're going to enter." 13 70
	hostname=$(dialog --title "FUSS FUCC" --inputbox "Please, pick an HOSTNAME for this PC" --output-fd 1 10 70)
	images=$(ls -1 /home/partimag|nl)
	_img=$(dialog --title "FUSS FUCC" --output-fd 1 --menu "Choose an image to be installed" 15 50 4 $images)
	img=$(ls -1 /home/partimag | sed -n "${_img}p")
	dialog --title "FUSS FUCC" --msgbox "Set image $img" 13 70
	if dialog --title "FUSS FUCC" --yesno "Join this client to the FUSS domain?" 13 70; then
		join="join"
	else
		join="no"
	fi
	cluster=$(dialog --title "FUSS FUCC" --inputbox "Please, pick a CLUSTER for this PC" --output-fd 1 10 70)
	if dialog --title "FUSS FUCC" --yesno "Hostname: $hostname \nMAC: $macAddress \nImage: $img \nJoin: $join \nCluster: $cluster \nContinue?" 13 70; then
		echo $hostname $macAddress $img $join $cluster >> $computerList
		dialog --title "FUSS FUCC" --msgbox "Configuration completed, now start cloning." 15 70
	else
		dialog --title "FUSS FUCC" --msgbox "Will now reboot" 13 70
		reboot
		exit 0
	fi
fi

imageName=$(cat $computerList | grep "$macAddress" | awk '{print $3}')

ocs-sr -b -g auto -e1 auto -e2 -r -j2 -scr -p true restoredisk $imageName $diskVar

# # PRT 2 ---------------------------
mount $rootDisk$rootPartition $mountPoint

currentName="$(cat $mountPoint/etc/hostname)"
newName="$(grep "$macAddress" $computerList | awk '{print $1}')"

echo "SETTING HOSTNAME"

for i in hostname hosts mailname

do
    if [ -e $mountPoint/etc/$i ]; then
	sed -ie "s/$currentName/$newName/g" $mountPoint/etc/$i
    else
	echo "The file $i is not present on this system"
    fi
done



# ###  Here I insert the part for the joining preparation of the machine

# Verify whether the machine has to be joined to the domain or not.
# If so the .ssh keys are needed and also a script .....

joinVar="$(cat $computerList | grep "$macAddress" | awk '{print $4}')"

clusterVar="$(cat $computerList | grep "$macAddress" | awk '{print $5}')"

if [ "$joinVar" == "join" ];then
    rsync -a /home/partimag/.ssh/ /mnt/root/.ssh/
    cp /root/.ssh/known_hosts /mnt/root/.ssh/known_hosts
    cp /home/partimag/clientScripts/rc.local /mnt/etc/
    chmod 770 /mnt/etc/rc.local
    cp /home/partimag/clientScripts/clientScript /mnt/root/
    if [ -e /home/partimag/clientScripts/new_root_pw ] ; then
        cp /home/partimag/clientScripts/new_root_pw /mnt/root/
    fi
    chmod 770 /mnt/root/clientScript
    touch /mnt/root/reboot
fi

if [ "$clusterVar" != "" ];then
    clusterJoin="-g $clusterVar"
    echo "$clusterJoin" > /mnt/root/cluster
fi



# ### END of the preparation stuff


reboot
