#!/bin/bash
#IP Adresse des HTTP Server von dem die preseed.cf geladen werden kann. Dieses Skript braucht schreibrechte im Webroot dieses HTTP Servers!
http_server=172.23.43.2
#Für / leerlassen, mit abschliessendem /
http_path=
#HTTP Webroot, dieses Skript braucht dort Schreibrechte!
http_webroot=/var/www/
#TFTP Boot Verzeichniss, dieses Skript braucht dort Schreibrechte!
tftpboot_dir=/var/lib/tftpboot/pxelinux.cfg/
#Voreinstellungen Basisteil
locale=de_DE
keyboard_layout=de
network_interface=eth0
client_hostname=testinstallation
#Globale Einstellungen zum APT Mirror
apt_protocol=http
apt_server=172.23.43.2:9999
apt_directory=/ubuntu
apt_http_proxy=
apt_universe=false
apt_multiverse=false

############### Ab hier muss nichts mehr geändert werden! ############################



echo "##############################################################"
echo "Dieses Skript hilft Ihnen eine preseed.cfg zu erstellen "
echo "Einige Einstellungen können global direkt in diesem "
echo "Skript vorgenommen werden. z.b. APT Mirror "
echo "Einstellung zum HTTP Server _muessen_ Global definiert werden!"
echo "##############################################################\n"

mac_true=n
while [ $mac_true = n ]; do
	echo -e "\nBitte geben Sie die MAC Adresse im Format: xx-xx-xx-xx-xx-xx-xx ein. "
	read mac
	echo -e "\nIst die MAC Adresse $mac richtig? (y/n)"
	read mac_true
done
#Erstmal löschen wir die alte pxeconfig
old_pxeconfig=`ls /tmp/$mac 2> /dev/null`
if [ "$old_pxeconfig" ]; then
	rm /tmp/$mac
fi
#Abfrage der Paramter für die pxeconfig und das preseed file
#Erstellen der pxeconfig
echo -e "\n########## Basis, diese Werte sollten angepasst werden! ########## \n"

basis_ready=n
while [ $basis_ready != y ]; do
	echo -e "Folgende Basis Konfiguration ist vorhanden:\n"
	echo -e "1) Locales: $locale"
	echo -e "2) Tastaturbelegung: $keyboard_layout"
	echo -e "3) Netzwerkkarte: $network_interface"
	echo -e "4) Hostname: $client_hostname"
	echo -e "\n0) Konfiguration übernhemne"
	echo -e "\nWelche Konfiguration soll geändert werden?\n"
	read basis_change
	case "$basis_change" in
		1)
			echo "Locales:"
			read locale
			;;
		2)
			echo "Tastaturlayout:"
			read keyboard_layout
			;;
		3)
			echo "Netzwerkkarte:"
			read network_interface
			;;
		4)	
			echo "Hostname:"
			read client_hostname
			;;
		0)
                        echo "Konfiguration wird übernommen"
                        basis_ready=y
                        ;;
        esac
done

#Erstellen der PXE Config mit den Werten aus Basis
echo "DISPLAY ubuntu-installer/i386/boot-screens/boot.txt" >> /tmp/$mac
echo "LABEL install_server" >> /tmp/$mac
echo "kernel ubuntu-installer/i386/linux" >> /tmp/$mac
echo "append tasks=standard vga=normal pkgsel/language-pack-patterns= pkgsel/install-language-support=false initrd=ubuntu-installer/i386/initrd.gz locale=$locale console-setup/layoutcode=$keyboard_layout netcfg/wireless_wep= netcfg/choose_interface=$network_interface netcfg/get_hostname=$client_hostname preseed/url=http://$http_server/${http_path}preseed_$mac.cfg --" >>/tmp/$mac
echo "PROMPT 1" >>/tmp/$mac
echo "TIMEOUT 0" >>/tmp/$mac

#Ab hier wird die preseed config erstellt
#Verschieben der alten Datei
old_preseedcfg=`ls /tmp/preseed_$mac.cfg 2> /dev/null`
if [ "$old_preseedcfg" ]; then
	mv /tmp/preseed_$mac.cfg /tmp/preseed_$mac.cfg.back
	echo "Vorhandene preseed Datei für $mac nach /tmp/preseed_$mac.cfg.back verschoben"
fi

#Werte aus Basis in die preseed.cfg schreiben
echo "d-i debian-installer/locale string $locale" >> /tmp/preseed_$mac.cfg
echo "d-i console-setup/ask_detect boolean false" >> /tmp/preseed_$mac.cfg
echo "d-i console-setup/layoutcode string $keyboard_layout" >> /tmp/preseed_$mac.cfg


echo -e "\n########## APT Konfiguration ##########\n"
apt_ready=n

while [ $apt_ready != y ]; do
	echo -e "Folgende APT Konfiguration ist vorhanden:"
	echo -e "1) Protokoll: $apt_protocol"
	echo -e "2) Server: $apt_server"
	echo -e "3) Ordner: $apt_directory"
	echo -e "4) HTTP Proxy: $apt_http_proxy"
	echo -e "5) Universe aktiviert: $apt_universe"
	echo -e "6) Multiverse aktiviert: $apt_multiverse"
	echo -e "\n0) Konfiguration übernehmen"
	echo -e "\nWelche Konfiguration soll geändert werden?\n"
	read apt_change
	case "$apt_change" in
		1)	
			echo "Welches Protokoll soll für APT verwendet werden? ($apt_protocol)"
			read apt_protocol
			;;
		2)		
			echo "Welcher deb Host soll genutzt werden? Alternativ Port via :PORT angeben ($apt_server)"
			read apt_server
			;;
		3)
			echo "Verzeichnissname:"
			read apt_directory
			;;
		4)
			echo "Wird einHTTP Proxy genutzt? Wenn nein leer lassen"
			read apt_http_proxy
			;;
		5)
			echo "Soll Universe genutzt werden? (true/false)"
			read apt_universe
			;;
		6)			
			echo "Soll Multiverse genutzt werden? (true/false)"
			read apt_multiverse
			;;
		0)
			echo "Konfiguration wird übernommen"
			apt_ready=y
			;;
	esac
done

		echo "d-i mirror/country string enter information manually" >> /tmp/preseed_$mac.cfg
		echo "d-i mirror/protocol string $apt_protocol" >>/tmp/preseed_$mac.cfg
		echo "d-i mirror/http/hostname string $apt_server" >> /tmp/preseed_$mac.cfg
		echo "d-i mirror/http/directory string $apt_directory" >> /tmp/preseed_$mac.cfg
		echo "d-i mirror/http/proxy string $http_proxy" >> /tmp/preseed_$mac.cfg
		echo "d-i apt-setup/universe boolean $apt_universe" >> /tmp/preseed_$mac.cfg
		echo "d-i apt-setup/multiverse boolean $apt_multiverse" >> /tmp/preseed_$mac.cfg



echo -e "\n########## Zeitzonen Einstellung ##########\n"

#Zeiteinstellungen
echo "d-i clock-setup/utc boolean true" >>/tmp/preseed_$mac.cfg
echo "In welcher Zeitzone befindet sich der Client? (z.b. Europe/Berlin)"
read timezone
echo "d-i time/zone string $timezone" >>/tmp/preseed_$mac.cfg
echo "Soll ein Zeitserver genutzt werden? (y/n)"
read ntp_answer
case "$ntp_answer" in
	y)
		echo "d-i clock-setup/ntp boolean true" >> /tmp/preseed_$mac.cfg
		echo "Bitte geben Sie den NTP Server an: (z.b. time.fu-berlin.de)"
		read ntp_server
		echo "d-i clock-setup/ntp-server string $ntp_server" >>/tmp/preseed_$mac.cfg
		;;
	*)
		echo "Es wird kein NTP Server genutzt"
		echo "d-i clock-setup/ntp boolean false" >> /tmp/preseed_$mac.cfg
		;;
esac

echo -e "\n########## Partitionierung ##########\n"

#Partitionierung
echo "Welche Festplatte soll genutzt werden? (z.b. /dev/sda)"
read harddisk
echo "d-i partman-auto/disk string $harddisk" >> /tmp/preseed_$mac.cfg
echo "Wie soll die Festplatte aufgeteilt werden?"
echo "Folgende Optionen stehen zur Auswahl, eine SWAP Partition wird immer angelegt:"
echo "root - Alle Dateien in eine Partition"
echo "home - Extra /home Partition"
echo "multi - Extra /home, /usr, /var, und /tmp Partitionen"
read harddisk_partitions
case "$harddisk_partitions" in
	root)
		echo "d-i partman-auto/choose_recipe select atomic" >>/tmp/preseed_$mac.cfg
		;;
	home)
		echo "d-i partman-auto/choose_recipe select home" >>/tmp/preseed_$mac.cfg
		;;
	multi)
		echo "d-i partman-auto/choose_recipe select multi" >>/tmp/preseed_$mac.cfg
		;;

esac
echo "Wollen Sie LVM benutzen? (y/n)"
read lvm_answer
case "$lvm_answer" in
	y)
		echo "d-i partman-auto/method string lvm" >>/tmp/preseed_$mac.cfg
		echo "d-i partman-lvm/confirm boolean true " >>/tmp/preseed_$mac.cfg
		echo "d-i partman/confirm_write_new_label boolean true ">>/tmp/preseed_$mac.cfg
		echo "d-i partman-lvm/device_remove_lvm boolean true" >>/tmp/preseed_$mac.cfg

		;;
	*)
		echo "d-i partman-auto/method string regular ">>/tmp/preseed_$mac.cfg
		;;
esac

echo "d-i partman/choose_partition select Finish partitioning and write changes to disk" >>/tmp/preseed_$mac.cfg
echo "d-i partman/confirm boolean true " >>/tmp/preseed_$mac.cfg



echo -e "\n########## Benutzerdaten ##########\n"
echo "Vollstaendigername des Benutzers"
read user_full_name
echo "d-i passwd/user-fullname string $user_full_name" >>/tmp/preseed_$mac.cfg
echo "Loginname des Benutzers"
read user_login
echo "d-i passwd/username string $user_login" >>/tmp/preseed_$mac.cfg
echo "Passwort:"
read user_password
echo "d-i passwd/user-password $user_password $user_password" >>/tmp/preseed_$mac.cfg
echo "d-i passwd/user-password-again $user_password $user_password" >>/tmp/preseed_$mac.cfg

echo -e "\n########## Pakete die Installiert werden sollen ##########\n"
echo "Welche Pakete sollen installiert werden? (z.b. openssh-server vim)"
read install_packages
echo "d-i pkgsel/include string $install_packages" >>/tmp/preseed_$mac.cfg

#fixme
#echo -e "\n########## GRUB ##########\n"
echo "d-i grub-installer/only_debian boolean true" >>/tmp/preseed_$mac.cfg

echo "d-i finish-install/reboot_in_progress note" >>/tmp/preseed_$mac.cfg

#POSTINSTALL SKRIPT
#d-i preseed/late_command string \
#	cd /target; \
#	wget http://172.23.43.2/post-install; \
#	chmod +x ./post-install; \
#	chroot ./ ./post-install; \
#	rm -f ./post-install

echo -e "\n########## Fertige preseed Konfig ##########\n"
cat /tmp/preseed_$mac.cfg

echo "Soll diese preseed Konfig aktiviert werden? (y/n)"
read activate_preseed
case "$activate_preseed" in
	y)
		echo "Pressed Konfig wird nun aktivert"
		mv /tmp/preseed_$mac.cfg $http_webroot
		;;
	*)
		echo "Pressed Konfig _nicht_ aktivert, sie finden die preseed Konfig in /tmp/preseed_$mac.cfg"
		;;
esac



echo -e "\n########## Fertige pxe Konfig ##########\n"
cat /tmp/$mac
echo "Soll diese pxe Konfig aktiviert werden? (y/n)"
read activate_pxe
case "$activate_pxe" in
	y)
		echo "Pressed Konfig wird nun aktivert"
		mv /tmp/$mac $tftpboot_dir
		;;
	*)
		echo "Pressed Konfig _nicht_ aktivert, sie finden die pxe Konfig in /tmp/$mac.cfg"
		;;
esac

exit 0;

