#!/bin/bash

# extract the serial console device and options from
# the kernel command line and spawns a getty on it
# passing all options as options to getty plus the -L
# option (local line) and with "vt102" as the terminal

regex="\bconsole=([^,/[:space:]]+),?([0-9]*)"

cl="$(</proc/cmdline)"

if [[ "$cl" =~ $regex ]]; then
    port="${BASH_REMATCH[1]}"
    baud="${BASH_REMATCH[2]}"
    case "$baud" in
	115200 | 57600 | 56000 | 38400 | 28800 | 19200 | \
	14400 | 9600 | 4800 | 2400 | 1200 | 600 | 300 | 110 )
	    # standard rate
	    ;;
	"" )
	    # no rate => use kernel default
	    baud=9600
	    ;;
	*)
	    # non-standard rate => use kernel default
	    baud=9600
    esac
    ignore=
    if [ -c /dev/"$port" ]; then
        for p in ; do
            [ "$p" = "$port" ] || continue
            ignore=1
            break
        done
    else
        ignore=1
    fi
    [ -n "$ignore" ] || exec /sbin/getty "$@" -L "$baud" "$port" vt102
fi

# failed, just sleep to avoid init spawning this script repeatedly
exec /bin/sleep 2147483647 < /dev/null # max signed 32 bits
