#!/bin/sh
#
# Executed during installation to implement hardware specific workarounds.

set -e

if [ -t 1 ] ; then
    MANUAL_RUN=true
else
    MANUAL_RUN=false
fi

info() {
    echo "info: $@" 1>&2
}

append_if_missing() {
    file="$1"
    string="$2"
    if [ -f "$file" ] && grep -q "$string" "$file" ; then
        :
    else
        (
            if [ -f "$file" ] ; then cat "$file" ; fi
	    echo "$string"
        ) > "$file.new" && mv "$file.new" "$file"
    fi
}

xorgconf_hp_mini_2133() {
    template=xserver-xorg/config/inputdevice/keyboard/layout
    keyboard="$(debconf-get-selections | grep $template | awk '{print $4}')"
    if [ -z "$keyboard" ] ; then keyboard=us ; fi
    info "Replacing /etc/X11/xorg.conf for HP Mini 2133 (keyboard $keyboard)"
    cat <<EOF > /etc/X11/xorg.conf
# From http://wiki.debian.org/InstallingDebianOn/HP/HP2133,
# inserted by debian-edu-hwsetup
Section "Files"
EndSection

Section "InputDevice"
	Identifier	"Generic Keyboard"
	Driver		"kbd"
	Option		"XkbLayout"	"$keyboard"
EndSection

Section "InputDevice"
	Identifier	"Configured Mouse"
	Driver		"mouse"
	Option		"CorePointer"
	Option		"Device"		"/dev/input/mice"
	Option		"Protocol"		"ImPS/2"
	Option		"Emulate3Buttons"	"true"
EndSection

Section "InputDevice"
	Identifier	"Synaptics Touchpad"
	Driver		"synaptics"
	Option		"SendCoreEvents"	"true"
	Option		"Device"		"/dev/psaux"
	Option		"Protocol"		"auto-dev"
	Option		"HorizScrollDelta"	"0"
EndSection

Section "Device"
	Identifier	"Video Openchrome"
	Driver		"openchrome"
	BusID		"PCI:1:0:0"
	Screen		0
	Option		"ActiveDevice" "LCD,CRT"
	Option		"ForceLCD"
	Option		"SWCursor" "True"
EndSection

Section "Monitor"
	Identifier	"Monitor LCD"
	VertRefresh	50.00-100.00			# X11 discovery claim
	HorizSync	30.00-113.00			# X11 discovery claim
	DisplaySize	193 116				# Approximate
	UseModes	"HP-2133 LCD Modes"
	Option		"PreferredMode"		"1280x768-60.0"
	Option		"DPMS"
EndSection

Section "Modes"         # See also "HP-2133 Known Modes" at file end for others.
        Identifier      "HP-2133 LCD Modes"
        # Default mode "1024x600": 49.0 MHz, 37.3 kHz, 60.0 Hz
        Modeline "1280x768-60.0"   48.96  1280 1064 1168 1312  768 601 604 622 -hsync +vsync
        # Default mode "1024x512": 41.3 MHz, 31.9 kHz, 60.0 Hz
        Modeline "1024x512-60.0"   41.30  1024 1056 1160 1296  512 513 516 531 -hsync +vsync
        # Default mode "720x480": 26.7 MHz, 29.8 kHz, 60.0 Hz
        Modeline "720x480-60.0"   26.70  720 736 808 896  480 481 484 497 -hsync +vsync
EndSection

Section "Screen"
	Identifier	"Default Screen"
	Monitor		"Monitor LCD"
	SubSection "Display"
		Modes  "1280x768" 
		Virtual 1280 768
		Depth  24
	EndSubSection
	Device		"Video Openchrome"
EndSection

Section "ServerLayout"
	Identifier	"Default Layout"
	Screen		"Default Screen"
	InputDevice	"Generic Keyboard" "CoreKeyboard"
	InputDevice	"Configured Mouse" "CorePointer"
	InputDevice	"Synaptics Touchpad"
EndSection

Section "Extensions"
	Option		"Composite" "true"
	Option		"DAMAGE" "true"
EndSection
EOF
}

# Based on recipe from http://wiki.debian.org/iwlagn
install_wifi_iwlagn() {
    if lsmod | grep -q iwlagn ; then
	echo "info: Kernel module iwlagn already available, not need to enable it."
	return
    fi
    cat <<EOF | sed 's/^/error: /'
To get the wireless card working in this computer, the recipe 
from http://wiki.debian.org/iwlagn can be used to enable
the iwlagn kernel module.  These steps are needed 2010-02-01:

  echo 'deb http://www.backports.org/debian lenny-backports main contrib non-free' \
    >> /etc/apt/sources.list
  aptitude update
  aptitude -t lenny-backports install debian-backports-keyring
  aptitude update
  aptitude -t lenny-backports install linux-image-2.6-$(uname -r | sed 's,.*-,,g') firmware-iwlwifi wireless-tools
  reboot

This will install a updated kernel from backports.org, which
need to be security patched manually by the system administrator.
EOF
}

remove_usplash() {
    hwinfo="$1"
    info "Removing usplash, not supported on this hardware ($hwinfo)"
    aptitude purge -y usplash
}

info "looking for PCI device specific overrides"
for id in $(lspci -n|awk '{print $3}' | tr a-f A-F) ; do
    case "$id" in
	# From http://wiki.debian.org/iwlagn 2010-01-22
	'8086:0082'|'8086:0083'|'8086:0084'|'8086:0085'|'8086:0086'|'8086:0087'|'8086:0088'|'8086:0089'|'8086:4229'|'8086:422B'|'8086:422C'|'8086:4230'|'8086:4232'|'8086:4235'|'8086:4236'|'8086:4237'|'8086:4238'|'8086:4239'|'8086:423A'|'8086:423B'|'8086:423C'|'8086:423D')
	    install_wifi_iwlagn
	    ;;
        # 10DE:06EB nVidia card on Dell Latitude E6500, tested 2010-01-22
        # 1002:515E ATI Technologies Inc ES1000 on HP Proliant DL385 G6
	'10DE:06EB'|'1002:515E')
	    remove_usplash "PCI id $id"
	    ;;
    esac
done

info "looking for DMI manufacturer and product specific overrides"
manufacturer=$(dmidecode -s system-manufacturer)
productname=$(dmidecode -s system-product-name)

case "$manufacturer" in
    Acer)
	case "$productname" in 
    	    AOA110)
    	        # During boot, vt-is-UTF8 hang and X fail to start
	        # when usplash is enabled on the Acer Aspire Onc used
	        # for testing at Debian Edu gatherings.  This is to
	        # work around that problem.  The issue might be
	        # related to
	        # https://bugs.launchpad.net/ubuntu/+source/usplash/+bug/403815
		remove_usplash "$manufacturer $productname"
		;;
	esac
	;;
    'Hewlett-Packard')
	case "$productname" in 
    	    'HP 2133')
		# Avoid white on white console.  See
		# http://wiki.debian.org/InstallingDebianOn/HP/HP2133
		# http://morethan.org/mini-note/xorg-mini.html
		append_if_missing /etc/default/acpi-support \
		    'SAVE_VBE_STATE=false'
		# LCD screen lack DDC information and require
		# xorg.conf to work properly.
		xorgconf_hp_mini_2133

		# Should fetch b43 wireless firmware installer from
		# contrib.  Probably better to update discover-data to
		# let discover-pkginstall do it below.  Need contrib
		# enabled in apt, though.
		#DEBIAN_FRONTEND=noninteractive apt-get install b43-fwcutter
		;;
	esac
	;;
esac
