#!/bin/bash

# bootloader/grub
# kernel
# inittab
# securetty

### systemd serial console is NOT handled by this script. Yet.
### do this:
### cd /etc/systemd/system/getty.target.wants
### ln -s /usr/lib/systemd/system/getty@.service getty@ttyS0.service

MYNAME=${0##*/}
DATESTRING=`date +"%Y%m%d-%H%M"`

DEVNUM="0"
SPEED="115200"
DOIT=1

function usage() {

echo "$MYNAME: usage:"
echo "$MYNAME [-r][-n] <baud> <serialno>"
echo "defaults:"
echo "  serialno: $DEVNUM"
echo "  baud:     $SPEED"
exit 1

}


while [ -n "$1" ]; do
  case "$1" in
    [0-9][0-9][0-9][0-9]*)
	# this is baud.
	SPEED="$1"
	shift
	;;
    [0-9])
	# serial device number.
	DEVNUM="$1"
	shift
	;;
    -r)
	shift
	ROOT=$1
	shift
	if [ "$ROOT" != "" ]; then
		if [ ! -d "$ROOT" ]; then
			echo "$0 -r $ROOT : not a directory. Fatal."
			exit 1
		fi
	fi
	;;
    -n)
	DOIT=0
	shift
	;;
    -h)
	usage
	exit 1
	;;
    *)
	usage
	shift
	;;
  esac
done

##############################################################################
## grub

if [ -f "$ROOT"/boot/grub/menu.lst ]; then
awk -v DEVNUM=$DEVNUM -v SPEED=$SPEED '
/^(serial|terminal)/ {
	next; 
	}
/^timeout / {
	print $0
	printf("serial --unit=%i --speed=%i\n", DEVNUM, SPEED);
	printf("terminal --timeout=5 serial console\n");
	next;
	}

/^ *kernel.*memtest/ {
	print;
	next;
	}

/^ *kernel / {
        origline=$0;
	i=1; ididit=0;
	printf("    ");
	while(i < NF) {
		if( index($i, "vga") == 1 ) {
			continue;
		}
		if( index($i, "console") == 1 ) {
		  if ( ididit == 0 ) {
		    printf("console=tty0 console=ttyS%i,%i ", DEVNUM, SPEED);
		    ididit=1 ;
		  }
		} else {
		  printf("%s ", $i);
		}
	  i++;
	  }
	if(ididit == 0) {
		printf("console=tty0 console=ttyS%i,%i ", DEVNUM, SPEED);
		ididit=1
	}
	printf("\n");
	next;
	}

{ print $0; }
END { printf("# %s\n", origline); }
' < "$ROOT"/boot/grub/menu.lst > "$ROOT"/boot/grub/menu.lst.serial

cp -a "$ROOT"/boot/grub/menu.lst "$ROOT"/boot/grub/menu.lst-$DATESTRING
if [ "$DOIT" = "1" ]; then
    cat "$ROOT"/boot/grub/menu.lst.serial > "$ROOT"/boot/grub/menu.lst
fi

else # not grub 1
echo "
grub2 configuration change is not implemented.
Please do these steps manually:

Change /etc/default/grub :
GRUB_CMDLINE_LINUX='console=tty0 console=ttyS0,115200n8'
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"

run:
grub2-mkconfig -o /boot/grub2/grub.cfg
"

fi


if [ -x /bin/systemctl ]; then
# well...

ln -s /usr/lib/systemd/system/serial-getty@.service $ROOT/etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service

else

##############################################################################
## inittab

awk -v DEVNUM=$DEVNUM -v SPEED=$SPEED '
BEGIN { ididit=0; }

/^S0:12345:respawn:.*/ {
		if( DEVNUM == 0 ) {
			printf("S%s:12345:respawn:/sbin/agetty -L %s ttyS%s vt102\n", DEVNUM, SPEED, DEVNUM);
			ididit=1;
		} else {
			print;
		}
		next;
	}

/^S1:12345:respawn:.*/ {
		if( DEVNUM == 1 ) {
			printf("S%s:12345:respawn:/sbin/agetty -L %s ttyS%s vt102\n", DEVNUM, SPEED, DEVNUM);
			ididit=1;
		} else {
			print;
		}
		next;
	}

{ print; }

END 	{
	if ( ididit == 0 ) {
		printf("\nS%s:12345:respawn:/sbin/agetty -L %s ttyS%s vt102\n", DEVNUM, SPEED, DEVNUM);
	}
	}
' < "$ROOT"/etc/inittab > "$ROOT"/etc/inittab.serial
cp -a "$ROOT"/etc/inittab "$ROOT"/etc/inittab-$DATESTRING
if [ "$DOIT" = "1" ]; then
    cat "$ROOT"/etc/inittab.serial > "$ROOT"/etc/inittab
    if [ "$ROOT" = "" ]; then
	/sbin/init q
    fi
fi

fi

##############################################################################
## securetty
if [ "$DOIT" = "1" ]; then
  if grep ttyS$DEVNUM $ROOT/etc/securetty > /dev/null ; then
	:
  else
	echo "ttyS$DEVNUM" >> $ROOT/etc/securetty
  fi
fi


