#!/bin/bash
# pruneXf86video.sh  
# created and copyrighted: 31 Dec 10
#
# author: Evsiz (evsiz@this-voice.org)
# The author releases this file under the GNU GPL
# license. See GNU's COPYING file for specifics.
#
# /etc/X11/xorg.conf
#Section "Device"
#    Identifier  "VESA Framebuffer"
#    Driver      "vesa"
#    VideoRam    4096
#    #Insert Clocks lines here if appropriate
#EndSection

# consts -----------------
CONF=/etc/X11/xorg.conf
TMPF=~/_px86vTmp
TMPF2=~/_px86vTmp2

# code -------------------
LANG=C
export LANG
LC_ALL=C
export LC_ALL

# 0. Sanity Check
echo -n "
WARNING - READ THIS CAREFULLY - WARNING

This script will run Slackware's removepkg on
/var/log/packages/xf86-video* except the one used
in /etc/X11/xorg.conf and a few other basic ones,
like vesa. All other video drivers will be
removed.

You must run this script as root.

It assumes you use ONLY ONE video driver.

Proceed (y/N): "
read PROCEED
if [ "$PROCEED" != "y" ]; then
    exit
fi

# 1. GET THE DRIVER !!! we're assuming only one driver for now !!!
# length of xorg.conf
if [ ! -f $CONF ]; then
    clear
    echo "
no $CONF found.
Exiting.
"
    exit 1
    fi
LEN=`wc -l $CONF | cut -d" " -f 1`
# line of Section "Device"
LINE=`grep -n 'Section "Device"' $CONF | cut -d: -f 1`
# xorg.conf from LINE down
TAIL=$( echo $LEN - $LINE | bc )
# find end of Section "Device" and extract (first) Driver
SUB=`tail -n $TAIL $CONF | grep -n EndSection | head -n1 | cut -d: -f 1`
HEAD=$( echo $TAIL + $SUB | bc )
DRIVER=`tail -n $TAIL $CONF | head -n$HEAD | grep Driver | cut -d\" -f 2`

# 2. GET THE DRIVER PACKAGES
cd /var/log/packages
ls -1 xf86-video* > $TMPF
# reduce to target packages
awk '$0 !~ /'$DRIVER'/ && $0 !~ /vesa/ && $0 !~ /noarch/ && $0 !~ /dummy/ \
        { print > "'$TMPF2'" }' $TMPF
# reduce to names for removepkg
awk 'BEGIN { FS = "-" } ; { print $1"-"$2"-"$3 > "'$TMPF'" }' $TMPF2
rm $TMPF2

# 3. REMOVE PACKAGES
DRIVERS=`cat $TMPF`
for d in $DRIVERS; do
    removepkg $d
done
rm $TMPF
