#!/bin/sh
#
# Example init.d script with LSB support.
#
# Please read this init.d carefully and modify the sections to
# adjust it to the program you want to run.
#
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
### BEGIN INIT INFO
# Provides:          samsung-console-init
# Required-Start:    udev checkroot
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set up a serial console on Samsung platforms
# Description:       Finds the proper serial device, links it to
#                    /dev/samsung-console and adds to /etc/securetty
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

NAME=samsung-console-init
DESC=samsung-console-init

. /lib/lsb/init-functions

set -e

case "$1" in
  start|restart|force-reload|reload)
        log_daemon_msg "Starting $DESC " "$NAME"
        sed -r 's/.*console=([[:alnum:]]+[[:alpha:]])([[:digit:]]*).*/\1 \2/' /proc/cmdline |
        while read con_base con_num
        do
            grep "$con_base " /proc/tty/drivers |
            while read con_base con_dev rest
            do
                CON="$con_dev$con_num"
                CON_BASE="`basename $CON`"
                echo " $CON"
                [ ! -e /etc/securetty ] || grep -q "$CON_BASE" /etc/securetty || echo "$CON_BASE" >>/etc/securetty
                ln -sf "$CON" /dev/samsung-console
            done
        done
        ;;
  stop)
        ;;
  force-stop)
        ;;
  status)
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0

