#!/bin/sh
NAME="LED logo framebuffer config"
FB_NAME="logo"
LOGO_MAX_BRIGHTNESS=255

ORDINARY_BOOT="\xf4\x0e\x6e\xf4\x0e\x6e\xf4\x0e\x6e\xf4\x0e\x6e\xf4\x0e\x6e\xf4\x0e\x6e"
FACTORY_RESET_BOOT="\x00\xe6\xff\x00\xe6\xff\x00\xe6\xff\x00\xe6\xff\x00\xe6\xff\x00\xe6\xff"

error() {
  echo "Error: $@" > /dev/stderr
  exit 1
}

fbdevice() {
  local dev=$(grep -xlF "$@" /sys/class/graphics/*/name | head -1)
  [ "$dev" ] || return
  dirname "$dev"
}

backlightdevice() {
  local dev=$(ls $1/device/backlight/*/brightness | head -1)
  [ "$dev" ] || return
  dirname "$dev"
}

case "$1" in
start)
    echo "Starting $NAME"

    dev=$(fbdevice "$FB_NAME")
    bl_dev=$(backlightdevice $dev)

    echo "Set max brightness $LOGO_MAX_BRIGHTNESS"
    echo $LOGO_MAX_BRIGHTNESS > $bl_dev/brightness

    if [ -e /etc/init.d/S01swupdate ]; then
      echo "Booting in recovery mode"
      table=$FACTORY_RESET_BOOT
    else
      table=$ORDINARY_BOOT
    fi

    dev_fb=$(basename $dev)
    echo -e -n $table > /dev/$dev_fb
    ;;
*)
  echo "Usage: $0 start"
  exit 1
esac
