#!/bin/sh
set -e

. /usr/share/debconf/confmodule

log() {
	logger -t finish-install "$@"
}

write_console() {
	if ! grep -q "$1" "$2"; then
		if ! grep -q "# serial console added by debian-installer" "$2"; then
			echo "" >> "$2"
			echo "# serial console added by debian-installer" >> "$2"
		fi
		echo "$1" >> "$2"
	fi
}

uses_hw_flowcontrol() {
	local dev="$1"

	chroot /target stty -a --file /dev/$dev | tr ' ' '\n' | \
	    grep -q '^crtscts$'
}

KEEP_VT=
if db_get finish-install/keep-consoles && [ "$RET" = true ]; then
	KEEP_VT=1
fi

# Since this script is running with debconf, 'tty' does
# not give reliable answers about what sort of terminal
# we have.  The stdin of /sbin/debian-installer seems
# to tell the truth.

case "$(udpkg --print-os)" in
	hurd)
                # TODO: detect VGA hurd console, and enable it in installed
                # system.
		consoles=/dev/console
		;;
	*)
		consoles=$(cat /var/run/console-devices)
		;;
esac

for console in $consoles
do
	console=${console#/dev/}

	case "$console" in
	    tty[A-Zu]*|duart*)
		log "Configuring init for serial console"
		consoletype=${console%%[0-9]*}
		ttyline=${console#$consoletype}
		ttyspeed=$(chroot /target stty --file /dev/$console speed)
		ttyterm="$TERM"

		flowctrlarg=""
		if uses_hw_flowcontrol $console; then
		    flowctrlarg="-h "
		fi

		if [ -z "$ttyterm" ]; then ttyterm=vt100; fi
		if [ -z "$ttyspeed" ]; then ttyspeed=9600; fi

		if [ -f /target/etc/inittab ]; then
			# Disable regular VTs
			if [ -z "$KEEP_VT" ]; then
				sed -i -e "s/^\([1-6]\):/#\1:/" /target/etc/inittab
			fi
			# Enable serial console
			sed -i -e "s/^#T0\(.*\)ttyS.*/T$ttyline\1$console $ttyspeed $ttyterm/" \
			/target/etc/inittab
		    sed -i -e "s/^\(T$ttyline.*\) -8/\1/" /target/etc/inittab
		    sed -i -e "s/^\(T$ttyline.* \)-L/\1$flowctrlarg-L/" /target/etc/inittab
		fi
		if [ "$(readlink /target/sbin/init)" = "/lib/systemd/systemd" ] ; then
		    chroot /target systemctl --no-reload --quiet enable serial-getty@"$console".service
		fi

		write_console "$rawconsole" /target/etc/securetty
		if [ -n "$console" ] && [ "$console" != "$rawconsole" ]; then
		    write_console "$console" /target/etc/securetty
		fi
		;;
	esac
done

# Set up virtualized console via onboard service processor (hvsi/hvc)
DT_ROOT=/proc/device-tree
if [ -e $DT_ROOT/chosen/linux,stdout-path ]; then
	chosen_dev=$(cat $DT_ROOT/chosen/linux,stdout-path)
	case $chosen_dev in
	    /vdevice/vty@30000000|/vdevice/vty@71000000)
		case $(cat ${DT_ROOT}${chosen_dev}/compatible) in
		    hvterm-protocol)
			console=hvsi0 ;;
		    hvterm|hvterm1)
			console=hvc0 ;;
		    *)
			log "Unable to determine type of virtualized console"
			exit 1
			;;
		esac
		;;
	    /vdevice/vty@30000001)
		console=hvsi1 ;;
	    /spider/serial)
		console=hvc0 ;;
	    /ibm,opal/consoles/serial@0)
		console=hvc0 ;;
	    /ibm,opal/consoles/serial@1)
		console=hvc1 ;;
	    /ibm,opal/consoles/serial@2)
		console=hvc2 ;;
	    *)
		exit 0 ;;
	esac

	log "Setting up virtualized serial console on /dev/$console"
	if [ -f /target/etc/inittab ]; then
		# Disable regular VTs
		if [ -z "$KEEP_VT" ]; then
			sed -i -e "s/^\([1-6]\):/#\1:/" /target/etc/inittab
		fi

		console_line="co:2345:respawn:/sbin/getty $console 9600 vt100"
		if grep -q "^#\?co:" /target/etc/inittab; then
			sed -i -e "s|^#\?co:.*$|$console_line|" \
				/target/etc/inittab
		else
			sedexp="/^#1:/i\\$console_line\\"
			sed -i -e "$sedexp" /target/etc/inittab
		fi
		sed -i -e "s/^\(co:.*\) -8/\1/" /target/etc/inittab
	fi
elif [ -e /sys/bus/xen ] && [ -e /dev/hvc0 ]; then
	console=hvc0
	log "Setting up virtualized serial console on /dev/$console"
	if [ -f /target/etc/inittab ]; then
		log "adding console to /etc/inittab"
		console_line="co:2345:respawn:/sbin/getty $console 9600 linux"
		if grep -q "^#\?co:" /target/etc/inittab; then
			sed -i -e "s|^#\?co:.*$|$console_line|" \
				/target/etc/inittab
		else
			sedexp="/^1:/i\\$console_line\\"
			sed -i -e "$sedexp" /target/etc/inittab
		fi
		sed -i -e "s/^\(co:.*\) -8/\1/" /target/etc/inittab
	fi
fi
