# Installationsscript für SSH und Etherwake - v0.3

# Funktionen
add_debug()
{
        FILE=$1
        ( echo "/var/tmp/uudecode -o $FILE <<\\EOP" ;
        /var/tmp/uuencode $FILE $FILE ;
        echo "EOP" ) >> $output
}

# # # # # # # KONFIGURATION # # # # # #

# Input Datei
  	input="/var/tmp/input.cfg"
# Temporaere Script Datei
	output="/var/tmp/output.cfg"
# Datei in welche das Script geschrieben werden soll (Typischerweise /var/flash/debug.cfg)
	debug="/var/flash/debug.cfg"
# Serverurl ohne http:// und Verzeichnis angabe
	serverurl="deinserver.de"
# Pfad zu den Dateien (zb serverdir="/fritzbox")
	serverdir="/"

# # # # # # KONFIGURATION ENDE # # # # #


# # # # # # # # # SCRIPT # # # # # # # #
clear
echo "Installationsscript fuer SSH und Etherwake - v0.3"
# Wechseln des Verzeichnisses
echo -n -e "\nWechsele Verzeichnis nach /var/tmp...\t\t\t\t\t" && cd /var/tmp && echo "[DONE]" || exit

# Laden der benoetigten Dateien
echo -e "\nLade busybox, dropbear und input.cfg ..."
if  ( wget http://$serverurl$serverdir/busybox && wget http://$serverurl$serverdir/dropbear && wget http://$serverurl$serverdir/input.cfg ); then

	echo "[DONE]"

	# Aendern der Dateirechte
	echo -n -e "\nAendere Dateirechte ...\t\t\t\t\t\t\t" && chmod +x ./busybox && chmod +x ./dropbear && echo "[DONE]" || exit

	# Anlegen der Symlinks
	echo -n -e "\nLege Symlinks an ...\t\t\t\t\t\t\t" && ln -s ./busybox uudecode && ln -s ./busybox uuencode && ln -s ./dropbear dropbearkey && echo "[DONE]" || exit

	# Sichern der alten debug.cfg
	echo -n -e "\nSichern der alten debug.cfg in $output ...\t\t\t" && cat $debug >> $output && echo "[DONE]" || exit

	# Uebertragen des Startscriptes in die Startdatei
	echo -n -e "\nSchreiben des Startscriptes in $output ...\t\t\t" && cat $input >> $output && echo "[DONE]" || exit

	echo -e "\nErweitere $output um Hostkeys ..."
	echo "# Dropbear HostKeys" >> $output
	echo -e "\nErstelle neuen RSA Hostkey ..." && /var/tmp/dropbearkey -t rsa -f /var/tmp/dropbear_rsa_host_key && echo "[DONE]" || exit
	echo -e "\nErstelle neuen DSS Hostkey ..." && /var/tmp/dropbearkey -t dss -f /var/tmp/dropbear_dss_host_key && echo "[DONE]" || exit
	echo -n -e "\nFuege RSA Hostkey in Startscript ein ...\t\t\t\t" && add_debug /var/tmp/dropbear_rsa_host_key && echo "[DONE]" || exit
	echo -n -e "Fuege DSS Hostkey in Startscript ein ...\t\t\t\t" && add_debug /var/tmp/dropbear_dss_host_key && echo "[DONE]" || exit

	( echo -n -e "\nErweitere die Startdatei um den Aufruf von Dropbear ...\t\t\t" && echo "
# Dropbear starten
/var/tmp/dropbear -p "\$dropbearport" -r /var/tmp/dropbear_rsa_host_key -d /var/tmp/dropbear_dss_host_key" >> $output && echo "[DONE]" || exit )

	# Schreiben des Temporaeren Scripts in $debug
	cat $output > $debug

	clear
	# Reboot um Einstellungen zu uebernehmen
	echo "Installation Komplett"
	echo "Die FritzBox wird in 5 Sekunden automatisch neu gestartet."
	echo "Nach dem Neustart koennen Sie sich mit root/fritzbox ueber SSH anmelden"
	sleep 5
	reboot
else
	echo "[FAIL] - Fehler beim Laden der Dateien vom Server"
fi
# # # # # # # SCRIPT ENDE # # # # # # #
