#!/bin/sh # ------- # File: llxcfg-xorg-conf # Description: LliureX simple xorg.conf setup tool # Author: Luis Garcia Gisbert # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- # vars SECTION_REGEXP="/^Section[[:blank:]]\+\"Device\"/,/^EndSection/" DRIVER_REGEXP="/[[:blank:]]\+Driver/" GETDRIVER_REGEXP="$SECTION_REGEXP{$DRIVER_REGEXP{s%[^\"]*\"%%;s%\".*$%%;p}}" XORG_CONF="/etc/X11/xorg.conf" XORG_MOD="${XORG_CONF}.llxcfg" XORG_BACK="${XORG_MOD}.BACKUP" XORG_MD5="${XORG_BACK}.md5sum" # funcs get_driver(){ CONF_FILE="$1" [ "$CONF_FILE" ] || CONF_FILE="$XORG_CONF" sed -ne "${GETDRIVER_REGEXP}" ${CONF_FILE} } show_current(){ echo "Current driver: $(get_driver)" } show_backup(){ if [ -r "$XORG_BACK" ] ; then echo "Backup configuration: $(get_driver "$XORG_BACK")" else echo "No backup configuration" fi } set_driver(){ NEW_DRIVER="$1" NEW_CONF="${XORG_MOD}.${NEW_DRIVER}" if [ ! -r "$NEW_CONF" ] ; then SETDRIVER_REGEXP="$SECTION_REGEXP{$DRIVER_REGEXP{s%\".*\"%\"$NEW_DRIVER\"%}}" sed -e "${SETDRIVER_REGEXP}" ${XORG_CONF} > "$NEW_CONF" fi llxcfg-install "$NEW_CONF" "$XORG_CONF" } current_was_generated(){ if [ -r "$XORG_BACK" -a -r "$XORG_MD5" ] && md5sum -c --status $XORG_MD5 ; then # the current xorg.conf was generated by this program return 0 fi return 1 } do_backup(){ if ! test_backup > /dev/null ; then cp -a $XORG_CONF $XORG_BACK fi return 0 } do_restore(){ if [ -r "$XORG_BACK" ] ; then llxcfg-install $XORG_BACK $XORG_CONF else warn_msg "There is no backup file to restore" fi rm -f $XORG_BACK $XORG_MD5 } do_md5sum(){ md5sum $XORG_CONF > $XORG_MD5 return 0 } error_msg(){ echo "$1" >&2 exit 1 } warn_msg(){ echo "Warning: $1" } test_write(){ [ -w "$XORG_CONF" ] || error_msg "Error: can't write to $XORG_CONF" } test_backup(){ [ -r "$XORG_BACK" ] && return 0 warn_msg "There is no backup file to restore" return 1 } usage(){ error_msg "Usage: $0 {set driver DRIVER|unset driver |restore driver |show driver |getcurrent driver }" } # main [ -r "$XORG_CONF" ] || error_msg "Error: unreadable $XORG_CONF" ACTION="$1" [ "$ACTION" ] || usage shift SECTION="$1" # only driver at this moment [ "$SECTION" = "driver" ] || usage case $ACTION in show) show_current show_backup ;; set) [ "$2" ] || usage test_write DRIVER="$2" OLD_DRIVER="$(get_driver)" [ "$DRIVER" = "$OLD_DRIVER" ] && return 0 do_backup set_driver "$DRIVER" do_md5sum ;; unset) test_write if current_was_generated ; then do_restore elif test_backup ; then warn_msg "There are custom modifications in current config. Skipping restore" fi ;; restore) # unconditional restore test_write do_restore ;; getcurrent) get_driver ;; esac return 0