#!/bin/bash
PATH=$PATH:/bin:/sbin:/usr/bin

# script variables
SCRIPTNAME=`basename $0`
VERBOSE=0
OVERWRITE=0
ISCONFIGED=0
XLINK="/etc/X11/X"
XTMPFILE="/tmp/xconfig.tmp"
XCONFIGERROR="Please run XConfigurator $XCONFIGURATORFLAG; before running $SCRIPTNAME."
USAGE="$SCRIPTNAME -f|--file {filename} [-o|--overwrite] [-v|--verbose] [-h|--help]"
HELPMESSAGE="Try \`xcorrect --help\` for more information."

# include external functions
. /etc/init.d/functions

# functions
stepstatus (){
	STATUS=$1
	PASSEDTEXT=$2
	FAILEDTEXT=$3
	EXTRATEXT=$4
	if [ $STATUS = 0 ]; then
		if [ $VERBOSE = 1 ]; then
			action "$PASSEDTEXT" /bin/true
		fi
	else
		action "$FAILEDTEXT" /bin/false
		echo ""
		echo $EXTRATEXT
		exit 1
	fi
	}
printactionlabel (){
	TITLE=$1
	NEWLINE=$2
	if [ $VERBOSE = 1 ]; then
		if [ $NEWLINE = 0 ]; then
			echo -n $TITLE
		else
			echo $TITLE
		fi
	fi
	}
lookforconffile (){
	CONFIGFILE=$1
	if [ -z $CONFIGFILE ]; then
		stepstatus 1 "" "No configuration file specified; please see $SCRIPTFILE -h for usage." ""
	else
		printactionlabel "Lookin for config file..." 1
		if [ -f /etc/xcorrect/$CONFIGFILE ]; then
			. /etc/xcorrect/$CONFIGFILE
			ISCONFIGED=1
		elif [ -f $CONFIGFILE ]; then
			. $CONFIGFILE
			ISCONFIGED=1
		fi
		if [ $ISCONFIGED = 1 ]; then
			RETVAL=0
		else
			RETVAL=1
		fi
		stepstatus $RETVAL "	$CONFIGFILE found" "$CONFIGFILE: No such file" ""
	fi
	}
checkforcard (){
	printactionlabel "Looking for $CARDLABEL..." 0
	lspci -n | grep "$PCIID" > /dev/null
	RETVAL=$?
	stepstatus $RETVAL " found in pcitable" "not found in pcitable" "Please ensure that the $CARDLABEL is installed correctly."
	}
checkXserver (){
	printactionlabel "Checking X Config..." 0
	`file $XLINK | grep $XSERVER > /dev/null`
	RETVAL=$?
	stepstatus $RETVAL " $XSERVER link found at $XLINK" " $XSERVER link not found" $XCONFIGERROR
	}
checkforXconfig (){
	printactionlabel "Checking for X config file..." 1
	`test -f /etc/X11/XF86Config-4`
	RETVAL=$?
	stepstatus $RETVAL "    $XSERVER config file found at $XCONFIGFILE" "    $XSERVER config file not found" $XCONFIGERROR
	}
checkforcardconfig (){
	printactionlabel "Looking for \"$CARDLABEL\" section..." 1
	`grep "$CARDLABEL" $XCONFIGFILE -n | grep Identifier > /dev/null`
	RETVAL=$?
	stepstatus $RETVAL "    Section found in $XCONFIGFILE" "    Section not found in $XCONFIGFILE" $XCONFIGERROR
	}
createnewXconfig (){
	printactionlabel "Creating New Config File..." 1
	`awk ' { FS = "\n"; RS = "\nEndSection" } \
	/'"$CARDLABEL"'/ && /Section "Device"/ && $0!~/[^#].\w?Option\w?.?[^#]?"'$XOPTION'"/ \
	{ printf($0"\n\tOption\t\"'$XOPTION'\""RT); $0=""; } \
	length($0) > 0 { printf($0RT) } \
	' $XCONFIGFILE > $XTMPFILE`
	RETVAL=$?
	if [ $OVERWRITE = 0 ]; then
		VERBOSE=1;
		stepstatus $RETVAL " $XTMPFILE created" " Failed to create $XTMPFILE" "Please check the permissions of /tmp and /tmp/xconfig."
	fi		
	}
backupoldXconfig (){
	printactionlabel "Moving $XCONFIGFILE to $XCONFIGFILE.orig..." 1
	yes | cp $XCONFIGFILE $XCONFIGFILE.orig
	RETVAL=$?
	stepstatus $RETVAL "    Done" "    Failed" "Please check the permissions of $XCONFIGFILE and $XCONFIGFILE.orig."
	}
installnewXconfig () {
	printactionlabel "Moving $XTMPFILE to $XCONFIGFILE..." 1
	yes | mv $XTMPFILE $XCONFIGFILE
	RETVAL=$?
	VERBOSE=1
	stepstatus $RETVAL "    Done" "    Failed" "Please check the permissions of $XTMPFILE and $XCONFIGFILE." 
	}

# main
while [ $# -gt 0 ]; do
	case "$1" in
	  -f|--file)	
		lookforconffile $2 
		shift
		;;
	  -v|--verbose)
	        VERBOSE=1 
	        ;;
	  -o|--overwrite)
		OVERWRITE=1
		;;
	  -h|--help)
		echo "USAGE:"
		echo $USAGE
		exit 1
		;;
	  *)
		echo "$SCRIPTNAME: invalid option $1"
		echo $HELPMESSAGE
		exit 1
		;;
	esac
	shift
done

if [ $ISCONFIGED = 1 ]; then
	echo -n "Updating Xconfiguration..."
	if [ $VERBOSE = 1 ]; then
		echo ""
	fi

	checkforcard
	checkXserver
	checkforXconfig
	checkforcardconfig
	createnewXconfig

	if [ $OVERWRITE = 1 ]; then
		backupoldXconfig
		installnewXconfig
	fi
else
	lookforconffile 
fi
