#!/bin/bash

ME=$(basename $0)

splash_ctl=fbcondecor_ctl.static
LOG_FILE=/var/log/$ME.log

usage() {
    local ret=${1:0}
    cat <<Usage
Usage: $ME [options] [1-6] theme
Set the background console decoration on one virtual console

Options:
    -a --auto     Only run if a splash= boot parameter was given.
                  Set vt to 1, set theme to default or as defined
                  in the splash boot parameter
                  Log messages to $LOG_FILE
    -h --help     Show this help
    -q --quiet    Supress messages

Themes:
$(list_themes)
Usage
    exit $ret
}

main() {

    local param
    while [ $# -gt 0 -a -n "$1" -a -z "${1##-*}" ]; do
        param=${1#-}
        shift
        case $param in
            -auto|a) do_auto_mode  ;  exit 0         ;;
            -help|h) usage                           ;;
           -quiet|q) QUIET_MODE=true                 ;;
        esac
    done

    case $# in
        0) usage                           ;;
        1) term=$(fgconsole); theme=$1     ;;
        2) term=$1; theme=$2               ;;
        *)  error "Extra command line parameters" ;;
    esac

    which $splash_ctl &>/dev/null || error "Could not find program $splash_ctl"

    if [ ! -d /etc/splash/$theme ]; then
        cat <<Theme_Not_Found >&2
$ME Error: Theme "$theme" not found.
Valid themes:
$(list_themes)
Theme_Not_Found
         exit 1
    fi

    case $term in
        [1-9]|[1-6][0-9]) ;;
            *) error "Invalid console number: $term" ;;
    esac

    set_splash $term "$theme"
}

set_splash() {
    local term=$1  theme=$2
    [ "$QUIET_MODE" ] || echo "$ME $term -> $theme"
    $splash_ctl --tty=$term -t "$theme" -c setcfg 2>/dev/null
    $splash_ctl --tty=$term -t "$theme" -c setpic 2>/dev/null
    $splash_ctl --tty=$term -c on
}

do_auto_mode() {

    local fb_name fb_name_file=/sys/class/graphics/fb0/name
    test -e $fb_name_file || return
    read fb_name 2>/dev/null < $fb_name_file

    case $fb_name in
      "VESA VGA"|simple) return ;;
    esac

    read_splash_param
    local tsplash=/live/bin/tsplash
    local tsplash_tty_file=/live/config/tsplash-tty
    local tsplash_tty=$(cat $tsplash_tty_file 2>/dev/null)

    local do_tsplash

    [ -x $tsplash -a -n "$tsplash_tty" ] && do_tsplash=true

    if [ -n "$SPLASH" ]; then
        set_splash 1 ${THEME:-default} >> $LOG_FILE
        [ "$do_tsplash" ] || return
        set_splash  $tsplash_tty ${THEME:-default} >> $LOG_FILE
    fi

    [ "$do_tsplash" ] || return
    openvt -c$tsplash_tty $tsplash redraw
    exit 0
}

# Not currently used
set_font() {
    prog=/live/bin/set-console-width
    test -x $prog && $prog --auto splash-term
}

read_splash_param() {
    local param sparam
    for param in ${CMDLINE:-$(cat /proc/cmdline)}; do
        case $param in
            splash)  SPLASH=on ;;
          splash=*)  SPLASH=$SPLASH${SPLASH:+,}${param#*=} ;;
        esac
    done

    for sparam in ${SPLASH//,/ }; do
        case $sparam in
            theme=*|t=*) THEME=${sparam#*=} ;;
                    off) SPLASH=            ;;
        esac
    done
}

list_themes() {
    local width=$(stty size 2>/dev/null | cut -d" " -f2)
    /bin/ls /etc/splash | column -x -c $((width - 2))
}

error() {
    echo "$ME Error: $*" >&2
    exit 2
}

main "$@"
