#! /bin/sh -e
#Passes on the information gathered in ubiquity
#about additional drivers, firmware
# Written by Mario Limonciello <superm1@ubuntu.com>
# Copyright (C) 2007 Mario Limonciello

. /usr/share/debconf/confmodule

if [ "$1" ]; then
    ROOT="$1"
    chroot=chroot
    log='log-output -t mythbuntu'
fi

ati_xorg()
{
    if [ "$1" = "Composite Video Output" ]; then
        $log $chroot $ROOT /usr/bin/aticonfig --initial --tvs VIDEO --tvf $2
    elif [ "$1" = "S-Video Video Output" ]; then
        $log $chroot $ROOT /usr/bin/aticonfig --initial --tvs VIDEO --tvf $2
    elif [ "$1" = "Component Video Output" ]; then
        $log $chroot $ROOT /usr/bin/aticonfig --initial --tvs YUV --tvf $2
    else
        $log $chroot $ROOT /usr/bin/aticonfig --initial
    fi
}

nvidia_xorg()
{
    cat > /tmp/xorg.conf <<EOF
Section "ServerLayout"
    Identifier     "Default Layout"
    Screen         "Default Screen" 0 0
    InputDevice    "Generic Keyboard"
    InputDevice    "Configured Mouse"
EndSection

Section "Module"
    Load           "glx"
EndSection

Section "InputDevice"
    Identifier     "Generic Keyboard"
    Driver         "kbd"
    Option         "CoreKeyboard"
    Option         "XkbRules" "xorg"
    Option         "XkbModel" "pc105"
    Option         "XkbLayout" "us"
EndSection

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

Section "Monitor"
    Identifier     "Generic Monitor"
    Option         "DPMS"
EndSection

Section "Device"
    Identifier     "Generic Video Card"
    Driver         "nvidia"
    Option         "DPI" "100x100"
    Option         "UseEvents" "1"
    Option         "AddARGBVisuals" "1"
    Option         "AddARGBGLXVisuals" "1"
    Option         "NoLogo" "1"
EOF
    if [ "$1" = "Composite Video Output" ]; then
        cat >> /tmp/xorg.conf <<EOF
    Option         "UseDisplayDevice" "TV"
    Option         "TVOutFormat" "COMPOSITE"
    Option         "TVStandard" "$2"
EOF
    elif [ "$1" = "S-Video Video Output" ]; then
        cat >> /tmp/xorg.conf <<EOF
    Option         "UseDisplayDevice" "TV"
    Option         "TVOutFormat" "SVIDEO"
    Option         "TVStandard" "$2"
EOF
    elif [ "$1" = "Component Video Output" ]; then
        cat >> /tmp/xorg.conf <<EOF
    Option         "UseDisplayDevice" "TV"
    Option         "TVOutFormat" "COMPONENT"
    Option         "TVStandard" "$2"
EOF
    fi
    cat >> /tmp/xorg.conf <<EOF
EndSection

Section "Screen"
    Identifier     "Default Screen"
    Device         "Generic Video Card"
    Monitor        "Generic Monitor"
    DefaultDepth    24
    SubSection     "Display"
        Depth       24
EOF
    if [ "$1" = "Component Video Output" ]; then
        cat >> /tmp/xorg.conf <<EOF
        Modes      "1920x1080" "1280x720" "1024x768" "720x480" "800x600" "640x480"
    EndSubSection
EndSection
EOF
    else
        cat >> /tmp/xorg.conf <<EOF
        Modes      "nvidia-auto-select" "1920x1080" "1280x720" "1024x768" "720x480" "800x600" "640x480"
    EndSubSection
EndSection
EOF
    fi
}

nvidia_xvmc()
{
    mv $ROOT/etc/X11/XvMCConfig $ROOT/etc/X11/XvMCConfig.livecd
    cat > $ROOT/etc/X11/XvMCConfig <<EOF
libXvMCNVIDIA_dynamic.so.1
EOF
}

#Enable Proprietary Drivers as necessary
db_get mythbuntu/video_driver
PROP="$RET"
if [ "$PROP" = "nvidia_new" ] || [ "$PROP" = "nvidia" ] || [ "$PROP" = "fglrx" ] || [ "$PROP" = "nvidia_legacy" ]; then
    #Due to bug 119562, restricted manager won't do this
    #For us automatically.
    #$log $chroot $ROOT /usr/bin/restricted-manager -e $PROP

    #Work out a config that should be fairly generic
    #Also, sort tv-out mess here
    db_get mythbuntu/tvout
    TVOUT="$RET"
    db_get mythbuntu/tvstandard
    TVSTANDARD="$RET"
    if [ "$PROP" = "fglrx" ]; then
        #$log $chroot $ROOT /usr/bin/aticonfig --initial
        ati_xorg "$TVOUT" "$TVSTANDARD"
    elif [ "$PROP" = "nvidia_new" ] || [ "$PROP" = "nvidia" ] || [ "$PROP" = "nvidia_legacy" ]; then
        #$log $chroot $ROOT nvidia-xconfig
        nvidia_xorg "$TVOUT" "$TVSTANDARD"
        nvidia_xvmc
        mv $ROOT/etc/X11/xorg.conf $ROOT/etc/X11/xorg.conf.livecd
        mv /tmp/xorg.conf $1/etc/X11/xorg.conf
    fi
fi

exit 0
