#!/bin/sh
#
# Initialize Elite 3D Framebuffer firmware

### BEGIN INIT INFO
# Provides:          afbinit
# Required-Start:    $local_fs $remote_fs
# Required-Stop:    
# Default-Start:     S 
# Default-Stop:      
# Short-Description: Elite 3D Framebuffer firmware initializer
# Description:       afbinit initializes the microcode firmware
#                    needed by Elite 3D framebuffer cards found on many
#                    UltraSPARC systems
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
set -e

. /lib/lsb/init-functions

# This only applies to UltraSPARC's
if [ `uname -m` != "sparc64" ];then
    log_failure_msg "The architecture is not sparc64"
    exit 0
fi

# The microcode loader binary and the microcode itself must exist.
if [ ! -x /usr/sbin/afbinit ];then
    log_failure_msg "Cannot find afbinit (/usr/sbin/afbinit)"
	exit 1
fi
if [ ! -f /usr/lib/afb.ucode ];then
    log_failure_msg "Cannot find AFB microcode (/usr/lib/afb.ucode)"
    log_failure_msg "Please read /usr/share/doc/afbinit/README.Debian to see how to obtain it"
    exit 0
fi

case "$1" in
  start)
	# Make FB device list.
	afb_devs=$(awk '/Elite/ {printf "fb%d\n",$1}' /proc/fb)
	[ -n "${afb_devs}" ] || exit 0

	# Load microcode onto each card.
    log_begin_msg "Loading Elite3D microcode on: "
	for AFB in ${afb_devs}; do
		log_begin_msg ${AFB}
		/usr/sbin/afbinit /dev/${AFB} /usr/lib/afb.ucode > \
            /dev/null
        log_end_msg $?
	done
	;;
  stop|restart|force-reload) # Nothing
	;;
  *)
	log_success_msg "Usage: /etc/init.d/afbinit start"
	exit 1
	;;
esac

exit 0
