#!/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.
		console=console
		;;
	*)
		console=$(cat /var/run/console-device)
		console=${console#/dev/}
		;;
esac

if [ -f /target/etc/init/tty1.conf ]; then
	upstart_tty1=/target/etc/init/tty1.conf
	upstart_console () {
		echo "/target/etc/init/$1.conf"
	}
elif [ -f /target/etc/event.d/tty1 ]; then
	upstart_tty1=/target/etc/event.d/tty1
	upstart_console () {
		echo "/target/etc/event.d/$1"
	}
else
	upstart_tty1=
fi

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 [ "$upstart_tty1" ]; then
		sed -e "s/^\(exec.*getty \).*/\1-L $console $ttyspeed $ttyterm/" \
		    -e "s/tty1/$console/g" \
		    "$upstart_tty1" > "$(upstart_console "$console")"
		sed -i -e "s/^\(exec.*\) -8/\1/" "$(upstart_console "$console")"
		sed -i -e "s/^\(exec.*\)-L/\1$flowctrlarg-L/" "$(upstart_console "$console")"
	fi

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

case "$(archdetect)" in
        # Workaround LP: #1171582
        armhf/generic*) exit 0 ;;
        *) : ;;
esac

# 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
	if [ "$upstart_tty1" ]; then
		sed -e "s/^\(exec.*getty \).*/\1-L $console 9600 vt100/" \
		    -e "s/tty1/$console/g" \
		    "$upstart_tty1" > "$(upstart_console "$console")"
		sed -i -e "s/^\(exec.*\) -8/\1/" "$(upstart_console "$console")"
	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
	if [ "$upstart_tty1" ]; then
		log "adding console to ${upstart_tty1#/target}"
		sed -e "s/^\(exec.*getty \).*/\1-L $console 9600 linux/" \
		    -e "s/tty1/$console/g" \
		    "$upstart_tty1" > "$(upstart_console "$console")"
		sed -i -e "s/^\(exec.*\) -8/\1/" "$(upstart_console "$console")"
	fi
fi
