#!/bin/sh -euC

GETTY=${PWD##*@}
if [ $GETTY = 'default' ]; then GETTY=tty1 ; fi
# Disable service, if /dev/tty{N} does not exist. See #914788.
if ! test -c /dev/$GETTY ; then
	mv /etc/service/getty@$GETTY /etc/service/.getty@$GETTY
	echo "device /dev/$GETTY not found, service disabled"
	exit 0
fi

NOCLEAR=
DEBKERN=linux
BAUD=

if [ "$GETTY" != 'console' ]; then
	fuser -v /dev/$GETTY && exit 1
else
	BAUD=9600
fi

#Hurd?
if [ $(uname -s) = 'GNU' ]; then DEBKERN= ; fi
if [ "$GETTY" = 'tty1' ] || [ "$GETTY" = 'console' ]; then NOCLEAR=--noclear ; fi

! type fgetty >/dev/null 2>&1 || exec chpst -P fgetty "$GETTY"
exec chpst -P getty ${NOCLEAR:-} ${BAUD:-38400} "$GETTY" ${DEBKERN:-}
