#!/bin/sh

logsavedir=/var/log/installer

header () {
	echo
	echo "=============================================="
	echo "$@"
	echo "=============================================="
}

echo
echo "$(gettext "Now some questions about your install ...")"

gettext "How did you boot the installer (CD/floppy/network/..)?"
printf " "
read bootmethod

gettext "What image did you use to install? (If you can, give its URL and build date)"
echo
printf "> "
read image

gettext "Describe your machine (eg, IBM Thinkpad R32):"
printf " "
read machine

perl -e '
	$tpl=shift;
	$bootmethod=shift;
	$image=shift;
	$machine=shift;

	$|=1;
	open (IN, "$tpl") || die "$tpl: $!";
	while (<IN>) {
		if (/^Boot method: <.*>/i) {
			s/<.*>/$bootmethod/;
			print;
		}
		elsif (/^Image version: <.*>/i) {
			s/<.*>/$image/;
			print;
		}
		elsif (/^Machine: <.*>/i) {
			s/<.*>/$machine/;
			print;
		}
		else {
			print;
		}
	}
' /usr/share/installation-report/install-report.template "$bootmethod" "$image" "$machine" | tail +2 >&3

if [ ! -e "$logsavedir" ] || [ ! -e "$logsavedir/hardware-summary" ]; then
	if [ -e "/var/log/debian-installer" ]; then # old name
		logsavedir=/var/log/debian-installer
	fi
fi

if [ ! -e "$logsavedir/hardware-summary" ]; then
	gettext "Cannot find installation logs in /var/log/installer"
	echo
	yesno "$(gettext "Use installation logs from another location?") " yep
	if [ "$REPLY" = yep ]; then
		while [ ! -e "$logsavedir/hardware-summary" ]; do
			gettext "Enter location of installation logs:"
			printf " "
			read logsavedir
			if [ ! -e "$logsavedir/hardware-summary" ]; then
				gettext "$logsavedir/hardware-summary does not exist"
				echo
			fi
		done
	fi
fi

if [ -e "$logsavedir/lsb-release" ]; then
	gettext "Including installer lsb-release file in report.."
	echo
	header "Installer lsb-release file:" >&3
	cat "$logsavedir/lsb-release" 2>&1 >&3
fi

if [ -e "$logsavedir/debconf-seed" ]; then
	gettext "
Preseed information can be included in the report, if you want.
Passwords will be stripped out, and you will have the opportunity
to review the preseed information for other sensitive data before sending
the report."
	echo
	yesno "$(gettext "Include preseed information in report?") " yep
	if [ "$REPLY" = yep ]; then
		# A bit heavy handed, but including usernames is also a bit
		# insecure..
		gettext "Including preseed information in report.."
		echo
		header "Preseed file" >&3
		grep -v 'passwd' "$logsavedir/debconf-seed" 2>&1 >&3
	fi
fi

if [ -e "$logsavedir/hardware-summary" ]; then
	gettext "Including hardware summart in report.."
	echo
	header "Hardware information for installed system:" >&3
	cat "$logsavedir/hardware-summary" 2>&1 >&3
else
	gettext "Running report-hw to summarise hardware for report."
	echo
	header "Hardware information for running system:" >&3
	report-hw 2>&1 >&3
fi

if [ -e "$logsavedir/status" ]; then
	gettext "Including installer status file in report.."
	echo
	header "Installer status file:" >&3
	cat "$logsavedir/status" 2>&1 >&3
fi

if [ -e "$logsavedir/messages" ] || [ -e "$logsavedir/syslog" ] || \
   [ -e "$logsavedir/partman" ]; then
	gettext "
Additional installer log files can be included in the report.
This is generally only necessary for debugging if there was a problem with
the install."
	echo
	yesno "$(gettext "Include additional log files in report?") " nop
	if [ "$REPLY" = yep ]; then
		if [ -e "$logsavedir/syslog" ]; then
			gettext "Including installer syslog in report.."
			echo
			header "Installer syslog:" >&3
			cat "$logsavedir/syslog" 2>&1 >&3
		fi
		if [ -e "$logsavedir/messages" ]; then
			gettext "Including installer messages log in report.."
			echo
			header "Installer messages log:" >&3
			cat "$logsavedir/messages" 2>&1 >&3
		fi
		if [ -e "$logsavedir/partman" ]; then
			gettext "Including installer partman log in report.."
			echo
			header "Installer partman log:" >&3
			cat "$logsavedir/syslog" 2>&1 >&3
		fi
	fi
fi
