#!/bin/bash
#set -x
#SET DISPLAYRES to force Xres, ENV takes precendent over cmdline


LOGFILE=/tmp/xconfig.log
for i in ati intel mga savage sis unichrome
do
    templist=`echo $i-dri xf86-video-$i`
    NVPKGS=`echo $NVPKGS  $templist`

done
BASE=""
MYTH_RUN_STATUS="1"
. /etc/profile

#MYTHHOME=$BASE/home/mythtv

. /usr/MythVantage/bin/install_functions.sh
home_check


function Xvalues {
    CMDLINE=$(cat /proc/cmdline)
    echo "$CMDLINE" | grep -q forceXvesa
    if [ $? = 0  ]
    then
        echo "Forcing driver to vesa"
        VGACARDTYPE=vesa
        Xcardtype=$VGACARDTYPE
    else
        VGACARDTYPE=`lspci | grep -i vga | sort -r | head -n 1 | cut -d: -f3 | cut -d" " -f2`
        VGAPCIID=`lspci | grep -i vga | sort -r | head -n 1 | cut -d" " -f1`
        CARDID=`lspci -n | grep "$VGAPCIID" | grep 10de | cut -d" " -f3 | cut -d: -f2`
        VENDORID=`lspci -n | grep "$VGAPCIID" | grep 10de | cut -d" " -f3 | cut -d: -f1`
    fi
    echo "Detected $VGACARDTYPE as the video card type"
}

function presetupX {
    if  [ "$XIgnoreConfig" = 1 ]
    then
        echo "Using User supplied X"
        if [ -f "$MYTHHOME/templates/xorg.user" ]
        then
            cp -f "$MYTHHOME/templates/xorg.user" "$XORG_CONF"
        else
            echo "Didn't find the file"
            echo "Copy your config file to $MYTHHOME/templates/xorg.user" > "$XORG_CONF"
        fi
    else
        setupX
    fi
}

function nvidia_prepare {
    for i in libgl nvidia nvidia-utils nvidia-173xx nvidia-173xx-utils nvidia-96xx nvidia-96xx-utils
    do
        pacman --noconfirm -Rdd $i >> $LOGFILE 2>&1
    done

    for i in `echo ${NVPKGS}`
    do
        pacman --noconfirm  -Rc $i >> $LOGFILE 2>&1
    done
}

function pkg_check () {
    echo "  Checking for $1 in cache or online"
    pacman --noconfirm -Sw $1 >> $LOGFILE 2>&1
    rc=$?
    if [ $rc != 0 ]
    then
        echo "  Couldn't find $1 for install, aborting"
        echo "  Couldn't find $1 for install, aborting" >> $LOGFILE 2>&1
        exit 1
    fi
}

function nvidia_driver_install {
    echo "Installing Nvidia driver"
    cp "$TEMPLATES/xorg/xorg.nvidia"  $BASE/etc/X11/xorg.conf.d/20-automv.conf
    #install latest drivers
    pkg_check nvidia-utils
    echo "Installing current nvidia drivers"
    nvidia_prepare
    pacman --noconfirm  -S nvidia nvidia-utils >> $LOGFILE 2>&1
    installed=1
    echo "/usr/lib/libXvMCNVIDIA_dynamic.so.1" > "$XVMC_CONFIG"
}

function vmware_driver_install {
    echo "Installing VMware driver"
    echo "Installing VMware driver" >> $LOGFILE 2>&1
    LIST="xf86-input-vmmouse  xf86-video-vmware xf86-video-vesa svga-dri"
    for i in `echo ${LIST}`
    do
        pkg_check $i
    done
    for i in `echo ${LIST}`
    do
        pacman -S --noconfirm $i >> $LOGFILE 2>&1
    done

}

function vesa_driver_install {
    echo "Installing Vesa driver"
    echo "Installing Vesa driver" >> $LOGFILE 2>&1
    pacman -S --noconfirm xf86-video-vesa  >> $LOGFILE 2>&1
}

function intel_driver_install {
    echo "Installing Intel driver"
    echo "Installing Intel driver" >> $LOGFILE 2>&1
    pkg_check xf86-video-intel
    #this is used to remove nvidia
    nvidia_prepare
    pacman -S --noconfirm xf86-video-intel >> $LOGFILE 2>&1
    echo "/usr/lib/libIntelXvMC.so.1" > "$XVMC_CONFIG"
}

# if [ "$Xcategory" = "Nvidia" ]
# 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

function set_res {
    echo "    Setting modeline to ${Xres}"
    echo "    Setting modeline to ${Xres}" >> $LOGFILE 2>&1
    cat > /etc/X11/xorg.conf.d/30-screen.conf <<EOF
    Section "Screen"
        Identifier             "Screen0"  #Collapse Monitor and Device section to Screen section
        Device                 "Device0"
        Monitor                "Monitor0"
        DefaultDepth            24 #Choose the depth (16||24)
        SubSection             "Display"
            Depth               24
            Modes              "${Xres}"  #Choose the resolution
        EndSubSection
    EndSection
EOF
}


function setupX {
    Xvalues
    if [ x = x ]
    then
        case $VGACARDTYPE in
            NVIDIA|nVidia|nvidia)
                    nvidia_driver_install
                    if [ \"$Xres\" = "Auto" ]
                    then
                        Xres="nvidia-auto-select"
                    fi

                    if [ ${Xres} != "Auto" ]
                    then
                        set_res
                    else
                        rm -f /etc/X11/xorg.conf.d/30-screen.conf
                    fi
                ;;

            VMware)
                    vmware_driver_install
                    cp $TEMPLATES/xorg/xorg.vmware  /etc/X11/xorg.conf.d/20-automv.conf
                    echo "copy in autostart functions" >> $LOGFILE 2>&1
                    mkdir -p /etc/X11/autostart  >> $LOGFILE 2>&1
                    echo "#!/bin/bash" > /etc/X11/autostart/vmware.sh
                    echo "/usr/bin/vmware-user-suid-wrapper" >> /etc/X11/autostart/vmware.sh
                    chmod 755 /etc/X11/autostart/vmware.sh
                    if [ ${Xres} != "Auto" ]
                    then
                        set_res
                    else
                        rm -f /etc/X11/xorg.conf.d/30-screen.conf
                    fi
                    ;;

            Intel|intel)
                    intel_driver_install
                    cp $TEMPLATES/xorg/xorg.intel  /etc/X11/xorg.conf.d/20-automv.conf
                    if [ ${Xres} != "Auto" ]
                    then
                        set_res
                    else
                        rm -f /etc/X11/xorg.conf.d/30-screen.conf
                    fi
                    ;;

            vesa|InnoTek|*) #leaving this as * and vesa for no real reason other then it's explicit to catch vesa
                  vesa_driver_install
                  cp $TEMPLATES/xorg/xorg.vesa  /etc/X11/xorg.conf.d/20-automv.conf
                  if [ ${Xres} != "Auto" ]
                    then
                        set_res
                    else
                        rm -f /etc/X11/xorg.conf.d/30-screen.conf
                    fi

                ;;

        esac
    fi

}


#---------------------------------------------------------------------------------
#MAIN


LINE=`grep -i xorg /etc/mythvantage.cfg|grep -q False`
rc=$?
if  [ $rc = 0 ]
then
    echo "**   xorg configuration disabled in /etc/mythvantage.cfg"
    exit 1
fi

if [ -f /etc/sysconfig/IGNORE_X ]
then
    echo "**   Will not run xconfig.sh due to presence of /etc/sysconfig/IGNORE_X"
    exit 1
fi



if [ -f /etc/systemconfig ]
then
    . /etc/systemconfig
else
    Xres="800x600"
fi
CMDLINE=$(cat /proc/cmdline)
echo $CMDLINE | grep -q displayres
if [ $? = 0 ]
then
    TEMPVAR=${CMDLINE#*displayres=}
    DSR=${TEMPVAR%% *}
fi
if [ ! x$DSR = x ]
then
    Xres=$DSR
fi
if [ ! x$DISPLAYRES = x ]
then
    Xres=$DISPLAYRES
fi



XORG_CONF="/etc/X11/xorg.conf"
XVMC_CONFIG="/etc/X11/XvMCConfig"

presetupX