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

set -e

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
}

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
		info "Removing usplash, not supported on this hardware ($manufacturer $productname)"
		aptitude purge -y usplash
		;;
	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

info "Installing hardware specific packages known to discover"
DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND
discover-pkginstall
