#!/bin/bash
BASE=""
. /etc/profile
#TEMPLATES="/usr/share/templates"

function Xvalues {
    VGACARDTYPE=`lspci|grep -i vga|sort -r|head -n 1| cut -d: -f3|cut -d" " -f 2`
    echo $VGACARDTYPE
    currentmode=`cat $BASE/etc/X11/xorg.conf |grep Modes|awk -Fs ' { print $2 } '`
    }

function presetupX {
if [ x$XUseAdvanced =  x1 ]
then
    if  [ x$XIgnoreConfig  = x1 ]
    then
        echo "Using User supplied X"
        if [ -f  $BASE/data/home/mythtv/templates/xorg.user ]
        then
            cp -f  $BASE/data/home/mythtv/templates/xorg.user  $XORG_CONF
        else
            echo "Didn't find the file"
            echo "Copy your config file to $BASE/data/home/mythtv/templates/xorg.use  " >>  $XORG_CONF
         fi
    else
    setupAdvancedX
    fi
else
      setupX
fi

}

function setupAdvancedX {
echo "#doing the advanced" > /tmp/options
        Xcatagory=` echo $Xresadvanced |cut -d_ -f1`
        Xres=` echo $Xresadvanced |cut -d_ -f2`
        echo $Xcatagory
        if [ x$Xcatagory = "xNvidia" ]
        then
            Xres=""
            if [  $XnVidia1080p = "1" ]
            then
                Xres="${Xres} \"1920x1080_60\""
            fi
            if [  $XnVidia1080i = "1" ]
            then
                Xres="${Xres}  \"1920x1080_60i\""
            fi
            if [  $XnVidia720p = "1" ]
            then
                Xres="${Xres}  \"1280x720_60\""
            fi
            if [  $XnVidia480p = "1" ]
            then
                Xres="${Xres}  \"720x480_60\""
            fi
                ML="\t Modes ${Xres}"
        else

         ML="\t Modes \"${Xres}\""
        fi
        echo $ML
    case $Xcardtype in
        NVIDIA|nVidia )

                         case $Xconnection in
                                DVI)    ConnectedMonitor=DFP ;;
                                vga)    ConnectedMonitor=CRT;;
                                Auto)   ConnectedMonitor=Auto;;
                                TV)    ConnectedMonitor=TV
                                          echo "Option \"TVStandard\" \"$XTVstandard\""  >>/tmp/options
                                          echo "Option \"TVOutFormat\" \"$XTVconnection\"" >> /tmp/options
                                           ;;
                            esac
                            if [ $ConnectedMonitor = Auto ]
                            then
                              echo "auto connection"
                            else
                             echo    "Option \"ConnectedMonitor\" \"$ConnectedMonitor\"" >> /tmp/options
                             echo    "Option \"FlatPanelProperties\" \"Scaling = centered, Dithering = enabled\"" >> /tmp/options
                            echo    "Option \"DigitalVibrance\" \"0\"" >> /tmp/options
                            fi

                            if  [ x$XignoreEDID = x1 ]
                                    then
                                        EDIDvalue=true
                                        EDIDFREQ=false
                                        DS="     DisplaySize $XDisplaysize"
                                        HS="    HorizSync  $XHsync"
                                        VR="    VertRefresh $XVrefresh"
                                      else
                                        EDIDvalue=false
                                        EDIDFREQ=true
                                        DS="# Display size edid used "
                                        HS="#Hsync edid used "
                                        VR="#Vertrefresh edid used "

                            fi
                            echo   "Option  \"UseEdidFreqs\" \"$EDIDFREQ\"" >> /tmp/options
                            echo   "Option  \"IgnoreEDID\" \"$EDIDvalue\"" >> /tmp/options


                sed -e "s/^.*Modes.*$/${ML}/" \
                       -e "/INSERT MODELINE/r  $TEMPLATES/xorg/modeline.$Xcatagory" \
                       -e "s/^.*DisplaySize.*$/  $DS/" \
                       -e "s/^#.*HorizSync.*$/ $HS/g" \
		       -e "s/^#.*VertRefresh.*$/ $VR/g" \
                       -e "/INSERT HERE/r /tmp/options"  $TEMPLATES/xorg/xorg.conf.nvidia  > $XORG_CONF

          ;;
         ati|ATI)
	 	 case $Xconnection in
                                DVI)    ConnectedMonitor="tmds1,nocrt1,notv";;
                                VGA)    ConnectedMonitor="crt1,notmds1,notv" ;;
                                TV)    ConnectedMonitor="tv,nocrt1,notmds1"
                                       echo "Option \"TVStandard\" \"$XTVstandard\""  >>/tmp/options
                                       echo "Option \"TVFormat\" \"$XTVconnection\"" >> /tmp/options
					;;

 		esac


                      if  [ x$XignoreEDID = x1 ]
                                    then
                                        DS="     DisplaySize $XDisplaysize"
                                        HS="    HorizSync  $XHsync"
                                        VR="    VertRefresh $XVrefresh"
                                      else
                                                        DS="# Display size edid used "
                                        HS="#Hsync edid used "
                                        VR="#Vertrefresh edid used "

                            fi

		echo "Option \"ForceMonitors\" \"$ConnectedMonitor\"" >> /tmp/options

                  sed -e "s/^.*Modes.*$/\t $ML/" \
                     -e "/INSERT MODELINE/r  $TEMPLATES/xorg/modeline.$Xcatagory" \
                     -e "s/^.*DisplaySize.*$/  $DS/" \
                     -e "s/^#.*HorizSync.*$/ $HS/g" \
		     -e "s/^#.*VertRefresh.*$/ $VR/g" \
		     -e "/INSERT HERE/r /tmp/options"  $TEMPLATES/xorg/xorg.conf.ati  > $XORG_CONF
	;;

        via|VIA|Via) echo "no via here"
            ;;

        *)
                echo "unknown card type"
        ;;
    esac
}



function setupX {
    Xvalues
    echo "$currentmode  ____    ${Xres}"
    #if [ !  x${currentmode} = x\"${Xres}\" ]
    if [   x = x ]
    then
        case $VGACARDTYPE in
            nVidia )
                    sed -e "s/^.*Modes.*$/\t Modes \"${Xres}\"/" $TEMPLATES/xorg/xorg.conf.nvidia > $XORG_CONF
                    if [ !  x$TESTMODE = "xtest" ]
                    then
                            pacman --noconfirm  -R libgl-dri
#                            pacman --noconfirm  -S nvidia-STB nvidia-utils
                            pacman --noconfirm  -S nvidia nvidia-utils
                    fi
                                ;;
            VMware )
                       sed -e "s/^.*Modes.*$/\t Modes \"${Xres}\"/" $TEMPLATES/xorg/xorg.conf.vmware > $XORG_CONF
                    ;;

            VIA )
                    sed -e "s/^.*Modes.*$/\t Modes \"${Xres}\"/" $TEMPLATES/xorg/xorg.conf.via > $XORG_CONF
                    if [ ! x$TESTMODE = "xtest" ]
                    then
                        pacman --noconfirm  -R nvidia-STB nvidia-utils
                        pacman --noconfirm  -S libgl-dri
                    fi

                    ;;
             Intel)
                    sed -e "s/^.*Modes.*$/\t Modes \"${Xres}\"/" $TEMPLATES/xorg/xorg.conf.intel > $XORG_CONF
                    if [ !  x$TESTMODE = "xtest" ]
                    then
                            pacman --noconfirm  -Rd nvidia-utils
                            pacman --noconfirm -S libgl-dri
                    fi
                                ;;

	*)  #XCONF=`hwd -x |grep etc`
        XCONF=`hwd -x |grep etc|tr -d [:cntrl:]|cut -d\/ -f 2-`

         
  		   cat /$XCONF |grep -v Modes | sed -e '/Depth     16/a Modes "800x600"' -e 's/dev\/mouse/dev\/psaux/g'> $XORG_CONF
		;;
        esac
    fi

}

#MAIN
TESTMODE=$1
if [ x$TESTMODE = "xtest" ]
then
    XORG_CONF="/tmp/xorg.conf.test"
    XUseAdvanced="1"
    XIgnoreConfig="${2}"
    Xcardtype="${3}"
    XIgnoreEDID="${4}"
    Xconnection="${5}"
    XHsync="${6}"
    XVrefresh="${7}"
    Xresadvanced="${8}"
    XTVstandard="${9}"
    XTVconnection="${10}"
    XDisplaysize="${11}"
else
    if [ -f   /etc/systemconfig ]
    then
        . /etc/systemconfig
    else
        Xres="800x600"
    fi
    XORG_CONF="/etc/X11/xorg.conf"
fi
presetupX