#!/bin/sh
### BEGIN INIT INFO
# Provides:          fa-set-7in-rows
# Required-Start:    $local_fs $remote_fs $console-screen
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Description:       This script ensures that all the lines are visible on the 6100's 7-in screen after we've booted the system.
# Short-Description:	Readjust console rows for 6100
### END INIT INFO


# Author: Andrew Dorney <adorney@forceamerica.com>
# Project: P1131 - 6100 Spreader Control
# Created 12/03/2008
# Modified 12/05/2008
# Modified 01/20/2009 By Andrew Dorney
#		- Change the fb test to include video= as well.
# Modified 02/25/2010 By Andrew Dorney
#       - Remove the test cases since "we control the horizontal and the vertical", as well as GRUB's menu.lst.
#       - Fix tabbing issues, add some comments.

. /lib/lsb/init-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
STTY="/bin/stty"
FGCONSOLE="/bin/fgconsole"
STTY_OPTS="rows 30"
DEVICE_PREFIX="/dev/tty"

setup()
{
    # If stty or fgconsole isn't executable, quit.
    if [[ ! -x "$STTY" ]]; then
        log_action_end_msg 1
        echo "stty is not executable."
        return 0
    fi

    if [[ ! -x "$FGCONSOLE" ]]; then
        log_action_end_msg 1
        echo "fgconsole is not executable."
        return 0
    fi

    NUM_CONSOLES=`fgconsole --next-available`
    NUM_CONSOLES=$(($NUM_CONSOLES - 1))

    # If NUM_CONSOLES is = 1 or 0, set it to 7, otherwise continue.
    if [[ ${NUM_CONSOLES} -eq 1 ]]; then
        NUM_CONSOLES=7
    elif [[ ${NUM_CONSOLES} -eq 0 ]]; then
        NUM_CONSOLES=7
    fi

    # Loop to resize each active console to 30 rows.
    i=1
    while [[ "$i" -lt "$NUM_CONSOLES" ]]; do

        # Set the current console rows to 30.
        ${STTY} --file=${DEVICE_PREFIX}$i ${STTY_OPTS}

        # If the $(STTY) call above failed, then quit.
        if [[ $? -ne 0 ]]; then
            if [[ "$VERBOSE" != "no" ]]; then
                log_action_end_msg 1
            fi
            break

        # Otherwise, if we have iterated through all consoles,
        # end with a DONE log message.
        elif [ "$i" -eq "$NUM_CONSOLES" ]; then
            if [[ "$VERBOSE" != "no" ]]; then
                log_action_end_msg 0
            fi
        fi
        i=$(($i + 1))
    done
}

case "$1" in
    start|reload|restart|force-reload)
        log_action_msg "Readjusting console rows for the 6100"
        setup
    ;;
    stop)
    ;;
    *)
        setup
    ;;
esac
